clovie 0.2.19 → 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 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
- await clovie.run.serve();
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
- const gracefulShutdown = async (signal) => {
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
- // Set a timeout to force exit if shutdown hangs
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('āš ļø Shutdown timeout - forcing exit');
371
+ console.log(`āš ļø Shutdown took longer than ${timeoutMs} ms - forcing exit`);
366
372
  process.exit(1);
367
- }, 5000); // 5 second timeout
373
+ }, timeoutMs);
368
374
 
369
375
  try {
370
- // Stop server if it's running
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
- // Handle multiple termination signals (using once to prevent duplicates)
389
- process.once('SIGINT', () => gracefulShutdown('SIGINT'));
390
- process.once('SIGTERM', () => gracefulShutdown('SIGTERM'));
391
- process.once('SIGQUIT', () => gracefulShutdown('SIGQUIT'));
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
 
@@ -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(httpServer, opts) {},
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
  {
@@ -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.19",
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",