From ccc48b0480dadb612d3a6b372d4e189fca9035c2 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Sun, 16 Aug 2020 11:39:56 +0200 Subject: [PATCH] Add dry run upload and adding additional verbose output --- cli/commands/publish.ts | 63 ++++++++++++++++++++++------------------- cli/denreg.ts | 3 ++ cli/meta.json | 10 ++----- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/cli/commands/publish.ts b/cli/commands/publish.ts index 2cd5dab..78c565e 100644 --- a/cli/commands/publish.ts +++ b/cli/commands/publish.ts @@ -1,7 +1,7 @@ import { Colors, Path, FS, Compress, Base64 } from "../deps.ts"; import { getMeta, IMeta, log, getConfig } from "../global.ts"; -export default async function publish() { +export default async function publish(options: { dry: boolean }) { const meta: IMeta = await getMeta(); if (!meta.name) throw new Error("name is not set in meta.json"); @@ -10,7 +10,7 @@ export default async function publish() { throw new Error("files is not set or empty in meta.json"); const tmpDir = await Deno.makeTempDir(); - const packedFile = await Deno.makeTempFile(); + const packedFile = (await Deno.makeTempFile()) + ".tar"; try { const walker = FS.walk(".", { @@ -31,9 +31,10 @@ export default async function publish() { for await (const file of walker) { await copy(file.path); + log("Adding file:", file.path); } - log("Compressing file"); + log("Compressing files into", packedFile); await Compress.Tar.compress(tmpDir, packedFile, { excludeSrc: true, @@ -42,34 +43,38 @@ export default async function publish() { const url = new URL(getConfig("registry")); url.pathname = "/api/package/" + meta.name; - log("Uploading new package version"); + if (!options.dry) { + log("Uploading new package version"); - await fetch(url, { - method: "POST", - body: await Deno.readFile(packedFile), - headers: { - Authorization: - "Basic " + - Base64.encode( - getConfig("username") + ":" + getConfig("password") - ), - }, - }) - .then((res) => - res.status === 200 - ? res.json() - : Promise.reject(new Error(res.statusText)) - ) - .then((res) => { - if (!res.success) { - throw new Error(res.message); - } else { - console.log(Colors.green("Upload successfull")); - } + await fetch(url, { + method: "POST", + body: await Deno.readFile(packedFile), + headers: { + Authorization: + "Basic " + + Base64.encode( + getConfig("username") + ":" + getConfig("password") + ), + }, }) - .catch((err) => { - console.log(Colors.red("Error: " + err.message)); - }); + .then((res) => + res.status === 200 + ? res.json() + : Promise.reject(new Error(res.statusText)) + ) + .then((res) => { + if (!res.success) { + throw new Error(res.message); + } else { + console.log(Colors.green("Upload successfull")); + } + }) + .catch((err) => { + console.log(Colors.red("Error: " + err.message)); + }); + } else { + console.log(Colors.yellow("Dry run. Skipping upload")); + } } finally { await Deno.remove(tmpDir, { recursive: true }); await Deno.remove(packedFile); diff --git a/cli/denreg.ts b/cli/denreg.ts index 5926376..72eee11 100644 --- a/cli/denreg.ts +++ b/cli/denreg.ts @@ -50,6 +50,9 @@ const flags = await new Cliffy.Command() new Cliffy.Command() .description("Upload package") .action(commandWrapper(publishCMD)) + .option("-d, --dry [dry:boolean]", "Dry run", { + default: false, + }) ) .command( "init", diff --git a/cli/meta.json b/cli/meta.json index 53cdca4..df30483 100644 --- a/cli/meta.json +++ b/cli/meta.json @@ -1,12 +1,8 @@ { "name": "@denreg-cli", - "version": "0.1.9", + "version": "0.1.10", "description": "CLI for the DenReg package registry", "author": "Fabian Stamm ", "contributors": [], - "files": [ - "**/*.ts", - "**/*.js", - "README.md" - ] -} \ No newline at end of file + "files": ["**/*.ts", "**/*.js", "README.md"] +}