UNPKG

@mail-core/cli

Version:

Инструментарий для написания cli-скриптов

78 lines 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = void 0; const command_1 = require("../../../command/command"); const fs_1 = require("fs"); const path_1 = require("path"); const pkg_1 = require("../../../pkg"); const create_1 = require("../create"); const exec_1 = require("../../../command/exec"); const summary_1 = require("../../../process/summary"); exports.init = command_1.createCommand({ name: 'init', describe: 'Init Command Line Interface', options: { dirName: { desc: 'Dir name', type: 'string', default: 'cli', }, }, async handler(argv, { console, describe, style }) { console.important(describe); const cliDir = path_1.join(pkg_1.ROOT_DIR, argv.dirName); const cliIndex = path_1.join(cliDir, 'index.ts'); const pkg = pkg_1.readPackageJson(); const cliPkg = pkg_1.readPackageJson(__dirname); if (!pkg.dependencies || !pkg.dependencies[cliPkg.name]) { await exec_1.npmInstallPackage(cliPkg.name, true, { spinner: console.spinner(`Install ${style.bold(cliPkg.name)}`), }); } console.spinner(`Create '${cliDir}'`).try(() => { fs_1.mkdirSync(cliDir, { recursive: true, mode: '0777', }); }); if (!fs_1.existsSync(cliIndex)) { console.spinner(`Create '${cliIndex}'`).try(() => { const isProd = pkg_1.getPackageInstallType(__dirname) === 'self'; const pkgName = isProd ? '../command/command' : '@mail-core/cli'; fs_1.writeFileSync(cliIndex, `import {initCLI} from '${pkgName}';\n` + `\n` + `initCLI(\n` + ` // Env\n` + ` {__dirname},\n` + `\n` + ` // Command List\n` + `);\n` + `\n`); }); console.hr(); await command_1.execCommand(create_1.create, { $0: argv.$0, _: argv._, name: 'demo', desc: 'Demo command', demo: true, }); } if (!pkg.bin) { pkg.bin = './cli/index.js'; pkg_1.updatePackageJson(undefined, pkg); } if (pkg.scripts == null) { pkg.scripts = {}; } if (!pkg.scripts.cli || /mail-core-cli/.test(pkg.scripts.cli)) { pkg.scripts.cli = 'npx mail-core-cli local'; pkg_1.updatePackageJson(undefined, pkg); } const binName = typeof pkg.bin === 'string' ? pkg.name : Object.keys(pkg.bin)[0]; summary_1.addProcessSummary('Run demo command', style.cyan(`npm run cli -- demo`)); summary_1.addProcessSummary('Local running', style.cyan(`npm run cli -- --help`)); summary_1.addProcessSummary('Production (run inside another package)', style.cyan(`npx ${binName} --help`)); }, }); //# sourceMappingURL=index.js.map