UNPKG

2.11 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __importStar = (this && this.__importStar) || function (mod) {
6 if (mod && mod.__esModule) return mod;
7 var result = {};
8 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9 result["default"] = mod;
10 return result;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const chalk_1 = __importDefault(require("chalk"));
14const commander_1 = require("commander");
15const semver = __importStar(require("semver"));
16class CliBuilder {
17 constructor() {
18 this.__program = new commander_1.Command();
19 this.__program.usage('<command> [options]');
20 }
21 addCommand(commandName, alias, description, options, callback) {
22 const cmd = this.__program.command(commandName).description(description);
23 if (alias) {
24 cmd.alias(alias);
25 }
26 Object.keys(options).forEach((key) => {
27 const optionName = options[key];
28 const optionAlias = `${optionName.alias ? '-' + optionName.alias + ' ,' : ''}`;
29 cmd.option(`${optionAlias}--${key} [optionName]`, optionName.desc);
30 });
31 cmd.action(callback);
32 }
33 set version(version) {
34 this.__version = version;
35 this.__program.version(this.__version);
36 }
37 checkNodeVersion(version) {
38 if (semver.lt(process.version, version)) {
39 console.log(chalk_1.default `nanachi only support {green.bold v8.6.0} or later (current {green.bold ${process.version}}) of Node.js`);
40 process.exit(1);
41 }
42 }
43 run() {
44 this.__program
45 .arguments('<command>')
46 .action(() => {
47 console.log(chalk_1.default.yellow('无效 nanachi 命令'));
48 this.__program.outputHelp();
49 });
50 this.__program.parse(process.argv);
51 if (!process.argv.slice(2).length) {
52 this.__program.outputHelp();
53 }
54 }
55}
56exports.default = CliBuilder;