From 3f7f2573d8eb3e5ad6fe27b162b1abaec3738f67 Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 7 Feb 2024 19:33:57 -0600 Subject: [PATCH] fix: spawning --- packages/build/build/commands.js | 120 ++++++++++-------- .../server/build/server.webpack.config.js | 1 - packages/server/build/start.js | 25 ++-- 3 files changed, 76 insertions(+), 70 deletions(-) diff --git a/packages/build/build/commands.js b/packages/build/build/commands.js index e6de9d5..a920f30 100644 --- a/packages/build/build/commands.js +++ b/packages/build/build/commands.js @@ -229,70 +229,80 @@ exports.commands = (program, flecks) => { '--config', webpackConfig, '--mode', (production && !hot) ? 'production' : 'development', ]; + const options = { + env: { + FLECKS_BUILD_IS_PRODUCTION: production, + ...(target ? {FLECKS_CORE_BUILD_LIST: target} : {}), + ...(hot ? {FLECKS_ENV__flecks_server__hot: 'true'} : {}), + }, + useFork: true, + }; + if (!watch) { + return spawnWith(cmd, options); + } + await rootsDependencies(flecks.roots, flecks.resolver); + const watched = Object.keys(dependencies); + watched.push( + ...await Promise.all( + flecks.roots.map(([, request]) => flecks.resolver.resolve(join(request, 'package.json'))), + ), + ); + watched.push(join(FLECKS_CORE_ROOT, 'build/flecks.yml')); + const watcher = chokidar.watch(watched, { + awaitWriteFinish: { + stabilityThreshold: 50, + pollInterval: 5, + }, + }); let webpack; const spawnWebpack = () => { - webpack = spawnWith( - cmd, - { - env: { - FLECKS_BUILD_IS_PRODUCTION: production, - ...(target ? {FLECKS_CORE_BUILD_LIST: target} : {}), - ...(hot ? {FLECKS_ENV__flecks_server__hot: 'true'} : {}), - }, - useFork: true, - }, - ); + webpack = spawnWith(cmd, options); webpack.on('message', (message) => { - if ('restart' === message) { - webpack.kill(); - spawnWebpack(); + switch (message) { + case 'kill': + debug('killing...'); + webpack.kill(); + watcher.close(); + break; + case 'restart': + debug('restarting webpack...'); + webpack.kill(); + spawnWebpack(); + break; + default: } }); }; spawnWebpack(); - if (watch) { - await rootsDependencies(flecks.roots, flecks.resolver); - const watched = Object.keys(dependencies); - watched.push( - ...await Promise.all( - flecks.roots.map(([, request]) => flecks.resolver.resolve(join(request, 'package.json'))), - ), - ); - watched.push(join(FLECKS_CORE_ROOT, 'build/flecks.yml')); - const watcher = chokidar.watch(watched, { - awaitWriteFinish: { - stabilityThreshold: 50, - pollInterval: 5, - }, - }); - await new Promise((resolve, reject) => { - watcher.on('error', reject); - watcher.on('ready', resolve); - }); - const configPath = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml'); - const initialConfig = loadYml(await readFile(configPath)); - watcher.on('all', async (event, path) => { - let respawn = false; - if (configPath === path) { - const config = loadYml(await readFile(configPath)); - if ( - JSON.stringify(Object.keys(initialConfig).sort()) - !== JSON.stringify(Object.keys(config).sort()) - ) { - debug('Config keys changed'); - respawn = true; - } - } - else { + await new Promise((resolve, reject) => { + watcher.on('error', reject); + watcher.on('ready', resolve); + }); + const configPath = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml'); + const initialConfig = loadYml(await readFile(configPath)); + watcher.on('all', async (event, path) => { + let respawn = false; + if (configPath === path) { + const config = loadYml(await readFile(configPath)); + if ( + JSON.stringify(Object.keys(initialConfig).sort()) + !== JSON.stringify(Object.keys(config).sort()) + ) { + debug('Config keys changed'); respawn = true; } - if (respawn) { - debug('Respawning...'); - webpack.kill(); - spawnWebpack(); - } - }); - } + } + else { + respawn = true; + } + if (respawn) { + debug('restarting webpack...'); + webpack.kill(); + spawnWebpack(); + } + }); + // Persist... + return new Promise(() => {}); }, }; } diff --git a/packages/server/build/server.webpack.config.js b/packages/server/build/server.webpack.config.js index 13a1c8e..139b72b 100644 --- a/packages/server/build/server.webpack.config.js +++ b/packages/server/build/server.webpack.config.js @@ -62,7 +62,6 @@ module.exports = async (env, argv, flecks) => { NODE_PRESERVE_SYMLINKS: flecks.roots.some(([path, request]) => path !== request) ? 1 : 0, }, exec: 'index.js', - killOnExit: !hot, // Bail hard on unhandled rejections and report. nodeArgs: [...nodeArgs, '--unhandled-rejections=strict', '--trace-uncaught'], // HMR. diff --git a/packages/server/build/start.js b/packages/server/build/start.js index d211687..4dc9832 100644 --- a/packages/server/build/start.js +++ b/packages/server/build/start.js @@ -92,20 +92,17 @@ class StartServerPlugin { ...(inspectPort && {inspectPort}), }); this.worker = cluster.fork(env); - if (killOnExit) { - this.worker.on('exit', (code) => { - process.exit(code); - }); - } - else { - this.worker.on('disconnect', () => { - if (this.worker.exitedAfterDisconnect) { - // eslint-disable-next-line no-console - console.error('[HMR] Restarting application...'); - process.send('restart'); - } - }); - } + this.worker.on('disconnect', () => { + if (this.worker.exitedAfterDisconnect) { + // eslint-disable-next-line no-console + console.error('[HMR] Restarting application...'); + process.send('restart'); + } + else if (killOnExit) { + process.send('kill'); + process.exit(0); + } + }); return new Promise((resolve, reject) => { this.worker.on('error', reject); this.worker.on('online', resolve);