Improve upgrade script
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
User user 2021-04-28 12:16:50 +02:00
parent e5829d9a4f
commit 3cda4ee8c8
8 changed files with 64 additions and 12 deletions

View File

@ -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",

View File

@ -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();

View File

@ -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";

26
cli/helper/permission.ts Normal file
View File

@ -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);
}
});
};

View File

@ -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",

View File

@ -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 <dev@fabianstamm.de>",
"contributors": [],

0
cli/test.ts Normal file
View File

View File

@ -1 +1 @@
export const version = "0.3.0"
export const version = "0.3.2"