Files
wow-pi/scripts/install.ts
2026-06-09 00:39:43 +08:00

94 lines
2.3 KiB
TypeScript

/**
* wow-pi install/uninstall script.
*
* Builds the extension and copies the output into pi's extensions directory.
* The installed plugin is hard-copied so pi can load it without depending on
* workspace symlinks or source paths.
*
* Usage:
* bun scripts/install.ts # build + install
* bun scripts/install.ts --uninstall # remove installed plugin
*/
import * as fs from "node:fs/promises";
import * as path from "node:path";
import arg from "arg";
const DIST_REL = "packages/wow-pi/dist";
const TARGET_REL = ".pi/agent/extensions/wow-pi";
const PROJECT_ROOT = path.resolve(import.meta.dir, "..");
const HOME = process.env.HOME;
if (!HOME) {
console.error("[install] HOME is not set");
process.exit(1);
}
const TARGET = path.join(HOME, TARGET_REL);
async function main(): Promise<void> {
const parsed = arg(
{ "--uninstall": Boolean },
{ argv: process.argv.slice(2) },
);
if (parsed["--uninstall"]) {
await uninstall();
return;
}
await install();
}
async function install(): Promise<void> {
const buildTarget = process.env.WOW_RELEASE === "1" ? "release" : "debug";
const build = await Bun.$`bun scripts/build.ts --target ${buildTarget}`.cwd(
PROJECT_ROOT,
);
if (build.exitCode !== 0) {
console.error(`[install] build failed (target: ${buildTarget})`);
process.exit(1);
}
const distPath = path.join(PROJECT_ROOT, DIST_REL);
await fs.mkdir(path.dirname(TARGET), { recursive: true });
try {
await fs.rm(TARGET, { recursive: true, force: true });
} catch {
// Existing target may not exist.
}
await fs.cp(distPath, TARGET, { recursive: true });
console.log(`[install] ${distPath} -> ${TARGET}`);
}
async function uninstall(): Promise<void> {
try {
await fs.rm(TARGET, { recursive: true, force: true });
const exists = await fs
.stat(TARGET)
.then(() => true)
.catch(() => false);
if (exists) {
console.error(`[install] failed to remove ${TARGET}`);
process.exit(1);
}
console.log(`[install] removed ${TARGET}`);
} catch (err: unknown) {
const code = (err as { code?: string }).code;
if (code === "ENOENT") {
console.log(`[install] nothing to remove (${TARGET} does not exist)`);
} else {
throw err;
}
}
}
main().catch((err) => {
console.error("[install] failed:", err);
process.exit(1);
});