erii 2.0.6 → 3.0.0-beta.2
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 +193 -0
- package/dist/index.mjs +234 -0
- package/package.json +58 -30
- package/readme.md +145 -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,145 @@
|
|
|
1
|
-
# Erii
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
Erii
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
description:
|
|
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
|
-
argument: {
|
|
123
|
-
name:
|
|
124
|
-
description:
|
|
125
|
-
validate:
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
**Example Output for Argument Validation**
|
|
147
|
-
|
|
148
|
-
```
|
|
149
|
-
PS D:\Git\erii.test> node index.js --help --verbose f
|
|
150
|
-
Argument validation failed for option 'verbose'.
|
|
151
|
-
<level> should be a/an Int.
|
|
152
|
-
```
|
|
1
|
+
# Erii
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Use Node.js 22.18 or later and set `"type": "module"` in your `package.json`.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install erii
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Define your commands and options, register handlers, then call `start()`.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import Erii from "erii";
|
|
19
|
+
|
|
20
|
+
type CLI = {
|
|
21
|
+
commonOptions: {
|
|
22
|
+
verbose?: boolean;
|
|
23
|
+
};
|
|
24
|
+
commands: {
|
|
25
|
+
build: {
|
|
26
|
+
aliases: "b";
|
|
27
|
+
argument: string;
|
|
28
|
+
options: {
|
|
29
|
+
outDir?: string;
|
|
30
|
+
minify?: boolean;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
serve: {
|
|
34
|
+
options: {
|
|
35
|
+
port?: number;
|
|
36
|
+
mode?: "dev" | "prod";
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const cli = new Erii<CLI>();
|
|
43
|
+
cli.setMetaInfo({ name: "example", version: "1.0.0" });
|
|
44
|
+
|
|
45
|
+
cli.bind(
|
|
46
|
+
{
|
|
47
|
+
name: ["build", "b"],
|
|
48
|
+
description: "Build the project",
|
|
49
|
+
argument: { name: "path", description: "Source directory" },
|
|
50
|
+
},
|
|
51
|
+
(ctx, options) => {
|
|
52
|
+
const path = ctx.getArgument(); // string | undefined
|
|
53
|
+
const output = options.outDir; // string | undefined
|
|
54
|
+
const verbose = options.verbose; // boolean | undefined
|
|
55
|
+
console.log({ path, output, verbose });
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
cli.bind({ name: "serve" }, (_, options) => {
|
|
60
|
+
console.log(options.port, options.mode);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
cli.addOption({ name: ["verbose", "v"] });
|
|
64
|
+
cli.addOption({ command: "build", name: ["outDir", "out-dir", "o"] });
|
|
65
|
+
cli.addOption({ command: "b", name: "minify" });
|
|
66
|
+
cli.addOption({ command: "serve", name: "port" });
|
|
67
|
+
cli.addOption({
|
|
68
|
+
command: "serve",
|
|
69
|
+
name: "mode",
|
|
70
|
+
argument: {
|
|
71
|
+
name: "mode",
|
|
72
|
+
description: "Server mode",
|
|
73
|
+
validate: (value) => value === "dev" || value === "prod",
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
cli.start(); // okite() is also available.
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
node index.js --build src --out-dir dist --verbose
|
|
82
|
+
node index.js serve --port 3000 --mode dev
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Aliases
|
|
86
|
+
|
|
87
|
+
Put the primary name first and aliases after it. Read options using the primary name, such as `options.outDir`.
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
node index.js -b src -o dist -v
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Reading arguments
|
|
94
|
+
|
|
95
|
+
Use `ctx.getArgument()` inside a handler, or look up a command by name:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const path = cli.getArgument("build");
|
|
99
|
+
if (path !== undefined) {
|
|
100
|
+
console.log(path);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Validation
|
|
105
|
+
|
|
106
|
+
Use `argument.validate` to check values with a validator.js method:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
cli.addOption({
|
|
110
|
+
command: "serve",
|
|
111
|
+
name: "port",
|
|
112
|
+
argument: { name: "port", description: "Listening port", validate: "isInt" },
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
To use a custom validator, return `true` for accepted input:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
cli.addOption({
|
|
120
|
+
command: "serve",
|
|
121
|
+
name: "port",
|
|
122
|
+
argument: {
|
|
123
|
+
name: "port",
|
|
124
|
+
description: "Listening port",
|
|
125
|
+
validate: (value) => typeof value === "number" && value > 0 && value <= 65535,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Help and lifecycle
|
|
131
|
+
|
|
132
|
+
Register lifecycle handlers before calling `start()`:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
cli.always(() => {
|
|
136
|
+
console.log("Starting");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
cli.default(() => {
|
|
140
|
+
cli.showHelp(); // Show help when no arguments are supplied.
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
cli.showVersion();
|
|
144
|
+
cli.start();
|
|
145
|
+
```
|
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;
|