erii 2.0.6 → 3.0.0-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/dist/index.d.mts +117 -0
- package/dist/index.mjs +239 -0
- package/package.json +55 -30
- package/readme.md +167 -152
- package/dist/index.d.ts +0 -108
- package/dist/index.js +0 -349
- package/dist/index.js.map +0 -1
- package/dist/utils/convert.d.ts +0 -12
- package/dist/utils/convert.js +0 -45
- package/dist/utils/convert.js.map +0 -1
- package/src/index.ts +0 -394
- package/src/utils/convert.ts +0 -44
- package/tsconfig.json +0 -14
package/readme.md
CHANGED
|
@@ -1,152 +1,167 @@
|
|
|
1
|
-
# Erii
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
[](https://badge.fury.io/js/erii)
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
`npm install erii --save`
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
1
|
+
# Erii
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/erii)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
`npm install erii --save`
|
|
9
|
+
This package is ESM-only and requires Node.js 22.18 or later. Use `import` from an ESM project (`"type": "module"` in package.json, or an `.mjs` file).
|
|
10
|
+
|
|
11
|
+
## Development
|
|
12
|
+
|
|
13
|
+
- `npm run build`: bundle ESM and TypeScript declarations with tsdown.
|
|
14
|
+
- `npm run typecheck`: check source and consumer types in strict mode.
|
|
15
|
+
- `npm test`: build, check types, and run regression tests.
|
|
16
|
+
- `npm run test:coverage`: run the same checks with coverage gates (100% lines/functions, 95% branches).
|
|
17
|
+
|
|
18
|
+
See [test/README.md](test/README.md) for the test scenario matrix. Runtime tests exercise the built ESM package; utility tests also cover source functions that are removed from the bundle when unused.
|
|
19
|
+
|
|
20
|
+
## TypeScript
|
|
21
|
+
|
|
22
|
+
The package exports `Command`, `Option`, `Argument`, `CommandCtx`, `CommandHandler`, `CommandOptions`, `ArgumentValue`, `ArgumentValidator`, `ValidatorName`, `ParsedArguments`, and `MetaInfo` types. Handler options are always provided; individual options and `getArgument()` can be `undefined`. Values retain the parser's strings, numbers, booleans, arrays, and nested objects, so narrow them before use. String validators accept only validator.js methods callable with one string and returning a boolean.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```JavaScript
|
|
27
|
+
import Erii from 'erii';
|
|
28
|
+
|
|
29
|
+
Erii.setMetaInfo({
|
|
30
|
+
version: '0.0.1',
|
|
31
|
+
name: 'example'
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Bind commands
|
|
35
|
+
Erii.bind({
|
|
36
|
+
name: ['help', 'h'], // `h` will be set as an alias
|
|
37
|
+
description: 'Show Help', // command description
|
|
38
|
+
argument: {
|
|
39
|
+
name: 'command',
|
|
40
|
+
description: 'query help of a specified command'
|
|
41
|
+
}
|
|
42
|
+
}, (ctx, options) => {
|
|
43
|
+
ctx.showHelp(); // show help text
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// add options for `help` command
|
|
47
|
+
Erii.addOption({
|
|
48
|
+
name: ['verbose', 'debug'],
|
|
49
|
+
command: 'help', // bind to command
|
|
50
|
+
description: 'debug output', // option description
|
|
51
|
+
argument: { // definition of option argument
|
|
52
|
+
name: 'level',
|
|
53
|
+
description: 'level of debug output'
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
Erii.addOption({
|
|
58
|
+
name: ['test'],
|
|
59
|
+
// without binding to a specified command,
|
|
60
|
+
// this option will be set as a common option.
|
|
61
|
+
description: 'show test information',
|
|
62
|
+
argument: {
|
|
63
|
+
name: 'test-argument',
|
|
64
|
+
description: 'test argument'
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
Erii.start(); // don't forget to start Erii.
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Example**
|
|
72
|
+
|
|
73
|
+
Call with
|
|
74
|
+
|
|
75
|
+
`node index.js --help xxx --debug 1`
|
|
76
|
+
|
|
77
|
+
```Javascript
|
|
78
|
+
// ...
|
|
79
|
+
// PART OF CODE
|
|
80
|
+
Erii.bind({
|
|
81
|
+
name: ['help', 'h'],
|
|
82
|
+
description: 'Show Help',
|
|
83
|
+
argument: {
|
|
84
|
+
name: 'command',
|
|
85
|
+
description: 'query help of a specified command'
|
|
86
|
+
}
|
|
87
|
+
}, (ctx, options) => {
|
|
88
|
+
const { verbose } = options; // option aliases use the primary name
|
|
89
|
+
console.log(verbose); // 1
|
|
90
|
+
console.log(ctx.getArgument()); // 'xxx'
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
Erii.addOption({
|
|
94
|
+
name: ['verbose', 'debug'],
|
|
95
|
+
description: 'show verbose output',
|
|
96
|
+
argument: {
|
|
97
|
+
name: 'level',
|
|
98
|
+
description: 'level of verbose output'
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
Erii.start();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
**Help Text**
|
|
107
|
+
```
|
|
108
|
+
example / 0.0.1
|
|
109
|
+
|
|
110
|
+
Help:
|
|
111
|
+
Commands Description Alias
|
|
112
|
+
|
|
113
|
+
--help <command> Show Help --h
|
|
114
|
+
<command> query help of a specified comm
|
|
115
|
+
|
|
116
|
+
Options:
|
|
117
|
+
|
|
118
|
+
Options Description
|
|
119
|
+
--verbose, debug <level> show verbose output
|
|
120
|
+
<level> level of verbose output
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Argument Validation**
|
|
124
|
+
|
|
125
|
+
Argument validation are based on [validator.js](https://github.com/chriso/validator.js/).
|
|
126
|
+
|
|
127
|
+
`Erii.validator` points to a `validator` exported by `validator.js`.
|
|
128
|
+
|
|
129
|
+
Erii can validate arguments automatically.
|
|
130
|
+
|
|
131
|
+
Define the validate methods in `argument` parameter.
|
|
132
|
+
|
|
133
|
+
```JavaScript
|
|
134
|
+
Erii.addOption({
|
|
135
|
+
name: ['verbose', 'debug'],
|
|
136
|
+
description: 'show verbose output',
|
|
137
|
+
argument: {
|
|
138
|
+
name: 'level',
|
|
139
|
+
description: 'level of verbose output',
|
|
140
|
+
validate: 'isInt'
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`validate` can also be a function, for example:
|
|
146
|
+
|
|
147
|
+
```JavaScript
|
|
148
|
+
Erii.addOption({
|
|
149
|
+
name: ['verbose', 'debug'],
|
|
150
|
+
description: 'show verbose output',
|
|
151
|
+
argument: {
|
|
152
|
+
name: 'level',
|
|
153
|
+
description: 'level of verbose output',
|
|
154
|
+
validate: (value) => Erii.validator.isInt(String(value))
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`argument.validate` works in both command and option definitions.
|
|
160
|
+
|
|
161
|
+
**Example Output for Argument Validation**
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
PS D:\Git\erii.test> node index.js --help --verbose f
|
|
165
|
+
Argument validation failed for option 'verbose'.
|
|
166
|
+
<level> should be a/an Int.
|
|
167
|
+
```
|
package/dist/index.d.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
export interface Command {
|
|
2
|
-
name: string | string[];
|
|
3
|
-
description?: string;
|
|
4
|
-
argument?: Argument;
|
|
5
|
-
alias?: string[];
|
|
6
|
-
redirect?: string;
|
|
7
|
-
options?: Option[];
|
|
8
|
-
handler?: (ctx: CommandCtx, options?: object) => any;
|
|
9
|
-
}
|
|
10
|
-
export interface Option {
|
|
11
|
-
name: string | string[];
|
|
12
|
-
description?: string;
|
|
13
|
-
command?: string;
|
|
14
|
-
argument?: Argument;
|
|
15
|
-
}
|
|
16
|
-
export interface CommandMap {
|
|
17
|
-
[key: string]: Command;
|
|
18
|
-
}
|
|
19
|
-
export interface CommandCtx {
|
|
20
|
-
showVersion: () => void;
|
|
21
|
-
showHelp: () => void;
|
|
22
|
-
getArgument: (commandName?: string) => string;
|
|
23
|
-
}
|
|
24
|
-
export interface Argument {
|
|
25
|
-
name: string;
|
|
26
|
-
description: string;
|
|
27
|
-
validate?: string | ((value: any, logger: (message: string) => void) => boolean);
|
|
28
|
-
}
|
|
29
|
-
export declare class Erii {
|
|
30
|
-
rawArguments: string[];
|
|
31
|
-
parsedArguments: {
|
|
32
|
-
_?: string[];
|
|
33
|
-
[key: string]: string | string[];
|
|
34
|
-
};
|
|
35
|
-
private version;
|
|
36
|
-
private name;
|
|
37
|
-
commands: CommandMap;
|
|
38
|
-
commonOptions: Option[];
|
|
39
|
-
validator: any;
|
|
40
|
-
alwaysHandler: () => void;
|
|
41
|
-
defaultHandler: () => void;
|
|
42
|
-
constructor();
|
|
43
|
-
/**
|
|
44
|
-
* 绑定命令处理函数
|
|
45
|
-
* @param config
|
|
46
|
-
* @param handler
|
|
47
|
-
*/
|
|
48
|
-
bind(config: Command, handler: (ctx: CommandCtx, ...extraArguments: any[]) => any): void;
|
|
49
|
-
/**
|
|
50
|
-
* 总是执行
|
|
51
|
-
* @param handler
|
|
52
|
-
*/
|
|
53
|
-
always(handler: () => any): void;
|
|
54
|
-
/**
|
|
55
|
-
*
|
|
56
|
-
*/
|
|
57
|
-
default(handler: () => any): void;
|
|
58
|
-
/**
|
|
59
|
-
* 增加设置项
|
|
60
|
-
* @param config
|
|
61
|
-
*/
|
|
62
|
-
addOption(config: Option): void;
|
|
63
|
-
/**
|
|
64
|
-
*
|
|
65
|
-
* @param command
|
|
66
|
-
*/
|
|
67
|
-
private commandCtx;
|
|
68
|
-
/**
|
|
69
|
-
* 设定基础信息
|
|
70
|
-
* @param metaInfo
|
|
71
|
-
*/
|
|
72
|
-
setMetaInfo({ version, name }?: {
|
|
73
|
-
version?: string;
|
|
74
|
-
name?: string;
|
|
75
|
-
}): void;
|
|
76
|
-
/**
|
|
77
|
-
* 显示帮助信息
|
|
78
|
-
*/
|
|
79
|
-
showHelp(command?: string): void;
|
|
80
|
-
/**
|
|
81
|
-
* 显示版本号
|
|
82
|
-
*/
|
|
83
|
-
showVersion(): void;
|
|
84
|
-
/**
|
|
85
|
-
* 启动
|
|
86
|
-
*/
|
|
87
|
-
start(): void;
|
|
88
|
-
/**
|
|
89
|
-
* 执行命令担当函数
|
|
90
|
-
* @param command
|
|
91
|
-
* @param extraArguments
|
|
92
|
-
*/
|
|
93
|
-
private exec;
|
|
94
|
-
validateArgument(argumentValue: any, argument: Argument): any;
|
|
95
|
-
/**
|
|
96
|
-
* 获得命令的参数
|
|
97
|
-
* @param commandName
|
|
98
|
-
* @param followRedirect 是否遵循重定向
|
|
99
|
-
*/
|
|
100
|
-
getArgument(commandName: string, followRedirect?: boolean): string;
|
|
101
|
-
/**
|
|
102
|
-
* 启动
|
|
103
|
-
* エリイ 起きてます❤
|
|
104
|
-
*/
|
|
105
|
-
okite(): void;
|
|
106
|
-
}
|
|
107
|
-
declare const _default: Erii;
|
|
108
|
-
export default _default;
|