Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/the-system/scripts/init.mjs

Raw
#!/usr/bin/env node
import { applyInitialization, inspectInitialization } from "../init.ts";

const [command = "inspect", cwd = process.cwd(), rawPrefix] =
	process.argv.slice(2);
const prefix = rawPrefix || undefined;
try {
	if (command === "inspect") {
		const result = inspectInitialization(cwd, prefix);
		console.log(`ROOT ${result.root}`);
		console.log(`PREFIX ${result.prefix ?? "required"}`);
		for (const core of result.cores)
			console.log(`CORE ${core.state} ${core.path}`);
		for (const item of result.creates) console.log(`CREATE ${item}`);
		for (const item of result.rewrites) console.log(`REWRITE ${item}`);
		if (
			result.creates.length === 0 &&
			result.rewrites.length === 0 &&
			result.cores.every((core) => core.state === "preserve")
		)
			console.log("OK machine state current");
	} else if (command === "apply" || command === "rewrite") {
		const result = applyInitialization(cwd, prefix, command === "rewrite");
		console.log(`ROOT ${result.root}`);
		console.log(`PREFIX ${result.prefix}`);
		for (const item of result.changed) console.log(`WROTE ${item}`);
		for (const item of result.pending) console.log(`PENDING ${item}`);
		if (result.pending.length === 0) console.log("OK machine state applied");
	} else
		throw new Error(
			`Unknown command ${command}; expected inspect, apply, or rewrite`,
		);
} catch (error) {
	const value = error instanceof Error ? error : new Error(String(error));
	const code =
		"code" in value && typeof value.code === "string"
			? value.code
			: "init.failed";
	console.error(`ERROR ${code}: ${value.message}`);
	process.exitCode = 1;
}