@aiot-toolkit/commander 2.0.5-widget-provider-beta.2 → 2.0.6-beta.1
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/README.md +170 -170
- package/lib/Command.js +7 -7
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
# `commander`
|
|
2
|
-
|
|
3
|
-
仅需配置即可生成命令行工具,支持
|
|
4
|
-
|
|
5
|
-
1. 默认命令
|
|
6
|
-
1. 命令参数
|
|
7
|
-
1. 询问式参数
|
|
8
|
-
1. 常驻式操作
|
|
9
|
-
|
|
10
|
-
## 询问式参数与命令参数的关系
|
|
11
|
-
|
|
12
|
-
- 询问式参数是普通参数的子集,询问式参数一定可以直接在命令中输入,以确保工具可自动化运行
|
|
13
|
-
|
|
14
|
-
* 某参数在命令中填写后,不会在询问中出现
|
|
15
|
-
|
|
16
|
-
## 快速开始
|
|
17
|
-
|
|
18
|
-
1. 创建配置内容
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
// bin.ts
|
|
22
|
-
|
|
23
|
-
import { Command, IProgram } from '@aiot-toolkit/commander'
|
|
24
|
-
|
|
25
|
-
const config: IProgram = {
|
|
26
|
-
name: 'myTool',
|
|
27
|
-
description: 'myTool is ...',
|
|
28
|
-
version: '1.0.1',
|
|
29
|
-
// 命令列表
|
|
30
|
-
commandList: [
|
|
31
|
-
{
|
|
32
|
-
// 命令名称
|
|
33
|
-
name: 'start',
|
|
34
|
-
// 命令描述
|
|
35
|
-
description: 'start command is ...',
|
|
36
|
-
// 参数列表
|
|
37
|
-
paramList: [
|
|
38
|
-
{
|
|
39
|
-
// 参数名称
|
|
40
|
-
name: 'path',
|
|
41
|
-
// 参数描述
|
|
42
|
-
description: 'path is ...',
|
|
43
|
-
// 是否可询问(true 表示未输入时,则询问)
|
|
44
|
-
enableInquirer: true
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
name: 'path2',
|
|
48
|
-
description: 'path2 is ...',
|
|
49
|
-
enableInquirer: true
|
|
50
|
-
}
|
|
51
|
-
],
|
|
52
|
-
// 命令对应的方法
|
|
53
|
-
action: (option: any) => {
|
|
54
|
-
console.log(option)
|
|
55
|
-
},
|
|
56
|
-
// 常驻式操作
|
|
57
|
-
waiter: new PersistentCommand({
|
|
58
|
-
description: 'start 的常驻式操作',
|
|
59
|
-
options: [
|
|
60
|
-
{
|
|
61
|
-
// 按键
|
|
62
|
-
key: 'a',
|
|
63
|
-
description: 'a 的描述',
|
|
64
|
-
// 按下 a 执行的方法
|
|
65
|
-
action: () => {}
|
|
66
|
-
}
|
|
67
|
-
]
|
|
68
|
-
})
|
|
69
|
-
}
|
|
70
|
-
]
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
Command.registeProgram(config)
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
2. 执行命令
|
|
77
|
-
|
|
78
|
-
- 执行 `ts-node bin.ts start --path /Documents/temp`
|
|
79
|
-
|
|
80
|
-
* 此时,会询问 path2 的值;输入后,进入 action,输出 option
|
|
81
|
-
|
|
82
|
-
- 命令执行完成后,会显示常驻操作菜单
|
|
83
|
-
|
|
84
|
-
## 配置
|
|
85
|
-
|
|
86
|
-
### IProgram
|
|
87
|
-
|
|
88
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
89
|
-
| -------------- | ---------------------------------- | ---------- | ----- |
|
|
90
|
-
| name | 工具名称 | string | true |
|
|
91
|
-
| description | 工具描述 | string | true |
|
|
92
|
-
| version | 版本号 | string | true |
|
|
93
|
-
| defaultCommand | 默认命令,无命令名称时,执行此命令 | ICommand | false |
|
|
94
|
-
| commandList | 命令列表 | ICommand[] | false |
|
|
95
|
-
|
|
96
|
-
### ICommand
|
|
97
|
-
|
|
98
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
99
|
-
| ------------ | -------------------------------------------------- | --------- | ----- |
|
|
100
|
-
| name | 命令名称 | string | true |
|
|
101
|
-
| description | 命令描述 | string | true |
|
|
102
|
-
| argumentList | 无名称参数, `tool arg1 arg2` | string[] | false |
|
|
103
|
-
| paramList | 有名称参数 | ParamType | false |
|
|
104
|
-
| action | 命令的执行函数,`action:(arg1, arg2, options)=>{}` | true |
|
|
105
|
-
|
|
106
|
-
### ParamType
|
|
107
|
-
|
|
108
|
-
ParamType 分为4类,继承自IParam
|
|
109
|
-
|
|
110
|
-
#### IParam
|
|
111
|
-
|
|
112
|
-
参数基础结构
|
|
113
|
-
|
|
114
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
115
|
-
| ----------------- | ------------------------------------------- | ------- | ----- |
|
|
116
|
-
| name | 参数名称 | string | true |
|
|
117
|
-
| description | 参数描述 | string | true |
|
|
118
|
-
| defaultValue | 默认值 | any | false |
|
|
119
|
-
| enableInquirer | 是否启用交互式询问 | boolean | false |
|
|
120
|
-
| deprecated | 废弃的提示信息, 如果有值,表示此参数已废弃 | string | |
|
|
121
|
-
| deprecatedVersion | 废弃的版本号 | string | |
|
|
122
|
-
|
|
123
|
-
#### 1. InputParam
|
|
124
|
-
|
|
125
|
-
文本式参数,在IParam的基础上,增加
|
|
126
|
-
|
|
127
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
128
|
-
| -------- | -------------------- | -------- | ----- |
|
|
129
|
-
| type | 固定值 string | 'string' | true |
|
|
130
|
-
| validate | 校验输入合法性的方法 | Function | false |
|
|
131
|
-
|
|
132
|
-
#### 2. SelectParam
|
|
133
|
-
|
|
134
|
-
单选参数,在 IParam 的基础上,增加
|
|
135
|
-
|
|
136
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
137
|
-
| ------- | ------------- | ------------------------------------------------------------------------------------------- | ---- |
|
|
138
|
-
| type | 固定值 select | 'select' | true |
|
|
139
|
-
| choices | 选项列表 | {<br/>value:选项值(必填), <br/> name:选项名称(可选),<br/>description: 描述(可选)<br/>}[] | true |
|
|
140
|
-
|
|
141
|
-
#### 3. CheckboxParam
|
|
142
|
-
|
|
143
|
-
多选参数,在SelectParam 基础上,修改
|
|
144
|
-
|
|
145
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
146
|
-
| ---- | --------------- | ---------- | ---- |
|
|
147
|
-
| type | 固定值 checkbox | 'checkbox' | true |
|
|
148
|
-
|
|
149
|
-
#### 4. ConfirmParam
|
|
150
|
-
|
|
151
|
-
boolean 参数,在 IParam 的基础上,增加
|
|
152
|
-
|
|
153
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
154
|
-
| ---- | -------------- | --------- | ---- |
|
|
155
|
-
| type | 固定值 confirm | 'confirm' | true |
|
|
156
|
-
|
|
157
|
-
### waiter
|
|
158
|
-
|
|
159
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
160
|
-
| ----------- | ------------ | ------------------------ | ---- |
|
|
161
|
-
| description | 常驻命令描述 | string | true |
|
|
162
|
-
| options | 常驻命令列表 | IPersistentCommandItem[] | true |
|
|
163
|
-
|
|
164
|
-
#### IPersistentCommandItem
|
|
165
|
-
|
|
166
|
-
| 属性 | 描述 | 类型 | 必填 |
|
|
167
|
-
| ----------- | -------------------- | ----------------------------- | ---- |
|
|
168
|
-
| key | 按键,只能是一个字符 | string | true |
|
|
169
|
-
| description | 描述 | string | true |
|
|
170
|
-
| action | 按键执行的方法 | () => Promise<void>, ()=>void | true |
|
|
1
|
+
# `commander`
|
|
2
|
+
|
|
3
|
+
仅需配置即可生成命令行工具,支持
|
|
4
|
+
|
|
5
|
+
1. 默认命令
|
|
6
|
+
1. 命令参数
|
|
7
|
+
1. 询问式参数
|
|
8
|
+
1. 常驻式操作
|
|
9
|
+
|
|
10
|
+
## 询问式参数与命令参数的关系
|
|
11
|
+
|
|
12
|
+
- 询问式参数是普通参数的子集,询问式参数一定可以直接在命令中输入,以确保工具可自动化运行
|
|
13
|
+
|
|
14
|
+
* 某参数在命令中填写后,不会在询问中出现
|
|
15
|
+
|
|
16
|
+
## 快速开始
|
|
17
|
+
|
|
18
|
+
1. 创建配置内容
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
// bin.ts
|
|
22
|
+
|
|
23
|
+
import { Command, IProgram } from '@aiot-toolkit/commander'
|
|
24
|
+
|
|
25
|
+
const config: IProgram = {
|
|
26
|
+
name: 'myTool',
|
|
27
|
+
description: 'myTool is ...',
|
|
28
|
+
version: '1.0.1',
|
|
29
|
+
// 命令列表
|
|
30
|
+
commandList: [
|
|
31
|
+
{
|
|
32
|
+
// 命令名称
|
|
33
|
+
name: 'start',
|
|
34
|
+
// 命令描述
|
|
35
|
+
description: 'start command is ...',
|
|
36
|
+
// 参数列表
|
|
37
|
+
paramList: [
|
|
38
|
+
{
|
|
39
|
+
// 参数名称
|
|
40
|
+
name: 'path',
|
|
41
|
+
// 参数描述
|
|
42
|
+
description: 'path is ...',
|
|
43
|
+
// 是否可询问(true 表示未输入时,则询问)
|
|
44
|
+
enableInquirer: true
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'path2',
|
|
48
|
+
description: 'path2 is ...',
|
|
49
|
+
enableInquirer: true
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
// 命令对应的方法
|
|
53
|
+
action: (option: any) => {
|
|
54
|
+
console.log(option)
|
|
55
|
+
},
|
|
56
|
+
// 常驻式操作
|
|
57
|
+
waiter: new PersistentCommand({
|
|
58
|
+
description: 'start 的常驻式操作',
|
|
59
|
+
options: [
|
|
60
|
+
{
|
|
61
|
+
// 按键
|
|
62
|
+
key: 'a',
|
|
63
|
+
description: 'a 的描述',
|
|
64
|
+
// 按下 a 执行的方法
|
|
65
|
+
action: () => {}
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
Command.registeProgram(config)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
2. 执行命令
|
|
77
|
+
|
|
78
|
+
- 执行 `ts-node bin.ts start --path /Documents/temp`
|
|
79
|
+
|
|
80
|
+
* 此时,会询问 path2 的值;输入后,进入 action,输出 option
|
|
81
|
+
|
|
82
|
+
- 命令执行完成后,会显示常驻操作菜单
|
|
83
|
+
|
|
84
|
+
## 配置
|
|
85
|
+
|
|
86
|
+
### IProgram
|
|
87
|
+
|
|
88
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
89
|
+
| -------------- | ---------------------------------- | ---------- | ----- |
|
|
90
|
+
| name | 工具名称 | string | true |
|
|
91
|
+
| description | 工具描述 | string | true |
|
|
92
|
+
| version | 版本号 | string | true |
|
|
93
|
+
| defaultCommand | 默认命令,无命令名称时,执行此命令 | ICommand | false |
|
|
94
|
+
| commandList | 命令列表 | ICommand[] | false |
|
|
95
|
+
|
|
96
|
+
### ICommand
|
|
97
|
+
|
|
98
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
99
|
+
| ------------ | -------------------------------------------------- | --------- | ----- |
|
|
100
|
+
| name | 命令名称 | string | true |
|
|
101
|
+
| description | 命令描述 | string | true |
|
|
102
|
+
| argumentList | 无名称参数, `tool arg1 arg2` | string[] | false |
|
|
103
|
+
| paramList | 有名称参数 | ParamType | false |
|
|
104
|
+
| action | 命令的执行函数,`action:(arg1, arg2, options)=>{}` | true |
|
|
105
|
+
|
|
106
|
+
### ParamType
|
|
107
|
+
|
|
108
|
+
ParamType 分为4类,继承自IParam
|
|
109
|
+
|
|
110
|
+
#### IParam
|
|
111
|
+
|
|
112
|
+
参数基础结构
|
|
113
|
+
|
|
114
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
115
|
+
| ----------------- | ------------------------------------------- | ------- | ----- |
|
|
116
|
+
| name | 参数名称 | string | true |
|
|
117
|
+
| description | 参数描述 | string | true |
|
|
118
|
+
| defaultValue | 默认值 | any | false |
|
|
119
|
+
| enableInquirer | 是否启用交互式询问 | boolean | false |
|
|
120
|
+
| deprecated | 废弃的提示信息, 如果有值,表示此参数已废弃 | string | |
|
|
121
|
+
| deprecatedVersion | 废弃的版本号 | string | |
|
|
122
|
+
|
|
123
|
+
#### 1. InputParam
|
|
124
|
+
|
|
125
|
+
文本式参数,在IParam的基础上,增加
|
|
126
|
+
|
|
127
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
128
|
+
| -------- | -------------------- | -------- | ----- |
|
|
129
|
+
| type | 固定值 string | 'string' | true |
|
|
130
|
+
| validate | 校验输入合法性的方法 | Function | false |
|
|
131
|
+
|
|
132
|
+
#### 2. SelectParam
|
|
133
|
+
|
|
134
|
+
单选参数,在 IParam 的基础上,增加
|
|
135
|
+
|
|
136
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
137
|
+
| ------- | ------------- | ------------------------------------------------------------------------------------------- | ---- |
|
|
138
|
+
| type | 固定值 select | 'select' | true |
|
|
139
|
+
| choices | 选项列表 | {<br/>value:选项值(必填), <br/> name:选项名称(可选),<br/>description: 描述(可选)<br/>}[] | true |
|
|
140
|
+
|
|
141
|
+
#### 3. CheckboxParam
|
|
142
|
+
|
|
143
|
+
多选参数,在SelectParam 基础上,修改
|
|
144
|
+
|
|
145
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
146
|
+
| ---- | --------------- | ---------- | ---- |
|
|
147
|
+
| type | 固定值 checkbox | 'checkbox' | true |
|
|
148
|
+
|
|
149
|
+
#### 4. ConfirmParam
|
|
150
|
+
|
|
151
|
+
boolean 参数,在 IParam 的基础上,增加
|
|
152
|
+
|
|
153
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
154
|
+
| ---- | -------------- | --------- | ---- |
|
|
155
|
+
| type | 固定值 confirm | 'confirm' | true |
|
|
156
|
+
|
|
157
|
+
### waiter
|
|
158
|
+
|
|
159
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
160
|
+
| ----------- | ------------ | ------------------------ | ---- |
|
|
161
|
+
| description | 常驻命令描述 | string | true |
|
|
162
|
+
| options | 常驻命令列表 | IPersistentCommandItem[] | true |
|
|
163
|
+
|
|
164
|
+
#### IPersistentCommandItem
|
|
165
|
+
|
|
166
|
+
| 属性 | 描述 | 类型 | 必填 |
|
|
167
|
+
| ----------- | -------------------- | ----------------------------- | ---- |
|
|
168
|
+
| key | 按键,只能是一个字符 | string | true |
|
|
169
|
+
| description | 描述 | string | true |
|
|
170
|
+
| action | 按键执行的方法 | () => Promise<void>, ()=>void | true |
|
package/lib/Command.js
CHANGED
|
@@ -8,9 +8,9 @@ var _sharedUtils = require("@aiot-toolkit/shared-utils");
|
|
|
8
8
|
var _prompts = require("@inquirer/prompts");
|
|
9
9
|
var _commander = require("commander");
|
|
10
10
|
class Command {
|
|
11
|
-
/**
|
|
12
|
-
* 注册命令行项目
|
|
13
|
-
* @param programConfig 配置
|
|
11
|
+
/**
|
|
12
|
+
* 注册命令行项目
|
|
13
|
+
* @param programConfig 配置
|
|
14
14
|
*/
|
|
15
15
|
static registeProgram(programConfig) {
|
|
16
16
|
const {
|
|
@@ -118,10 +118,10 @@ class Command {
|
|
|
118
118
|
});
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
/**
|
|
122
|
-
* 采集参数。 根据不同的参数类型使用不同的询问方式
|
|
123
|
-
* @param param
|
|
124
|
-
* @returns
|
|
121
|
+
/**
|
|
122
|
+
* 采集参数。 根据不同的参数类型使用不同的询问方式
|
|
123
|
+
* @param param
|
|
124
|
+
* @returns
|
|
125
125
|
*/
|
|
126
126
|
static async collectParam(param) {
|
|
127
127
|
const {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiot-toolkit/commander",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6-beta.1",
|
|
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.
|
|
23
|
+
"@aiot-toolkit/shared-utils": "2.0.6-beta.1",
|
|
24
24
|
"@inquirer/prompts": "^3.0.3",
|
|
25
25
|
"chalk": "^4.0.0",
|
|
26
26
|
"commander": "^11.0.0"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "892743ccc51408960e6cee86675538501b3e47f1"
|
|
29
29
|
}
|