Add dry run upload and adding additional verbose output
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Fabian Stamm 2020-08-16 11:39:56 +02:00
parent a436a4ecb0
commit ccc48b0480
3 changed files with 40 additions and 36 deletions

View File

@ -1,7 +1,7 @@
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";
export default async function publish() { export default async function publish(options: { dry: boolean }) {
const meta: IMeta = await getMeta(); const meta: IMeta = await getMeta();
if (!meta.name) throw new Error("name is not set in meta.json"); 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"); throw new Error("files is not set or empty in meta.json");
const tmpDir = await Deno.makeTempDir(); const tmpDir = await Deno.makeTempDir();
const packedFile = await Deno.makeTempFile(); const packedFile = (await Deno.makeTempFile()) + ".tar";
try { try {
const walker = FS.walk(".", { const walker = FS.walk(".", {
@ -31,9 +31,10 @@ export default async function publish() {
for await (const file of walker) { for await (const file of walker) {
await copy(file.path); await copy(file.path);
log("Adding file:", file.path);
} }
log("Compressing file"); log("Compressing files into", packedFile);
await Compress.Tar.compress(tmpDir, packedFile, { await Compress.Tar.compress(tmpDir, packedFile, {
excludeSrc: true, excludeSrc: true,
@ -42,34 +43,38 @@ export default async function publish() {
const url = new URL(getConfig("registry")); const url = new URL(getConfig("registry"));
url.pathname = "/api/package/" + meta.name; url.pathname = "/api/package/" + meta.name;
log("Uploading new package version"); if (!options.dry) {
log("Uploading new package version");
await fetch(url, { await fetch(url, {
method: "POST", method: "POST",
body: await Deno.readFile(packedFile), body: await Deno.readFile(packedFile),
headers: { headers: {
Authorization: Authorization:
"Basic " + "Basic " +
Base64.encode( Base64.encode(
getConfig("username") + ":" + getConfig("password") 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"));
}
}) })
.catch((err) => { .then((res) =>
console.log(Colors.red("Error: " + err.message)); 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 { } finally {
await Deno.remove(tmpDir, { recursive: true }); await Deno.remove(tmpDir, { recursive: true });
await Deno.remove(packedFile); await Deno.remove(packedFile);

View File

@ -50,6 +50,9 @@ const flags = await new Cliffy.Command()
new Cliffy.Command() new Cliffy.Command()
.description("Upload package") .description("Upload package")
.action(commandWrapper(publishCMD)) .action(commandWrapper(publishCMD))
.option("-d, --dry [dry:boolean]", "Dry run", {
default: false,
})
) )
.command( .command(
"init", "init",

View File

@ -1,12 +1,8 @@
{ {
"name": "@denreg-cli", "name": "@denreg-cli",
"version": "0.1.9", "version": "0.1.10",
"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": [],
"files": [ "files": ["**/*.ts", "**/*.js", "README.md"]
"**/*.ts",
"**/*.js",
"README.md"
]
} }