gunshi 0.14.5 → 0.16.0
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/cli-B7eqtqBF.js +109 -0
- package/lib/{cli-JT5dcEoE.js → context-BROXRnNP.js} +11 -96
- package/lib/context.d.ts +54 -0
- package/lib/context.js +4 -0
- package/lib/definition-VzcnM0si.js +12 -0
- package/lib/{definition.d-TBObezo_.d.ts → definition.d-DllW_uD3.d.ts} +5 -5
- package/lib/definition.d.ts +2 -2
- package/lib/definition.js +1 -1
- package/lib/generator.d.ts +7 -7
- package/lib/generator.js +11 -2
- package/lib/index.d.ts +9 -9
- package/lib/index.js +5 -3
- package/lib/locales/en-US.json +1 -0
- package/lib/locales/ja-JP.json +1 -0
- package/lib/{renderer-DGPRTLmr.js → renderer-BNorS8VG.js} +38 -75
- package/lib/renderer.d.ts +14 -14
- package/lib/renderer.js +2 -1
- package/lib/{types.d-GVRYnEFw.d.ts → types.d-aDzUZTqM.d.ts} +58 -51
- package/lib/utils-BYPzZy9X.js +73 -0
- package/package.json +12 -6
- package/lib/definition-BAm6f1St.js +0 -8
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, create, resolveLazyCommand } from "./utils-BYPzZy9X.js";
|
|
2
|
+
import { createCommandContext } from "./context-BROXRnNP.js";
|
|
3
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BNorS8VG.js";
|
|
4
|
+
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
|
+
|
|
6
|
+
//#region src/cli.ts
|
|
7
|
+
/**
|
|
8
|
+
* Run the command.
|
|
9
|
+
* @param args Command line arguments
|
|
10
|
+
* @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
|
|
11
|
+
* @param opts A {@link CommandOptions | command options}
|
|
12
|
+
* @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
|
|
13
|
+
*/
|
|
14
|
+
async function cli(args, entry, opts = {}) {
|
|
15
|
+
const tokens = parseArgs(args);
|
|
16
|
+
const subCommand = getSubCommand(tokens);
|
|
17
|
+
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
18
|
+
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
|
|
19
|
+
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
20
|
+
const options = resolveArgOptions(command.options);
|
|
21
|
+
const { values, positionals, rest, error } = resolveArgs(options, tokens, {
|
|
22
|
+
optionGrouping: true,
|
|
23
|
+
allowNegative: true
|
|
24
|
+
});
|
|
25
|
+
const omitted = !subCommand;
|
|
26
|
+
const ctx = await createCommandContext({
|
|
27
|
+
options,
|
|
28
|
+
values,
|
|
29
|
+
positionals,
|
|
30
|
+
rest,
|
|
31
|
+
args,
|
|
32
|
+
tokens,
|
|
33
|
+
omitted,
|
|
34
|
+
command,
|
|
35
|
+
commandOptions: resolvedCommandOptions
|
|
36
|
+
});
|
|
37
|
+
if (values.version) {
|
|
38
|
+
showVersion(ctx);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const usageBuffer = [];
|
|
42
|
+
const header = await showHeader(ctx);
|
|
43
|
+
if (header) usageBuffer.push(header);
|
|
44
|
+
if (values.help) {
|
|
45
|
+
const usage = await showUsage(ctx);
|
|
46
|
+
if (usage) usageBuffer.push(usage);
|
|
47
|
+
return usageBuffer.join("\n");
|
|
48
|
+
}
|
|
49
|
+
if (error) {
|
|
50
|
+
await showValidationErrors(ctx, error);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await command.run(ctx);
|
|
54
|
+
}
|
|
55
|
+
function resolveArgOptions(options) {
|
|
56
|
+
return Object.assign(create(), options, COMMON_OPTIONS);
|
|
57
|
+
}
|
|
58
|
+
function resolveCommandOptions(options, entry) {
|
|
59
|
+
const subCommands = new Map(options.subCommands);
|
|
60
|
+
if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
|
|
61
|
+
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
62
|
+
return resolvedOptions;
|
|
63
|
+
}
|
|
64
|
+
function getSubCommand(tokens) {
|
|
65
|
+
const firstToken = tokens[0];
|
|
66
|
+
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
67
|
+
}
|
|
68
|
+
async function showUsage(ctx) {
|
|
69
|
+
if (ctx.env.renderUsage === null) return;
|
|
70
|
+
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
71
|
+
if (usage) {
|
|
72
|
+
ctx.log(usage);
|
|
73
|
+
return usage;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function showVersion(ctx) {
|
|
77
|
+
ctx.log(ctx.env.version);
|
|
78
|
+
}
|
|
79
|
+
async function showHeader(ctx) {
|
|
80
|
+
if (ctx.env.renderHeader === null) return;
|
|
81
|
+
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
82
|
+
if (header) {
|
|
83
|
+
ctx.log(header);
|
|
84
|
+
ctx.log();
|
|
85
|
+
return header;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function showValidationErrors(ctx, error) {
|
|
89
|
+
if (ctx.env.renderValidationErrors === null) return;
|
|
90
|
+
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
91
|
+
ctx.log(await render(ctx, error));
|
|
92
|
+
}
|
|
93
|
+
async function resolveCommand(sub, entry, options) {
|
|
94
|
+
const omitted = !sub;
|
|
95
|
+
if (typeof entry === "function") return [void 0, { run: entry }];
|
|
96
|
+
else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
|
|
97
|
+
else {
|
|
98
|
+
if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
|
|
99
|
+
const cmd = options.subCommands?.get(sub);
|
|
100
|
+
if (cmd == null) return [sub, void 0];
|
|
101
|
+
return [sub, await resolveLazyCommand(cmd, sub)];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function resolveEntryName(entry) {
|
|
105
|
+
return entry.name || "(anonymous)";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
export { cli };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT,
|
|
2
|
-
import { parseArgs, resolveArgs } from "args-tokens";
|
|
1
|
+
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveLazyCommand, resolveOptionKey } from "./utils-BYPzZy9X.js";
|
|
3
2
|
|
|
4
3
|
//#region src/locales/en-US.json
|
|
5
4
|
var COMMAND = "COMMAND";
|
|
@@ -9,6 +8,7 @@ var USAGE = "USAGE";
|
|
|
9
8
|
var OPTIONS = "OPTIONS";
|
|
10
9
|
var EXAMPLES = "EXAMPLES";
|
|
11
10
|
var FORMORE = "For more info, run any command with the `--help` flag:";
|
|
11
|
+
var NEGATABLE = "Negatable of";
|
|
12
12
|
var help = "Display this help message";
|
|
13
13
|
var version = "Display this version";
|
|
14
14
|
var en_US_default = {
|
|
@@ -19,6 +19,7 @@ var en_US_default = {
|
|
|
19
19
|
OPTIONS,
|
|
20
20
|
EXAMPLES,
|
|
21
21
|
FORMORE,
|
|
22
|
+
NEGATABLE,
|
|
22
23
|
help,
|
|
23
24
|
version
|
|
24
25
|
};
|
|
@@ -60,7 +61,12 @@ var DefaultTranslation = class {
|
|
|
60
61
|
//#endregion
|
|
61
62
|
//#region src/context.ts
|
|
62
63
|
const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
|
|
63
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Create a {@link CommandContext | command context}
|
|
66
|
+
* @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
|
|
67
|
+
* @returns A {@link CommandContext | command context}, which is readonly
|
|
68
|
+
*/
|
|
69
|
+
async function createCommandContext({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted = false }) {
|
|
64
70
|
/**
|
|
65
71
|
* normailize the options schema and values, to avoid prototype pollution
|
|
66
72
|
*/
|
|
@@ -121,6 +127,7 @@ async function createCommandContext({ options, values, positionals, args, tokens
|
|
|
121
127
|
options: _options,
|
|
122
128
|
values,
|
|
123
129
|
positionals,
|
|
130
|
+
rest,
|
|
124
131
|
_: args,
|
|
125
132
|
tokens,
|
|
126
133
|
log: commandOptions.usageSilent ? NOOP : log,
|
|
@@ -167,96 +174,4 @@ async function loadCommandResource(ctx, command) {
|
|
|
167
174
|
}
|
|
168
175
|
|
|
169
176
|
//#endregion
|
|
170
|
-
|
|
171
|
-
async function cli(args, entry, opts = {}) {
|
|
172
|
-
const tokens = parseArgs(args);
|
|
173
|
-
const subCommand = getSubCommand(tokens);
|
|
174
|
-
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
175
|
-
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
|
|
176
|
-
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
177
|
-
const options = resolveArgOptions(command.options);
|
|
178
|
-
const { values, positionals, error } = resolveArgs(options, tokens);
|
|
179
|
-
const omitted = !subCommand;
|
|
180
|
-
const ctx = await createCommandContext({
|
|
181
|
-
options,
|
|
182
|
-
values,
|
|
183
|
-
positionals,
|
|
184
|
-
args,
|
|
185
|
-
tokens,
|
|
186
|
-
omitted,
|
|
187
|
-
command,
|
|
188
|
-
commandOptions: resolvedCommandOptions
|
|
189
|
-
});
|
|
190
|
-
if (values.version) {
|
|
191
|
-
showVersion(ctx);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const usageBuffer = [];
|
|
195
|
-
const header = await showHeader(ctx);
|
|
196
|
-
if (header) usageBuffer.push(header);
|
|
197
|
-
if (values.help) {
|
|
198
|
-
const usage = await showUsage(ctx);
|
|
199
|
-
if (usage) usageBuffer.push(usage);
|
|
200
|
-
return usageBuffer.join("\n");
|
|
201
|
-
}
|
|
202
|
-
if (error) {
|
|
203
|
-
await showValidationErrors(ctx, error);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
await command.run(ctx);
|
|
207
|
-
}
|
|
208
|
-
function resolveArgOptions(options) {
|
|
209
|
-
return Object.assign(create(), options, COMMON_OPTIONS);
|
|
210
|
-
}
|
|
211
|
-
function resolveCommandOptions(options, entry) {
|
|
212
|
-
const subCommands = new Map(options.subCommands);
|
|
213
|
-
if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
|
|
214
|
-
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
215
|
-
return resolvedOptions;
|
|
216
|
-
}
|
|
217
|
-
function getSubCommand(tokens) {
|
|
218
|
-
const firstToken = tokens[0];
|
|
219
|
-
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
220
|
-
}
|
|
221
|
-
async function showUsage(ctx) {
|
|
222
|
-
if (ctx.env.renderUsage === null) return;
|
|
223
|
-
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
224
|
-
if (usage) {
|
|
225
|
-
ctx.log(usage);
|
|
226
|
-
return usage;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
function showVersion(ctx) {
|
|
230
|
-
ctx.log(ctx.env.version);
|
|
231
|
-
}
|
|
232
|
-
async function showHeader(ctx) {
|
|
233
|
-
if (ctx.env.renderHeader === null) return;
|
|
234
|
-
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
235
|
-
if (header) {
|
|
236
|
-
ctx.log(header);
|
|
237
|
-
ctx.log();
|
|
238
|
-
return header;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
async function showValidationErrors(ctx, error) {
|
|
242
|
-
if (ctx.env.renderValidationErrors === null) return;
|
|
243
|
-
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
244
|
-
ctx.log(await render(ctx, error));
|
|
245
|
-
}
|
|
246
|
-
async function resolveCommand(sub, entry, options) {
|
|
247
|
-
const omitted = !sub;
|
|
248
|
-
if (typeof entry === "function") return [void 0, { run: entry }];
|
|
249
|
-
else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
|
|
250
|
-
else {
|
|
251
|
-
if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
|
|
252
|
-
const cmd = options.subCommands?.get(sub);
|
|
253
|
-
if (cmd == null) return [sub, void 0];
|
|
254
|
-
return [sub, await resolveLazyCommand(cmd, sub)];
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
function resolveEntryName(entry) {
|
|
258
|
-
return entry.name || "(anonymous)";
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
//#endregion
|
|
262
|
-
export { DefaultTranslation, cli };
|
|
177
|
+
export { DefaultTranslation, createCommandContext };
|
package/lib/context.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Command, CommandContext, CommandOptions } from "./types.d-aDzUZTqM.js";
|
|
2
|
+
import { ArgOptions, ArgToken, ArgValues } from "args-tokens";
|
|
3
|
+
|
|
4
|
+
//#region src/context.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parameters of {@link createCommandContext}
|
|
7
|
+
*/
|
|
8
|
+
interface CommandContextParams<Options extends ArgOptions, Values> {
|
|
9
|
+
/**
|
|
10
|
+
* An options of target command
|
|
11
|
+
*/
|
|
12
|
+
options: Options;
|
|
13
|
+
/**
|
|
14
|
+
* A values of target command
|
|
15
|
+
*/
|
|
16
|
+
values: Values;
|
|
17
|
+
/**
|
|
18
|
+
* A positionals arguments, which passed to the target command
|
|
19
|
+
*/
|
|
20
|
+
positionals: string[];
|
|
21
|
+
/**
|
|
22
|
+
* A rest arguments, which passed to the target command
|
|
23
|
+
*/
|
|
24
|
+
rest: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Original command line arguments
|
|
27
|
+
*/
|
|
28
|
+
args: string[];
|
|
29
|
+
/**
|
|
30
|
+
* Argument tokens that are parsed by the `parseArgs` function
|
|
31
|
+
*/
|
|
32
|
+
tokens: ArgToken[];
|
|
33
|
+
/**
|
|
34
|
+
* Whether the command is omitted
|
|
35
|
+
*/
|
|
36
|
+
omitted: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* A target {@link Command | command}
|
|
39
|
+
*/
|
|
40
|
+
command: Command<Options>;
|
|
41
|
+
/**
|
|
42
|
+
* A command options, which is spicialized from `cli` function
|
|
43
|
+
*/
|
|
44
|
+
commandOptions: CommandOptions<Options>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a {@link CommandContext | command context}
|
|
48
|
+
* @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
|
|
49
|
+
* @returns A {@link CommandContext | command context}, which is readonly
|
|
50
|
+
*/
|
|
51
|
+
declare function createCommandContext<Options extends ArgOptions = ArgOptions, Values extends ArgValues<Options> = ArgValues<Options>>({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>;
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { createCommandContext };
|
package/lib/context.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/definition.ts
|
|
2
|
+
/**
|
|
3
|
+
* Define a {@link Command | command} with type inference
|
|
4
|
+
* @param definition A {@link Command | command} definition
|
|
5
|
+
* @returns A {@link Command | command} definition with type inference
|
|
6
|
+
*/
|
|
7
|
+
function define(definition) {
|
|
8
|
+
return definition;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { define };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Command } from "./types.d-
|
|
1
|
+
import { Command } from "./types.d-aDzUZTqM.js";
|
|
2
2
|
import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as ArgValues$1 } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/definition.d.ts
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
* Define a {@link Command | command} with type inference
|
|
7
|
+
* @param definition A {@link Command | command} definition
|
|
8
|
+
* @returns A {@link Command | command} definition with type inference
|
|
9
|
+
*/
|
|
10
10
|
declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>;
|
|
11
11
|
|
|
12
12
|
//#endregion
|
package/lib/definition.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./types.d-
|
|
2
|
-
import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-
|
|
1
|
+
import "./types.d-aDzUZTqM.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-DllW_uD3.js";
|
|
3
3
|
|
|
4
4
|
export { ArgOptionSchema, ArgOptions, ArgValues, define };
|
package/lib/definition.js
CHANGED
package/lib/generator.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { Command, CommandOptions } from "./types.d-
|
|
1
|
+
import { Command, CommandOptions } from "./types.d-aDzUZTqM.js";
|
|
2
2
|
import { ArgOptions } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/generator.d.ts
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
* Generate the command usage.
|
|
7
|
+
* @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
|
|
8
|
+
* @param entry - A {@link Command | entry command}
|
|
9
|
+
* @param opts - A {@link CommandOptions | command options}
|
|
10
|
+
* @returns A rendered usage.
|
|
11
|
+
*/
|
|
12
12
|
declare function generate<Options extends ArgOptions = ArgOptions>(command: string | null, entry: Command<Options>, opts?: CommandOptions<Options>): Promise<string>;
|
|
13
13
|
|
|
14
14
|
//#endregion
|
package/lib/generator.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import { create } from "./
|
|
2
|
-
import
|
|
1
|
+
import { create } from "./utils-BYPzZy9X.js";
|
|
2
|
+
import "./context-BROXRnNP.js";
|
|
3
|
+
import "./renderer-BNorS8VG.js";
|
|
4
|
+
import { cli } from "./cli-B7eqtqBF.js";
|
|
3
5
|
|
|
4
6
|
//#region src/generator.ts
|
|
7
|
+
/**
|
|
8
|
+
* Generate the command usage.
|
|
9
|
+
* @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
|
|
10
|
+
* @param entry - A {@link Command | entry command}
|
|
11
|
+
* @param opts - A {@link CommandOptions | command options}
|
|
12
|
+
* @returns A rendered usage.
|
|
13
|
+
*/
|
|
5
14
|
async function generate(command, entry, opts = {}) {
|
|
6
15
|
const args = ["-h"];
|
|
7
16
|
if (command != null) args.unshift(command);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types.d-
|
|
2
|
-
import { define$1 as define } from "./definition.d-
|
|
1
|
+
import { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types.d-aDzUZTqM.js";
|
|
2
|
+
import { define$1 as define } from "./definition.d-DllW_uD3.js";
|
|
3
3
|
import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues, parseArgs, resolveArgs } from "args-tokens";
|
|
4
4
|
|
|
5
5
|
//#region src/cli.d.ts
|
|
6
6
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
* Run the command.
|
|
8
|
+
* @param args Command line arguments
|
|
9
|
+
* @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
|
|
10
|
+
* @param opts A {@link CommandOptions | command options}
|
|
11
|
+
* @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
|
|
12
|
+
*/
|
|
13
13
|
declare function cli<Options extends ArgOptions$1 = ArgOptions$1>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
|
|
14
14
|
|
|
15
15
|
//#endregion
|
|
@@ -24,4 +24,4 @@ declare class DefaultTranslation implements TranslationAdapter {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
//#endregion
|
|
27
|
-
export { ArgOptionSchema, ArgOptions, ArgValues, Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, parseArgs, resolveArgs };
|
|
27
|
+
export { ArgOptionSchema, ArgOptions, ArgValues, Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, parseArgs, resolveArgs };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-BYPzZy9X.js";
|
|
2
|
+
import { DefaultTranslation } from "./context-BROXRnNP.js";
|
|
3
|
+
import { define } from "./definition-VzcnM0si.js";
|
|
4
|
+
import "./renderer-BNorS8VG.js";
|
|
5
|
+
import { cli } from "./cli-B7eqtqBF.js";
|
|
4
6
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
7
|
|
|
6
8
|
export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
|
package/lib/locales/en-US.json
CHANGED
package/lib/locales/ja-JP.json
CHANGED
|
@@ -1,74 +1,11 @@
|
|
|
1
|
+
import { create, resolveBuiltInKey, resolveOptionKey } from "./utils-BYPzZy9X.js";
|
|
1
2
|
|
|
2
|
-
//#region src/constants.ts
|
|
3
|
-
const DEFAULT_LOCALE = "en-US";
|
|
4
|
-
const BUILT_IN_PREFIX = "_";
|
|
5
|
-
const OPTION_PREFIX = "Option";
|
|
6
|
-
const BUILT_IN_KEY_SEPARATOR = ":";
|
|
7
|
-
const NOOP = () => {};
|
|
8
|
-
const COMMON_OPTIONS = {
|
|
9
|
-
help: {
|
|
10
|
-
type: "boolean",
|
|
11
|
-
short: "h",
|
|
12
|
-
description: "Display this help message"
|
|
13
|
-
},
|
|
14
|
-
version: {
|
|
15
|
-
type: "boolean",
|
|
16
|
-
short: "v",
|
|
17
|
-
description: "Display this version"
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
const COMMAND_OPTIONS_DEFAULT = {
|
|
21
|
-
name: void 0,
|
|
22
|
-
description: void 0,
|
|
23
|
-
version: void 0,
|
|
24
|
-
cwd: void 0,
|
|
25
|
-
usageSilent: false,
|
|
26
|
-
subCommands: void 0,
|
|
27
|
-
leftMargin: 2,
|
|
28
|
-
middleMargin: 10,
|
|
29
|
-
usageOptionType: false,
|
|
30
|
-
renderHeader: void 0,
|
|
31
|
-
renderUsage: void 0,
|
|
32
|
-
renderValidationErrors: void 0,
|
|
33
|
-
translationAdapterFactory: void 0
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/utils.ts
|
|
38
|
-
async function resolveLazyCommand(cmd, name) {
|
|
39
|
-
const resolved = Object.assign(create(), typeof cmd == "function" ? await cmd() : cmd);
|
|
40
|
-
if (resolved.name == null && name) resolved.name = name;
|
|
41
|
-
return deepFreeze(resolved);
|
|
42
|
-
}
|
|
43
|
-
function resolveBuiltInKey(key) {
|
|
44
|
-
return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
45
|
-
}
|
|
46
|
-
function resolveOptionKey(key) {
|
|
47
|
-
return `${OPTION_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
48
|
-
}
|
|
49
|
-
function mapResourceWithBuiltinKey(resource) {
|
|
50
|
-
return Object.entries(resource).reduce((acc, [key, value]) => {
|
|
51
|
-
acc[resolveBuiltInKey(key)] = value;
|
|
52
|
-
return acc;
|
|
53
|
-
}, create());
|
|
54
|
-
}
|
|
55
|
-
function create(obj = null) {
|
|
56
|
-
return Object.create(obj);
|
|
57
|
-
}
|
|
58
|
-
function log(...args) {
|
|
59
|
-
console.log(...args);
|
|
60
|
-
}
|
|
61
|
-
function deepFreeze(obj) {
|
|
62
|
-
if (obj === null || typeof obj !== "object") return obj;
|
|
63
|
-
for (const key of Object.keys(obj)) {
|
|
64
|
-
const value = obj[key];
|
|
65
|
-
if (typeof value === "object" && value !== null) deepFreeze(value);
|
|
66
|
-
}
|
|
67
|
-
return Object.freeze(obj);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
//#endregion
|
|
71
3
|
//#region src/renderer/header.ts
|
|
4
|
+
/**
|
|
5
|
+
* Render the header.
|
|
6
|
+
* @param ctx A {@link CommandContext | command context}
|
|
7
|
+
* @returns A rendered header.
|
|
8
|
+
*/
|
|
72
9
|
function renderHeader(ctx) {
|
|
73
10
|
const title = ctx.env.description || ctx.env.name || "";
|
|
74
11
|
return Promise.resolve(title ? `${title} (${ctx.env.name || ""}${ctx.env.version ? ` v${ctx.env.version}` : ""})` : title);
|
|
@@ -76,6 +13,11 @@ function renderHeader(ctx) {
|
|
|
76
13
|
|
|
77
14
|
//#endregion
|
|
78
15
|
//#region src/renderer/usage.ts
|
|
16
|
+
/**
|
|
17
|
+
* Render the usage.
|
|
18
|
+
* @param ctx A {@link CommandContext | command context}
|
|
19
|
+
* @returns A rendered usage.
|
|
20
|
+
*/
|
|
79
21
|
async function renderUsage(ctx) {
|
|
80
22
|
const messages = [];
|
|
81
23
|
if (!ctx.omitted) {
|
|
@@ -224,6 +166,11 @@ function hasAllDefaultOptions(ctx) {
|
|
|
224
166
|
function generateOptionsSymbols(ctx) {
|
|
225
167
|
return hasOptions(ctx) ? hasAllDefaultOptions(ctx) ? `[${ctx.translate(resolveBuiltInKey("OPTIONS"))}]` : `<${ctx.translate(resolveBuiltInKey("OPTIONS"))}>` : "";
|
|
226
168
|
}
|
|
169
|
+
function makeShortLongOptionPair(schema, name) {
|
|
170
|
+
let key = `--${name}`;
|
|
171
|
+
if (schema.short) key = `-${schema.short}, ${key}`;
|
|
172
|
+
return key;
|
|
173
|
+
}
|
|
227
174
|
/**
|
|
228
175
|
* Get options pairs for usage
|
|
229
176
|
* @param ctx A {@link CommandContext | command context}
|
|
@@ -231,13 +178,17 @@ function generateOptionsSymbols(ctx) {
|
|
|
231
178
|
*/
|
|
232
179
|
function getOptionsPairs(ctx) {
|
|
233
180
|
return Object.entries(ctx.options).reduce((acc, [name, value]) => {
|
|
234
|
-
let key =
|
|
235
|
-
if (value.short) key = `-${value.short}, ${key}`;
|
|
181
|
+
let key = makeShortLongOptionPair(value, name);
|
|
236
182
|
if (value.type !== "boolean") key = value.default ? `${key} [${name}]` : `${key} <${name}>`;
|
|
237
183
|
acc[name] = key;
|
|
184
|
+
if (value.type === "boolean" && !(name === "help" || name === "version")) acc[`no-${name}`] = `--no-${name}`;
|
|
238
185
|
return acc;
|
|
239
186
|
}, create());
|
|
240
187
|
}
|
|
188
|
+
const resolveNegatableKey = (key) => key.split("no-")[1];
|
|
189
|
+
function resolveNegatableType(key, ctx) {
|
|
190
|
+
return ctx.options[key.startsWith("no-") ? resolveNegatableKey(key) : key].type;
|
|
191
|
+
}
|
|
241
192
|
/**
|
|
242
193
|
* Generate options usage
|
|
243
194
|
* @param ctx A {@link CommandContext | command context}
|
|
@@ -246,10 +197,16 @@ function getOptionsPairs(ctx) {
|
|
|
246
197
|
*/
|
|
247
198
|
async function generateOptionsUsage(ctx, optionsPairs) {
|
|
248
199
|
const optionsMaxLength = Math.max(...Object.entries(optionsPairs).map(([_, value]) => value.length));
|
|
249
|
-
const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key, _]) => ctx.
|
|
200
|
+
const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key, _]) => resolveNegatableType(key, ctx).length)) : 0;
|
|
250
201
|
const usages = await Promise.all(Object.entries(optionsPairs).map(([key, value]) => {
|
|
251
|
-
|
|
252
|
-
|
|
202
|
+
let rawDesc = ctx.translate(resolveOptionKey(key));
|
|
203
|
+
if (!rawDesc && key.startsWith("no-")) {
|
|
204
|
+
const name = resolveNegatableKey(key);
|
|
205
|
+
const schema = ctx.options[name];
|
|
206
|
+
const optionKey = makeShortLongOptionPair(schema, name);
|
|
207
|
+
rawDesc = `${ctx.translate(resolveBuiltInKey("NEGATABLE"))} ${optionKey}`;
|
|
208
|
+
}
|
|
209
|
+
const optionsSchema = ctx.env.usageOptionType ? `[${resolveNegatableType(key, ctx)}] ` : "";
|
|
253
210
|
const desc = `${optionsSchema ? optionsSchema.padEnd(optionSchemaMaxLength + 3) : ""}${rawDesc}`;
|
|
254
211
|
const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}`;
|
|
255
212
|
return `${option.padStart(ctx.env.leftMargin + option.length)}`;
|
|
@@ -259,6 +216,12 @@ async function generateOptionsUsage(ctx, optionsPairs) {
|
|
|
259
216
|
|
|
260
217
|
//#endregion
|
|
261
218
|
//#region src/renderer/validation.ts
|
|
219
|
+
/**
|
|
220
|
+
* Render the validation errors.
|
|
221
|
+
* @param ctx A {@link CommandContext | command context}
|
|
222
|
+
* @param error An {@link AggregateError} of option in `args-token` validation
|
|
223
|
+
* @returns A rendered validation error.
|
|
224
|
+
*/
|
|
262
225
|
function renderValidationErrors(_ctx, error) {
|
|
263
226
|
const messages = [];
|
|
264
227
|
for (const err of error.errors) messages.push(err.message);
|
|
@@ -266,4 +229,4 @@ function renderValidationErrors(_ctx, error) {
|
|
|
266
229
|
}
|
|
267
230
|
|
|
268
231
|
//#endregion
|
|
269
|
-
export {
|
|
232
|
+
export { renderHeader, renderUsage, renderValidationErrors };
|
package/lib/renderer.d.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { CommandContext } from "./types.d-
|
|
1
|
+
import { CommandContext } from "./types.d-aDzUZTqM.js";
|
|
2
2
|
import { ArgOptions } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/renderer/header.d.ts
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
* Render the header.
|
|
7
|
+
* @param ctx A {@link CommandContext | command context}
|
|
8
|
+
* @returns A rendered header.
|
|
9
|
+
*/
|
|
10
10
|
declare function renderHeader<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
|
|
11
11
|
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/renderer/usage.d.ts
|
|
14
14
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
* Render the usage.
|
|
16
|
+
* @param ctx A {@link CommandContext | command context}
|
|
17
|
+
* @returns A rendered usage.
|
|
18
|
+
*/
|
|
19
19
|
declare function renderUsage<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
|
|
20
20
|
|
|
21
21
|
//#endregion
|
|
22
22
|
//#region src/renderer/validation.d.ts
|
|
23
23
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
* Render the validation errors.
|
|
25
|
+
* @param ctx A {@link CommandContext | command context}
|
|
26
|
+
* @param error An {@link AggregateError} of option in `args-token` validation
|
|
27
|
+
* @returns A rendered validation error.
|
|
28
|
+
*/
|
|
29
29
|
declare function renderValidationErrors<Options extends ArgOptions = ArgOptions>(_ctx: CommandContext<Options>, error: AggregateError): Promise<string>;
|
|
30
30
|
|
|
31
31
|
//#endregion
|
package/lib/renderer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./utils-BYPzZy9X.js";
|
|
2
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BNorS8VG.js";
|
|
2
3
|
|
|
3
4
|
export { renderHeader, renderUsage, renderValidationErrors };
|
|
@@ -11,8 +11,8 @@ declare namespace constants_d_exports {
|
|
|
11
11
|
export { BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, OPTION_PREFIX, }
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
* The default locale string, which format is BCP 47 language tag.
|
|
15
|
+
*/
|
|
16
16
|
declare const DEFAULT_LOCALE = "en-US";
|
|
17
17
|
declare const BUILT_IN_PREFIX = "_";
|
|
18
18
|
declare const OPTION_PREFIX = "Option";
|
|
@@ -32,7 +32,7 @@ type CommonOptionType = {
|
|
|
32
32
|
};
|
|
33
33
|
declare const COMMON_OPTIONS: CommonOptionType;
|
|
34
34
|
declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions>;
|
|
35
|
-
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE"];
|
|
35
|
+
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE"];
|
|
36
36
|
|
|
37
37
|
//#endregion
|
|
38
38
|
//#region src/types.d.ts
|
|
@@ -41,36 +41,39 @@ type RemoveIndexSignature<T> = {
|
|
|
41
41
|
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
|
|
42
42
|
};
|
|
43
43
|
/**
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
* Remove index signature from object or record type.
|
|
45
|
+
*/
|
|
46
46
|
type RemovedIndex<T> = RemoveIndexSignature<{
|
|
47
47
|
[K in keyof T]: T[K];
|
|
48
48
|
}>;
|
|
49
|
+
type KeyOfArgOptions<Options extends ArgOptions> = keyof Options | {
|
|
50
|
+
[K in keyof Options]: Options[K]['type'] extends 'boolean' ? `no-${Extract<K, string>}` : never;
|
|
51
|
+
}[keyof Options];
|
|
49
52
|
/**
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
* Generate a namespaced key.
|
|
54
|
+
*/
|
|
52
55
|
type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
|
|
53
56
|
/**
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
* Command i18n built-in options keys.
|
|
58
|
+
*/
|
|
56
59
|
type CommandBuiltinOptionsKeys = keyof (typeof constants_d_exports)['COMMON_OPTIONS'];
|
|
57
60
|
/**
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
* Command i18n built-in resource keys.
|
|
62
|
+
*/
|
|
60
63
|
type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
|
|
61
64
|
/**
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
* Command i18n built-in keys.
|
|
66
|
+
* The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
|
|
67
|
+
*/
|
|
65
68
|
type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | 'description' | 'examples';
|
|
66
69
|
/**
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<
|
|
70
|
+
* Command i18n option keys.
|
|
71
|
+
* The command i18n option keys are used to {@link CommandContext.translate | translate} function.
|
|
72
|
+
*/
|
|
73
|
+
type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>;
|
|
71
74
|
/**
|
|
72
|
-
|
|
73
|
-
|
|
75
|
+
* Command environment.
|
|
76
|
+
*/
|
|
74
77
|
interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
|
|
75
78
|
/**
|
|
76
79
|
* Current working directory.
|
|
@@ -136,8 +139,8 @@ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
|
|
|
136
139
|
renderValidationErrors: ((ctx: CommandContext<Options>, error: AggregateError) => Promise<string>) | null | undefined;
|
|
137
140
|
}
|
|
138
141
|
/**
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
* Command options.
|
|
143
|
+
*/
|
|
141
144
|
interface CommandOptions<Options extends ArgOptions = ArgOptions> {
|
|
142
145
|
/**
|
|
143
146
|
* Current working directory.
|
|
@@ -198,9 +201,9 @@ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
|
|
|
198
201
|
translationAdapterFactory?: TranslationAdapterFactory;
|
|
199
202
|
}
|
|
200
203
|
/**
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
+
* Command context.
|
|
205
|
+
* Command context is the context of the command execution.
|
|
206
|
+
*/
|
|
204
207
|
interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> {
|
|
205
208
|
/**
|
|
206
209
|
* Command name, that is the command that is executed.
|
|
@@ -236,6 +239,10 @@ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgVa
|
|
|
236
239
|
* Resolve positionals with `resolveArgs` from command arguments.
|
|
237
240
|
*/
|
|
238
241
|
positionals: string[];
|
|
242
|
+
/**
|
|
243
|
+
* Command rest arguments, that is the remaining argument not resolved by the optional command option delimiter `--`.
|
|
244
|
+
*/
|
|
245
|
+
rest: string[];
|
|
239
246
|
/**
|
|
240
247
|
* Original command line arguments.
|
|
241
248
|
* This argument is passed from `cli` function.
|
|
@@ -271,8 +278,8 @@ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgVa
|
|
|
271
278
|
translate: <T extends string = CommandBuiltinKeys, O = CommandOptionKeys<Options>, Key = CommandBuiltinKeys | O | T>(key: Key, values?: Record<string, unknown>) => string;
|
|
272
279
|
}
|
|
273
280
|
/**
|
|
274
|
-
|
|
275
|
-
|
|
281
|
+
* Command interface.
|
|
282
|
+
*/
|
|
276
283
|
interface Command<Options extends ArgOptions = ArgOptions> {
|
|
277
284
|
/**
|
|
278
285
|
* Command name.
|
|
@@ -304,8 +311,8 @@ interface Command<Options extends ArgOptions = ArgOptions> {
|
|
|
304
311
|
resource?: CommandResourceFetcher<Options>;
|
|
305
312
|
}
|
|
306
313
|
/**
|
|
307
|
-
|
|
308
|
-
|
|
314
|
+
* Command resource.
|
|
315
|
+
*/
|
|
309
316
|
type CommandResource<Options extends ArgOptions = ArgOptions> = {
|
|
310
317
|
/**
|
|
311
318
|
* Command description.
|
|
@@ -316,23 +323,23 @@ type CommandResource<Options extends ArgOptions = ArgOptions> = {
|
|
|
316
323
|
*/
|
|
317
324
|
examples: string;
|
|
318
325
|
} & {
|
|
319
|
-
[Option in GenerateNamespacedKey<
|
|
326
|
+
[Option in GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>]: string;
|
|
320
327
|
} & {
|
|
321
328
|
[key: string]: string;
|
|
322
329
|
};
|
|
323
330
|
/**
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
331
|
+
* Command resource fetcher.
|
|
332
|
+
* @param ctx A {@link CommandContext | command context}
|
|
333
|
+
* @returns A fetched {@link CommandResource | command resource}.
|
|
334
|
+
*/
|
|
328
335
|
type CommandResourceFetcher<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> = (ctx: Readonly<CommandContext<Options, Values>>) => Promise<CommandResource<Options>>;
|
|
329
336
|
/**
|
|
330
|
-
|
|
331
|
-
|
|
337
|
+
* Translation adapter factory.
|
|
338
|
+
*/
|
|
332
339
|
type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter;
|
|
333
340
|
/**
|
|
334
|
-
|
|
335
|
-
|
|
341
|
+
* Translation adapter factory options.
|
|
342
|
+
*/
|
|
336
343
|
interface TranslationAdapterFactoryOptions {
|
|
337
344
|
/**
|
|
338
345
|
* A locale.
|
|
@@ -344,10 +351,10 @@ interface TranslationAdapterFactoryOptions {
|
|
|
344
351
|
fallbackLocale: string;
|
|
345
352
|
}
|
|
346
353
|
/**
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
354
|
+
* Translation adapter.
|
|
355
|
+
* This adapter is used to custom message formatter like {@link https://github.com/intlify/vue-i18n/blob/master/spec/syntax.ebnf | Intlify message format}, {@link https://github.com/tc39/proposal-intl-messageformat | `Intl.MessageFormat` (MF2)}, and etc.
|
|
356
|
+
* This adapter will support localization with your preferred message format.
|
|
357
|
+
*/
|
|
351
358
|
interface TranslationAdapter<MessageResource = string> {
|
|
352
359
|
/**
|
|
353
360
|
* Get a resource of locale.
|
|
@@ -378,19 +385,19 @@ interface TranslationAdapter<MessageResource = string> {
|
|
|
378
385
|
translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
|
|
379
386
|
}
|
|
380
387
|
/**
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
388
|
+
* Command runner.
|
|
389
|
+
* @param ctx A {@link CommandContext | command context}
|
|
390
|
+
*/
|
|
384
391
|
type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
|
|
385
392
|
/**
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
393
|
+
* Lazy command interface.
|
|
394
|
+
* Lazy command that's not loaded until it is executed.
|
|
395
|
+
*/
|
|
389
396
|
type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Command<Options>>;
|
|
390
397
|
/**
|
|
391
|
-
|
|
392
|
-
|
|
398
|
+
* Define a command type.
|
|
399
|
+
*/
|
|
393
400
|
type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>;
|
|
394
401
|
|
|
395
402
|
//#endregion
|
|
396
|
-
export { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
|
|
403
|
+
export { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
/**
|
|
3
|
+
* The default locale string, which format is BCP 47 language tag.
|
|
4
|
+
*/
|
|
5
|
+
const DEFAULT_LOCALE = "en-US";
|
|
6
|
+
const BUILT_IN_PREFIX = "_";
|
|
7
|
+
const OPTION_PREFIX = "Option";
|
|
8
|
+
const BUILT_IN_KEY_SEPARATOR = ":";
|
|
9
|
+
const NOOP = () => {};
|
|
10
|
+
const COMMON_OPTIONS = {
|
|
11
|
+
help: {
|
|
12
|
+
type: "boolean",
|
|
13
|
+
short: "h",
|
|
14
|
+
description: "Display this help message"
|
|
15
|
+
},
|
|
16
|
+
version: {
|
|
17
|
+
type: "boolean",
|
|
18
|
+
short: "v",
|
|
19
|
+
description: "Display this version"
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const COMMAND_OPTIONS_DEFAULT = {
|
|
23
|
+
name: void 0,
|
|
24
|
+
description: void 0,
|
|
25
|
+
version: void 0,
|
|
26
|
+
cwd: void 0,
|
|
27
|
+
usageSilent: false,
|
|
28
|
+
subCommands: void 0,
|
|
29
|
+
leftMargin: 2,
|
|
30
|
+
middleMargin: 10,
|
|
31
|
+
usageOptionType: false,
|
|
32
|
+
renderHeader: void 0,
|
|
33
|
+
renderUsage: void 0,
|
|
34
|
+
renderValidationErrors: void 0,
|
|
35
|
+
translationAdapterFactory: void 0
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/utils.ts
|
|
40
|
+
async function resolveLazyCommand(cmd, name) {
|
|
41
|
+
const resolved = Object.assign(create(), typeof cmd == "function" ? await cmd() : cmd);
|
|
42
|
+
if (resolved.name == null && name) resolved.name = name;
|
|
43
|
+
return deepFreeze(resolved);
|
|
44
|
+
}
|
|
45
|
+
function resolveBuiltInKey(key) {
|
|
46
|
+
return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
47
|
+
}
|
|
48
|
+
function resolveOptionKey(key) {
|
|
49
|
+
return `${OPTION_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
50
|
+
}
|
|
51
|
+
function mapResourceWithBuiltinKey(resource) {
|
|
52
|
+
return Object.entries(resource).reduce((acc, [key, value]) => {
|
|
53
|
+
acc[resolveBuiltInKey(key)] = value;
|
|
54
|
+
return acc;
|
|
55
|
+
}, create());
|
|
56
|
+
}
|
|
57
|
+
function create(obj = null) {
|
|
58
|
+
return Object.create(obj);
|
|
59
|
+
}
|
|
60
|
+
function log(...args) {
|
|
61
|
+
console.log(...args);
|
|
62
|
+
}
|
|
63
|
+
function deepFreeze(obj) {
|
|
64
|
+
if (obj === null || typeof obj !== "object") return obj;
|
|
65
|
+
for (const key of Object.keys(obj)) {
|
|
66
|
+
const value = obj[key];
|
|
67
|
+
if (typeof value === "object" && value !== null) deepFreeze(value);
|
|
68
|
+
}
|
|
69
|
+
return Object.freeze(obj);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveBuiltInKey, resolveLazyCommand, resolveOptionKey };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gunshi",
|
|
3
3
|
"description": "Modern javascript command-line library",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.16.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -47,6 +47,12 @@
|
|
|
47
47
|
"require": "./lib/definition.js",
|
|
48
48
|
"default": "./lib/definition.js"
|
|
49
49
|
},
|
|
50
|
+
"./context": {
|
|
51
|
+
"types": "./lib/context.d.ts",
|
|
52
|
+
"import": "./lib/context.js",
|
|
53
|
+
"require": "./lib/context.js",
|
|
54
|
+
"default": "./lib/context.js"
|
|
55
|
+
},
|
|
50
56
|
"./renderer": {
|
|
51
57
|
"types": "./lib/renderer.d.ts",
|
|
52
58
|
"import": "./lib/renderer.js",
|
|
@@ -71,7 +77,7 @@
|
|
|
71
77
|
}
|
|
72
78
|
},
|
|
73
79
|
"dependencies": {
|
|
74
|
-
"args-tokens": "^0.
|
|
80
|
+
"args-tokens": "^0.15.1"
|
|
75
81
|
},
|
|
76
82
|
"devDependencies": {
|
|
77
83
|
"@eslint/markdown": "^6.3.0",
|
|
@@ -97,14 +103,14 @@
|
|
|
97
103
|
"eslint-plugin-yml": "^1.17.0",
|
|
98
104
|
"gh-changelogen": "^0.2.8",
|
|
99
105
|
"jsr": "^0.13.4",
|
|
106
|
+
"jsr-exports-lint": "^0.2.0",
|
|
100
107
|
"knip": "^5.50.2",
|
|
101
108
|
"lint-staged": "^15.5.0",
|
|
102
109
|
"messageformat": "4.0.0-10",
|
|
103
|
-
"pkg-pr-new": "^0.0.
|
|
110
|
+
"pkg-pr-new": "^0.0.43",
|
|
104
111
|
"prettier": "^3.5.3",
|
|
105
112
|
"publint": "^0.3.11",
|
|
106
|
-
"tsdown": "^0.9.
|
|
107
|
-
"tsdown-jsr-exports-lint": "^0.1.4",
|
|
113
|
+
"tsdown": "^0.9.6",
|
|
108
114
|
"typedoc": "^0.28.2",
|
|
109
115
|
"typedoc-plugin-markdown": "^4.6.2",
|
|
110
116
|
"typedoc-vitepress-theme": "^1.1.2",
|
|
@@ -153,7 +159,7 @@
|
|
|
153
159
|
"lint:knip": "knip",
|
|
154
160
|
"lint:prettier": "prettier . --check",
|
|
155
161
|
"release": "bumpp --commit \"release: v%s\" --all --push --tag",
|
|
156
|
-
"test": "vitest run",
|
|
162
|
+
"test": "vitest --typecheck run",
|
|
157
163
|
"typecheck": "pnpm run --stream --color \"/^typecheck:/\"",
|
|
158
164
|
"typecheck:deno": "deno check --all ./src",
|
|
159
165
|
"typecheck:tsc": "tsc --noEmit"
|