2020-02-16 18:30:00 +00: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')
|
|
|
|
|
|
|
|
const mainConfig = require('./webpack.main.config')
|
|
|
|
const rendererConfig = require('./webpack.renderer.config')
|
|
|
|
const workersConfig = require('./webpack.workers.config')
|
|
|
|
|
|
|
|
let electronProcess = null
|
|
|
|
let manualRestart = null
|
|
|
|
const remoteDebugging = !!(
|
|
|
|
process.argv[2] && process.argv[2] === '--remote-debug'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (remoteDebugging) {
|
|
|
|
// disable dvtools open in electron
|
|
|
|
process.env.RENDERER_REMOTE_DEBUGGING = true
|
|
|
|
}
|
|
|
|
|
2021-06-16 04:30:01 +00: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 17:58:56 +00:00
|
|
|
const port = 9080
|
|
|
|
|
2020-02-16 18:30:00 +00: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'),
|
2021-05-10 04:59:48 +00:00
|
|
|
// '--enable-logging', Enable to show logs from all electron processes
|
2020-02-16 18:30:00 +00:00
|
|
|
remoteDebugging ? '--inspect=9222' : '',
|
|
|
|
remoteDebugging ? '--remote-debugging-port=9223' : '',
|
|
|
|
])
|
|
|
|
|
2021-06-16 04:30:01 +00:00
|
|
|
electronProcess.on('exit', (code, _) => {
|
|
|
|
if (code === relaunchExitCode) {
|
|
|
|
electronProcess = null
|
|
|
|
restartElectron()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-16 18:30:00 +00:00
|
|
|
if (!manualRestart) process.exit(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function startMain() {
|
|
|
|
const webpackSetup = webpack([mainConfig, workersConfig])
|
|
|
|
|
|
|
|
webpackSetup.compilers.forEach(compiler => {
|
|
|
|
const { name } = compiler
|
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case 'workers':
|
|
|
|
compiler.hooks.afterEmit.tap('afterEmit', async () => {
|
|
|
|
console.log(`\nCompiled ${name} script!`)
|
|
|
|
console.log(`\nWatching file changes for ${name} script...`)
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'main':
|
|
|
|
default:
|
|
|
|
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...`)
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
webpackSetup.watch({
|
|
|
|
aggregateTimeout: 500,
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
if (err) console.error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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...`)
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = new WebpackDevServer(compiler, {
|
2021-09-23 07:04:00 +00:00
|
|
|
static: {
|
|
|
|
directory: path.join(process.cwd(), 'static'),
|
|
|
|
watch: {
|
2022-05-29 22:33:27 +00:00
|
|
|
ignored: [
|
|
|
|
/(dashFiles|storyboards)\/*/,
|
|
|
|
'/**/.DS_Store',
|
|
|
|
]
|
2021-09-23 07:04:00 +00:00
|
|
|
}
|
|
|
|
},
|
2021-06-19 17:58:56 +00:00
|
|
|
port
|
2020-02-16 18:30:00 +00:00
|
|
|
})
|
|
|
|
|
2021-06-19 17:58:56 +00:00
|
|
|
server.listen(port, '', err => {
|
2020-02-16 18:30:00 +00:00
|
|
|
if (err) console.error(err)
|
|
|
|
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
startRenderer(startMain)
|