Files
waggle-os/packages/launcher/src/args.ts
shumeiko.it 3a426a5423 feat(launcher): install the server from a prebuilt archive (P1.3c)
The launcher is now a self-contained installer with install, run, update,
doctor and uninstall. Install downloads waggle-server-<version>-<target>
.tar.gz, checks its SHA-256, unpacks it into a per-user program directory
and writes a waggle-server command that runs the tool on the archive's own
Node. Nothing is built on the user's machine and the data directory stays
WAGGLE_DATA_DIR or ~/.waggle.

scripts/build-server-archive.mjs packs the staged desktop sidecar payload
with the built web UI and a manifest. The download location waits for the
repository decision; until then install takes --from or
WAGGLE_RELEASE_BASE_URL (ADR-0009).

Removes install.sh, scripts/waggle-server.sh and their CI job, which the
installer replaces, and the bin name clash with @waggle/cli.
2026-11-01 01:35:54 +01:00

108 lines
3.7 KiB
TypeScript

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<string> = new Set(['install', 'update', 'run', 'doctor', 'uninstall', 'help', 'version']);
const VALUE_FLAGS: Record<string, keyof Options> = {
'--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} <command> [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 <path|url> Install from this archive instead of the release download
--sha256 <hex> Expected SHA-256 of the archive
--dir <path> Install directory
--bin-dir <path> Where to put the ${COMMAND_NAME} command
--data-dir <path> Data directory (default: $WAGGLE_DATA_DIR or ~/.waggle)
--port, -p <number> 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
`;
}