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/readme.md CHANGED
@@ -1,152 +1,145 @@
1
- # Erii
2
-
3
- ![](./logo.png)
4
-
5
- [![npm version](https://badge.fury.io/js/erii.svg)](https://badge.fury.io/js/erii)
6
-
7
- ## Installation
8
- `npm install erii --save`
9
- ## Usage
10
-
11
- ```JavaScript
12
- const Erii = require('erii').default;
13
-
14
- Erii.setMetaInfo({
15
- version: '0.0.1',
16
- name: 'example'
17
- });
18
-
19
- // Bind commands
20
- Erii.bind({
21
- name: ['help', 'h'], // `h` will be set as an alias
22
- description: 'Show Help', // command description
23
- argument: {
24
- name: 'command',
25
- description: 'query help of a specified command'
26
- }
27
- }, (ctx, options) => {
28
- ctx.showHelp(); // show help text
29
- });
30
-
31
- // add options for `help` command
32
- Erii.addOption({
33
- name: ['verbose', 'debug'],
34
- command: 'help', // bind to command
35
- description: 'debug output', // option description
36
- argument: { // definition of option argument
37
- name: 'level',
38
- description: 'level of debug output'
39
- }
40
- });
41
-
42
- Erii.addOption({
43
- name: ['test'],
44
- // without binding to a specified command,
45
- // this option will be set as a common option.
46
- description: 'show test information',
47
- argument: {
48
- name: 'test-argument',
49
- description: 'test argument'
50
- }
51
- });
52
-
53
- Erii.start(); // don't forget to start Erii.
54
- ```
55
-
56
- **Example**
57
-
58
- Call with
59
-
60
- `node index.js --help xxx --debug 1`
61
-
62
- ```Javascript
63
- // ...
64
- // PART OF CODE
65
- Erii.bind({
66
- name: ['help', 'h'],
67
- description: 'Show Help',
68
- argument: {
69
- name: 'command',
70
- description: 'query help of a specified command'
71
- }
72
- }, (ctx, options) => {
73
- const { debug } = options;
74
- console.log(debug); // '1'
75
- console.log(ctx.getArgument()); // 'xxx'
76
- });
77
-
78
- Erii.addOption({
79
- name: ['verbose', 'debug'],
80
- description: 'show verbose output',
81
- argument: {
82
- name: 'level',
83
- description: 'level of verbose output'
84
- }
85
- });
86
-
87
- Erii.start();
88
- ```
89
-
90
-
91
- **Help Text**
92
- ```
93
- example / 0.0.1
94
-
95
- Help:
96
- Commands Description Alias
97
-
98
- --help <command> Show Help --h
99
- <command> query help of a specified comm
100
-
101
- Options:
102
-
103
- Options Description
104
- --verbose, debug <level> show verbose output
105
- <level> level of verbose output
106
- ```
107
-
108
- **Argument Validation**
109
-
110
- Argument validation are based on [validator.js](https://github.com/chriso/validator.js/).
111
-
112
- `Erii.validator` points to a `validator` exported by `validator.js`.
113
-
114
- Erii can validate arguments automatically.
115
-
116
- Define the validate methods in `argument` parameter.
117
-
118
- ```JavaScript
119
- Erii.addOption({
120
- name: ['verbose', 'debug'],
121
- description: 'show verbose output',
122
- argument: {
123
- name: 'level',
124
- description: 'level of verbose output',
125
- validate: 'isInt'
126
- }
127
- });
128
- ```
129
-
130
- `validate` can also be a function, for example:
131
-
132
- ```JavaScript
133
- Erii.addOption({
134
- name: ['verbose', 'debug'],
135
- description: 'show verbose output',
136
- argument: {
137
- name: 'level',
138
- description: 'level of verbose output',
139
- validate: (value) => Erii.validator.isInt(value)
140
- }
141
- });
142
- ```
143
-
144
- `argument.validate` works in both command and option definitions.
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
+ ![](./logo.png)
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;