UNPKG

@httpc/cli

Version:

httpc cli for building function-based API with minimal code and end-to-end type safety

127 lines (126 loc) 4.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const commander_1 = require("commander"); const prompts_1 = __importDefault(require("prompts")); const utils_1 = require("../utils"); const CreateCommand = (0, commander_1.createCommand)("create") .description("quickly setup an httpc package from a template") .option("-t, --template <template-name>", "template to use") .option("-n, --name <package-name>", "the name of the package being created") .option("-f, --force", "no confirmation asked", false) .option("--no-interactive", "no prompt mode, exit with non-zero code when parameters missing") .argument("[dir]", "directory where to create the package") .action(async (dir, options) => { let { template, name, force, interactive, } = options; const TEMPLATES = (await fetchTemplateCatalogue().catch(() => [])).filter(x => x.type === "server"); if (TEMPLATES.length === 0) { utils_1.log.warn("Cannot access template catalogue, maybe a connection issue"); return; } if (template) { if (!TEMPLATES.some(x => x.id === template)) { utils_1.log.warn(`template '${template}' not found`); template = undefined; } } if (!template) { if (!interactive) { throw Error("Required: template (no interactive exit)"); } ({ template } = await (0, prompts_1.default)({ name: "template", type: "select", message: "Select a template you want to start with", choices: TEMPLATES.map(x => ({ value: x.id, title: x.title, description: x.description, })), })); } if (!dir) { if (!interactive) { throw Error("Required: output directory (no interactive exit)"); } ({ dir } = await (0, prompts_1.default)({ name: "dir", type: "text", message: "Specify the output directory", // format: (dir: string) => path.resolve(dir), })); } const outDir = path_1.default.resolve(dir); if (!await utils_1.fsUtils.isDirEmpty(outDir)) { if (!force && interactive) { const { confirm } = await await (0, prompts_1.default)({ name: "confirm", type: "confirm", message: `Output directory(${outDir}) not empty, all content will be deleted. Confirm?`, initial: true, }); if (!confirm) { return; } } else { utils_1.log.warn("Output directory(%s) not empty: all content will be deleted", outDir); } } if (!name) { let attemptName = path_1.default.basename(outDir); if (attemptName && !interactive) { name = attemptName; } else { ({ name } = await (0, prompts_1.default)({ name: "name", type: "text", message: "Specify the package name", initial: attemptName })); } } let packageJson = await utils_1.templateUtils.initialize(template, outDir, { packageName: name }); if (packageJson.httpc) { packageJson = await utils_1.packageUtils.patch(outDir, { httpc: { ...packageJson.httpc, name: `${name}-client` } }); } if (interactive && !force) { const packageManager = detectPackageManager() || "npm"; const { install } = await (0, prompts_1.default)({ name: "install", type: "confirm", message: `Install dependencies (${packageManager} install)?`, initial: true, }); if (install) { await (0, utils_1.run)(`${packageManager} install`, { cwd: outDir }); } } console.log("\n"); utils_1.log.success(`Package ${name} generated`); console.log("\n"); }); exports.default = CreateCommand; function detectPackageManager() { const ua = process.env.npm_config_user_agent; if (!ua) return; return ua?.split(" ")[0].split("/")[0].trim(); } async function fetchTemplateCatalogue() { // const url = "https://raw.githubusercontent.com/giuseppelt/httpc/templates/templates.json"; const url = "https://github.com/giuseppelt/httpc/raw/master/templates/templates.json"; const response = await fetch(url); return await response.json(); }