/** * Waggle server installer and launcher. * * npx install download the server for this system and install it * waggle-server start it and open the browser (same as `run`) * waggle-server doctor check the installation * waggle-server update install this tool's version over the installed one * waggle-server uninstall remove the program; the data directory stays * * The tool is one self-contained script. Install copies it next to the * payload, and the shim runs that copy with the payload's own Node. */ import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import { formatHelp, parseArgs } from './args.js'; import { UserError, doctor, install, run, uninstall, type Context } from './commands.js'; import { currentHost } from './platform.js'; declare const __WAGGLE_SERVER_VERSION__: string | undefined; const VERSION: string = typeof __WAGGLE_SERVER_VERSION__ === 'string' ? __WAGGLE_SERVER_VERSION__ : (JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')) as { version: string }).version; const MIN_NODE_MAJOR = 20; /** The bundled script can be copied into an install; a TypeScript source cannot run on its own. */ function launcherSource(): string | null { const self = fileURLToPath(import.meta.url); return /\.m?js$/.test(self) ? self : null; } async function main(): Promise { const major = Number(process.versions.node.split('.')[0]); if (major < MIN_NODE_MAJOR) { console.error(`\n Waggle needs Node.js ${MIN_NODE_MAJOR} or newer to install. This is ${process.versions.node}.\n`); return 1; } const { command, options, error } = parseArgs(process.argv.slice(2)); if (error) { console.error(`\n ${error}`); console.error(formatHelp(VERSION)); return 1; } const ctx: Context = { host: currentHost(), version: VERSION, launcherSource: launcherSource(), out: (line) => console.log(line), }; switch (command) { case 'help': console.log(formatHelp(VERSION)); return 0; case 'version': console.log(VERSION); return 0; case 'install': await install(ctx, options); return 0; case 'update': await install(ctx, options, 'update'); return 0; case 'doctor': return doctor(ctx, options); case 'uninstall': return uninstall(ctx, options); case 'run': return run(ctx, options); } } main().then( (code) => { process.exitCode = code; }, (error: unknown) => { console.error(`\n ${error instanceof Error ? error.message : String(error)}\n`); if (!(error instanceof UserError) && error instanceof Error && error.stack) console.error(error.stack); process.exitCode = 1; }, );