Skip to content

Commit

Permalink
refactor: run in child process to avoid module caching issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 20, 2024
1 parent 2f3bc06 commit fb85355
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
12 changes: 10 additions & 2 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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 <config>', 'Path to config file')
Expand Down
62 changes: 28 additions & 34 deletions packages/cli/lib/command/serve.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -22,7 +22,7 @@ declare module '@kopflos-cms/core' {
}
}

export default async function ({
async function run({
mode: _mode = 'production',
watch = _mode === 'development',
config,
Expand All @@ -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({
Expand All @@ -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<http.Server>((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)
Empty file added packages/cli/lib/serve.ts
Empty file.

0 comments on commit fb85355

Please sign in to comment.