Support for version flag

This commit is contained in:
Svallinn 2021-03-07 16:07:09 +00:00
parent 986053ad03
commit 3734bad6ef
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
1 changed files with 457 additions and 448 deletions

View File

@ -2,57 +2,65 @@ import { app, BrowserWindow, Menu, ipcMain, screen } from 'electron'
import { productName } from '../../package.json' import { productName } from '../../package.json'
import Datastore from 'nedb' import Datastore from 'nedb'
require('@electron/remote/main').initialize() if (process.argv.includes('--version')) {
console.log(`v${app.getVersion()}`)
app.exit(0)
} else {
runApp()
}
require('electron-context-menu')({ function runApp() {
require('@electron/remote/main').initialize()
require('electron-context-menu')({
showSearchWithGoogle: false, showSearchWithGoogle: false,
showSaveImageAs: true, showSaveImageAs: true,
showCopyImageAddress: true, showCopyImageAddress: true,
prepend: (params, browserWindow) => [] prepend: (params, browserWindow) => []
}) })
const localDataStorage = app.getPath('userData') // Grabs the userdata directory based on the user's OS const localDataStorage = app.getPath('userData') // Grabs the userdata directory based on the user's OS
const settingsDb = new Datastore({ const settingsDb = new Datastore({
filename: localDataStorage + '/settings.db', filename: localDataStorage + '/settings.db',
autoload: true autoload: true
}) })
// set app name // set app name
app.setName(productName) app.setName(productName)
// disable electron warning // disable electron warning
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true' process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
const path = require('path') const path = require('path')
const isDev = process.env.NODE_ENV === 'development' const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug') const isDebug = process.argv.includes('--debug')
let mainWindow let mainWindow
let startupUrl let startupUrl
// CORS somehow gets re-enabled in Electron v9.0.4 // CORS somehow gets re-enabled in Electron v9.0.4
// This line disables it. // This line disables it.
// This line can possible be removed if the issue is fixed upstream // This line can possible be removed if the issue is fixed upstream
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors') app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
app.commandLine.appendSwitch('enable-accelerated-video-decode') app.commandLine.appendSwitch('enable-accelerated-video-decode')
app.commandLine.appendSwitch('ignore-gpu-blacklist') app.commandLine.appendSwitch('ignore-gpu-blacklist')
// See: https://stackoverflow.com/questions/45570589/electron-protocol-handler-not-working-on-windows // See: https://stackoverflow.com/questions/45570589/electron-protocol-handler-not-working-on-windows
// remove so we can register each time as we run the app. // remove so we can register each time as we run the app.
app.removeAsDefaultProtocolClient('freetube') app.removeAsDefaultProtocolClient('freetube')
// If we are running a non-packaged version of the app && on windows // If we are running a non-packaged version of the app && on windows
if (isDev && process.platform === 'win32') { if (isDev && process.platform === 'win32') {
// Set the path of electron.exe and your app. // Set the path of electron.exe and your app.
// These two additional parameters are only available on windows. // These two additional parameters are only available on windows.
app.setAsDefaultProtocolClient('freetube', process.execPath, [path.resolve(process.argv[1])]) app.setAsDefaultProtocolClient('freetube', process.execPath, [path.resolve(process.argv[1])])
} else { } else {
app.setAsDefaultProtocolClient('freetube') app.setAsDefaultProtocolClient('freetube')
} }
// TODO: Uncomment if needed // TODO: Uncomment if needed
// only allow single instance of application // only allow single instance of application
if (!isDev) { if (!isDev) {
const gotTheLock = app.requestSingleInstanceLock() const gotTheLock = app.requestSingleInstanceLock()
if (gotTheLock) { if (gotTheLock) {
@ -134,7 +142,7 @@ if (!isDev) {
} else { } else {
app.quit() app.quit()
} }
} else { } else {
require('electron-debug')({ require('electron-debug')({
showDevTools: !(process.env.RENDERER_REMOTE_DEBUGGING === 'true') showDevTools: !(process.env.RENDERER_REMOTE_DEBUGGING === 'true')
}) })
@ -201,9 +209,9 @@ if (!isDev) {
} }
}) })
}) })
} }
async function installDevTools () { async function installDevTools () {
try { try {
/* eslint-disable */ /* eslint-disable */
require('devtron').install() require('devtron').install()
@ -212,9 +220,9 @@ async function installDevTools () {
} catch (err) { } catch (err) {
console.log(err) console.log(err)
} }
} }
function createWindow (useProxy = false, proxyUrl = '') { function createWindow (useProxy = false, proxyUrl = '') {
/** /**
* Initial window options * Initial window options
*/ */
@ -359,9 +367,9 @@ function createWindow (useProxy = false, proxyUrl = '') {
ipcMain.on('disableProxy', () => { ipcMain.on('disableProxy', () => {
mainWindow.webContents.session.setProxy({}) mainWindow.webContents.session.setProxy({})
}) })
} }
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
app.quit() app.quit()
} }
@ -379,18 +387,18 @@ app.on('window-all-closed', () => {
'cachestorage' 'cachestorage'
] ]
}) })
}) })
app.on('activate', () => { app.on('activate', () => {
if (mainWindow === null) { if (mainWindow === null) {
createWindow() createWindow()
} }
}) })
/* /*
* Callback when processing a freetube:// link (macOS) * Callback when processing a freetube:// link (macOS)
*/ */
app.on('open-url', (event, url) => { app.on('open-url', (event, url) => {
event.preventDefault() event.preventDefault()
if (mainWindow && mainWindow.webContents) { if (mainWindow && mainWindow.webContents) {
@ -398,30 +406,30 @@ app.on('open-url', (event, url) => {
} else { } else {
startupUrl = baseUrl(url) startupUrl = baseUrl(url)
} }
}) })
/* /*
* Check if an argument was passed and send it over to the GUI (Linux / Windows). * Check if an argument was passed and send it over to the GUI (Linux / Windows).
* Remove freetube:// protocol if present * Remove freetube:// protocol if present
*/ */
const url = getLinkUrl(process.argv) const url = getLinkUrl(process.argv)
if (url) { if (url) {
startupUrl = url startupUrl = url
} }
function baseUrl(arg) { function baseUrl(arg) {
return arg.replace('freetube://', '') return arg.replace('freetube://', '')
} }
function getLinkUrl(argv) { function getLinkUrl(argv) {
if (argv.length > 1) { if (argv.length > 1) {
return baseUrl(argv[argv.length - 1]) return baseUrl(argv[argv.length - 1])
} else { } else {
return null return null
} }
} }
/** /**
* Auto Updater * Auto Updater
* *
* Uncomment the following code below and install `electron-updater` to * Uncomment the following code below and install `electron-updater` to
@ -429,31 +437,31 @@ function getLinkUrl(argv) {
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
*/ */
/* /*
import { autoUpdater } from 'electron-updater' import { autoUpdater } from 'electron-updater'
autoUpdater.on('update-downloaded', () => { autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall() autoUpdater.quitAndInstall()
}) })
app.on('ready', () => { app.on('ready', () => {
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates() if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
}) })
*/ */
/* eslint-disable-next-line */ /* eslint-disable-next-line */
const sendMenuEvent = async data => { const sendMenuEvent = async data => {
mainWindow.webContents.send('change-view', data) mainWindow.webContents.send('change-view', data)
} }
const template = [{ const template = [{
label: 'File', label: 'File',
submenu: [ submenu: [
{ {
role: 'quit' role: 'quit'
} }
] ]
}, },
{ {
label: 'Edit', label: 'Edit',
submenu: [{ submenu: [{
role: 'cut' role: 'cut'
@ -478,8 +486,8 @@ const template = [{
role: 'selectall' role: 'selectall'
} }
] ]
}, },
{ {
label: 'View', label: 'View',
submenu: [{ submenu: [{
role: 'reload' role: 'reload'
@ -510,8 +518,8 @@ const template = [{
role: 'togglefullscreen' role: 'togglefullscreen'
} }
] ]
}, },
{ {
role: 'window', role: 'window',
submenu: [{ submenu: [{
role: 'minimize' role: 'minimize'
@ -520,10 +528,10 @@ const template = [{
role: 'close' role: 'close'
} }
] ]
} }
] ]
function setMenu () { function setMenu () {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
template.unshift({ template.unshift({
label: app.getName(), label: app.getName(),
@ -553,4 +561,5 @@ function setMenu () {
const menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu) Menu.setApplicationMenu(menu)
}
} }