2026-01-06 09:08:25 +01:00
|
|
|
#!/usr/bin/env node
|
2026-01-06 16:03:04 +01:00
|
|
|
import { spawn, spawnSync } from "node:child_process";
|
2026-01-06 09:08:25 +01:00
|
|
|
import fs from "node:fs";
|
2026-01-06 16:03:04 +01:00
|
|
|
import { createRequire } from "node:module";
|
2026-01-06 09:08:25 +01:00
|
|
|
import path from "node:path";
|
|
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
|
|
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
const repoRoot = path.resolve(here, "..");
|
|
|
|
|
const uiDir = path.join(repoRoot, "ui");
|
|
|
|
|
|
2026-02-16 20:48:55 -05:00
|
|
|
const WINDOWS_SHELL_EXTENSIONS = new Set([".cmd", ".bat", ".com"]);
|
|
|
|
|
const WINDOWS_UNSAFE_SHELL_ARG_PATTERN = /[\r\n"&|<>^%!]/;
|
|
|
|
|
|
2026-01-06 09:08:25 +01:00
|
|
|
function usage() {
|
|
|
|
|
// keep this tiny; it's invoked from npm scripts too
|
2026-02-16 14:51:10 -07:00
|
|
|
process.stderr.write("Usage: node scripts/ui.js <install|dev|build|test> [...args]\n");
|
2026-01-06 09:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function which(cmd) {
|
|
|
|
|
try {
|
|
|
|
|
const key = process.platform === "win32" ? "Path" : "PATH";
|
|
|
|
|
const paths = (process.env[key] ?? process.env.PATH ?? "")
|
|
|
|
|
.split(path.delimiter)
|
|
|
|
|
.filter(Boolean);
|
|
|
|
|
const extensions =
|
|
|
|
|
process.platform === "win32"
|
2026-02-16 14:51:10 -07:00
|
|
|
? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean)
|
2026-01-06 09:08:25 +01:00
|
|
|
: [""];
|
|
|
|
|
for (const entry of paths) {
|
|
|
|
|
for (const ext of extensions) {
|
2026-02-16 14:51:10 -07:00
|
|
|
const candidate = path.join(entry, process.platform === "win32" ? `${cmd}${ext}` : cmd);
|
2026-01-06 09:08:25 +01:00
|
|
|
try {
|
2026-01-31 21:29:14 +09:00
|
|
|
if (fs.existsSync(candidate)) {
|
|
|
|
|
return candidate;
|
|
|
|
|
}
|
2026-01-06 09:08:25 +01:00
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveRunner() {
|
|
|
|
|
const pnpm = which("pnpm");
|
2026-01-31 21:29:14 +09:00
|
|
|
if (pnpm) {
|
|
|
|
|
return { cmd: pnpm, kind: "pnpm" };
|
|
|
|
|
}
|
2026-01-06 09:08:25 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:48:55 -05:00
|
|
|
export function shouldUseShellForCommand(cmd, platform = process.platform) {
|
|
|
|
|
if (platform !== "win32") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const extension = path.extname(cmd).toLowerCase();
|
|
|
|
|
return WINDOWS_SHELL_EXTENSIONS.has(extension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function assertSafeWindowsShellArgs(args, platform = process.platform) {
|
|
|
|
|
if (platform !== "win32") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const unsafeArg = args.find((arg) => WINDOWS_UNSAFE_SHELL_ARG_PATTERN.test(arg));
|
|
|
|
|
if (!unsafeArg) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// SECURITY: `shell: true` routes through cmd.exe; reject risky metacharacters
|
|
|
|
|
// in forwarded args to prevent shell control-flow/env-expansion injection.
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Unsafe Windows shell argument: ${unsafeArg}. Remove shell metacharacters (" & | < > ^ % !).`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createSpawnOptions(cmd, args, envOverride) {
|
|
|
|
|
const useShell = shouldUseShellForCommand(cmd);
|
|
|
|
|
if (useShell) {
|
|
|
|
|
assertSafeWindowsShellArgs(args);
|
|
|
|
|
}
|
|
|
|
|
return {
|
2026-01-06 09:08:25 +01:00
|
|
|
cwd: uiDir,
|
|
|
|
|
stdio: "inherit",
|
2026-02-16 20:48:55 -05:00
|
|
|
env: envOverride ?? process.env,
|
|
|
|
|
...(useShell ? { shell: true } : {}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function run(cmd, args) {
|
|
|
|
|
let child;
|
|
|
|
|
try {
|
|
|
|
|
child = spawn(cmd, args, createSpawnOptions(cmd, args));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Failed to launch ${cmd}:`, err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 14:29:05 -07:00
|
|
|
child.on("error", (err) => {
|
|
|
|
|
console.error(`Failed to launch ${cmd}:`, err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
child.on("exit", (code) => {
|
|
|
|
|
if (code !== 0) {
|
|
|
|
|
process.exit(code ?? 1);
|
2026-01-31 21:29:14 +09:00
|
|
|
}
|
2026-01-06 09:08:25 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 13:38:46 +01:00
|
|
|
function runSync(cmd, args, envOverride) {
|
2026-02-16 20:48:55 -05:00
|
|
|
let result;
|
|
|
|
|
try {
|
|
|
|
|
result = spawnSync(cmd, args, createSpawnOptions(cmd, args, envOverride));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Failed to launch ${cmd}:`, err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-31 21:29:14 +09:00
|
|
|
if (result.signal) {
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
if ((result.status ?? 1) !== 0) {
|
|
|
|
|
process.exit(result.status ?? 1);
|
|
|
|
|
}
|
2026-01-06 16:03:04 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-09 13:38:46 +01:00
|
|
|
function depsInstalled(kind) {
|
2026-01-06 16:03:04 +01:00
|
|
|
try {
|
|
|
|
|
const require = createRequire(path.join(uiDir, "package.json"));
|
|
|
|
|
require.resolve("vite");
|
|
|
|
|
require.resolve("dompurify");
|
2026-01-09 13:38:46 +01:00
|
|
|
if (kind === "test") {
|
|
|
|
|
require.resolve("vitest");
|
|
|
|
|
require.resolve("@vitest/browser-playwright");
|
|
|
|
|
require.resolve("playwright");
|
|
|
|
|
}
|
2026-01-06 16:03:04 +01:00
|
|
|
return true;
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:48:55 -05:00
|
|
|
function resolveScriptAction(action) {
|
|
|
|
|
if (action === "install") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (action === "dev") {
|
|
|
|
|
return "dev";
|
|
|
|
|
}
|
|
|
|
|
if (action === "build") {
|
|
|
|
|
return "build";
|
|
|
|
|
}
|
|
|
|
|
if (action === "test") {
|
|
|
|
|
return "test";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2026-01-06 09:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:48:55 -05:00
|
|
|
export function main(argv = process.argv.slice(2)) {
|
|
|
|
|
const [action, ...rest] = argv;
|
|
|
|
|
if (!action) {
|
|
|
|
|
usage();
|
|
|
|
|
process.exit(2);
|
|
|
|
|
}
|
2026-01-06 09:08:25 +01:00
|
|
|
|
2026-02-16 20:48:55 -05:00
|
|
|
const runner = resolveRunner();
|
|
|
|
|
if (!runner) {
|
|
|
|
|
process.stderr.write("Missing UI runner: install pnpm, then retry.\n");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const script = resolveScriptAction(action);
|
|
|
|
|
if (action !== "install" && !script) {
|
|
|
|
|
usage();
|
|
|
|
|
process.exit(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action === "install") {
|
|
|
|
|
run(runner.cmd, ["install", ...rest]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-06 09:08:25 +01:00
|
|
|
|
2026-01-18 18:46:18 +00:00
|
|
|
if (!depsInstalled(action === "test" ? "test" : "build")) {
|
|
|
|
|
const installEnv =
|
2026-02-16 14:51:10 -07:00
|
|
|
action === "build" ? { ...process.env, NODE_ENV: "production" } : process.env;
|
|
|
|
|
const installArgs = action === "build" ? ["install", "--prod"] : ["install"];
|
2026-01-18 18:46:18 +00:00
|
|
|
runSync(runner.cmd, installArgs, installEnv);
|
2026-01-06 16:03:04 +01:00
|
|
|
}
|
2026-02-16 20:48:55 -05:00
|
|
|
|
2026-01-18 18:46:18 +00:00
|
|
|
run(runner.cmd, ["run", script, ...rest]);
|
2026-01-06 09:08:25 +01:00
|
|
|
}
|
2026-02-16 20:48:55 -05:00
|
|
|
|
|
|
|
|
const isDirectExecution = (() => {
|
|
|
|
|
const entry = process.argv[1];
|
|
|
|
|
return Boolean(entry && path.resolve(entry) === fileURLToPath(import.meta.url));
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
if (isDirectExecution) {
|
|
|
|
|
main();
|
|
|
|
|
}
|