gunshi 0.17.0 → 0.18.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 → cli-8GpxrUOM.js} +9 -8
- package/lib/{context-BROXRnNP.js → context-DSuCy-i-.js} +5 -1
- package/lib/context.d.ts +1 -1
- package/lib/context.js +2 -2
- package/lib/definition-DZJeZYb2.js +28 -0
- package/lib/definition.d-DaKM6lt0.d.ts +21 -0
- package/lib/definition.d.ts +3 -3
- package/lib/definition.js +2 -2
- package/lib/generator.d.ts +1 -1
- package/lib/generator.js +4 -4
- package/lib/index.d.ts +3 -3
- package/lib/index.js +6 -6
- package/lib/locales/en-US.json +2 -0
- package/lib/locales/ja-JP.json +2 -0
- package/lib/{renderer-pNEj3FtQ.js → renderer-BgAkL9Re.js} +19 -3
- package/lib/renderer.d.ts +1 -1
- package/lib/renderer.js +2 -2
- package/lib/{types.d-DomXJWKH.d.ts → types.d-BqXvgR9J.d.ts} +36 -16
- package/lib/{utils-BYPzZy9X.js → utils-6RnUrIMZ.js} +27 -4
- package/package.json +2 -2
- package/lib/definition-VzcnM0si.js +0 -12
- package/lib/definition.d-wYlroF3H.d.ts +0 -12
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, create, resolveLazyCommand } from "./utils-
|
|
2
|
-
import { createCommandContext } from "./context-
|
|
3
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-
|
|
1
|
+
import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, create, resolveLazyCommand } from "./utils-6RnUrIMZ.js";
|
|
2
|
+
import { createCommandContext } from "./context-DSuCy-i-.js";
|
|
3
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BgAkL9Re.js";
|
|
4
4
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
5
|
|
|
6
6
|
//#region src/cli.ts
|
|
@@ -15,7 +15,7 @@ async function cli(args, entry, opts = {}) {
|
|
|
15
15
|
const tokens = parseArgs(args);
|
|
16
16
|
const subCommand = getSubCommand(tokens);
|
|
17
17
|
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
18
|
-
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
|
|
18
|
+
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
|
|
19
19
|
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
20
20
|
const options = resolveArgOptions(command.options);
|
|
21
21
|
const { values, positionals, rest, error } = resolveArgs(options, tokens, { optionGrouping: true });
|
|
@@ -47,6 +47,7 @@ async function cli(args, entry, opts = {}) {
|
|
|
47
47
|
await showValidationErrors(ctx, error);
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
|
+
if (!command.run) throw new Error(`'run' not found on Command \`${name || ""}\``);
|
|
50
51
|
await command.run(ctx);
|
|
51
52
|
}
|
|
52
53
|
function resolveArgOptions(options) {
|
|
@@ -87,15 +88,15 @@ async function showValidationErrors(ctx, error) {
|
|
|
87
88
|
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
88
89
|
ctx.log(await render(ctx, error));
|
|
89
90
|
}
|
|
90
|
-
async function resolveCommand(sub, entry, options) {
|
|
91
|
+
async function resolveCommand(sub, entry, options, needRunResolving = false) {
|
|
91
92
|
const omitted = !sub;
|
|
92
93
|
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 if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry, "", needRunResolving)] : [void 0, void 0];
|
|
94
95
|
else {
|
|
95
|
-
if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
|
|
96
|
+
if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry, "", needRunResolving)];
|
|
96
97
|
const cmd = options.subCommands?.get(sub);
|
|
97
98
|
if (cmd == null) return [sub, void 0];
|
|
98
|
-
return [sub, await resolveLazyCommand(cmd, sub)];
|
|
99
|
+
return [sub, await resolveLazyCommand(cmd, sub, needRunResolving)];
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
102
|
function resolveEntryName(entry) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveLazyCommand, resolveOptionKey } from "./utils-
|
|
1
|
+
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveLazyCommand, resolveOptionKey } from "./utils-6RnUrIMZ.js";
|
|
2
2
|
|
|
3
3
|
//#region src/locales/en-US.json
|
|
4
4
|
var COMMAND = "COMMAND";
|
|
@@ -9,6 +9,8 @@ var OPTIONS = "OPTIONS";
|
|
|
9
9
|
var EXAMPLES = "EXAMPLES";
|
|
10
10
|
var FORMORE = "For more info, run any command with the `--help` flag:";
|
|
11
11
|
var NEGATABLE = "Negatable of";
|
|
12
|
+
var DEFAULT = "default";
|
|
13
|
+
var CHOICES = "choices";
|
|
12
14
|
var help = "Display this help message";
|
|
13
15
|
var version = "Display this version";
|
|
14
16
|
var en_US_default = {
|
|
@@ -20,6 +22,8 @@ var en_US_default = {
|
|
|
20
22
|
EXAMPLES,
|
|
21
23
|
FORMORE,
|
|
22
24
|
NEGATABLE,
|
|
25
|
+
DEFAULT,
|
|
26
|
+
CHOICES,
|
|
23
27
|
help,
|
|
24
28
|
version
|
|
25
29
|
};
|
package/lib/context.d.ts
CHANGED
package/lib/context.js
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* Define a {@link LazyCommand | lazy command} with command loader, which is attached with command definition as usage metadata.
|
|
12
|
+
* @param loader A {@link CommandLoader | command loader}
|
|
13
|
+
* @param definition A {@link Command | command} definition
|
|
14
|
+
* @returns A {@link LazyCommand | lazy command} loader
|
|
15
|
+
*/
|
|
16
|
+
function lazy(loader, definition) {
|
|
17
|
+
if (definition != null) {
|
|
18
|
+
loader.commandName = definition.name;
|
|
19
|
+
loader.description = definition.description;
|
|
20
|
+
loader.options = definition.options;
|
|
21
|
+
loader.examples = definition.examples;
|
|
22
|
+
loader.resource = definition.resource;
|
|
23
|
+
}
|
|
24
|
+
return loader;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { define, lazy };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Command, CommandLoader, LazyCommand } from "./types.d-BqXvgR9J.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>; /**
|
|
12
|
+
* Define a {@link LazyCommand | lazy command} with command loader, which is attached with command definition as usage metadata.
|
|
13
|
+
* @param loader A {@link CommandLoader | command loader}
|
|
14
|
+
* @param definition A {@link Command | command} definition
|
|
15
|
+
* @returns A {@link LazyCommand | lazy command} loader
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
declare function lazy<Options extends ArgOptions = ArgOptions>(loader: CommandLoader<Options>, definition?: Command<Options>): LazyCommand<Options>;
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ArgOptionSchema, ArgOptions$1 as ArgOptions, ArgValues$1 as ArgValues, define as define$1, lazy as lazy$1 };
|
package/lib/definition.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./types.d-
|
|
2
|
-
import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-
|
|
3
|
-
export { ArgOptionSchema, ArgOptions, ArgValues, define };
|
|
1
|
+
import "./types.d-BqXvgR9J.js";
|
|
2
|
+
import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define, lazy$1 as lazy } from "./definition.d-DaKM6lt0.js";
|
|
3
|
+
export { ArgOptionSchema, ArgOptions, ArgValues, define, lazy };
|
package/lib/definition.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { define } from "./definition-
|
|
1
|
+
import { define, lazy } from "./definition-DZJeZYb2.js";
|
|
2
2
|
|
|
3
|
-
export { define };
|
|
3
|
+
export { define, lazy };
|
package/lib/generator.d.ts
CHANGED
package/lib/generator.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { create } from "./utils-
|
|
2
|
-
import "./context-
|
|
3
|
-
import "./renderer-
|
|
4
|
-
import { cli } from "./cli-
|
|
1
|
+
import { create } from "./utils-6RnUrIMZ.js";
|
|
2
|
+
import "./context-DSuCy-i-.js";
|
|
3
|
+
import "./renderer-BgAkL9Re.js";
|
|
4
|
+
import { cli } from "./cli-8GpxrUOM.js";
|
|
5
5
|
|
|
6
6
|
//#region src/generator.ts
|
|
7
7
|
/**
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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, CommandLoader, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types.d-BqXvgR9J.js";
|
|
2
|
+
import { define$1 as define, lazy$1 as lazy } from "./definition.d-DaKM6lt0.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
|
|
@@ -25,4 +25,4 @@ declare class DefaultTranslation implements TranslationAdapter {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
|
-
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 };
|
|
28
|
+
export { ArgOptionSchema, ArgOptions, ArgValues, Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandLoader, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, lazy, parseArgs, resolveArgs };
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-
|
|
2
|
-
import { DefaultTranslation } from "./context-
|
|
3
|
-
import { define } from "./definition-
|
|
4
|
-
import "./renderer-
|
|
5
|
-
import { cli } from "./cli-
|
|
1
|
+
import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-6RnUrIMZ.js";
|
|
2
|
+
import { DefaultTranslation } from "./context-DSuCy-i-.js";
|
|
3
|
+
import { define, lazy } from "./definition-DZJeZYb2.js";
|
|
4
|
+
import "./renderer-BgAkL9Re.js";
|
|
5
|
+
import { cli } from "./cli-8GpxrUOM.js";
|
|
6
6
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
7
7
|
|
|
8
|
-
export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
|
|
8
|
+
export { DEFAULT_LOCALE, DefaultTranslation, cli, define, lazy, parseArgs, resolveArgs };
|
package/lib/locales/en-US.json
CHANGED
package/lib/locales/ja-JP.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { create, resolveBuiltInKey, resolveOptionKey } from "./utils-
|
|
1
|
+
import { COMMON_OPTIONS, create, resolveBuiltInKey, resolveOptionKey } from "./utils-6RnUrIMZ.js";
|
|
2
2
|
|
|
3
3
|
//#region src/renderer/header.ts
|
|
4
4
|
/**
|
|
@@ -13,6 +13,7 @@ function renderHeader(ctx) {
|
|
|
13
13
|
|
|
14
14
|
//#endregion
|
|
15
15
|
//#region src/renderer/usage.ts
|
|
16
|
+
const COMMON_OPTIONS_KEYS = Object.keys(COMMON_OPTIONS);
|
|
16
17
|
/**
|
|
17
18
|
* Render the usage.
|
|
18
19
|
* @param ctx A {@link CommandContext | command context}
|
|
@@ -181,7 +182,7 @@ function getOptionsPairs(ctx) {
|
|
|
181
182
|
let key = makeShortLongOptionPair(value, name);
|
|
182
183
|
if (value.type !== "boolean") key = value.default ? `${key} [${name}]` : `${key} <${name}>`;
|
|
183
184
|
acc[name] = key;
|
|
184
|
-
if (value.type === "boolean" && value.negatable && !(name
|
|
185
|
+
if (value.type === "boolean" && value.negatable && !COMMON_OPTIONS_KEYS.includes(name)) acc[`no-${name}`] = `--no-${name}`;
|
|
185
186
|
return acc;
|
|
186
187
|
}, create());
|
|
187
188
|
}
|
|
@@ -189,6 +190,20 @@ const resolveNegatableKey = (key) => key.split("no-")[1];
|
|
|
189
190
|
function resolveNegatableType(key, ctx) {
|
|
190
191
|
return ctx.options[key.startsWith("no-") ? resolveNegatableKey(key) : key].type;
|
|
191
192
|
}
|
|
193
|
+
function generateDefaultDisplayValue(ctx, schema) {
|
|
194
|
+
return `${ctx.translate(resolveBuiltInKey("DEFAULT"))}: ${schema.default}`;
|
|
195
|
+
}
|
|
196
|
+
function resolveDisplayValue(ctx, key) {
|
|
197
|
+
if (COMMON_OPTIONS_KEYS.includes(key)) return "";
|
|
198
|
+
const schema = ctx.options[key];
|
|
199
|
+
if ((schema.type === "boolean" || schema.type === "number" || schema.type === "string") && schema.default !== void 0) return `(${generateDefaultDisplayValue(ctx, schema)})`;
|
|
200
|
+
if (schema.type === "enum") {
|
|
201
|
+
const _default = schema.default !== void 0 ? generateDefaultDisplayValue(ctx, schema) : "";
|
|
202
|
+
const choices = `${ctx.translate(resolveBuiltInKey("CHOICES"))}: ${schema.choices.join(" | ")}`;
|
|
203
|
+
return `(${_default ? `${_default}, ${choices}` : choices})`;
|
|
204
|
+
}
|
|
205
|
+
return "";
|
|
206
|
+
}
|
|
192
207
|
/**
|
|
193
208
|
* Generate options usage
|
|
194
209
|
* @param ctx A {@link CommandContext | command context}
|
|
@@ -207,8 +222,9 @@ async function generateOptionsUsage(ctx, optionsPairs) {
|
|
|
207
222
|
rawDesc = `${ctx.translate(resolveBuiltInKey("NEGATABLE"))} ${optionKey}`;
|
|
208
223
|
}
|
|
209
224
|
const optionsSchema = ctx.env.usageOptionType ? `[${resolveNegatableType(key, ctx)}] ` : "";
|
|
225
|
+
const valueDesc = key.startsWith("no-") ? "" : resolveDisplayValue(ctx, key);
|
|
210
226
|
const desc = `${optionsSchema ? optionsSchema.padEnd(optionSchemaMaxLength + 3) : ""}${rawDesc}`;
|
|
211
|
-
const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}`;
|
|
227
|
+
const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}${valueDesc ? ` ${valueDesc}` : ""}`;
|
|
212
228
|
return `${option.padStart(ctx.env.leftMargin + option.length)}`;
|
|
213
229
|
}));
|
|
214
230
|
return usages.join("\n");
|
package/lib/renderer.d.ts
CHANGED
package/lib/renderer.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./utils-
|
|
2
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-
|
|
1
|
+
import "./utils-6RnUrIMZ.js";
|
|
2
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BgAkL9Re.js";
|
|
3
3
|
|
|
4
4
|
export { renderHeader, renderUsage, renderValidationErrors };
|
|
@@ -26,7 +26,7 @@ type CommonOptionType = {
|
|
|
26
26
|
};
|
|
27
27
|
declare const COMMON_OPTIONS: CommonOptionType;
|
|
28
28
|
declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions>;
|
|
29
|
-
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE"];
|
|
29
|
+
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
30
30
|
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region src/types.d.ts
|
|
@@ -109,10 +109,16 @@ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
|
|
|
109
109
|
* @see {@link CommandOptions.usageOptionType}
|
|
110
110
|
*/
|
|
111
111
|
usageOptionType: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Whether to display the option value.
|
|
114
|
+
* @default true
|
|
115
|
+
* @see {@link CommandOptions.usageOptionValue}
|
|
116
|
+
*/
|
|
117
|
+
usageOptionValue: boolean;
|
|
112
118
|
/**
|
|
113
119
|
* Whether to display the command usage.
|
|
114
120
|
* @default false
|
|
115
|
-
* @see {@link}
|
|
121
|
+
* @see {@link CommandOptions.usageSilent}
|
|
116
122
|
*/
|
|
117
123
|
usageSilent: boolean;
|
|
118
124
|
/**
|
|
@@ -175,6 +181,10 @@ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
|
|
|
175
181
|
* Whether to display the usage option type.
|
|
176
182
|
*/
|
|
177
183
|
usageOptionType?: boolean;
|
|
184
|
+
/**
|
|
185
|
+
* Whether to display the option value.
|
|
186
|
+
*/
|
|
187
|
+
usageOptionValue?: boolean;
|
|
178
188
|
/**
|
|
179
189
|
* Whether to display the command usage.
|
|
180
190
|
*/
|
|
@@ -195,11 +205,11 @@ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
|
|
|
195
205
|
* Translation adapter factory.
|
|
196
206
|
*/
|
|
197
207
|
translationAdapterFactory?: TranslationAdapterFactory;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
* Command context.
|
|
201
|
-
|
|
202
|
-
|
|
208
|
+
} /**
|
|
209
|
+
* Command context.
|
|
210
|
+
* Command context is the context of the command execution.
|
|
211
|
+
*/
|
|
212
|
+
|
|
203
213
|
interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> {
|
|
204
214
|
/**
|
|
205
215
|
* Command name, that is the command that is executed.
|
|
@@ -300,7 +310,7 @@ interface Command<Options extends ArgOptions = ArgOptions> {
|
|
|
300
310
|
/**
|
|
301
311
|
* Command runner. it's the command to be executed
|
|
302
312
|
*/
|
|
303
|
-
run
|
|
313
|
+
run?: CommandRunner<Options>;
|
|
304
314
|
/**
|
|
305
315
|
* Command resource fetcher.
|
|
306
316
|
*/
|
|
@@ -382,14 +392,24 @@ interface TranslationAdapter<MessageResource = string> {
|
|
|
382
392
|
* @param ctx A {@link CommandContext | command context}
|
|
383
393
|
*/
|
|
384
394
|
|
|
385
|
-
type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
395
|
+
type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
|
|
396
|
+
type CommandLoader<Options extends ArgOptions = ArgOptions> = () => Awaitable<Command<Options> | CommandRunner<Options>>; /**
|
|
397
|
+
* Lazy command interface.
|
|
398
|
+
* Lazy command that's not loaded until it is executed.
|
|
399
|
+
*/
|
|
389
400
|
|
|
390
|
-
type LazyCommand<Options extends ArgOptions = ArgOptions> =
|
|
391
|
-
|
|
392
|
-
|
|
401
|
+
type LazyCommand<Options extends ArgOptions = ArgOptions> = {
|
|
402
|
+
/**
|
|
403
|
+
* Command load function
|
|
404
|
+
*/
|
|
405
|
+
(): Awaitable<Command<Options> | CommandRunner<Options>>;
|
|
406
|
+
/**
|
|
407
|
+
* Command name
|
|
408
|
+
*/
|
|
409
|
+
commandName?: string;
|
|
410
|
+
} & Omit<Command<Options>, 'run' | 'name'>; /**
|
|
411
|
+
* Define a command type.
|
|
412
|
+
*/
|
|
393
413
|
|
|
394
414
|
type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>; //#endregion
|
|
395
|
-
export { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
|
|
415
|
+
export { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandLoader, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
|
|
@@ -29,6 +29,7 @@ const COMMAND_OPTIONS_DEFAULT = {
|
|
|
29
29
|
leftMargin: 2,
|
|
30
30
|
middleMargin: 10,
|
|
31
31
|
usageOptionType: false,
|
|
32
|
+
usageOptionValue: true,
|
|
32
33
|
renderHeader: void 0,
|
|
33
34
|
renderUsage: void 0,
|
|
34
35
|
renderValidationErrors: void 0,
|
|
@@ -37,10 +38,32 @@ const COMMAND_OPTIONS_DEFAULT = {
|
|
|
37
38
|
|
|
38
39
|
//#endregion
|
|
39
40
|
//#region src/utils.ts
|
|
40
|
-
async function resolveLazyCommand(cmd, name) {
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
|
|
41
|
+
async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
42
|
+
let command;
|
|
43
|
+
if (typeof cmd === "function") {
|
|
44
|
+
command = Object.assign(create(), {
|
|
45
|
+
name: cmd.commandName,
|
|
46
|
+
description: cmd.description,
|
|
47
|
+
options: cmd.options,
|
|
48
|
+
examples: cmd.examples,
|
|
49
|
+
resource: cmd.resource
|
|
50
|
+
});
|
|
51
|
+
if (needRunResolving) {
|
|
52
|
+
const loaded = await cmd();
|
|
53
|
+
if (typeof loaded === "function") command.run = loaded;
|
|
54
|
+
else if (typeof loaded === "object") {
|
|
55
|
+
if (loaded.run == null) throw new TypeError(`'run' is required in command: ${cmd.name || name}`);
|
|
56
|
+
command.run = loaded.run;
|
|
57
|
+
command.name = loaded.name;
|
|
58
|
+
command.description = loaded.description;
|
|
59
|
+
command.options = loaded.options;
|
|
60
|
+
command.examples = loaded.examples;
|
|
61
|
+
command.resource = loaded.resource;
|
|
62
|
+
} else throw new TypeError(`Cannot resolve command: ${cmd.name || name}`);
|
|
63
|
+
}
|
|
64
|
+
} else command = Object.assign(create(), cmd);
|
|
65
|
+
if (command.name == null && name) command.name = name;
|
|
66
|
+
return deepFreeze(command);
|
|
44
67
|
}
|
|
45
68
|
function resolveBuiltInKey(key) {
|
|
46
69
|
return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
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.18.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
}
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"args-tokens": "^0.16.
|
|
80
|
+
"args-tokens": "^0.16.2"
|
|
81
81
|
},
|
|
82
82
|
"devDependencies": {
|
|
83
83
|
"@eslint/markdown": "^6.4.0",
|
|
@@ -1,12 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 };
|