DenReg/cli/denreg.ts

115 lines
3.4 KiB
TypeScript

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";
import publishCMD from "./commands/publish.ts";
import deprecateCMD from "./commands/deprecate.ts";
import upgradeCMD from "./commands/upgrade.ts";
import runCMD from "./commands/run.ts";
const HOME_FOLDER = Deno.env.get("HOME") || Deno.env.get("USERPROFILE") || "";
type CommandHandler = (...opts: any[]) => void | Promise<void>;
let command: CommandHandler | undefined = undefined;
let opts: any[] = [];
// const debounce = (fnc: any) => (...args: any[]) => {
// new Promise((y) => setTimeout(y, 0)).then(() => fnc(...args));
// }; //Put function call into queue
const commandWrapper = (cmd: CommandHandler) => {
return (...params: any[]) => {
command = cmd;
opts = params;
};
};
const flags = await new Cliffy.Command()
.name("denreg")
.version(version)
.description("CLI for the Open Source DenReg package registry")
.option("-i, --interactive [interactive:boolean]", "Interactive mode", {
default: true,
global: true,
})
.option("-c, --config <config>", "Config file", {
default: Path.resolve(HOME_FOLDER, ".denreg"),
global: true,
})
.option("-v, --verbose [verbose:boolean]", "Verbose", {
default: false,
global: true,
})
.command(
"setup",
new Cliffy.Command()
.description("Configure cli")
.action(commandWrapper(setupCMD))
)
.command(
"publish",
new Cliffy.Command()
.description("Upload package")
.action(commandWrapper(publishCMD))
.option("-d, --dry [dry:boolean]", "Dry run", {
default: false,
})
)
.command(
"init",
new Cliffy.Command()
.description("Create meta.json")
.action(commandWrapper(initCMD))
)
.command(
"bump",
new Cliffy.Command()
.complete("major|minor|patch", () => ["major", "minor", "patch"])
.arguments("<major|minor|patch>")
.description("Change package version")
.action(commandWrapper(bumpCMD))
)
.command(
"deprecate",
new Cliffy.Command()
.description("Deprecate package")
.action(commandWrapper(deprecateCMD))
)
.command(
"upgrade",
new Cliffy.Command()
.description("Upgrade to latest version of denreg cli")
.action(commandWrapper(upgradeCMD))
)
.command(
"run",
new Cliffy.Command()
.arguments("<name>")
// .complete("name", ()=>) //TODO: add autocomplete?
.description("Run script from meta.json")
.action(commandWrapper(runCMD))
)
.command("completions", new Cliffy.CompletionsCommand())
.command("help", new Cliffy.HelpCommand().global())
.parse(Deno.args);
await init(flags.options);
if (command) {
try {
await Promise.resolve((command as CommandHandler)(...opts));
} catch (err) {
console.log(Colors.bold(Colors.red("An error occured:")), err.message);
}
} else {
flags.cmd.showHelp();
}