DenReg/cli/helper/run_script.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-04-28 09:47:52 +00:00
import { Colors } from "../deps.ts";
2021-04-28 10:16:50 +00:00
import { checkPermOrExit } from "../helper/permission.ts";
2021-04-28 09:47:52 +00:00
export async function runScript(script: string) {
2021-04-28 10:16:50 +00:00
await checkPermOrExit(
"run",
"Requires --allow-run to run scripts and hooks"
);
2021-04-28 09:47:52 +00:00
console.log(Colors.bold(Colors.blue("Running script:")), script);
const runPerm = await Deno.permissions.query({
name: "run",
});
if (runPerm.state !== "granted") {
console.log(
Colors.red("Missing --allow-run permission. Cannot run hooks!")
);
throw new Error("Missing --allow-run permission. Cannot run hooks!");
}
const process = Deno.run({
cmd: ["deno", "run", "-A", "--unstable", script],
});
const status = await process.status();
console.log(Colors.bold(Colors.blue("Finished script:")), script);
if (!status.success) {
throw new Error(
"A script did not complete sucessfully. This is not a issue of denreg!"
);
}
}
export async function runHooks(hooks: undefined | string | string[]) {
if (!hooks) return;
if (typeof hooks === "string") {
hooks = [hooks];
}
for (const hook of hooks) {
try {
await runScript(hook);
} catch (err) {
throw new Error(
"A hook did not complete sucessfully. This is not a issue of denreg!"
);
}
}
}