Lines 6-46javascript
6import { existsSync } from 'node:fs';
7import { readFile } from 'node:fs/promises';
8import { homedir } from 'node:os';
9import { dirname, join } from 'node:path';
10import { fileURLToPath } from 'node:url';
12const rootDir = join(dirname(fileURLToPath(import.meta.url)), '..');
13const cliEntry = join(rootDir, 'dist', 'cli', 'index.js');
14const buildEntry = join(rootDir, 'build', 'index.js');
16// `serve` is the one long-running command whose work outlives the CLI promise:
17// the imported adapter-node server starts listening and then the import resolves,
18// so the listening socket keeps the process alive on its own. Every other command
19// is one-shot, and the bundled graph (Ink/clack touch stdin at import) leaves a
20// module-level handle that would otherwise stop the process exiting — so we force
21// a clean exit for those, but NOT for `serve` (which must keep running).
22const firstArg = process.argv.slice(2).find((a) => !a.startsWith('-'));
23const isLongRunning = firstArg === 'serve';
25// ── 1. Prefer the CLI bundle ──────────────────────────────────────────────────
26if (existsSync(cliEntry)) {
27 await import(cliEntry);
28 // One-shot commands must be force-exited: the bundled graph (Ink/clack touch
MediumDynamic Require
Package source references dynamic require/import behavior.
bin/chaching.jsView on unpkg · L26 29 // stdin at import) leaves a module-level handle that blocks a natural exit.
30 // `serve` is exempt — its listening socket keeps the process alive on its own.
31 if (!isLongRunning) process.exit(0);
33 // ── 2. Legacy fallback: bare server boot ─────────────────────────────────────
34 // Reached only when dist/cli/ is missing (e.g. `npm run build:sk` without
35 // `build:cli`). Warn the user so they know to run the full build.
36 console.error('[chaching] CLI bundle not found at dist/cli/index.js.');
37 console.error('Run `npm run build:cli` (or `npm run build`) to build it.');
38 console.error('Falling back to direct server boot for `chaching serve`...\n');
40 if (!existsSync(buildEntry)) {
41 console.error('[chaching] SvelteKit build artifact also missing. Run `npm run build`.');
45 const configHome = process.env.XDG_CONFIG_HOME?.trim() || join(homedir(), '.config');
46 const configFile = join(configHome, 'chaching', 'config.json');