@mail-core/cli
Version:
Инструментарий для написания cli-скриптов
83 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = void 0;
const command_1 = require("../../../command/command");
const path_1 = require("path");
const fs_1 = require("fs");
const pkg_1 = require("../../../pkg");
const summary_1 = require("../../../process/summary");
const CLI_PATH = path_1.resolve(pkg_1.ROOT_DIR, 'cli');
exports.create = command_1.createCommand({
name: 'create',
describe: 'Create CLI-Command ⌨️',
options: {
name: {
desc: 'Name',
type: 'string',
},
desc: {
desc: 'Description',
type: 'string',
},
demo: {
desc: 'Disable process summary',
hidden: true,
type: 'boolean',
},
},
async handler(argv, { console, describe, style }) {
console.important(describe);
const name = argv.name || await console.cli.input('Enter name:', '');
const descr = argv.desc || await console.cli.input('Enter description:', '');
const path = path_1.resolve(CLI_PATH, 'command', name);
const fileName = path_1.join(path, 'index.ts');
const varName = name.replace(/[-_](.)/g, (_, chr) => chr.toUpperCase());
if (fs_1.existsSync(path)) {
console.fail(`${path} — exists`);
return;
}
console.spinner(`Create '${path}'`).try(() => {
fs_1.mkdirSync(path, { recursive: true });
});
console.spinner(`Write '${fileName}'`).try(() => {
const isProd = pkg_1.getPackageInstallType(__dirname) === 'self';
const pkgName = isProd ? '../../../command/command' : '@mail-core/cli';
fs_1.writeFileSync(fileName, `import {createCommand} from '${pkgName}';\n` +
`\n` +
`export const ${varName} = createCommand({\n` +
` name: '${name}',\n` +
` describe: '${descr}',\n` +
`\n` +
` options: {\n` +
` val: {\n` +
` desc: 'Any value',\n` +
` type: 'string',\n` +
` default: 'Wow!',\n` +
` },\n` +
` },\n` +
`\n` +
` async handler(argv, {console, describe, options, style}) {\n` +
` // Print command description\n` +
` console.important(describe);\n` +
`\n` +
` // Prompt user for option or use default\n` +
` const value = await console.cli.require(options.val);\n` +
`\n` +
` // Print 'value' with style and color\n` +
` console.log('value:', style.bold.cyan(value), style.gray(argv.val));\n` +
` },\n` +
`});\n` +
`\n`);
});
console.spinner(`Register '${style.bold(name)}' command`).try(() => {
const index = path_1.join(CLI_PATH, 'index.ts');
fs_1.writeFileSync(index, `${fs_1.readFileSync(index)}`.replace(/(\n+initCLI\([^)]+)/, `\nimport {${varName}} from './command/${name}';` +
`$1\t${varName},\n`));
});
if (!argv.demo) {
summary_1.addProcessSummary('Local running', style.cyan(`npm run cli -- ${name}`));
summary_1.addProcessSummary('Production (run inside another package)', style.cyan(`npx ${pkg_1.readPackageJson().name} ${name}`));
}
},
});
//# sourceMappingURL=index.js.map