From fb8535543e5b03794227ceae75aab5e672e05681 Mon Sep 17 00:00:00 2001 From: Tomasz Pluskiewicz Date: Wed, 20 Nov 2024 15:54:08 +0100 Subject: [PATCH] refactor: run in child process to avoid module caching issues --- packages/cli/index.ts | 12 +++++- packages/cli/lib/command/serve.ts | 62 ++++++++++++++----------------- packages/cli/lib/serve.ts | 0 3 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 packages/cli/lib/serve.ts diff --git a/packages/cli/index.ts b/packages/cli/index.ts index d5e7f59..39eb86a 100755 --- a/packages/cli/index.ts +++ b/packages/cli/index.ts @@ -1,9 +1,9 @@ import 'ulog' +import { fork } from 'node:child_process' import { program } from 'commander' import { variable } from './lib/options.js' import deploy from './lib/command/deploy.js' import build from './lib/command/build.js' -import serve from './lib/command/serve.js' program.name('kopflos') @@ -17,7 +17,15 @@ program.command('serve') .option('--trust-proxy [proxy]', 'Trust the X-Forwarded-Host header') .option('--watch', 'Enable watching for changes') .option('--no-watch', 'Disable watching for changes') - .action(serve) + .action((options) => { + (function serve() { + const proc = fork(new URL('./lib/command/serve.js', import.meta.url)) + + proc.send(options) + + proc.on('exit', serve) + })() + }) program.command('build') .option('-c, --config ', 'Path to config file') diff --git a/packages/cli/lib/command/serve.ts b/packages/cli/lib/command/serve.ts index 7e7259b..391393f 100644 --- a/packages/cli/lib/command/serve.ts +++ b/packages/cli/lib/command/serve.ts @@ -1,4 +1,4 @@ -import type http from 'http' +import 'ulog' import log from '@kopflos-cms/logger' import express from 'express' import * as chokidar from 'chokidar' @@ -22,7 +22,7 @@ declare module '@kopflos-cms/core' { } } -export default async function ({ +async function run({ mode: _mode = 'production', watch = _mode === 'development', config, @@ -40,7 +40,7 @@ export default async function ({ } if (mode === 'development' && watch === false) { - log.warn('Watch mode disabled in development mode') + log.warn('Watch disabled in development mode') } const { config: loadedConfig, filepath: configPath } = await loadConfig({ @@ -57,45 +57,39 @@ export default async function ({ }, } - let server: http.Server + const app = express() - async function startServer() { - const app = express() - - if (trustProxy) { - app.set('trust proxy', trustProxy) - } - - const { instance, middleware } = await kopflos(finalOptions) - app.use(middleware) + if (trustProxy) { + app.set('trust proxy', trustProxy) + } - await instance.start() + const { instance, middleware } = await kopflos(finalOptions) + app.use(middleware) - return new Promise((resolve) => { - const server = app.listen(port, host, () => { - log.info(`Server running on ${port}. API URL: ${finalOptions.baseIri}`) - resolve(server) - }) + await instance.start() - server.on('close', () => { - instance.stop() - }) - }) - } - - server = await startServer() + const server = app.listen(port, host, () => { + log.info(`Server running on ${port}. API URL: ${finalOptions.baseIri}`) + }) if (finalOptions.watch) { log.info(`Watch mode. Watching for changes in: ${finalOptions.watch.join(', ')}`) + async function restartServer(path: string) { + log.info('Changes detected, restarting server') + log.debug(`Changed file: ${path}`) - chokidar.watch(finalOptions.watch) - .on('change', async (path) => { - log.info('Changes detected, restarting server') - log.debug(`Changed file: ${path}`) + await instance.stop() + server.close() + process.exit(1) + } - server.close(async () => { - server = await startServer() - }) - }) + chokidar.watch(finalOptions.watch, { + ignoreInitial: true, + }) + .on('change', restartServer) + .on('add', restartServer) + .on('unlink', restartServer) } } + +process.on('message', run) diff --git a/packages/cli/lib/serve.ts b/packages/cli/lib/serve.ts new file mode 100644 index 0000000..e69de29