UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

165 lines (164 loc) 7.12 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Create a new CLI */ const NewCommand = { alias: ['c'], description: 'Create new CLI project', hidden: false, name: 'create', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h; // Retrieve the tools we need const { config, filesystem, git, helper, meta, parameters, print: { error, info, spin, success }, prompt: { ask }, strings: { kebabCase }, system, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); const configAuthor = (_c = (_b = (_a = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _a === void 0 ? void 0 : _a.cli) === null || _b === void 0 ? void 0 : _b.create) === null || _c === void 0 ? void 0 : _c.author; const configLink = (_f = (_e = (_d = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _d === void 0 ? void 0 : _d.cli) === null || _e === void 0 ? void 0 : _e.create) === null || _f === void 0 ? void 0 : _f.link; // Load global defaults const globalAuthor = config.getGlobalDefault(ltConfig, 'author'); // Determine noConfirm with priority: CLI > command > global > default const noConfirm = config.getNoConfirm({ cliValue: parameters.options.noConfirm || parameters.options.y, commandConfig: (_h = (_g = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _g === void 0 ? void 0 : _g.cli) === null || _h === void 0 ? void 0 : _h.create, config: ltConfig, }); // Info info('Create a new CLI'); // Check git if (!(yield git.gitInstalled())) { return; } // Get name const name = yield helper.getInput(parameters.first, { name: 'CLI name', showError: true, }); if (!name) { return; } // Determine author with priority: CLI > config > global > interactive const cliAuthor = parameters.options.author; let author; if (cliAuthor) { author = cliAuthor; } else if (configAuthor) { author = configAuthor; info(`Using author from lt.config commands.cli.create: ${configAuthor}`); } else if (globalAuthor) { author = globalAuthor; info(`Using author from lt.config defaults: ${globalAuthor}`); } else { author = yield helper.getInput(null, { name: 'Author', showError: true, }); } if (!author) { return; } // Determine link with priority: CLI > config > interactive let link; if (parameters.options.link) { link = true; } else if (parameters.options.nolink) { link = false; } else if (configLink !== undefined) { link = configLink; info(`Using link from lt.config commands.cli.create: ${link}`); } else if (noConfirm) { link = false; // Default to false when noConfirm is set } else { link = !!(yield ask({ message: 'Link when finished?', name: 'link', type: 'confirm', })).link; } // Start timer const timer = system.startTimer(); // Set project directory const projectDir = kebabCase(name); // kebab-case // Check if directory already exists if (filesystem.exists(projectDir)) { info(''); error(`There's already a folder named "${projectDir}" here.`); return; } // Clone git repository const cloneSpinner = spin('Clone https://github.com/lenneTech/cli-starter.git'); try { yield system.run(`git clone https://github.com/lenneTech/cli-starter.git ${projectDir}`); if (filesystem.isDirectory(`./${projectDir}`)) { filesystem.remove(`./${projectDir}/.git`); cloneSpinner.succeed('Repository cloned from https://github.com/lenneTech/cli-starter.git'); } else { cloneSpinner.fail(`The directory "${projectDir}" could not be created.`); return; } } catch (err) { cloneSpinner.fail(`Failed to clone repository: ${err.message}`); return; } // Install packages const detectedPm = toolbox.pm.detect(projectDir); const installSpinner = spin('Install packages'); try { yield system.run(`cd ${projectDir} && ${toolbox.pm.install(detectedPm)}`); installSpinner.succeed('Packages installed'); } catch (err) { installSpinner.fail(`Failed to install packages: ${err.message}`); return; } // Rename files and data const renameSpinner = spin(`Rename files & data ${link ? ' and link' : ''}`); try { yield system.run(`cd ${projectDir} && ${toolbox.pm.run('rename', detectedPm)} -- "${name}" --author "${author}" --${link ? 'link' : 'nolink'}`); renameSpinner.succeed(`Files & data renamed${link ? ' and linked' : ''}`); } catch (err) { renameSpinner.fail(`Failed to rename files: ${err.message}`); return; } // Init git const initGitSpinner = spin('Initialize git'); try { yield system.run(`cd ${projectDir} && git init && git add . && git commit -am "Init via lenne.Tech CLI ${meta.version()}"`); initGitSpinner.succeed('Git initialized'); } catch (err) { initGitSpinner.fail(`Failed to initialize git: ${err.message}`); return; } // We're done, so show what to do next info(''); success(`Generated ${name} CLI with lenne.Tech CLI ${meta.version()} in ${helper.msToMinutesAndSeconds(timer())}m.`); // Exit if not running from menu if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `created cli ${name}`; }), }; exports.default = NewCommand;