2020-02-16 19:30:00 +01:00
|
|
|
process.env.NODE_ENV = 'development'
|
|
|
|
|
|
|
|
const electron = require('electron')
|
|
|
|
const webpack = require('webpack')
|
|
|
|
const WebpackDevServer = require('webpack-dev-server')
|
|
|
|
const kill = require('tree-kill')
|
|
|
|
|
|
|
|
const path = require('path')
|
|
|
|
const { spawn } = require('child_process')
|
|
|
|
|
2024-05-14 18:35:50 +02:00
|
|
|
const ProcessLocalesPlugin = require('./ProcessLocalesPlugin')
|
|
|
|
|
2020-02-16 19:30:00 +01:00
|
|
|
let electronProcess = null
|
|
|
|
let manualRestart = null
|
2022-10-18 14:50:32 +02:00
|
|
|
|
|
|
|
const remoteDebugging = process.argv.indexOf('--remote-debug') !== -1
|
|
|
|
const web = process.argv.indexOf('--web') !== -1
|
2020-02-16 19:30:00 +01:00
|
|
|
|
2023-02-04 08:04:44 +01:00
|
|
|
let mainConfig
|
|
|
|
let rendererConfig
|
|
|
|
let webConfig
|
|
|
|
|
|
|
|
if (!web) {
|
|
|
|
mainConfig = require('./webpack.main.config')
|
|
|
|
rendererConfig = require('./webpack.renderer.config')
|
|
|
|
} else {
|
|
|
|
webConfig = require('./webpack.web.config')
|
|
|
|
}
|
|
|
|
|
2020-02-16 19:30:00 +01:00
|
|
|
if (remoteDebugging) {
|
|
|
|
// disable dvtools open in electron
|
|
|
|
process.env.RENDERER_REMOTE_DEBUGGING = true
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:30:01 +02:00
|
|
|
// Define exit code for relaunch and set it in the environment
|
|
|
|
const relaunchExitCode = 69
|
|
|
|
process.env.FREETUBE_RELAUNCH_EXIT_CODE = relaunchExitCode
|
|
|
|
|
2021-06-19 19:58:56 +02:00
|
|
|
const port = 9080
|
|
|
|
|
2020-02-16 19:30:00 +01:00
|
|
|
async function killElectron(pid) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (pid) {
|
|
|
|
kill(pid, err => {
|
|
|
|
if (err) reject(err)
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function restartElectron() {
|
|
|
|
console.log('\nStarting electron...')
|
|
|
|
|
|
|
|
const { pid } = electronProcess || {}
|
|
|
|
await killElectron(pid)
|
|
|
|
|
|
|
|
electronProcess = spawn(electron, [
|
|
|
|
path.join(__dirname, '../dist/main.js'),
|
2022-10-25 04:33:08 +02:00
|
|
|
// '--enable-logging', // Enable to show logs from all electron processes
|
2020-02-16 19:30:00 +01:00
|
|
|
remoteDebugging ? '--inspect=9222' : '',
|
2022-10-25 04:33:08 +02:00
|
|
|
remoteDebugging ? '--remote-debugging-port=9223' : ''
|
|
|
|
],
|
|
|
|
// { stdio: 'inherit' } // required for logs to actually appear in the stdout
|
|
|
|
)
|
2020-02-16 19:30:00 +01:00
|
|
|
|
2021-06-16 06:30:01 +02:00
|
|
|
electronProcess.on('exit', (code, _) => {
|
|
|
|
if (code === relaunchExitCode) {
|
|
|
|
electronProcess = null
|
|
|
|
restartElectron()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-16 19:30:00 +01:00
|
|
|
if (!manualRestart) process.exit(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-14 18:35:50 +02:00
|
|
|
/**
|
|
|
|
* @param {import('webpack').Compiler} compiler
|
|
|
|
* @param {WebpackDevServer} devServer
|
|
|
|
*/
|
|
|
|
function setupNotifyLocaleUpdate(compiler, devServer) {
|
|
|
|
const notifyLocaleChange = (updatedLocales) => {
|
|
|
|
devServer.sendMessage(devServer.webSocketServer.clients, "freetube-locale-update", updatedLocales)
|
|
|
|
}
|
|
|
|
|
|
|
|
compiler.options.plugins
|
|
|
|
.filter(plugin => plugin instanceof ProcessLocalesPlugin)
|
|
|
|
.forEach((/** @type {ProcessLocalesPlugin} */plugin) => {
|
|
|
|
plugin.notifyLocaleChange = notifyLocaleChange
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-16 19:30:00 +01:00
|
|
|
function startMain() {
|
2022-11-06 09:17:33 +01:00
|
|
|
const compiler = webpack(mainConfig)
|
|
|
|
const { name } = compiler
|
|
|
|
|
|
|
|
compiler.hooks.afterEmit.tap('afterEmit', async () => {
|
|
|
|
console.log(`\nCompiled ${name} script!`)
|
|
|
|
|
|
|
|
manualRestart = true
|
|
|
|
await restartElectron()
|
|
|
|
setTimeout(() => {
|
|
|
|
manualRestart = false
|
|
|
|
}, 2500)
|
|
|
|
|
|
|
|
console.log(`\nWatching file changes for ${name} script...`)
|
2020-02-16 19:30:00 +01:00
|
|
|
})
|
|
|
|
|
2022-11-06 09:17:33 +01:00
|
|
|
compiler.watch({
|
2020-02-16 19:30:00 +01:00
|
|
|
aggregateTimeout: 500,
|
|
|
|
},
|
2022-11-06 09:17:33 +01:00
|
|
|
err => {
|
|
|
|
if (err) console.error(err)
|
|
|
|
})
|
2020-02-16 19:30:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function startRenderer(callback) {
|
|
|
|
const compiler = webpack(rendererConfig)
|
|
|
|
const { name } = compiler
|
|
|
|
|
|
|
|
compiler.hooks.afterEmit.tap('afterEmit', () => {
|
|
|
|
console.log(`\nCompiled ${name} script!`)
|
|
|
|
console.log(`\nWatching file changes for ${name} script...`)
|
|
|
|
})
|
|
|
|
|
2022-06-01 13:12:30 +02:00
|
|
|
const server = new WebpackDevServer({
|
2021-09-23 09:04:00 +02:00
|
|
|
static: {
|
2024-04-18 03:09:29 +02:00
|
|
|
directory: path.resolve(__dirname, '..', 'static'),
|
2021-09-23 09:04:00 +02:00
|
|
|
watch: {
|
2022-05-30 00:33:27 +02:00
|
|
|
ignored: [
|
|
|
|
/(dashFiles|storyboards)\/*/,
|
|
|
|
'/**/.DS_Store',
|
2024-05-14 18:35:50 +02:00
|
|
|
'**/static/locales/*'
|
2022-05-30 00:33:27 +02:00
|
|
|
]
|
2024-04-18 03:09:29 +02:00
|
|
|
},
|
|
|
|
publicPath: '/static'
|
2021-09-23 09:04:00 +02:00
|
|
|
},
|
2021-06-19 19:58:56 +02:00
|
|
|
port
|
2022-06-01 13:12:30 +02:00
|
|
|
}, compiler)
|
2020-02-16 19:30:00 +01:00
|
|
|
|
2022-06-01 13:12:30 +02:00
|
|
|
server.startCallback(err => {
|
2020-02-16 19:30:00 +01:00
|
|
|
if (err) console.error(err)
|
|
|
|
|
2024-05-14 18:35:50 +02:00
|
|
|
setupNotifyLocaleUpdate(compiler, server)
|
|
|
|
|
2020-02-16 19:30:00 +01:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-25 22:05:03 +02:00
|
|
|
function startWeb () {
|
2022-10-18 14:50:32 +02:00
|
|
|
const compiler = webpack(webConfig)
|
|
|
|
const { name } = compiler
|
|
|
|
|
|
|
|
compiler.hooks.afterEmit.tap('afterEmit', () => {
|
|
|
|
console.log(`\nCompiled ${name} script!`)
|
|
|
|
console.log(`\nWatching file changes for ${name} script...`)
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = new WebpackDevServer({
|
2024-04-25 22:05:03 +02:00
|
|
|
open: true,
|
2022-10-18 14:50:32 +02:00
|
|
|
static: {
|
2024-05-14 18:35:50 +02:00
|
|
|
directory: path.resolve(__dirname, '..', 'static'),
|
2022-10-18 14:50:32 +02:00
|
|
|
watch: {
|
|
|
|
ignored: [
|
|
|
|
/(dashFiles|storyboards)\/*/,
|
|
|
|
'/**/.DS_Store',
|
2024-05-14 18:35:50 +02:00
|
|
|
'**/static/locales/*'
|
2022-10-18 14:50:32 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
port
|
|
|
|
}, compiler)
|
|
|
|
|
|
|
|
server.startCallback(err => {
|
|
|
|
if (err) console.error(err)
|
2024-05-14 18:35:50 +02:00
|
|
|
|
|
|
|
setupNotifyLocaleUpdate(compiler, server)
|
2022-10-18 14:50:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (!web) {
|
|
|
|
startRenderer(startMain)
|
|
|
|
} else {
|
2024-04-25 22:05:03 +02:00
|
|
|
startWeb()
|
2022-10-18 14:50:32 +02:00
|
|
|
}
|