68 lines
2.0 KiB
JavaScript
68 lines
2.0 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { program } from 'commander';
|
|
import { existsSync, lstatSync } from 'node:fs';
|
|
import { resolve, join } from 'node:path';
|
|
import pkg from '../package.json' assert { type: 'json' };
|
|
import WebServer from './runner.js';
|
|
|
|
function validatePath(path) {
|
|
const fullPath = resolve(path);
|
|
console.log('Validating path:', fullPath);
|
|
|
|
if (!path) {
|
|
throw new Error('Build directory path must be provided');
|
|
}
|
|
|
|
if (!existsSync(fullPath)) {
|
|
throw new Error(`Invalid path provided. The directory at "${fullPath}" doesn't exist`);
|
|
}
|
|
|
|
if (!lstatSync(fullPath).isDirectory()) {
|
|
throw new Error(`Invalid path provided. The path "${fullPath}" must be a directory`);
|
|
}
|
|
|
|
// Check for SvelteKit adapter-node structure
|
|
const indexPath = join(fullPath, 'index.js');
|
|
if (!existsSync(indexPath)) {
|
|
throw new Error(`SvelteKit handler not found at "${indexPath}". Make sure you've built the project with @sveltejs/adapter-node`);
|
|
}
|
|
|
|
return fullPath;
|
|
}
|
|
|
|
try {
|
|
program
|
|
.name(Object.keys(pkg.bin)[0])
|
|
.description('Start the SvelteKit server')
|
|
.version(pkg.version)
|
|
.argument('<buildDir>', 'Location of the SvelteKit build directory (containing index.js)')
|
|
.action(async (buildDir) => {
|
|
try {
|
|
console.log('Starting SvelteKit server...');
|
|
const fullPath = validatePath(buildDir);
|
|
const server = await WebServer.create(fullPath);
|
|
|
|
// Handle graceful shutdown
|
|
process.on('SIGINT', () => {
|
|
console.log('\nShutting down server...');
|
|
if (server && server.close) {
|
|
server.close(() => {
|
|
console.log('Server shutdown complete');
|
|
process.exit(0);
|
|
});
|
|
} else {
|
|
process.exit(0);
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Error:', error.message);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
|
|
program.parse();
|
|
} catch (error) {
|
|
console.error('Failed to start CLI:', error.message);
|
|
process.exit(1);
|
|
} |