QOL improvements or CLI
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Fabian Stamm 2020-07-31 19:45:14 +02:00
parent ebc6c83f29
commit 0bee324519
6 changed files with 43 additions and 234 deletions

View File

@ -1,3 +1,4 @@
{ {
"deno.enable": true "deno.enable": true,
"deno.unstable": true
} }

20
cli/README.md Normal file
View File

@ -0,0 +1,20 @@
A CLI for the denreg registry.
## Installation
Recommended way for installation is
`deno install -A --unstable https://deno.hibas123.de/raw/@denreg-cli/denreg.ts`
Since the cli requires access to:
- reading and writing files
- access to environment variables
- network access
The -A flag is the easiest way to install. You can however manually grant the required permissions.
| permission | reason |
| ------------- | ------------------------------------------------------------------------------------------------- |
| --allow-read | Read configuration files (~/.denreg) as well the your project files on publish |
| --allow-write | Write configuration file while using setup and init as well as during publish for temporary files |
| --allow-net | Access to network for uploading to the registry (can be exclusive to the registry) |
| --allow-env | Required to get config file path relative to user home directory |

View File

@ -1,4 +1,4 @@
import { Cliffy, Path, FS, Compress, Base64 } from "../deps.ts"; import { Cliffy, Path, FS } from "../deps.ts";
import { import {
getMeta, getMeta,
setMeta, setMeta,
@ -18,7 +18,7 @@ export default async function init() {
description: "", description: "",
author: getConfig("author"), author: getConfig("author"),
contributors: [], contributors: [],
files: ["**/*.ts", "**/*.js"], files: ["**/*.ts", "**/*.js", "README.md"],
...existing, ...existing,
}; };
@ -35,6 +35,19 @@ export default async function init() {
message: "Who's the author of your package?", message: "Who's the author of your package?",
default: meta.author, default: meta.author,
}); });
if (!(await FS.exists("README.md"))) {
const res = await Cliffy.Confirm.prompt({
message: "Autogenerate README?",
default: true,
});
if (res) {
await Deno.writeFile(
"README.md",
new TextEncoder().encode(meta.description || "")
);
}
}
} }
await setMeta(meta); await setMeta(meta);

View File

@ -1,7 +1,4 @@
import { Ini, Cliffy, Compress, Base64 } from "./deps.ts"; import { Cliffy, Path } from "./deps.ts";
import * as Colors from "https://deno.land/std@0.62.0/fmt/colors.ts";
import * as Path from "https://deno.land/std@0.62.0/path/mod.ts";
import * as FS from "https://deno.land/std@0.62.0/fs/mod.ts";
import { init } from "./global.ts"; import { init } from "./global.ts";
import setupCMD from "./commands/setup.ts"; import setupCMD from "./commands/setup.ts";
@ -38,7 +35,7 @@ const flags = await new Cliffy.Command()
default: Path.resolve(HOME_FOLDER, ".denreg"), default: Path.resolve(HOME_FOLDER, ".denreg"),
global: true, global: true,
}) })
.option("-v, --verbose", "Verbose", { .option("-v, --verbose [verbose:boolean]", "Verbose", {
default: false, default: false,
global: true, global: true,
}) })
@ -76,222 +73,3 @@ await init(flags.options);
if (command) { if (command) {
await Promise.resolve((command as CommandHandler)(...opts)); await Promise.resolve((command as CommandHandler)(...opts));
} }
// function log(...args: any[]) {
// if (flags.options.verbose) console.log(...args);
// }
// const CONFIG_LOCATION = flags.options.config;
// function loadConfigSync() {
// try {
// const data = Deno.readFileSync(CONFIG_LOCATION);
// return Ini.decode(new TextDecoder().decode(data));
// } catch (err) {
// return {};
// }
// }
// const config = loadConfigSync();
// const { username, password, registry } = config;
// async function setConfig(name: string, value: string) {
// config[name] = value;
// const data = Ini.encode(config);
// await Deno.writeFile(CONFIG_LOCATION, new TextEncoder().encode(data), {
// create: true,
// });
// }
// async function setup() {
// const registry = await Cliffy.Input.prompt({
// message: "What's your registry?",
// default: config.registry,
// });
// const username = await Cliffy.Input.prompt({
// message: "What's your username?",
// default: config.username,
// });
// const password = await Cliffy.Secret.prompt({
// message: "What's your password?",
// hidden: true,
// default: config.password,
// });
// const author = await Cliffy.Input.prompt({
// message: "Who are you? (optional) Name <email@example.com>",
// default: config.author,
// });
// await setConfig("registry", registry);
// await setConfig("username", username);
// await setConfig("password", password);
// await setConfig("author", author);
// }
// interface IMeta {
// name: string;
// version: string;
// description?: string;
// author?: string;
// contributors?: string[];
// files: string[];
// }
// 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: config.author,
// contributors: [],
// files: ["**/*.ts", "**/*.js", "importmap.json"],
// ...existing,
// };
// if (flags.options.interactive) {
// 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);
// }
// 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++;
// break;
// case "minor":
// minor++;
// 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);
// }
// async function uploadPackage() {
// 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(config.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(config.username + ":" + config.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);
// }
// }
// async function _getMeta(): Promise<IMeta> {
// log("Reading meta.json");
// return (await FS.readJson("meta.json")) as IMeta;
// }
// async function _setMeta(meta: IMeta): Promise<void> {
// log("Saving meta.json");
// return FS.writeJson("meta.json", meta, {
// spaces: " ",
// });
// }
// if (!username || !password || !registry) {
// if (!flags.options.interactive) {
// console.error(
// Colors.red("Run setup or set necessary value in " + CONFIG_LOCATION)
// );
// } else {
// log("Running setup");
// await setup();
// }
// }

View File

@ -1,4 +1,4 @@
import { Ini, Cliffy, Compress, Base64, FS, Colors } from "./deps.ts"; import { Ini, FS, Colors } from "./deps.ts";
import setupCMD from "./commands/setup.ts"; import setupCMD from "./commands/setup.ts";
export interface IMeta { export interface IMeta {

View File

@ -4,8 +4,5 @@
"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"
]
} }