import { COMMAND_NAME } from './platform.js'; export type Command = 'install' | 'update' | 'run' | 'doctor' | 'uninstall' | 'help' | 'version'; export interface Options { /** Archive path or URL; replaces the default download location. */ from?: string; /** Expected SHA-256 of the archive. */ sha256?: string; /** Install directory. */ dir?: string; /** Directory for the command shim. */ binDir?: string; /** Data directory passed to the server as WAGGLE_DATA_DIR. */ dataDir?: string; port: number; open: boolean; force: boolean; } export interface ParsedArgs { command: Command; options: Options; error?: string; } export const DEFAULT_PORT = 3333; const COMMANDS: ReadonlySet = new Set(['install', 'update', 'run', 'doctor', 'uninstall', 'help', 'version']); const VALUE_FLAGS: Record = { '--from': 'from', '--sha256': 'sha256', '--dir': 'dir', '--bin-dir': 'binDir', '--data-dir': 'dataDir', }; /** `args` is argv without the node and script entries. With no command, `run`. */ export function parseArgs(args: string[]): ParsedArgs { const options: Options = { port: DEFAULT_PORT, open: true, force: false }; let command: Command | undefined; const fail = (error: string): ParsedArgs => ({ command: command ?? 'run', options, error }); for (let i = 0; i < args.length; i++) { const arg = args[i]; if (arg === '--help' || arg === '-h') { command = 'help'; } else if (arg === '--version' || arg === '-v') { command = 'version'; } else if (arg === '--no-open') { options.open = false; } else if (arg === '--force') { options.force = true; } else if (arg === '--port' || arg === '-p') { const value = args[++i]; const port = Number(value); if (value === undefined || !Number.isInteger(port) || port < 1 || port > 65_535) { return fail(`Invalid port: ${value ?? '(missing)'}. Use a number between 1 and 65535.`); } options.port = port; } else if (arg in VALUE_FLAGS) { const value = args[++i]; if (value === undefined || value.startsWith('--')) return fail(`Missing value for ${arg}.`); (options[VALUE_FLAGS[arg]] as string) = value; } else if (arg.startsWith('-')) { return fail(`Unknown option: ${arg}`); } else if (command === undefined && COMMANDS.has(arg)) { command = arg as Command; } else { return fail(`Unknown command: ${arg}`); } } if (options.sha256 !== undefined && !/^[0-9a-fA-F]{64}$/.test(options.sha256)) { return fail('--sha256 takes the 64-character hex digest of the archive.'); } return { command: command ?? 'run', options }; } export function formatHelp(version: string): string { return ` Waggle server ${version} Usage: ${COMMAND_NAME} [options] Commands: install Download and install the Waggle server for this system update Replace the installed server with this version (or --from) run Start the server and open it in the browser (default) doctor Check the installation and report problems uninstall Remove the installed server; your data stays Options: --from Install from this archive instead of the release download --sha256 Expected SHA-256 of the archive --dir Install directory --bin-dir Where to put the ${COMMAND_NAME} command --data-dir Data directory (default: $WAGGLE_DATA_DIR or ~/.waggle) --port, -p Server port (default: ${DEFAULT_PORT}) --no-open Do not open the browser --force Reinstall even when this version is installed --help, -h Show this help --version, -v Show the version `; }