gunshi 0.15.0 → 0.17.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-D2dWSSRj.js +106 -0
- package/lib/{cli--6qYDY8U.js → context-BROXRnNP.js} +7 -99
- package/lib/context.d.ts +63 -0
- package/lib/context.js +4 -0
- package/lib/definition-VzcnM0si.js +12 -0
- package/lib/definition.d-wYlroF3H.d.ts +12 -0
- package/lib/definition.d.ts +2 -3
- package/lib/definition.js +1 -1
- package/lib/generator.d.ts +8 -7
- package/lib/generator.js +11 -2
- package/lib/index.d.ts +15 -14
- package/lib/index.js +5 -3
- package/lib/{renderer-BeuJazdk.js → renderer-pNEj3FtQ.js} +19 -71
- package/lib/renderer.d.ts +15 -14
- package/lib/renderer.js +2 -1
- package/lib/types.d-DomXJWKH.d.ts +395 -0
- package/lib/utils-BYPzZy9X.js +73 -0
- package/package.json +27 -21
- package/lib/definition-BAm6f1St.js +0 -8
- package/lib/definition.d-DD_GraLw.d.ts +0 -13
- package/lib/types.d-zAq1WIpD.d.ts +0 -403
|
@@ -0,0 +1,106 @@
|
|
|
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-pNEj3FtQ.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, { optionGrouping: true });
|
|
22
|
+
const omitted = !subCommand;
|
|
23
|
+
const ctx = await createCommandContext({
|
|
24
|
+
options,
|
|
25
|
+
values,
|
|
26
|
+
positionals,
|
|
27
|
+
rest,
|
|
28
|
+
args,
|
|
29
|
+
tokens,
|
|
30
|
+
omitted,
|
|
31
|
+
command,
|
|
32
|
+
commandOptions: resolvedCommandOptions
|
|
33
|
+
});
|
|
34
|
+
if (values.version) {
|
|
35
|
+
showVersion(ctx);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const usageBuffer = [];
|
|
39
|
+
const header = await showHeader(ctx);
|
|
40
|
+
if (header) usageBuffer.push(header);
|
|
41
|
+
if (values.help) {
|
|
42
|
+
const usage = await showUsage(ctx);
|
|
43
|
+
if (usage) usageBuffer.push(usage);
|
|
44
|
+
return usageBuffer.join("\n");
|
|
45
|
+
}
|
|
46
|
+
if (error) {
|
|
47
|
+
await showValidationErrors(ctx, error);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
await command.run(ctx);
|
|
51
|
+
}
|
|
52
|
+
function resolveArgOptions(options) {
|
|
53
|
+
return Object.assign(create(), options, COMMON_OPTIONS);
|
|
54
|
+
}
|
|
55
|
+
function resolveCommandOptions(options, entry) {
|
|
56
|
+
const subCommands = new Map(options.subCommands);
|
|
57
|
+
if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
|
|
58
|
+
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
59
|
+
return resolvedOptions;
|
|
60
|
+
}
|
|
61
|
+
function getSubCommand(tokens) {
|
|
62
|
+
const firstToken = tokens[0];
|
|
63
|
+
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
64
|
+
}
|
|
65
|
+
async function showUsage(ctx) {
|
|
66
|
+
if (ctx.env.renderUsage === null) return;
|
|
67
|
+
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
68
|
+
if (usage) {
|
|
69
|
+
ctx.log(usage);
|
|
70
|
+
return usage;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function showVersion(ctx) {
|
|
74
|
+
ctx.log(ctx.env.version);
|
|
75
|
+
}
|
|
76
|
+
async function showHeader(ctx) {
|
|
77
|
+
if (ctx.env.renderHeader === null) return;
|
|
78
|
+
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
79
|
+
if (header) {
|
|
80
|
+
ctx.log(header);
|
|
81
|
+
ctx.log();
|
|
82
|
+
return header;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function showValidationErrors(ctx, error) {
|
|
86
|
+
if (ctx.env.renderValidationErrors === null) return;
|
|
87
|
+
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
88
|
+
ctx.log(await render(ctx, error));
|
|
89
|
+
}
|
|
90
|
+
async function resolveCommand(sub, entry, options) {
|
|
91
|
+
const omitted = !sub;
|
|
92
|
+
if (typeof entry === "function") return [void 0, { run: entry }];
|
|
93
|
+
else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
|
|
94
|
+
else {
|
|
95
|
+
if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
|
|
96
|
+
const cmd = options.subCommands?.get(sub);
|
|
97
|
+
if (cmd == null) return [sub, void 0];
|
|
98
|
+
return [sub, await resolveLazyCommand(cmd, sub)];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function resolveEntryName(entry) {
|
|
102
|
+
return entry.name || "(anonymous)";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
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";
|
|
@@ -62,6 +61,11 @@ var DefaultTranslation = class {
|
|
|
62
61
|
//#endregion
|
|
63
62
|
//#region src/context.ts
|
|
64
63
|
const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
|
|
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
|
+
*/
|
|
65
69
|
async function createCommandContext({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted = false }) {
|
|
66
70
|
/**
|
|
67
71
|
* normailize the options schema and values, to avoid prototype pollution
|
|
@@ -170,100 +174,4 @@ async function loadCommandResource(ctx, command) {
|
|
|
170
174
|
}
|
|
171
175
|
|
|
172
176
|
//#endregion
|
|
173
|
-
|
|
174
|
-
async function cli(args, entry, opts = {}) {
|
|
175
|
-
const tokens = parseArgs(args);
|
|
176
|
-
const subCommand = getSubCommand(tokens);
|
|
177
|
-
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
178
|
-
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
|
|
179
|
-
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
180
|
-
const options = resolveArgOptions(command.options);
|
|
181
|
-
const { values, positionals, rest, error } = resolveArgs(options, tokens, {
|
|
182
|
-
optionGrouping: true,
|
|
183
|
-
allowNegative: true
|
|
184
|
-
});
|
|
185
|
-
const omitted = !subCommand;
|
|
186
|
-
const ctx = await createCommandContext({
|
|
187
|
-
options,
|
|
188
|
-
values,
|
|
189
|
-
positionals,
|
|
190
|
-
rest,
|
|
191
|
-
args,
|
|
192
|
-
tokens,
|
|
193
|
-
omitted,
|
|
194
|
-
command,
|
|
195
|
-
commandOptions: resolvedCommandOptions
|
|
196
|
-
});
|
|
197
|
-
if (values.version) {
|
|
198
|
-
showVersion(ctx);
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
const usageBuffer = [];
|
|
202
|
-
const header = await showHeader(ctx);
|
|
203
|
-
if (header) usageBuffer.push(header);
|
|
204
|
-
if (values.help) {
|
|
205
|
-
const usage = await showUsage(ctx);
|
|
206
|
-
if (usage) usageBuffer.push(usage);
|
|
207
|
-
return usageBuffer.join("\n");
|
|
208
|
-
}
|
|
209
|
-
if (error) {
|
|
210
|
-
await showValidationErrors(ctx, error);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
await command.run(ctx);
|
|
214
|
-
}
|
|
215
|
-
function resolveArgOptions(options) {
|
|
216
|
-
return Object.assign(create(), options, COMMON_OPTIONS);
|
|
217
|
-
}
|
|
218
|
-
function resolveCommandOptions(options, entry) {
|
|
219
|
-
const subCommands = new Map(options.subCommands);
|
|
220
|
-
if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
|
|
221
|
-
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
222
|
-
return resolvedOptions;
|
|
223
|
-
}
|
|
224
|
-
function getSubCommand(tokens) {
|
|
225
|
-
const firstToken = tokens[0];
|
|
226
|
-
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
227
|
-
}
|
|
228
|
-
async function showUsage(ctx) {
|
|
229
|
-
if (ctx.env.renderUsage === null) return;
|
|
230
|
-
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
231
|
-
if (usage) {
|
|
232
|
-
ctx.log(usage);
|
|
233
|
-
return usage;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
function showVersion(ctx) {
|
|
237
|
-
ctx.log(ctx.env.version);
|
|
238
|
-
}
|
|
239
|
-
async function showHeader(ctx) {
|
|
240
|
-
if (ctx.env.renderHeader === null) return;
|
|
241
|
-
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
242
|
-
if (header) {
|
|
243
|
-
ctx.log(header);
|
|
244
|
-
ctx.log();
|
|
245
|
-
return header;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
async function showValidationErrors(ctx, error) {
|
|
249
|
-
if (ctx.env.renderValidationErrors === null) return;
|
|
250
|
-
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
251
|
-
ctx.log(await render(ctx, error));
|
|
252
|
-
}
|
|
253
|
-
async function resolveCommand(sub, entry, options) {
|
|
254
|
-
const omitted = !sub;
|
|
255
|
-
if (typeof entry === "function") return [void 0, { run: entry }];
|
|
256
|
-
else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
|
|
257
|
-
else {
|
|
258
|
-
if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
|
|
259
|
-
const cmd = options.subCommands?.get(sub);
|
|
260
|
-
if (cmd == null) return [sub, void 0];
|
|
261
|
-
return [sub, await resolveLazyCommand(cmd, sub)];
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
function resolveEntryName(entry) {
|
|
265
|
-
return entry.name || "(anonymous)";
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
//#endregion
|
|
269
|
-
export { DefaultTranslation, cli };
|
|
177
|
+
export { DefaultTranslation, createCommandContext };
|
package/lib/context.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Command, CommandContext, CommandOptions } from "./types.d-DomXJWKH.js";
|
|
2
|
+
import { ArgOptions, ArgToken, ArgValues } from "args-tokens";
|
|
3
|
+
|
|
4
|
+
//#region src/context.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parameters of {@link createCommandContext}
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
interface CommandContextParams<Options extends ArgOptions, Values> {
|
|
10
|
+
/**
|
|
11
|
+
* An options of target command
|
|
12
|
+
*/
|
|
13
|
+
options: Options;
|
|
14
|
+
/**
|
|
15
|
+
* A values of target command
|
|
16
|
+
*/
|
|
17
|
+
values: Values;
|
|
18
|
+
/**
|
|
19
|
+
* A positionals arguments, which passed to the target command
|
|
20
|
+
*/
|
|
21
|
+
positionals: string[];
|
|
22
|
+
/**
|
|
23
|
+
* A rest arguments, which passed to the target command
|
|
24
|
+
*/
|
|
25
|
+
rest: string[];
|
|
26
|
+
/**
|
|
27
|
+
* Original command line arguments
|
|
28
|
+
*/
|
|
29
|
+
args: string[];
|
|
30
|
+
/**
|
|
31
|
+
* Argument tokens that are parsed by the `parseArgs` function
|
|
32
|
+
*/
|
|
33
|
+
tokens: ArgToken[];
|
|
34
|
+
/**
|
|
35
|
+
* Whether the command is omitted
|
|
36
|
+
*/
|
|
37
|
+
omitted: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* A target {@link Command | command}
|
|
40
|
+
*/
|
|
41
|
+
command: Command<Options>;
|
|
42
|
+
/**
|
|
43
|
+
* A command options, which is spicialized from `cli` function
|
|
44
|
+
*/
|
|
45
|
+
commandOptions: CommandOptions<Options>;
|
|
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
|
+
|
|
52
|
+
declare function createCommandContext<Options extends ArgOptions = ArgOptions, Values extends ArgValues<Options> = ArgValues<Options>>({
|
|
53
|
+
options,
|
|
54
|
+
values,
|
|
55
|
+
positionals,
|
|
56
|
+
rest,
|
|
57
|
+
args,
|
|
58
|
+
tokens,
|
|
59
|
+
command,
|
|
60
|
+
commandOptions,
|
|
61
|
+
omitted
|
|
62
|
+
}: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>; //#endregion
|
|
63
|
+
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 };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Command } from "./types.d-DomXJWKH.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as ArgValues$1 } from "args-tokens";
|
|
3
|
+
|
|
4
|
+
//#region src/definition.d.ts
|
|
5
|
+
/**
|
|
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
|
+
|
|
11
|
+
declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>; //#endregion
|
|
12
|
+
export { ArgOptionSchema, ArgOptions$1 as ArgOptions, ArgValues$1 as ArgValues, define as define$1 };
|
package/lib/definition.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "./types.d-
|
|
2
|
-
import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-
|
|
3
|
-
|
|
1
|
+
import "./types.d-DomXJWKH.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-wYlroF3H.js";
|
|
4
3
|
export { ArgOptionSchema, ArgOptions, ArgValues, define };
|
package/lib/definition.js
CHANGED
package/lib/generator.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { Command, CommandOptions } from "./types.d-
|
|
1
|
+
import { Command, CommandOptions } from "./types.d-DomXJWKH.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
13
|
declare function generate<Options extends ArgOptions = ArgOptions>(command: string | null, entry: Command<Options>, opts?: CommandOptions<Options>): Promise<string>;
|
|
13
14
|
|
|
14
15
|
//#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-pNEj3FtQ.js";
|
|
4
|
+
import { cli } from "./cli-D2dWSSRj.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,26 +1,27 @@
|
|
|
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-
|
|
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-DomXJWKH.js";
|
|
2
|
+
import { define$1 as define } from "./definition.d-wYlroF3H.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
14
|
declare function cli<Options extends ArgOptions$1 = ArgOptions$1>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
|
|
14
15
|
|
|
15
16
|
//#endregion
|
|
16
17
|
//#region src/translation.d.ts
|
|
17
18
|
declare class DefaultTranslation implements TranslationAdapter {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
#private;
|
|
20
|
+
constructor(options: TranslationAdapterFactoryOptions);
|
|
21
|
+
getResource(locale: string): Record<string, string> | undefined;
|
|
22
|
+
setResource(locale: string, resource: Record<string, string>): void;
|
|
23
|
+
getMessage(locale: string, key: string): string | undefined;
|
|
24
|
+
translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
//#endregion
|
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-pNEj3FtQ.js";
|
|
5
|
+
import { cli } from "./cli-D2dWSSRj.js";
|
|
4
6
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
7
|
|
|
6
8
|
export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
|
|
@@ -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) {
|
|
@@ -239,7 +181,7 @@ function getOptionsPairs(ctx) {
|
|
|
239
181
|
let key = makeShortLongOptionPair(value, name);
|
|
240
182
|
if (value.type !== "boolean") key = value.default ? `${key} [${name}]` : `${key} <${name}>`;
|
|
241
183
|
acc[name] = key;
|
|
242
|
-
if (value.type === "boolean" && !(name === "help" || name === "version")) acc[`no-${name}`] = `--no-${name}`;
|
|
184
|
+
if (value.type === "boolean" && value.negatable && !(name === "help" || name === "version")) acc[`no-${name}`] = `--no-${name}`;
|
|
243
185
|
return acc;
|
|
244
186
|
}, create());
|
|
245
187
|
}
|
|
@@ -274,6 +216,12 @@ async function generateOptionsUsage(ctx, optionsPairs) {
|
|
|
274
216
|
|
|
275
217
|
//#endregion
|
|
276
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
|
+
*/
|
|
277
225
|
function renderValidationErrors(_ctx, error) {
|
|
278
226
|
const messages = [];
|
|
279
227
|
for (const err of error.errors) messages.push(err.message);
|
|
@@ -281,4 +229,4 @@ function renderValidationErrors(_ctx, error) {
|
|
|
281
229
|
}
|
|
282
230
|
|
|
283
231
|
//#endregion
|
|
284
|
-
export {
|
|
232
|
+
export { renderHeader, renderUsage, renderValidationErrors };
|
package/lib/renderer.d.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
|
-
import { CommandContext } from "./types.d-
|
|
1
|
+
import { CommandContext } from "./types.d-DomXJWKH.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
11
|
declare function renderHeader<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
|
|
11
12
|
|
|
12
13
|
//#endregion
|
|
13
14
|
//#region src/renderer/usage.d.ts
|
|
14
15
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
* Render the usage.
|
|
17
|
+
* @param ctx A {@link CommandContext | command context}
|
|
18
|
+
* @returns A rendered usage.
|
|
19
|
+
*/
|
|
19
20
|
declare function renderUsage<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
|
|
20
21
|
|
|
21
22
|
//#endregion
|
|
22
23
|
//#region src/renderer/validation.d.ts
|
|
23
24
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
* Render the validation errors.
|
|
26
|
+
* @param ctx A {@link CommandContext | command context}
|
|
27
|
+
* @param error An {@link AggregateError} of option in `args-token` validation
|
|
28
|
+
* @returns A rendered validation error.
|
|
29
|
+
*/
|
|
29
30
|
declare function renderValidationErrors<Options extends ArgOptions = ArgOptions>(_ctx: CommandContext<Options>, error: AggregateError): Promise<string>;
|
|
30
31
|
|
|
31
32
|
//#endregion
|
package/lib/renderer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./utils-BYPzZy9X.js";
|
|
2
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-pNEj3FtQ.js";
|
|
2
3
|
|
|
3
4
|
export { renderHeader, renderUsage, renderValidationErrors };
|