clovie 0.2.18 ā 0.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +12 -0
- package/bin/cli.js +23 -23
- package/config/clovie.config.js +8 -2
- package/lib/createClovie.js +6 -7
- package/lib/services/Run.js +16 -1
- package/package.json +2 -2
package/AGENTS.md
CHANGED
|
@@ -63,6 +63,15 @@ export default {
|
|
|
63
63
|
|
|
64
64
|
You can mix raw route objects and factories in the same array.
|
|
65
65
|
|
|
66
|
+
### Lifecycle hooks
|
|
67
|
+
|
|
68
|
+
`beforeListen(useContext, opts)` runs before the server listens, and `afterListen(useContext, opts, httpServer)`
|
|
69
|
+
after. On SIGINT, SIGTERM or SIGQUIT, `clovie serve` and `clovie dev` await `beforeStop(useContext, opts)` while
|
|
70
|
+
the server still answers, then close HTTP and exit 0. The whole stop gets `shutdownTimeoutMs` (default 5000);
|
|
71
|
+
past it, or if `beforeStop` throws, the process exits 1. An uncaught exception or unhandled rejection stops the
|
|
72
|
+
same way, and exits 1 however the stop goes. A repeated signal during a stop is ignored. Raise it when `beforeStop` waits on work of its own, and
|
|
73
|
+
give the process manager (Docker's `stop_grace_period`, say) longer still.
|
|
74
|
+
|
|
66
75
|
### CLI commands
|
|
67
76
|
|
|
68
77
|
```
|
|
@@ -219,6 +228,9 @@ Command branching order: `skills` -> `kill` -> `create` -> `command-line-args` m
|
|
|
219
228
|
|
|
220
229
|
Main commands: `build` (production, exits after), `serve` (production, stays alive), `dev`/`watch` (development, stays alive).
|
|
221
230
|
|
|
231
|
+
`serve` and `dev` install the signal handlers before they start, so a signal while they do still runs
|
|
232
|
+
`run.stop()` (`beforeStop`, then the server's stop) under `shutdownTimeoutMs`.
|
|
233
|
+
|
|
222
234
|
Legacy aliases: `watch` -> `dev`, `server` -> `serve`.
|
|
223
235
|
|
|
224
236
|
Config resolution: dynamic `import(optsPath)`, fallback to `config/clovie.config.js`.
|
package/bin/cli.js
CHANGED
|
@@ -327,17 +327,16 @@ async function executeCommand(clovie, command, options) {
|
|
|
327
327
|
|
|
328
328
|
case 'serve':
|
|
329
329
|
console.log('š Starting production server...');
|
|
330
|
-
|
|
331
|
-
// Keep process alive for server
|
|
330
|
+
// Before it starts, so a signal while it does still runs beforeStop rather than ending the process.
|
|
332
331
|
setupGracefulShutdown(clovie);
|
|
332
|
+
await clovie.run.serve();
|
|
333
333
|
break;
|
|
334
334
|
|
|
335
335
|
case 'dev':
|
|
336
336
|
case 'watch':
|
|
337
337
|
console.log('š Starting development server...');
|
|
338
|
-
await clovie.run.dev();
|
|
339
|
-
// Keep process alive for dev server
|
|
340
338
|
setupGracefulShutdown(clovie);
|
|
339
|
+
await clovie.run.dev();
|
|
341
340
|
break;
|
|
342
341
|
|
|
343
342
|
default:
|
|
@@ -347,11 +346,16 @@ async function executeCommand(clovie, command, options) {
|
|
|
347
346
|
}
|
|
348
347
|
}
|
|
349
348
|
|
|
349
|
+
// How long a stop gets before the process is forced out, unless the config's shutdownTimeoutMs says otherwise.
|
|
350
|
+
const DEFAULT_SHUTDOWN_TIMEOUT_MS = 5000;
|
|
351
|
+
|
|
350
352
|
// Setup graceful shutdown handlers
|
|
351
353
|
function setupGracefulShutdown(clovie) {
|
|
352
354
|
let isShuttingDown = false;
|
|
353
355
|
|
|
354
|
-
|
|
356
|
+
// A signal is a stop asked for, and exits 0 if it goes well; a crash (uncaught exception, unhandled
|
|
357
|
+
// rejection) still stops, so beforeStop runs, but always exits 1.
|
|
358
|
+
const gracefulShutdown = async (signal, { crashed = false } = {}) => {
|
|
355
359
|
if (isShuttingDown) {
|
|
356
360
|
console.log('ā ļø Shutdown already in progress...');
|
|
357
361
|
return;
|
|
@@ -360,24 +364,19 @@ function setupGracefulShutdown(clovie) {
|
|
|
360
364
|
isShuttingDown = true;
|
|
361
365
|
console.log(`\nš Received ${signal}, shutting down gracefully...`);
|
|
362
366
|
|
|
363
|
-
//
|
|
367
|
+
// The config's shutdownTimeoutMs, when it needs longer than the default to stop (its beforeStop
|
|
368
|
+
// waits on work of its own). Past it, the process exits 1 with whatever is still going.
|
|
369
|
+
const timeoutMs = clovie.configurator.opts?.shutdownTimeoutMs ?? DEFAULT_SHUTDOWN_TIMEOUT_MS;
|
|
364
370
|
const forceExitTimeout = setTimeout(() => {
|
|
365
|
-
console.log(
|
|
371
|
+
console.log(`ā ļø Shutdown took longer than ${timeoutMs} ms - forcing exit`);
|
|
366
372
|
process.exit(1);
|
|
367
|
-
},
|
|
373
|
+
}, timeoutMs);
|
|
368
374
|
|
|
369
375
|
try {
|
|
370
|
-
|
|
371
|
-
const server = clovie.server;
|
|
372
|
-
if (server && server.isRunning && server.isRunning()) {
|
|
373
|
-
console.log('š Stopping server...');
|
|
374
|
-
await server.stop();
|
|
375
|
-
console.log('ā
Server stopped');
|
|
376
|
-
}
|
|
377
|
-
|
|
376
|
+
await clovie.run.stop();
|
|
378
377
|
clearTimeout(forceExitTimeout);
|
|
379
378
|
console.log('ā
Graceful shutdown complete');
|
|
380
|
-
process.exit(0);
|
|
379
|
+
process.exit(crashed ? 1 : 0);
|
|
381
380
|
} catch (error) {
|
|
382
381
|
clearTimeout(forceExitTimeout);
|
|
383
382
|
console.error('ā Error during shutdown:', error.message);
|
|
@@ -385,20 +384,21 @@ function setupGracefulShutdown(clovie) {
|
|
|
385
384
|
}
|
|
386
385
|
};
|
|
387
386
|
|
|
388
|
-
//
|
|
389
|
-
|
|
390
|
-
process.
|
|
391
|
-
process.
|
|
387
|
+
// Listened to for as long as the process runs: Ctrl-C through `npm run` arrives twice, and the default
|
|
388
|
+
// action of a second signal would end a stop still under way. Repeats find it in progress and are ignored.
|
|
389
|
+
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
390
|
+
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
391
|
+
process.on('SIGQUIT', () => gracefulShutdown('SIGQUIT'));
|
|
392
392
|
|
|
393
393
|
// Handle uncaught exceptions and unhandled rejections
|
|
394
394
|
process.once('uncaughtException', async (error) => {
|
|
395
395
|
console.error('ā Uncaught Exception:', error);
|
|
396
|
-
await gracefulShutdown('uncaughtException');
|
|
396
|
+
await gracefulShutdown('uncaughtException', { crashed: true });
|
|
397
397
|
});
|
|
398
398
|
|
|
399
399
|
process.once('unhandledRejection', async (reason, promise) => {
|
|
400
400
|
console.error('ā Unhandled Rejection at:', promise, 'reason:', reason);
|
|
401
|
-
await gracefulShutdown('unhandledRejection');
|
|
401
|
+
await gracefulShutdown('unhandledRejection', { crashed: true });
|
|
402
402
|
});
|
|
403
403
|
}
|
|
404
404
|
|
package/config/clovie.config.js
CHANGED
|
@@ -40,10 +40,16 @@ export default {
|
|
|
40
40
|
// setup(engine) {},
|
|
41
41
|
|
|
42
42
|
// Lifecycle ā runs right before server.listen()
|
|
43
|
-
// beforeListen(opts) {},
|
|
43
|
+
// beforeListen(useContext, opts) {},
|
|
44
44
|
|
|
45
45
|
// Lifecycle ā runs right after server starts (attach WebSocket upgrades, etc.)
|
|
46
|
-
// afterListen(
|
|
46
|
+
// afterListen(useContext, opts, httpServer) {},
|
|
47
|
+
|
|
48
|
+
// Lifecycle ā on SIGINT/SIGTERM, awaited while the server still answers; then HTTP closes
|
|
49
|
+
// beforeStop(useContext, opts) {},
|
|
50
|
+
|
|
51
|
+
// How long a stop gets, beforeStop included, before the process exits 1 (default 5000)
|
|
52
|
+
// shutdownTimeoutMs: 5000,
|
|
47
53
|
|
|
48
54
|
routes: [
|
|
49
55
|
{
|
package/lib/createClovie.js
CHANGED
|
@@ -47,14 +47,13 @@ export const createClovie = async (config = {}) => {
|
|
|
47
47
|
clovie.install(LiveReload);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
// One install() call, so the services in it may require each other in
|
|
51
|
+
// any order (the engine counts a namespace the same call installs), and
|
|
52
|
+
// awaited, so createClovie resolves only once an async setup is done.
|
|
50
53
|
if (Array.isArray(opts.services)) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
} else {
|
|
55
|
-
clovie.install(service.configure(opts));
|
|
56
|
-
}
|
|
57
|
-
}
|
|
54
|
+
await clovie.install(
|
|
55
|
+
...opts.services.map((service) => (service.configured ? service : service.configure(opts))),
|
|
56
|
+
);
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
// Don't auto-execute commands - let CLI decide
|
package/lib/services/Run.js
CHANGED
|
@@ -40,7 +40,22 @@ export class Run extends ServiceProvider {
|
|
|
40
40
|
await opts.afterListen(useContext, opts, server.getHttpServer());
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
|
-
|
|
43
|
+
|
|
44
|
+
// The config's beforeStop runs to the end while the server still answers, then HTTP closes:
|
|
45
|
+
// the counterpart of beforeListen. A hook that throws rejects this, and the server stays open.
|
|
46
|
+
stop: async () => {
|
|
47
|
+
const opts = configurator.opts;
|
|
48
|
+
if (typeof opts.beforeStop === 'function') {
|
|
49
|
+
await opts.beforeStop(useContext, opts);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const server = useContext('server');
|
|
53
|
+
if (server?.isRunning?.()) {
|
|
54
|
+
log.info('š Stopping server...');
|
|
55
|
+
await server.stop();
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
|
|
44
59
|
dev: async () => {
|
|
45
60
|
const [relay, liveReload, server] = useContext('relay', 'liveReload', 'server');
|
|
46
61
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clovie",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.20",
|
|
4
4
|
"description": "Vintage web dev tooling with modern quality of life",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"static-site-generator",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"update:jucie.io": "npx npm-check-updates '@jucie.io/*' --target latest -u && npm install"
|
|
86
86
|
},
|
|
87
87
|
"dependencies": {
|
|
88
|
-
"@jucie.io/engine": "^1.1.
|
|
88
|
+
"@jucie.io/engine": "^1.1.85",
|
|
89
89
|
"@jucie.io/engine-esbuild": "^1.0.22",
|
|
90
90
|
"@jucie.io/engine-server": "^1.0.46",
|
|
91
91
|
"@jucie.io/reactive": "^1.1.7",
|