From 3efe4fc34ed974119b68cf221a8bf1ddf048a0c5 Mon Sep 17 00:00:00 2001 From: User user Date: Wed, 28 Apr 2021 11:19:29 +0200 Subject: [PATCH 1/4] Add dpm alias --- cli/dpm.ts | 1 + cli/meta.json | 10 +++++++--- cli/version.ts | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 cli/dpm.ts diff --git a/cli/dpm.ts b/cli/dpm.ts new file mode 100644 index 0000000..bafc48e --- /dev/null +++ b/cli/dpm.ts @@ -0,0 +1 @@ +import "./denreg.ts"; diff --git a/cli/meta.json b/cli/meta.json index f82f94a..f37319c 100644 --- a/cli/meta.json +++ b/cli/meta.json @@ -1,11 +1,15 @@ { "name": "@denreg-cli", - "version": "0.2.11", + "version": "0.2.12", "description": "CLI for the DenReg package registry", "author": "Fabian Stamm ", "contributors": [], - "files": ["**/*.ts", "**/*.js", "README.md"], + "files": [ + "**/*.ts", + "**/*.js", + "README.md" + ], "hooks": { "prepublish": "pre.ts" } -} +} \ No newline at end of file diff --git a/cli/version.ts b/cli/version.ts index 0336e56..071d925 100644 --- a/cli/version.ts +++ b/cli/version.ts @@ -1 +1 @@ -export const version = "0.2.11" \ No newline at end of file +export const version = "0.2.12" \ No newline at end of file From e5829d9a4f944e9411a40c4a8be7650378db9df5 Mon Sep 17 00:00:00 2001 From: User user Date: Wed, 28 Apr 2021 11:47:52 +0200 Subject: [PATCH 2/4] Add script running abilities --- cli/commands/publish.ts | 40 +--------------------------------- cli/commands/run.ts | 18 ++++++++++++++++ cli/denreg.ts | 9 ++++++++ cli/deps.ts | 8 +++---- cli/global.ts | 3 +++ cli/helper/run_script.ts | 46 ++++++++++++++++++++++++++++++++++++++++ cli/meta.json | 5 ++++- cli/version.ts | 2 +- 8 files changed, 86 insertions(+), 45 deletions(-) create mode 100644 cli/commands/run.ts create mode 100644 cli/helper/run_script.ts diff --git a/cli/commands/publish.ts b/cli/commands/publish.ts index e6f693a..97099fd 100644 --- a/cli/commands/publish.ts +++ b/cli/commands/publish.ts @@ -1,46 +1,8 @@ import { Colors, Path, FS, Compress, Base64 } from "../deps.ts"; import { getMeta, IMeta, log, getConfig } from "../global.ts"; +import { runHooks } from "../helper/run_script.ts"; import { ServerError } from "../helper/server_error.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 originalMeta = await getMeta(); diff --git a/cli/commands/run.ts b/cli/commands/run.ts new file mode 100644 index 0000000..684a393 --- /dev/null +++ b/cli/commands/run.ts @@ -0,0 +1,18 @@ +import { Colors, Path, FS, Compress, Base64 } from "../deps.ts"; +import { getMeta, IMeta, log, getConfig } from "../global.ts"; +import { ServerError } from "../helper/server_error.ts"; + +import { runScript } from "../helper/run_script.ts"; + +export default async function run(options: {}, name: string) { + const { scripts } = await getMeta(); + if (!scripts || !scripts[name]) { + console.log(Colors.bold(Colors.red("Script not found:")), name); + } else { + let script = scripts[name]; + if (!Array.isArray(script)) script = [script]; + for (const s of script) { + await runScript(s); + } + } +} diff --git a/cli/denreg.ts b/cli/denreg.ts index ba2670b..d01b21e 100644 --- a/cli/denreg.ts +++ b/cli/denreg.ts @@ -9,6 +9,7 @@ import bumpCMD from "./commands/bump.ts"; import publishCMD from "./commands/publish.ts"; import deprecateCMD from "./commands/deprecate.ts"; import upgradeCMD from "./commands/upgrade.ts"; +import runCMD from "./commands/run.ts"; const HOME_FOLDER = Deno.env.get("HOME") || Deno.env.get("USERPROFILE") || ""; @@ -83,6 +84,14 @@ const flags = await new Cliffy.Command() .description("Upgrade to latest version of denreg cli") .action(commandWrapper(upgradeCMD)) ) + .command( + "run", + new Cliffy.Command() + .arguments("") + // .complete("name", ()=>) //TODO: add autocomplete? + .description("Run script from meta.json") + .action(commandWrapper(runCMD)) + ) .command("completions", new Cliffy.CompletionsCommand()) .command("help", new Cliffy.HelpCommand().global()) .parse(Deno.args); diff --git a/cli/deps.ts b/cli/deps.ts index 1e3bc74..00cc7bb 100644 --- a/cli/deps.ts +++ b/cli/deps.ts @@ -1,7 +1,7 @@ export * as Compress from "https://deno.hibas123.de/raw/@denreg-tar/mod.ts"; export * as Ini from "https://deno.land/x/ini@v2.1.0/mod.ts"; export * as Cliffy from "https://deno.land/x/cliffy@v0.18.2/mod.ts"; -export * as Base64 from "https://deno.land/std@0.91.0/encoding/base64.ts"; -export * as FS from "https://deno.land/std@0.91.0/fs/mod.ts"; -export * as Colors from "https://deno.land/std@0.91.0/fmt/colors.ts"; -export * as Path from "https://deno.land/std@0.91.0/path/mod.ts"; +export * as Base64 from "https://deno.land/std@0.95.0/encoding/base64.ts"; +export * as FS from "https://deno.land/std@0.95.0/fs/mod.ts"; +export * as Colors from "https://deno.land/std@0.95.0/fmt/colors.ts"; +export * as Path from "https://deno.land/std@0.95.0/path/mod.ts"; diff --git a/cli/global.ts b/cli/global.ts index 6243f57..887c5b7 100644 --- a/cli/global.ts +++ b/cli/global.ts @@ -14,6 +14,9 @@ export interface IMeta { prepublish?: string | string[]; postpublish?: string | string[]; }; + scripts?: { + [key: string]: string | string[]; + }; } let verbose = false; diff --git a/cli/helper/run_script.ts b/cli/helper/run_script.ts new file mode 100644 index 0000000..0d42804 --- /dev/null +++ b/cli/helper/run_script.ts @@ -0,0 +1,46 @@ +import { Colors } from "../deps.ts"; + +export 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 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!" + ); + } + } +} diff --git a/cli/meta.json b/cli/meta.json index f37319c..53d85e8 100644 --- a/cli/meta.json +++ b/cli/meta.json @@ -1,6 +1,6 @@ { "name": "@denreg-cli", - "version": "0.2.12", + "version": "0.3.0", "description": "CLI for the DenReg package registry", "author": "Fabian Stamm ", "contributors": [], @@ -9,6 +9,9 @@ "**/*.js", "README.md" ], + "scripts": { + "test": "version.ts" + }, "hooks": { "prepublish": "pre.ts" } diff --git a/cli/version.ts b/cli/version.ts index 071d925..9bb0956 100644 --- a/cli/version.ts +++ b/cli/version.ts @@ -1 +1 @@ -export const version = "0.2.12" \ No newline at end of file +export const version = "0.3.0" \ No newline at end of file From 3cda4ee8c816c002ceee1a592599f37d898cc4cd Mon Sep 17 00:00:00 2001 From: User user Date: Wed, 28 Apr 2021 12:16:50 +0200 Subject: [PATCH 3/4] Improve upgrade script --- cli/commands/publish.ts | 3 +++ cli/commands/upgrade.ts | 29 +++++++++++++++++++++-------- cli/denreg.ts | 9 +++++++-- cli/helper/permission.ts | 26 ++++++++++++++++++++++++++ cli/helper/run_script.ts | 5 +++++ cli/meta.json | 2 +- cli/test.ts | 0 cli/version.ts | 2 +- 8 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 cli/helper/permission.ts create mode 100644 cli/test.ts diff --git a/cli/commands/publish.ts b/cli/commands/publish.ts index 97099fd..67b4c5f 100644 --- a/cli/commands/publish.ts +++ b/cli/commands/publish.ts @@ -2,6 +2,7 @@ import { Colors, Path, FS, Compress, Base64 } from "../deps.ts"; import { getMeta, IMeta, log, getConfig } from "../global.ts"; import { runHooks } from "../helper/run_script.ts"; import { ServerError } from "../helper/server_error.ts"; +import { checkPermOrExit } from "../helper/permission.ts"; export default async function publish(options: { dry: boolean }) { const originalMeta = await getMeta(); @@ -62,6 +63,8 @@ export default async function publish(options: { dry: boolean }) { ); if (!options.dry) { + await checkPermOrExit("net", "Net permission required for publishing"); + log("Uploading new package version"); await fetch(url, { method: "POST", diff --git a/cli/commands/upgrade.ts b/cli/commands/upgrade.ts index d1d18f9..896cb9c 100644 --- a/cli/commands/upgrade.ts +++ b/cli/commands/upgrade.ts @@ -1,7 +1,12 @@ import { Cliffy, Colors } from "../deps.ts"; import { version } from "../version.ts"; +import { requestPermOrExit } from "../helper/permission.ts"; export default async function upgrade() { + await requestPermOrExit( + "net", + "Net permission required to fetch new version" + ); const meta = await fetch( "https://deno.hibas123.de/raw/@denreg-cli/meta.json" ).then((e) => e.json()); @@ -20,15 +25,23 @@ export default async function upgrade() { }); if (res) { + const cmd = [ + "deno", + "install", + "-A", + "--unstable", + "-f", + `https://deno.hibas123.de/raw/@denreg-cli@${meta.version}/denreg.ts`, + ]; + + await requestPermOrExit( + "run", + "Run permission required to install new version. Or run it manually: " + + cmd.join(" ") + ); + const process = Deno.run({ - cmd: [ - "deno", - "install", - "-A", - "--unstable", - "-f", - `https://deno.hibas123.de/raw/@denreg-cli@${meta.version}/denreg.ts`, - ], + cmd, }); const s = await process.status(); diff --git a/cli/denreg.ts b/cli/denreg.ts index d01b21e..8a4e2ae 100644 --- a/cli/denreg.ts +++ b/cli/denreg.ts @@ -1,8 +1,13 @@ import { Cliffy, Path, Colors } from "./deps.ts"; + +import { checkPermOrExit } from "./helper/permission.ts"; + +await checkPermOrExit("env", "Requires --allow-env"); +await checkPermOrExit("read", "Requires --allow-read"); +await checkPermOrExit("write", "Requires --allow-write"); + import { init } from "./global.ts"; - import { version } from "./version.ts"; - import setupCMD from "./commands/setup.ts"; import initCMD from "./commands/init.ts"; import bumpCMD from "./commands/bump.ts"; diff --git a/cli/helper/permission.ts b/cli/helper/permission.ts new file mode 100644 index 0000000..d7bd4be --- /dev/null +++ b/cli/helper/permission.ts @@ -0,0 +1,26 @@ +import { Colors } from "../deps.ts"; + +export const checkPermOrExit = (name: string, err: string) => + Deno.permissions.query({ name: name as any }).then((res) => { + if (res.state !== "granted") { + console.log(Colors.bold(Colors.red(err))); + Deno.exit(1); + } + }); + +export const requestPermOrExit = (name: string, err: string) => { + Deno.permissions + .query({ name: name as any }) + .then((res) => { + if (res.state === "prompt") { + return Deno.permissions.request({ name: name as any }); + } + return res; + }) + .then((res) => { + if (res.state !== "granted") { + console.log(Colors.bold(Colors.red(err))); + Deno.exit(1); + } + }); +}; diff --git a/cli/helper/run_script.ts b/cli/helper/run_script.ts index 0d42804..13a1949 100644 --- a/cli/helper/run_script.ts +++ b/cli/helper/run_script.ts @@ -1,6 +1,11 @@ import { Colors } from "../deps.ts"; +import { checkPermOrExit } from "../helper/permission.ts"; export async function runScript(script: string) { + await checkPermOrExit( + "run", + "Requires --allow-run to run scripts and hooks" + ); console.log(Colors.bold(Colors.blue("Running script:")), script); const runPerm = await Deno.permissions.query({ name: "run", diff --git a/cli/meta.json b/cli/meta.json index 53d85e8..7a3a969 100644 --- a/cli/meta.json +++ b/cli/meta.json @@ -1,6 +1,6 @@ { "name": "@denreg-cli", - "version": "0.3.0", + "version": "0.3.2", "description": "CLI for the DenReg package registry", "author": "Fabian Stamm ", "contributors": [], diff --git a/cli/test.ts b/cli/test.ts new file mode 100644 index 0000000..e69de29 diff --git a/cli/version.ts b/cli/version.ts index 9bb0956..9df321f 100644 --- a/cli/version.ts +++ b/cli/version.ts @@ -1 +1 @@ -export const version = "0.3.0" \ No newline at end of file +export const version = "0.3.2" \ No newline at end of file From a0fef1ef76632537bfa7d0a95128178427ffa7ea Mon Sep 17 00:00:00 2001 From: User user Date: Wed, 28 Apr 2021 12:20:30 +0200 Subject: [PATCH 4/4] Improve upgrade script --- cli/commands/upgrade.ts | 34 +++++++++++++++++++++++----------- cli/meta.json | 2 +- cli/version.ts | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cli/commands/upgrade.ts b/cli/commands/upgrade.ts index 896cb9c..7d25d4d 100644 --- a/cli/commands/upgrade.ts +++ b/cli/commands/upgrade.ts @@ -25,27 +25,39 @@ export default async function upgrade() { }); if (res) { - const cmd = [ - "deno", - "install", - "-A", - "--unstable", - "-f", + const cmd_base = ["deno", "install", "-A", "--unstable", "-f"]; + + const cmd1 = [ + ...cmd_base, `https://deno.hibas123.de/raw/@denreg-cli@${meta.version}/denreg.ts`, ]; + const cmd2 = [ + ...cmd_base, + `https://deno.hibas123.de/raw/@denreg-cli@${meta.version}/dpm.ts`, + ]; + await requestPermOrExit( "run", "Run permission required to install new version. Or run it manually: " + - cmd.join(" ") + cmd1.join(" ") ); - const process = Deno.run({ - cmd, + const process1 = Deno.run({ + cmd: cmd1, }); - const s = await process.status(); - if (!s) { + const s1 = await process1.status(); + if (!s1) { + console.log(Colors.red("Upgrade failed!")); + } + + const process2 = Deno.run({ + cmd: cmd2, + }); + + const s2 = await process2.status(); + if (!s2) { console.log(Colors.red("Upgrade failed!")); } } diff --git a/cli/meta.json b/cli/meta.json index 7a3a969..14c52b7 100644 --- a/cli/meta.json +++ b/cli/meta.json @@ -1,6 +1,6 @@ { "name": "@denreg-cli", - "version": "0.3.2", + "version": "0.3.3", "description": "CLI for the DenReg package registry", "author": "Fabian Stamm ", "contributors": [], diff --git a/cli/version.ts b/cli/version.ts index 9df321f..1a7f98a 100644 --- a/cli/version.ts +++ b/cli/version.ts @@ -1 +1 @@ -export const version = "0.3.2" \ No newline at end of file +export const version = "0.3.3" \ No newline at end of file