Add hooks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Fabian Stamm 2020-08-16 19:20:37 +02:00
parent 775eca793b
commit 7abe63429f
4 changed files with 55 additions and 3 deletions

View File

@ -1,6 +1,45 @@
import { Colors, Path, FS, Compress, Base64 } from "../deps.ts"; import { Colors, Path, FS, Compress, Base64 } from "../deps.ts";
import { getMeta, IMeta, log, getConfig } from "../global.ts"; import { getMeta, IMeta, log, getConfig } from "../global.ts";
async function runScript(script: string) {
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 hook did not complete sucessfully. This is not a issue of denreg!"
);
}
}
async function runHooks(hooks: undefined | string | string[]) {
if (!hooks) return;
if (typeof hooks === "string") {
hooks = [hooks];
}
for (const hook of hooks) {
await runScript(hook);
}
}
export default async function publish(options: { dry: boolean }) { export default async function publish(options: { dry: boolean }) {
const meta: IMeta = await getMeta(); const meta: IMeta = await getMeta();
@ -9,6 +48,11 @@ export default async function publish(options: { dry: boolean }) {
if (!meta.files || !Array.isArray(meta.files) || meta.files.length <= 0) if (!meta.files || !Array.isArray(meta.files) || meta.files.length <= 0)
throw new Error("files is not set or empty in meta.json"); throw new Error("files is not set or empty in meta.json");
if (meta.hooks) {
log("Running prepublish hooks");
await runHooks(meta.hooks.prepublish);
}
const tmpDir = await Deno.makeTempDir(); const tmpDir = await Deno.makeTempDir();
const packedFile = (await Deno.makeTempFile()) + ".tar"; const packedFile = (await Deno.makeTempFile()) + ".tar";

View File

@ -1,4 +1,4 @@
import { Cliffy, Path } from "./deps.ts"; import { Cliffy, Path, Colors } from "./deps.ts";
import { init } from "./global.ts"; import { init } from "./global.ts";
import setupCMD from "./commands/setup.ts"; import setupCMD from "./commands/setup.ts";
@ -80,5 +80,9 @@ const flags = await new Cliffy.Command()
await init(flags.options); await init(flags.options);
if (command) { if (command) {
await Promise.resolve((command as CommandHandler)(...opts)); try {
await Promise.resolve((command as CommandHandler)(...opts));
} catch (err) {
console.log(Colors.bold(Colors.red("An error occured:")), err.message);
}
} }

View File

@ -9,6 +9,10 @@ export interface IMeta {
contributors?: string[]; contributors?: string[];
deprecated?: boolean; deprecated?: boolean;
files: string[]; files: string[];
hooks?: {
prepublish?: string | string[];
postpublish?: string | string[];
};
} }
let verbose = false; let verbose = false;

View File

@ -1,6 +1,6 @@
{ {
"name": "@denreg-cli", "name": "@denreg-cli",
"version": "0.1.11", "version": "0.2.0",
"description": "CLI for the DenReg package registry", "description": "CLI for the DenReg package registry",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"contributors": [], "contributors": [],