import { Ini, Cliffy, Compress, Base64 } 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 setupCMD from "./commands/setup.ts"; import initCMD from "./commands/init.ts"; import bumpCMD from "./commands/bump.ts"; import publishCMD from "./commands/publish.ts"; const HOME_FOLDER = Deno.env.get("HOME") || Deno.env.get("USERPROFILE") || ""; type CommandHandler = (...opts: any[]) => void | Promise; 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("0.0.1") .description("CLI for the Open Source DenReg package registry") .option("-i, --interactive [interactive:boolean]", "Interactive mode", { default: true, global: true, }) .option("-c, --config ", "Config file", { default: Path.resolve(HOME_FOLDER, ".denreg"), global: true, }) .option("-v, --verbose", "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)) ) .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("") .description("Change package version") .action(commandWrapper(bumpCMD)) ) .command("completions", new Cliffy.CompletionsCommand()) .parse(Deno.args); await init(flags.options); if (command) { 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 ", // 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 { // log("Reading meta.json"); // return (await FS.readJson("meta.json")) as IMeta; // } // async function _setMeta(meta: IMeta): Promise { // 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(); // } // }