73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
import path from "node:path";
|
|
import { loadEnvFile } from "node:process";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
|
const repositoryRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
|
|
function isMissingFileError(error: unknown): boolean {
|
|
return (
|
|
typeof error === "object" &&
|
|
error !== null &&
|
|
"code" in error &&
|
|
(error as { readonly code?: unknown }).code === "ENOENT"
|
|
);
|
|
}
|
|
|
|
async function runCommand(command: string, arguments_: readonly string[]): Promise<number> {
|
|
return await new Promise<number>((resolve) => {
|
|
const child = spawn(command, arguments_, {
|
|
cwd: repositoryRoot,
|
|
stdio: "ignore",
|
|
});
|
|
child.once("error", () => resolve(1));
|
|
child.once("exit", (code) => resolve(code ?? 1));
|
|
});
|
|
}
|
|
|
|
export async function runDoctor(): Promise<number> {
|
|
const buildExitCode = await runCommand("turbo", ["run", "build"]);
|
|
if (buildExitCode !== 0) {
|
|
console.error("Ekklesia Doctor build preparation failed. Run pnpm build for details.");
|
|
return 1;
|
|
}
|
|
|
|
try {
|
|
loadEnvFile(path.join(repositoryRoot, ".env"));
|
|
} catch (error) {
|
|
if (!isMissingFileError(error)) {
|
|
console.error("Ekklesia Doctor configuration could not be loaded safely.");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
const moduleUrl = pathToFileURL(
|
|
path.join(repositoryRoot, "core", "dist", "cli", "doctor.js"),
|
|
).href;
|
|
const doctorModule = (await import(moduleUrl)) as {
|
|
readonly runDoctorCommand?: unknown;
|
|
};
|
|
if (typeof doctorModule.runDoctorCommand !== "function") {
|
|
throw new TypeError("Invalid Doctor command module.");
|
|
}
|
|
return await doctorModule.runDoctorCommand(repositoryRoot);
|
|
}
|
|
|
|
function isDirectExecution(): boolean {
|
|
const entrypoint = process.argv[1];
|
|
return (
|
|
entrypoint !== undefined && import.meta.url === pathToFileURL(path.resolve(entrypoint)).href
|
|
);
|
|
}
|
|
|
|
if (isDirectExecution()) {
|
|
runDoctor()
|
|
.then((exitCode) => {
|
|
process.exitCode = exitCode;
|
|
})
|
|
.catch(() => {
|
|
console.error("Ekklesia Doctor failed before diagnostics could be composed.");
|
|
process.exitCode = 1;
|
|
});
|
|
}
|