@aiot-toolkit/commander 2.0.3-beta.7 → 2.0.3-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Command.js CHANGED
@@ -1,125 +1,174 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const shared_utils_1 = require("@aiot-toolkit/shared-utils");
13
- const prompts_1 = require("@inquirer/prompts");
14
- const commander_1 = require("commander");
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _sharedUtils = require("@aiot-toolkit/shared-utils");
8
+ var _prompts = require("@inquirer/prompts");
9
+ var _commander = require("commander");
15
10
  class Command {
16
- /**
17
- * 注册命令行项目
18
- * @param programConfig 配置
19
- */
20
- static registeProgram(programConfig) {
21
- const { name, description, version, commandList, defaultCommand } = programConfig;
22
- commander_1.program.name(name).description(description).version(version, '-v, --version');
23
- if (defaultCommand) {
24
- this.registeCommand(defaultCommand);
11
+ /**
12
+ * 注册命令行项目
13
+ * @param programConfig 配置
14
+ */
15
+ static registeProgram(programConfig) {
16
+ const {
17
+ name,
18
+ description,
19
+ version,
20
+ commandList,
21
+ defaultCommand
22
+ } = programConfig;
23
+ _commander.program.name(name).description(description).version(version, '-v, --version');
24
+ if (defaultCommand) {
25
+ this.registeCommand(defaultCommand);
26
+ }
27
+ if (commandList) {
28
+ commandList.forEach(item => {
29
+ Command.registeCommand(item);
30
+ });
31
+ }
32
+ _commander.program.parse();
33
+ }
34
+ static registeCommand(command) {
35
+ var _this = this;
36
+ const {
37
+ name,
38
+ description,
39
+ argumentList,
40
+ action,
41
+ paramList,
42
+ waiter
43
+ } = command;
44
+ const instance = name ? _commander.program.command(name).description(description) : _commander.program;
45
+
46
+ // 设置argument
47
+ argumentList?.forEach(item => {
48
+ const {
49
+ name,
50
+ description,
51
+ defaultValue
52
+ } = item;
53
+ instance.argument(name, description, defaultValue);
54
+ });
55
+
56
+ // 设置option
57
+ paramList?.forEach(item => {
58
+ const {
59
+ type,
60
+ name,
61
+ description,
62
+ defaultValue
63
+ } = item;
64
+ const effectName = type === 'confirm' ? `--${name}` : `--${name} <value>`;
65
+ instance.option(effectName, description, defaultValue);
66
+ });
67
+
68
+ // 设置交互式问题
69
+ instance.action(async function () {
70
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
71
+ args[_key] = arguments[_key];
72
+ }
73
+ let option = args[args.length - 2];
74
+ if (paramList) {
75
+ // 检查询问式参数
76
+ for (let item of paramList) {
77
+ const {
78
+ name,
79
+ enableInquirer
80
+ } = item;
81
+ const cameName = _sharedUtils.StringUtil.hyphenedToCamel(name);
82
+ const valueSource = instance.getOptionValueSource(cameName);
83
+ const haveSet = valueSource && valueSource !== 'default';
84
+ // 如果启用询问,且未已显式设置值,则通过询问收集参数
85
+ if (enableInquirer && !haveSet) {
86
+ const result = await _this.collectParam(item);
87
+ if (result) {
88
+ for (let key in result) {
89
+ option[key] = result[key];
90
+ }
91
+ }
92
+ }
25
93
  }
26
- if (commandList) {
27
- commandList.forEach((item) => {
28
- Command.registeCommand(item);
94
+
95
+ // 检查弃用的参数
96
+ const deprecatedList = paramList.filter(item => item.deprecated);
97
+ deprecatedList.forEach(item => {
98
+ const {
99
+ name
100
+ } = item;
101
+ const cameName = _sharedUtils.StringUtil.hyphenedToCamel(name);
102
+ if (option.hasOwnProperty(cameName)) {
103
+ _sharedUtils.ColorConsole.warn({
104
+ word: item.name
105
+ }, 'is deprecated', ...(item.deprecatedVersion ? [', will be removed in version:', {
106
+ word: item.deprecatedVersion
107
+ }] : []), '.', {
108
+ word: item.deprecated || ''
29
109
  });
30
- }
31
- commander_1.program.parse();
32
- }
33
- static registeCommand(command) {
34
- const { name, description, argumentList, action, paramList, waiter } = command;
35
- const instance = name ? commander_1.program.command(name).description(description) : commander_1.program;
36
- // 设置argument
37
- argumentList === null || argumentList === void 0 ? void 0 : argumentList.forEach((item) => {
38
- const { name, description, defaultValue } = item;
39
- instance.argument(name, description, defaultValue);
110
+ }
40
111
  });
41
- // 设置option
42
- paramList === null || paramList === void 0 ? void 0 : paramList.forEach((item) => {
43
- const { type, name, description, defaultValue } = item;
44
- const effectName = type === 'confirm' ? `--${name}` : `--${name} <value>`;
45
- instance.option(effectName, description, defaultValue);
112
+ }
113
+ await action.apply(null, args);
114
+ if (waiter) {
115
+ waiter.describe();
116
+ waiter.start();
117
+ }
118
+ });
119
+ }
120
+
121
+ /**
122
+ * 采集参数。 根据不同的参数类型使用不同的询问方式
123
+ * @param param
124
+ * @returns
125
+ */
126
+ static async collectParam(param) {
127
+ const {
128
+ name,
129
+ description,
130
+ type,
131
+ defaultValue
132
+ } = param;
133
+ const cameName = _sharedUtils.StringUtil.hyphenedToCamel(name);
134
+ let result;
135
+ switch (type) {
136
+ case 'string':
137
+ const {
138
+ validate
139
+ } = param;
140
+ result = await (0, _prompts.input)({
141
+ message: description,
142
+ default: defaultValue?.toString(),
143
+ ...(validate ? {
144
+ validate
145
+ } : {})
46
146
  });
47
- // 设置交互式问题
48
- instance.action((...args) => __awaiter(this, void 0, void 0, function* () {
49
- let option = args[args.length - 2];
50
- if (paramList) {
51
- // 检查询问式参数
52
- for (let item of paramList) {
53
- const { name, enableInquirer } = item;
54
- const cameName = shared_utils_1.StringUtil.hyphenedToCamel(name);
55
- const valueSource = instance.getOptionValueSource(cameName);
56
- const haveSet = valueSource && valueSource !== 'default';
57
- // 如果启用询问,且未已显式设置值,则通过询问收集参数
58
- if (enableInquirer && !haveSet) {
59
- const result = yield this.collectParam(item);
60
- if (result) {
61
- for (let key in result) {
62
- option[key] = result[key];
63
- }
64
- }
65
- }
66
- }
67
- // 检查弃用的参数
68
- const deprecatedList = paramList.filter((item) => item.deprecated);
69
- deprecatedList.forEach((item) => {
70
- const { name } = item;
71
- const cameName = shared_utils_1.StringUtil.hyphenedToCamel(name);
72
- if (option.hasOwnProperty(cameName)) {
73
- shared_utils_1.ColorConsole.warn({ word: item.name }, 'is deprecated', ...(item.deprecatedVersion
74
- ? [', will be removed in version:', { word: item.deprecatedVersion }]
75
- : []), '.', { word: item.deprecated || '' });
76
- }
77
- });
78
- }
79
- yield action.apply(null, args);
80
- if (waiter) {
81
- waiter.describe();
82
- waiter.start();
83
- }
84
- }));
85
- }
86
- /**
87
- * 采集参数。 根据不同的参数类型使用不同的询问方式
88
- * @param param
89
- * @returns
90
- */
91
- static collectParam(param) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- const { name, description, type, defaultValue } = param;
94
- const cameName = shared_utils_1.StringUtil.hyphenedToCamel(name);
95
- let result;
96
- switch (type) {
97
- case 'string':
98
- const { validate } = param;
99
- result = yield (0, prompts_1.input)(Object.assign({ message: description, default: defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.toString() }, (validate ? { validate } : {})));
100
- break;
101
- case 'confirm':
102
- result = yield (0, prompts_1.confirm)({ message: description, default: Boolean(defaultValue) });
103
- break;
104
- case 'select':
105
- result = yield (0, prompts_1.select)({
106
- message: description,
107
- choices: param.choices
108
- });
109
- break;
110
- case 'checkbox':
111
- result = yield (0, prompts_1.checkbox)({
112
- message: description,
113
- choices: param.choices
114
- });
115
- break;
116
- default:
117
- break;
118
- }
119
- return {
120
- [cameName]: result
121
- };
147
+ break;
148
+ case 'confirm':
149
+ result = await (0, _prompts.confirm)({
150
+ message: description,
151
+ default: Boolean(defaultValue)
152
+ });
153
+ break;
154
+ case 'select':
155
+ result = await (0, _prompts.select)({
156
+ message: description,
157
+ choices: param.choices
158
+ });
159
+ break;
160
+ case 'checkbox':
161
+ result = await (0, _prompts.checkbox)({
162
+ message: description,
163
+ choices: param.choices
122
164
  });
165
+ break;
166
+ default:
167
+ break;
123
168
  }
169
+ return {
170
+ [cameName]: result
171
+ };
172
+ }
124
173
  }
125
- exports.default = Command;
174
+ var _default = exports.default = Command;
package/lib/index.js CHANGED
@@ -1,11 +1,50 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PersistentCommand = exports.Command = void 0;
7
- const Command_1 = __importDefault(require("./Command"));
8
- exports.Command = Command_1.default;
9
- const PersistentCommand_1 = __importDefault(require("./persistentCommand/PersistentCommand"));
10
- exports.PersistentCommand = PersistentCommand_1.default;
11
- exports.default = Command_1.default;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "Command", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _Command.default;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "ICommand", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _ICommand.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "IParam", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _IParam.default;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "IPersistentCommandData", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _IPersistentCommandData.default;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "IProgram", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _IProgram.default;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "PersistentCommand", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _PersistentCommand.default;
40
+ }
41
+ });
42
+ exports.default = void 0;
43
+ var _Command = _interopRequireDefault(require("./Command"));
44
+ var _ICommand = _interopRequireDefault(require("./interface/ICommand"));
45
+ var _IParam = _interopRequireDefault(require("./interface/IParam"));
46
+ var _IProgram = _interopRequireDefault(require("./interface/IProgram"));
47
+ var _IPersistentCommandData = _interopRequireDefault(require("./persistentCommand/IPersistentCommandData"));
48
+ var _PersistentCommand = _interopRequireDefault(require("./persistentCommand/PersistentCommand"));
49
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
50
+ var _default = exports.default = _Command.default;
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
@@ -1,2 +1,5 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -1,2 +1,5 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -1,2 +1,5 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -1,97 +1,98 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const chalk_1 = __importDefault(require("chalk"));
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _chalk = _interopRequireDefault(require("chalk"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
9
  const CTRL_C = '\u0003';
17
10
  const CTRL_L = '\u000C';
18
11
  const BLT = '\u203A';
19
12
  class PersistentCommand {
20
- constructor(data) {
21
- this.data = data;
22
- this.running = false;
23
- this.actionRunning = false;
24
- this.dataHandler = (data) => __awaiter(this, void 0, void 0, function* () {
25
- switch (data) {
26
- case CTRL_C:
27
- process.exit();
28
- break;
29
- case CTRL_L:
30
- this.clearLog();
31
- break;
32
- default:
33
- if (this.actionRunning) {
34
- console.log(chalk_1.default.yellow(`Wait for the task to complete`));
35
- return;
36
- }
37
- const item = this.data.options.find((item) => item.key === data);
38
- if (item) {
39
- this.actionRunning = true;
40
- try {
41
- yield item.action();
42
- }
43
- catch (error) {
44
- console.error(error);
45
- }
46
- this.actionRunning = false;
47
- }
48
- break;
49
- }
50
- });
13
+ running = false;
14
+ actionRunning = false;
15
+ constructor(data) {
16
+ this.data = data;
17
+ }
18
+ start() {
19
+ if (this.running) {
20
+ return;
51
21
  }
52
- start() {
53
- if (this.running) {
54
- return;
22
+ const {
23
+ stdin
24
+ } = process;
25
+ stdin.setRawMode(true);
26
+ stdin.resume();
27
+ stdin.setEncoding('utf8');
28
+ stdin.on('data', this.dataHandler);
29
+ this.running = true;
30
+ }
31
+ dataHandler = async data => {
32
+ switch (data) {
33
+ case CTRL_C:
34
+ process.exit();
35
+ break;
36
+ case CTRL_L:
37
+ this.clearLog();
38
+ break;
39
+ default:
40
+ if (this.actionRunning) {
41
+ console.log(_chalk.default.yellow(`Wait for the task to complete`));
42
+ return;
55
43
  }
56
- const { stdin } = process;
57
- stdin.setRawMode(true);
58
- stdin.resume();
59
- stdin.setEncoding('utf8');
60
- stdin.on('data', this.dataHandler);
61
- this.running = true;
62
- }
63
- stop() {
64
- const { stdin } = process;
65
- stdin.removeListener('data', this.dataHandler);
66
- stdin.setRawMode(false);
67
- stdin.resume();
68
- this.running = false;
69
- }
70
- writeLine() {
71
- const str = new Array(30)
72
- .fill(0)
73
- .map(() => '-')
74
- .join('');
75
- console.log(chalk_1.default.dim(str));
76
- }
77
- describe() {
78
- const { description, options } = this.data;
79
- this.writeLine();
80
- // 输出描述
81
- console.log(chalk_1.default.green(description));
82
- console.log('');
83
- // 输出命令列表
84
- options.forEach((item) => {
85
- const { key, description } = item;
86
- console.log(`${BLT} ${chalk_1.default.bold(key)} | ${description}`);
87
- });
88
- // 输出退出的方法
89
- console.log();
90
- console.log(chalk_1.default.dim(`Press Ctrl+C to exit.`));
91
- this.writeLine();
92
- }
93
- clearLog() {
94
- process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
44
+ const item = this.data.options.find(item => item.key === data);
45
+ if (item) {
46
+ this.actionRunning = true;
47
+ try {
48
+ await item.action();
49
+ } catch (error) {
50
+ console.error(error);
51
+ }
52
+ this.actionRunning = false;
53
+ }
54
+ break;
95
55
  }
56
+ };
57
+ stop() {
58
+ const {
59
+ stdin
60
+ } = process;
61
+ stdin.removeListener('data', this.dataHandler);
62
+ stdin.setRawMode(false);
63
+ stdin.resume();
64
+ this.running = false;
65
+ }
66
+ writeLine() {
67
+ const str = new Array(30).fill(0).map(() => '-').join('');
68
+ console.log(_chalk.default.dim(str));
69
+ }
70
+ describe() {
71
+ const {
72
+ description,
73
+ options
74
+ } = this.data;
75
+ this.writeLine();
76
+ // 输出描述
77
+ console.log(_chalk.default.green(description));
78
+ console.log('');
79
+
80
+ // 输出命令列表
81
+ options.forEach(item => {
82
+ const {
83
+ key,
84
+ description
85
+ } = item;
86
+ console.log(`${BLT} ${_chalk.default.bold(key)} | ${description}`);
87
+ });
88
+
89
+ // 输出退出的方法
90
+ console.log();
91
+ console.log(_chalk.default.dim(`Press Ctrl+C to exit.`));
92
+ this.writeLine();
93
+ }
94
+ clearLog() {
95
+ process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
96
+ }
96
97
  }
97
- exports.default = PersistentCommand;
98
+ var _default = exports.default = PersistentCommand;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/commander",
3
- "version": "2.0.3-beta.7",
3
+ "version": "2.0.3-beta.9",
4
4
  "description": "A tool that generates complete commands with configuration",
5
5
  "keywords": [
6
6
  "aiot",
@@ -20,10 +20,10 @@
20
20
  "test": "node ./__tests__/commander.test.js"
21
21
  },
22
22
  "dependencies": {
23
- "@aiot-toolkit/shared-utils": "2.0.3-beta.7",
23
+ "@aiot-toolkit/shared-utils": "2.0.3-beta.9",
24
24
  "@inquirer/prompts": "^3.0.3",
25
25
  "chalk": "^4.0.0",
26
26
  "commander": "^11.0.0"
27
27
  },
28
- "gitHead": "9621c52866d7bb1dc23110c4c9cc772b6169b682"
28
+ "gitHead": "d51c01beb50c17548d9ab5be522790d295b4c18d"
29
29
  }