diff --git a/cli/commands/publish.ts b/cli/commands/publish.ts index 78c565e..355f342 100644 --- a/cli/commands/publish.ts +++ b/cli/commands/publish.ts @@ -1,6 +1,45 @@ import { Colors, Path, FS, Compress, Base64 } from "../deps.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 }) { 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) 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 packedFile = (await Deno.makeTempFile()) + ".tar"; diff --git a/cli/denreg.ts b/cli/denreg.ts index 72eee11..c5f4b55 100644 --- a/cli/denreg.ts +++ b/cli/denreg.ts @@ -1,4 +1,4 @@ -import { Cliffy, Path } from "./deps.ts"; +import { Cliffy, Path, Colors } from "./deps.ts"; import { init } from "./global.ts"; import setupCMD from "./commands/setup.ts"; @@ -80,5 +80,9 @@ const flags = await new Cliffy.Command() await init(flags.options); 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); + } } diff --git a/cli/global.ts b/cli/global.ts index 467c29c..d5bad3f 100644 --- a/cli/global.ts +++ b/cli/global.ts @@ -9,6 +9,10 @@ export interface IMeta { contributors?: string[]; deprecated?: boolean; files: string[]; + hooks?: { + prepublish?: string | string[]; + postpublish?: string | string[]; + }; } let verbose = false; diff --git a/cli/meta.json b/cli/meta.json index b677705..a84bb7a 100644 --- a/cli/meta.json +++ b/cli/meta.json @@ -1,6 +1,6 @@ { "name": "@denreg-cli", - "version": "0.1.11", + "version": "0.2.0", "description": "CLI for the DenReg package registry", "author": "Fabian Stamm ", "contributors": [],