Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions packages/chronicle/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { spawn } from 'node:child_process';
import chalk from 'chalk';
import { Command } from 'commander';
import { loadCLIConfig } from '@/cli/utils/config';
import { PACKAGE_ROOT } from '@/cli/utils/resolve';
import { linkContent } from '@/cli/utils/scaffold';

function resolveServerEntry(projectRoot: string, preset?: string): string {
if (preset === 'vercel' || preset === 'vercel-static') {
return path.resolve(projectRoot, '.vercel/output/server/index.mjs');
}
return path.resolve(projectRoot, '.output/server/index.mjs');
}

export const startCommand = new Command('start')
.description('Start production server')
.option('-p, --port <port>', 'Port number', '3000')
.option('--config <path>', 'Path to chronicle.yaml')
.option('--host <host>', 'Host address', 'localhost')
.option('--host <host>', 'Host address', '0.0.0.0')
.option('--preset <preset>', 'Deploy preset (must match build preset)')
.action(async options => {
const { config, projectRoot, configPath } = await loadCLIConfig(options.config);
const port = parseInt(options.port, 10);
await linkContent(projectRoot, config);
const { config, projectRoot } = await loadCLIConfig(options.config);
const preset = options.preset ?? config.preset;
const serverEntry = resolveServerEntry(projectRoot, preset);

console.log(chalk.cyan('Starting production server...'));
const exists = await fs.access(serverEntry).then(() => true, () => false);
if (!exists) {
console.error(chalk.red(`No build found at ${serverEntry}`));
console.error(chalk.red('Run `chronicle build` first.'));
process.exit(1);
}

const { preview } = await import('vite');
const { createViteConfig } = await import('@/server/vite-config');
console.log(chalk.cyan('Starting production server...'));

const viteConfig = await createViteConfig({ packageRoot: PACKAGE_ROOT, projectRoot, configPath });
const server = await preview({
...viteConfig,
preview: { port, host: options.host }
const child = spawn(process.execPath, [serverEntry], {
stdio: 'inherit',
env: {
...process.env,
PORT: options.port,
HOST: options.host,
},
});

server.printUrls();

let shuttingDown = false;
const shutdown = async () => {
const shutdown = () => {
if (shuttingDown) return;
shuttingDown = true;
try {
await server.close();
} catch { /* ignore close errors */ }
process.exit(0);
child.kill('SIGTERM');
} catch { /* ignore if already exited */ }
};
process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);

child.on('exit', (code) => {
process.exit(code ?? 0);
});
});
Loading