UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

187 lines (186 loc) 8.22 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 }); const path_1 = require("path"); /** * Create a new TypeScript project */ const NewCommand = { alias: ['c', 'new', 'n'], description: 'Create TypeScript 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, npm, parameters, patching, print: { error, info, spin, success }, prompt: { confirm }, strings: { camelCase, kebabCase, pascalCase }, system, template, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); const configUpdatePackages = (_c = (_b = (_a = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _a === void 0 ? void 0 : _a.typescript) === null || _b === void 0 ? void 0 : _b.create) === null || _c === void 0 ? void 0 : _c.updatePackages; const configAuthor = (_f = (_e = (_d = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _d === void 0 ? void 0 : _d.typescript) === null || _e === void 0 ? void 0 : _e.create) === null || _f === void 0 ? void 0 : _f.author; // 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.typescript) === null || _h === void 0 ? void 0 : _h.create, config: ltConfig, }); // Start timer const timer = system.startTimer(); // Info info('Create a new TypeScript project'); // Check git if (!(yield git.gitInstalled())) { return; } // Get name const name = yield helper.getInput(parameters.first, { name: 'Project name', showError: true, }); if (!name) { return; } // Set project directory const projectDir = kebabCase(name); // 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/typescript-starter.git'); try { yield system.run(`git clone https://github.com/lenneTech/typescript-starter.git ${projectDir}`); if (filesystem.isDirectory(`./${projectDir}`)) { filesystem.remove(`./${projectDir}/.git`); cloneSpinner.succeed('Repository cloned from https://github.com/lenneTech/typescript-starter.git'); } } catch (err) { cloneSpinner.fail(`Failed to clone repository: ${err.message}`); return; } // Check directory if (!filesystem.isDirectory(`./${projectDir}`)) { error(`The directory "${projectDir}" could not be created.`); return; } // Determine author with priority: CLI > config > global > interactive const cliAuthor = parameters.second || parameters.options.author; let author; if (cliAuthor) { author = cliAuthor; } else if (configAuthor) { author = configAuthor; info(`Using author from lt.config commands.typescript.create: ${configAuthor}`); } else if (globalAuthor) { author = globalAuthor; info(`Using author from lt.config defaults: ${globalAuthor}`); } else { author = yield helper.getInput(null, { name: 'Author', showError: false, }); } const prepareSpinner = spin('Prepare files'); // Set up initial props (to pass into templates) const nameCamel = camelCase(name); const nameKebab = kebabCase(name); const namePascal = pascalCase(name); // Set readme yield template.generate({ props: { author, name, nameCamel, nameKebab, namePascal }, target: `./${projectDir}/README.md`, template: 'typescript-starter/README.md.ejs', }); // Set package.json yield patching.update(`./${projectDir}/package.json`, (config) => { config.author = author; config.bugs = { url: '', }; config.description = name; config.homepage = ''; config.name = nameKebab; config.repository = { type: 'git', url: '', }; config.version = '0.0.1'; return config; }); // Set package.json yield patching.update(`./${projectDir}/package-lock.json`, (config) => { config.name = nameKebab; config.version = '0.0.1'; return config; }); prepareSpinner.succeed('Files prepared'); // Determine updatePackages with priority: CLI > config > noConfirm > interactive let update; if (parameters.options.update !== undefined) { update = parameters.options.update; } else if (configUpdatePackages !== undefined) { update = configUpdatePackages; info(`Using updatePackages from lt.config: ${update}`); } else if (noConfirm) { update = true; // Default to true when noConfirm is set } else { update = yield confirm('Do you want to install the latest versions of the included packages?', true); } if (update) { // Update yield npm.update({ cwd: (0, path_1.join)(filesystem.cwd(), projectDir), install: true, showError: true }); } else { // Install packages const installSpinner = spin('Install packages'); try { yield system.run(`cd ${projectDir} && ${toolbox.pm.install(toolbox.pm.detect(projectDir))}`); installSpinner.succeed('Packages installed'); } catch (err) { installSpinner.fail(`Failed to install packages: ${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} with lenne.Tech CLI ${meta.version()} in ${helper.msToMinutesAndSeconds(timer())}m.`); info(''); // Exit if not running from menu if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `created project ${name}`; }), }; exports.default = NewCommand;