First commit of denreg cli

This commit is contained in:
Fabian Stamm
2020-07-28 19:43:58 +02:00
parent 2e92ec337c
commit 6e4a543d21
8 changed files with 578 additions and 0 deletions

38
cli/commands/bump.ts Normal file
View File

@ -0,0 +1,38 @@
import { Colors } from "../deps.ts";
import { getMeta, setMeta } from "../global.ts";
export default async function bump(
options: any,
type: "minor" | "major" | "patch"
) {
const meta = await getMeta();
let [major = 0, minor = 0, patch = 0] = meta.version.split(".").map(Number);
switch (type) {
case "major":
major++;
minor = 0;
patch = 0;
break;
case "minor":
minor++;
patch = 0;
break;
case "patch":
patch++;
break;
default:
throw new Error("type must be either major, minor or patch");
}
const newVersion = [major, minor, patch].join(".");
console.log(
"Bumping version from",
Colors.blue(meta.version),
"to",
Colors.blue(newVersion)
);
meta.version = newVersion;
await setMeta(meta);
}

41
cli/commands/init.ts Normal file
View File

@ -0,0 +1,41 @@
import { Cliffy, Path, FS, Compress, Base64 } from "../deps.ts";
import {
getMeta,
setMeta,
IMeta,
getConfig,
isInteractive,
} from "../global.ts";
export default async function init() {
let existing = {};
try {
existing = await getMeta();
} catch (err) {}
let meta: IMeta = {
name: Path.basename(Deno.cwd()).toLowerCase().replace(/\s+/g, "-"),
version: "0.0.1",
description: "",
author: getConfig("author"),
contributors: [],
files: ["**/*.ts", "**/*.js", "importmap.json"],
...existing,
};
if (isInteractive()) {
meta.name = await Cliffy.Input.prompt({
message: "What's the name of your package?",
default: meta.name,
});
meta.description = await Cliffy.Input.prompt({
message: "What's the description of your package?",
default: meta.description,
});
meta.author = await Cliffy.Input.prompt({
message: "Who's the author of your package?",
default: meta.author,
});
}
await setMeta(meta);
}

74
cli/commands/publish.ts Normal file
View File

@ -0,0 +1,74 @@
import { Colors, Path, FS, Compress, Base64 } from "../deps.ts";
import { getMeta, IMeta, log, getConfig } from "../global.ts";
export default async function publish() {
const meta: IMeta = await getMeta();
if (!meta.name) throw new Error("name is not set in meta.json");
if (!meta.version) throw new Error("version is not set in meta.json");
if (!meta.files || !Array.isArray(meta.files) || meta.files.length <= 0)
throw new Error("files is not set or empty in meta.json");
const tmpDir = await Deno.makeTempDir();
const packedFile = await Deno.makeTempFile();
try {
const walker = FS.walk(".", {
includeDirs: false,
includeFiles: true,
match: meta.files.map((file) => Path.globToRegExp(file)),
});
log("Copying files to package to", tmpDir);
const copy = async (path: string) => {
const dest = Path.join(tmpDir, path);
await FS.ensureDir(Path.dirname(dest));
await FS.copy(path, dest);
};
await copy("meta.json");
for await (const file of walker) {
await copy(file.path);
}
log("Compressing file");
await Compress.Tar.compress(tmpDir, packedFile, {
excludeSrc: true,
});
const url = new URL(getConfig("registry"));
url.pathname = "/api/package/" + meta.name;
log("Uploading new package version");
const res = 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() : res.statusText));
log("Upload finished. Result:", res);
if (typeof res === "string" || res.error) {
console.log(
Colors.red("Error: " + (typeof res == "string" ? res : res.error))
);
} else {
if (res.success) {
console.log(Colors.green("Upload successfull"));
}
}
} finally {
await Deno.remove(tmpDir, { recursive: true });
await Deno.remove(packedFile);
}
}

29
cli/commands/setup.ts Normal file
View File

@ -0,0 +1,29 @@
import { Cliffy } from "../deps.ts";
import { getConfig, setConfig } from "../global.ts";
export default async function setup() {
const registry = await Cliffy.Input.prompt({
message: "What's your registry?",
default: getConfig("registry"),
});
const username = await Cliffy.Input.prompt({
message: "What's your username?",
default: getConfig("username"),
});
const password = await Cliffy.Secret.prompt({
message: "What's your password?",
hidden: true,
default: getConfig("password"),
});
const author = await Cliffy.Input.prompt({
message: "Who are you? (optional) Name <email@example.com>",
default: getConfig("author"),
});
await setConfig("registry", registry);
await setConfig("username", username);
await setConfig("password", password);
await setConfig("author", author);
}