gunshi 0.21.0 → 0.23.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-DBIMF97F.js +146 -0
- package/lib/{context-BefYjlE-.js → context-B_pkfbdk.js} +7 -7
- package/lib/context.d.ts +3 -3
- package/lib/context.js +2 -2
- package/lib/{definition-DUNcKGVg.d.ts → definition-BFYUOPMR.d.ts} +1 -1
- package/lib/definition.d.ts +2 -2
- package/lib/generator.d.ts +9 -9
- package/lib/generator.js +11 -10
- package/lib/index.d.ts +10 -10
- package/lib/index.js +4 -4
- package/lib/{renderer-BpwhGYRZ.js → renderer-8HgcYJqp.js} +1 -1
- package/lib/renderer.d.ts +1 -1
- package/lib/renderer.js +2 -2
- package/lib/{types-BbgfNBFM.d.ts → types-ZXqvU8Ti.d.ts} +14 -14
- package/lib/{utils-STQBqMtf.js → utils-GlOAgmx0.js} +5 -2
- package/package.json +4 -4
- package/lib/cli-PK_LzdQr.js +0 -135
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, create, isLazyCommand, resolveLazyCommand } from "./utils-GlOAgmx0.js";
|
|
2
|
+
import { createCommandContext } from "./context-B_pkfbdk.js";
|
|
3
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-8HgcYJqp.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}, an {@link CommandRunner | inline command runner}, or a {@link LazyCommand | lazily-loaded command}
|
|
11
|
+
* @param options A {@link CliOptions | CLI options}
|
|
12
|
+
* @returns A rendered usage or undefined. if you will use {@link CliOptions.usageSilent} option, it will return rendered usage string.
|
|
13
|
+
*/
|
|
14
|
+
async function cli(argv, entry, options = {}) {
|
|
15
|
+
const cliOptions = resolveCliOptions(options, entry);
|
|
16
|
+
const tokens = parseArgs(argv);
|
|
17
|
+
const subCommand = getSubCommand(tokens);
|
|
18
|
+
const { commandName: name, command, callMode } = await resolveCommand(subCommand, entry, cliOptions);
|
|
19
|
+
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
20
|
+
const args = resolveArguments(getCommandArgs(command));
|
|
21
|
+
const { values, positionals, rest, error } = resolveArgs(args, tokens, {
|
|
22
|
+
optionGrouping: true,
|
|
23
|
+
skipPositional: cliOptions.subCommands.size > 0 ? 0 : -1
|
|
24
|
+
});
|
|
25
|
+
const omitted = !subCommand;
|
|
26
|
+
const ctx = await createCommandContext({
|
|
27
|
+
args,
|
|
28
|
+
values,
|
|
29
|
+
positionals,
|
|
30
|
+
rest,
|
|
31
|
+
argv,
|
|
32
|
+
tokens,
|
|
33
|
+
omitted,
|
|
34
|
+
callMode,
|
|
35
|
+
command,
|
|
36
|
+
cliOptions
|
|
37
|
+
});
|
|
38
|
+
if (values.version) {
|
|
39
|
+
showVersion(ctx);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const usageBuffer = [];
|
|
43
|
+
const header = await showHeader(ctx);
|
|
44
|
+
if (header) usageBuffer.push(header);
|
|
45
|
+
if (values.help) {
|
|
46
|
+
const usage = await showUsage(ctx);
|
|
47
|
+
if (usage) usageBuffer.push(usage);
|
|
48
|
+
return usageBuffer.join("\n");
|
|
49
|
+
}
|
|
50
|
+
if (error) {
|
|
51
|
+
await showValidationErrors(ctx, error);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
await executeCommand(command, ctx, name || "");
|
|
55
|
+
}
|
|
56
|
+
function getCommandArgs(cmd) {
|
|
57
|
+
if (isLazyCommand(cmd)) return cmd.args || create();
|
|
58
|
+
else if (typeof cmd === "object") return cmd.args || create();
|
|
59
|
+
else return create();
|
|
60
|
+
}
|
|
61
|
+
function resolveArguments(args) {
|
|
62
|
+
return Object.assign(create(), args, COMMON_ARGS);
|
|
63
|
+
}
|
|
64
|
+
function resolveCliOptions(options, entry) {
|
|
65
|
+
const subCommands = new Map(options.subCommands);
|
|
66
|
+
if (options.subCommands) {
|
|
67
|
+
if (isLazyCommand(entry)) subCommands.set(entry.commandName, entry);
|
|
68
|
+
else if (typeof entry === "object" && entry.name) subCommands.set(entry.name, entry);
|
|
69
|
+
}
|
|
70
|
+
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
71
|
+
return resolvedOptions;
|
|
72
|
+
}
|
|
73
|
+
function getSubCommand(tokens) {
|
|
74
|
+
const firstToken = tokens[0];
|
|
75
|
+
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
76
|
+
}
|
|
77
|
+
async function showUsage(ctx) {
|
|
78
|
+
if (ctx.env.renderUsage === null) return;
|
|
79
|
+
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
80
|
+
if (usage) {
|
|
81
|
+
ctx.log(usage);
|
|
82
|
+
return usage;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function showVersion(ctx) {
|
|
86
|
+
ctx.log(ctx.env.version);
|
|
87
|
+
}
|
|
88
|
+
async function showHeader(ctx) {
|
|
89
|
+
if (ctx.env.renderHeader === null) return;
|
|
90
|
+
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
91
|
+
if (header) {
|
|
92
|
+
ctx.log(header);
|
|
93
|
+
ctx.log();
|
|
94
|
+
return header;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function showValidationErrors(ctx, error) {
|
|
98
|
+
if (ctx.env.renderValidationErrors === null) return;
|
|
99
|
+
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
100
|
+
ctx.log(await render(ctx, error));
|
|
101
|
+
}
|
|
102
|
+
const CANNOT_RESOLVE_COMMAND = { callMode: "unexpected" };
|
|
103
|
+
async function resolveCommand(sub, entry, options) {
|
|
104
|
+
const omitted = !sub;
|
|
105
|
+
async function doResolveCommand() {
|
|
106
|
+
if (typeof entry === "function") if ("commandName" in entry && entry.commandName) return {
|
|
107
|
+
commandName: entry.commandName,
|
|
108
|
+
command: entry,
|
|
109
|
+
callMode: "entry"
|
|
110
|
+
};
|
|
111
|
+
else return {
|
|
112
|
+
command: { run: entry },
|
|
113
|
+
callMode: "entry"
|
|
114
|
+
};
|
|
115
|
+
else if (typeof entry === "object") return {
|
|
116
|
+
commandName: resolveEntryName(entry),
|
|
117
|
+
command: entry,
|
|
118
|
+
callMode: "entry"
|
|
119
|
+
};
|
|
120
|
+
else return CANNOT_RESOLVE_COMMAND;
|
|
121
|
+
}
|
|
122
|
+
if (omitted || options.subCommands?.size === 0) return doResolveCommand();
|
|
123
|
+
const cmd = options.subCommands?.get(sub);
|
|
124
|
+
if (cmd == null) return {
|
|
125
|
+
commandName: sub,
|
|
126
|
+
callMode: "unexpected"
|
|
127
|
+
};
|
|
128
|
+
if (isLazyCommand(cmd) && cmd.commandName == null) cmd.commandName = sub;
|
|
129
|
+
else if (typeof cmd === "object" && cmd.name == null) cmd.name = sub;
|
|
130
|
+
return {
|
|
131
|
+
commandName: sub,
|
|
132
|
+
command: cmd,
|
|
133
|
+
callMode: "subCommand"
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function resolveEntryName(entry) {
|
|
137
|
+
return entry.name || "(anonymous)";
|
|
138
|
+
}
|
|
139
|
+
async function executeCommand(cmd, ctx, name) {
|
|
140
|
+
const resolved = isLazyCommand(cmd) ? await resolveLazyCommand(cmd, name, true) : cmd;
|
|
141
|
+
if (resolved.run == null) throw new Error(`'run' not found on Command \`${name}\``);
|
|
142
|
+
await resolved.run(ctx);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { cli };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveArgKey, resolveExamples, resolveLazyCommand } from "./utils-
|
|
1
|
+
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveArgKey, resolveExamples, resolveLazyCommand } from "./utils-GlOAgmx0.js";
|
|
2
2
|
|
|
3
3
|
//#region src/locales/en-US.json
|
|
4
4
|
var COMMAND = "COMMAND";
|
|
@@ -72,7 +72,7 @@ const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
|
|
|
72
72
|
* @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
|
|
73
73
|
* @returns A {@link CommandContext | command context}, which is readonly
|
|
74
74
|
*/
|
|
75
|
-
async function createCommandContext({ args, values, positionals, rest, argv, tokens, command,
|
|
75
|
+
async function createCommandContext({ args, values, positionals, rest, argv, tokens, command, cliOptions, callMode = "entry", omitted = false }) {
|
|
76
76
|
/**
|
|
77
77
|
* normailize the options schema and values, to avoid prototype pollution
|
|
78
78
|
*/
|
|
@@ -83,10 +83,10 @@ async function createCommandContext({ args, values, positionals, rest, argv, tok
|
|
|
83
83
|
/**
|
|
84
84
|
* setup the environment
|
|
85
85
|
*/
|
|
86
|
-
const env = Object.assign(create(), COMMAND_OPTIONS_DEFAULT,
|
|
87
|
-
const locale = resolveLocale(
|
|
86
|
+
const env = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, cliOptions);
|
|
87
|
+
const locale = resolveLocale(cliOptions.locale);
|
|
88
88
|
const localeStr = locale.toString();
|
|
89
|
-
const translationAdapterFactory =
|
|
89
|
+
const translationAdapterFactory = cliOptions.translationAdapterFactory || createTranslationAdapter;
|
|
90
90
|
const adapter = translationAdapterFactory({
|
|
91
91
|
locale: localeStr,
|
|
92
92
|
fallbackLocale: DEFAULT_LOCALE
|
|
@@ -118,7 +118,7 @@ async function createCommandContext({ args, values, positionals, rest, argv, tok
|
|
|
118
118
|
let cachedCommands;
|
|
119
119
|
async function loadCommands() {
|
|
120
120
|
if (cachedCommands) return cachedCommands;
|
|
121
|
-
const subCommands = [...
|
|
121
|
+
const subCommands = [...cliOptions.subCommands || []];
|
|
122
122
|
return cachedCommands = await Promise.all(subCommands.map(async ([name, cmd]) => await resolveLazyCommand(cmd, name)));
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
@@ -137,7 +137,7 @@ async function createCommandContext({ args, values, positionals, rest, argv, tok
|
|
|
137
137
|
rest,
|
|
138
138
|
_: argv,
|
|
139
139
|
tokens,
|
|
140
|
-
log:
|
|
140
|
+
log: cliOptions.usageSilent ? NOOP : log,
|
|
141
141
|
loadCommands,
|
|
142
142
|
translate
|
|
143
143
|
}));
|
package/lib/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Command, CommandCallMode, CommandContext
|
|
1
|
+
import { CliOptions, Command, CommandCallMode, CommandContext } from "./types-ZXqvU8Ti.js";
|
|
2
2
|
import { ArgToken, ArgValues, Args } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/context.d.ts
|
|
@@ -49,7 +49,7 @@ interface CommandContextParams<A extends Args, V> {
|
|
|
49
49
|
/**
|
|
50
50
|
* A command options, which is spicialized from `cli` function
|
|
51
51
|
*/
|
|
52
|
-
|
|
52
|
+
cliOptions: CliOptions<A>;
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* Create a {@link CommandContext | command context}
|
|
@@ -64,7 +64,7 @@ declare function createCommandContext<A extends Args = Args, V extends ArgValues
|
|
|
64
64
|
argv,
|
|
65
65
|
tokens,
|
|
66
66
|
command,
|
|
67
|
-
|
|
67
|
+
cliOptions,
|
|
68
68
|
callMode,
|
|
69
69
|
omitted
|
|
70
70
|
}: CommandContextParams<A, V>): Promise<Readonly<CommandContext<A, V>>>; //#endregion
|
package/lib/context.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Command, CommandLoader, LazyCommand } from "./types-
|
|
1
|
+
import { Command, CommandLoader, LazyCommand } from "./types-ZXqvU8Ti.js";
|
|
2
2
|
import { ArgSchema, ArgValues as ArgValues$1, Args, Args as Args$1 } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/definition.d.ts
|
package/lib/definition.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./types-
|
|
2
|
-
import { ArgSchema, ArgValues, Args, define$1 as define, lazy$1 as lazy } from "./definition-
|
|
1
|
+
import "./types-ZXqvU8Ti.js";
|
|
2
|
+
import { ArgSchema, ArgValues, Args, define$1 as define, lazy$1 as lazy } from "./definition-BFYUOPMR.js";
|
|
3
3
|
export { ArgSchema, ArgValues, Args, define, lazy };
|
package/lib/generator.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { Command,
|
|
1
|
+
import { CliOptions, Command, LazyCommand } from "./types-ZXqvU8Ti.js";
|
|
2
2
|
import { Args } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/generator.d.ts
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
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.
|
|
6
|
+
* generate options of `generate` function.
|
|
11
7
|
*/
|
|
12
8
|
|
|
9
|
+
/**
|
|
10
|
+
* generate options of `generate` function.
|
|
11
|
+
*/
|
|
12
|
+
type GenerateOptions<A extends Args = Args> = CliOptions<A>;
|
|
13
13
|
/**
|
|
14
14
|
* Generate the command usage.
|
|
15
15
|
* @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`.
|
|
16
16
|
* @param entry - A {@link Command | entry command}
|
|
17
|
-
* @param
|
|
17
|
+
* @param options - A {@link CliOptions | cli options}
|
|
18
18
|
* @returns A rendered usage.
|
|
19
19
|
*/
|
|
20
|
-
declare function generate<A extends Args = Args>(command: string | null, entry: Command<A>,
|
|
20
|
+
declare function generate<A extends Args = Args>(command: string | null, entry: Command<A> | LazyCommand<A>, options?: GenerateOptions<A>): Promise<string>;
|
|
21
21
|
|
|
22
22
|
//#endregion
|
|
23
|
-
export { generate };
|
|
23
|
+
export { GenerateOptions, generate };
|
package/lib/generator.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import { create } from "./utils-
|
|
2
|
-
import "./context-
|
|
3
|
-
import "./renderer-
|
|
4
|
-
import { cli } from "./cli-
|
|
1
|
+
import { create } from "./utils-GlOAgmx0.js";
|
|
2
|
+
import "./context-B_pkfbdk.js";
|
|
3
|
+
import "./renderer-8HgcYJqp.js";
|
|
4
|
+
import { cli } from "./cli-DBIMF97F.js";
|
|
5
5
|
|
|
6
6
|
//#region src/generator.ts
|
|
7
7
|
/**
|
|
8
8
|
* Generate the command usage.
|
|
9
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
10
|
* @param entry - A {@link Command | entry command}
|
|
11
|
-
* @param
|
|
11
|
+
* @param options - A {@link CliOptions | cli options}
|
|
12
12
|
* @returns A rendered usage.
|
|
13
13
|
*/
|
|
14
|
-
async function generate(command, entry,
|
|
14
|
+
async function generate(command, entry, options = {}) {
|
|
15
15
|
const args = ["-h"];
|
|
16
16
|
if (command != null) args.unshift(command);
|
|
17
|
-
return await cli(args, entry,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
return await cli(args, entry, {
|
|
18
|
+
...create(),
|
|
19
|
+
...options,
|
|
20
|
+
usageSilent: true
|
|
21
|
+
}) || "";
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
//#endregion
|
package/lib/index.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader,
|
|
2
|
-
import { define$1 as define, lazy$1 as lazy } from "./definition-
|
|
1
|
+
import { CliOptions, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types-ZXqvU8Ti.js";
|
|
2
|
+
import { define$1 as define, lazy$1 as lazy } from "./definition-BFYUOPMR.js";
|
|
3
3
|
import { ArgSchema, ArgValues, Args, Args as Args$1, parseArgs, resolveArgs } from "args-tokens";
|
|
4
4
|
|
|
5
5
|
//#region src/cli.d.ts
|
|
6
6
|
/**
|
|
7
7
|
* Run the command.
|
|
8
8
|
* @param args Command line arguments
|
|
9
|
-
* @param entry A {@link Command | entry command}
|
|
10
|
-
* @param
|
|
11
|
-
* @returns A rendered usage or undefined. if you will use {@link
|
|
9
|
+
* @param entry A {@link Command | entry command}, an {@link CommandRunner | inline command runner}, or a {@link LazyCommand | lazily-loaded command}
|
|
10
|
+
* @param options A {@link CliOptions | CLI options}
|
|
11
|
+
* @returns A rendered usage or undefined. if you will use {@link CliOptions.usageSilent} option, it will return rendered usage string.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Run the command.
|
|
16
16
|
* @param args Command line arguments
|
|
17
|
-
* @param entry A {@link Command | entry command}
|
|
18
|
-
* @param
|
|
19
|
-
* @returns A rendered usage or undefined. if you will use {@link
|
|
17
|
+
* @param entry A {@link Command | entry command}, an {@link CommandRunner | inline command runner}, or a {@link LazyCommand | lazily-loaded command}
|
|
18
|
+
* @param options A {@link CliOptions | CLI options}
|
|
19
|
+
* @returns A rendered usage or undefined. if you will use {@link CliOptions.usageSilent} option, it will return rendered usage string.
|
|
20
20
|
*/
|
|
21
|
-
declare function cli<A extends Args$1 = Args$1>(argv: string[], entry: Command<A> | CommandRunner<A>,
|
|
21
|
+
declare function cli<A extends Args$1 = Args$1>(argv: string[], entry: Command<A> | CommandRunner<A> | LazyCommand<A>, options?: CliOptions<A>): Promise<string | undefined>;
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
24
|
//#region src/translation.d.ts
|
|
@@ -32,4 +32,4 @@ declare class DefaultTranslation implements TranslationAdapter {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
//#endregion
|
|
35
|
-
export { ArgSchema, ArgValues, Args, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader,
|
|
35
|
+
export { ArgSchema, ArgValues, Args, CliOptions, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgs, 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-
|
|
1
|
+
import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-GlOAgmx0.js";
|
|
2
|
+
import { DefaultTranslation } from "./context-B_pkfbdk.js";
|
|
3
3
|
import { define, lazy } from "./definition-DyVBFqB7.js";
|
|
4
|
-
import "./renderer-
|
|
5
|
-
import { cli } from "./cli-
|
|
4
|
+
import "./renderer-8HgcYJqp.js";
|
|
5
|
+
import { cli } from "./cli-DBIMF97F.js";
|
|
6
6
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
7
7
|
|
|
8
8
|
export { DEFAULT_LOCALE, DefaultTranslation, cli, define, lazy, parseArgs, resolveArgs };
|
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-GlOAgmx0.js";
|
|
2
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-8HgcYJqp.js";
|
|
3
3
|
|
|
4
4
|
export { renderHeader, renderUsage, renderValidationErrors };
|
|
@@ -25,7 +25,7 @@ type CommonArgType = {
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
declare const COMMON_ARGS: CommonArgType;
|
|
28
|
-
declare const COMMAND_OPTIONS_DEFAULT:
|
|
28
|
+
declare const COMMAND_OPTIONS_DEFAULT: CliOptions<Args>;
|
|
29
29
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
30
30
|
|
|
31
31
|
//#endregion
|
|
@@ -72,58 +72,58 @@ type CommandArgKeys<A extends Args> = GenerateNamespacedKey<KeyOfArgs<RemovedInd
|
|
|
72
72
|
interface CommandEnvironment<A extends Args = Args> {
|
|
73
73
|
/**
|
|
74
74
|
* Current working directory.
|
|
75
|
-
* @see {@link
|
|
75
|
+
* @see {@link CliOptions.cwd}
|
|
76
76
|
*/
|
|
77
77
|
cwd: string | undefined;
|
|
78
78
|
/**
|
|
79
79
|
* Command name.
|
|
80
|
-
* @see {@link
|
|
80
|
+
* @see {@link CliOptions.name}
|
|
81
81
|
*/
|
|
82
82
|
name: string | undefined;
|
|
83
83
|
/**
|
|
84
84
|
* Command description.
|
|
85
|
-
* @see {@link
|
|
85
|
+
* @see {@link CliOptions.description}
|
|
86
86
|
*
|
|
87
87
|
*/
|
|
88
88
|
description: string | undefined;
|
|
89
89
|
/**
|
|
90
90
|
* Command version.
|
|
91
|
-
* @see {@link
|
|
91
|
+
* @see {@link CliOptions.version}
|
|
92
92
|
*/
|
|
93
93
|
version: string | undefined;
|
|
94
94
|
/**
|
|
95
95
|
* Left margin of the command output.
|
|
96
96
|
* @default 2
|
|
97
|
-
* @see {@link
|
|
97
|
+
* @see {@link CliOptions.leftMargin}
|
|
98
98
|
*/
|
|
99
99
|
leftMargin: number;
|
|
100
100
|
/**
|
|
101
101
|
* Middle margin of the command output.
|
|
102
102
|
* @default 10
|
|
103
|
-
* @see {@link
|
|
103
|
+
* @see {@link CliOptions.middleMargin}
|
|
104
104
|
*/
|
|
105
105
|
middleMargin: number;
|
|
106
106
|
/**
|
|
107
107
|
* Whether to display the usage option type.
|
|
108
108
|
* @default false
|
|
109
|
-
* @see {@link
|
|
109
|
+
* @see {@link CliOptions.usageOptionType}
|
|
110
110
|
*/
|
|
111
111
|
usageOptionType: boolean;
|
|
112
112
|
/**
|
|
113
113
|
* Whether to display the option value.
|
|
114
114
|
* @default true
|
|
115
|
-
* @see {@link
|
|
115
|
+
* @see {@link CliOptions.usageOptionValue}
|
|
116
116
|
*/
|
|
117
117
|
usageOptionValue: boolean;
|
|
118
118
|
/**
|
|
119
119
|
* Whether to display the command usage.
|
|
120
120
|
* @default false
|
|
121
|
-
* @see {@link
|
|
121
|
+
* @see {@link CliOptions.usageSilent}
|
|
122
122
|
*/
|
|
123
123
|
usageSilent: boolean;
|
|
124
124
|
/**
|
|
125
125
|
* Sub commands.
|
|
126
|
-
* @see {@link
|
|
126
|
+
* @see {@link CliOptions.subCommands}
|
|
127
127
|
*/
|
|
128
128
|
subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
|
|
129
129
|
/**
|
|
@@ -140,9 +140,9 @@ interface CommandEnvironment<A extends Args = Args> {
|
|
|
140
140
|
renderValidationErrors: ((ctx: CommandContext<A>, error: AggregateError) => Promise<string>) | null | undefined;
|
|
141
141
|
}
|
|
142
142
|
/**
|
|
143
|
-
*
|
|
143
|
+
* CLI options of `cli` function.
|
|
144
144
|
*/
|
|
145
|
-
interface
|
|
145
|
+
interface CliOptions<A extends Args = Args> {
|
|
146
146
|
/**
|
|
147
147
|
* Current working directory.
|
|
148
148
|
*/
|
|
@@ -426,4 +426,4 @@ type LazyCommand<A extends Args = Args> = {
|
|
|
426
426
|
* Define a command type.
|
|
427
427
|
*/
|
|
428
428
|
type Commandable<A extends Args> = Command<A> | LazyCommand<A>; //#endregion
|
|
429
|
-
export { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader,
|
|
429
|
+
export { CliOptions, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
|
|
@@ -38,9 +38,12 @@ const COMMAND_OPTIONS_DEFAULT = {
|
|
|
38
38
|
|
|
39
39
|
//#endregion
|
|
40
40
|
//#region src/utils.ts
|
|
41
|
+
function isLazyCommand(cmd) {
|
|
42
|
+
return typeof cmd === "function" && "commandName" in cmd && !!cmd.commandName;
|
|
43
|
+
}
|
|
41
44
|
async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
42
45
|
let command;
|
|
43
|
-
if (
|
|
46
|
+
if (isLazyCommand(cmd)) {
|
|
44
47
|
command = Object.assign(create(), {
|
|
45
48
|
name: cmd.commandName,
|
|
46
49
|
description: cmd.description,
|
|
@@ -96,4 +99,4 @@ function deepFreeze(obj) {
|
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
//#endregion
|
|
99
|
-
export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveLazyCommand };
|
|
102
|
+
export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, isLazyCommand, log, mapResourceWithBuiltinKey, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveLazyCommand };
|
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.23.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -103,12 +103,12 @@
|
|
|
103
103
|
"eslint-plugin-vue-composable": "^1.0.0",
|
|
104
104
|
"eslint-plugin-yml": "^1.18.0",
|
|
105
105
|
"gh-changelogen": "^0.2.8",
|
|
106
|
-
"gunshi019": "npm:gunshi@0.
|
|
106
|
+
"gunshi019": "npm:gunshi@0.22.0",
|
|
107
107
|
"jsr": "^0.13.4",
|
|
108
108
|
"jsr-exports-lint": "^0.2.0",
|
|
109
109
|
"knip": "^5.53.0",
|
|
110
|
-
"lint-staged": "^
|
|
111
|
-
"messageformat": "4.0.0-
|
|
110
|
+
"lint-staged": "^16.0.0",
|
|
111
|
+
"messageformat": "4.0.0-11",
|
|
112
112
|
"mitata": "^1.0.34",
|
|
113
113
|
"pkg-pr-new": "^0.0.43",
|
|
114
114
|
"prettier": "^3.5.3",
|
package/lib/cli-PK_LzdQr.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, create, resolveLazyCommand } from "./utils-STQBqMtf.js";
|
|
2
|
-
import { createCommandContext } from "./context-BefYjlE-.js";
|
|
3
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BpwhGYRZ.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(argv, entry, opts = {}) {
|
|
15
|
-
const tokens = parseArgs(argv);
|
|
16
|
-
const subCommand = getSubCommand(tokens);
|
|
17
|
-
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
18
|
-
const [name, command, callMode] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
|
|
19
|
-
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
20
|
-
const args = resolveArguments(command.args);
|
|
21
|
-
const { values, positionals, rest, error } = resolveArgs(args, tokens, {
|
|
22
|
-
optionGrouping: true,
|
|
23
|
-
skipPositional: resolvedCommandOptions.subCommands.size > 0 ? 0 : -1
|
|
24
|
-
});
|
|
25
|
-
const omitted = !subCommand;
|
|
26
|
-
const ctx = await createCommandContext({
|
|
27
|
-
args,
|
|
28
|
-
values,
|
|
29
|
-
positionals,
|
|
30
|
-
rest,
|
|
31
|
-
argv,
|
|
32
|
-
tokens,
|
|
33
|
-
omitted,
|
|
34
|
-
callMode,
|
|
35
|
-
command,
|
|
36
|
-
commandOptions: resolvedCommandOptions
|
|
37
|
-
});
|
|
38
|
-
if (values.version) {
|
|
39
|
-
showVersion(ctx);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
const usageBuffer = [];
|
|
43
|
-
const header = await showHeader(ctx);
|
|
44
|
-
if (header) usageBuffer.push(header);
|
|
45
|
-
if (values.help) {
|
|
46
|
-
const usage = await showUsage(ctx);
|
|
47
|
-
if (usage) usageBuffer.push(usage);
|
|
48
|
-
return usageBuffer.join("\n");
|
|
49
|
-
}
|
|
50
|
-
if (error) {
|
|
51
|
-
await showValidationErrors(ctx, error);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (!command.run) throw new Error(`'run' not found on Command \`${name || ""}\``);
|
|
55
|
-
await command.run(ctx);
|
|
56
|
-
}
|
|
57
|
-
function resolveArguments(options) {
|
|
58
|
-
return Object.assign(create(), options, COMMON_ARGS);
|
|
59
|
-
}
|
|
60
|
-
function resolveCommandOptions(options, entry) {
|
|
61
|
-
const subCommands = new Map(options.subCommands);
|
|
62
|
-
if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
|
|
63
|
-
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
64
|
-
return resolvedOptions;
|
|
65
|
-
}
|
|
66
|
-
function getSubCommand(tokens) {
|
|
67
|
-
const firstToken = tokens[0];
|
|
68
|
-
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
69
|
-
}
|
|
70
|
-
async function showUsage(ctx) {
|
|
71
|
-
if (ctx.env.renderUsage === null) return;
|
|
72
|
-
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
73
|
-
if (usage) {
|
|
74
|
-
ctx.log(usage);
|
|
75
|
-
return usage;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
function showVersion(ctx) {
|
|
79
|
-
ctx.log(ctx.env.version);
|
|
80
|
-
}
|
|
81
|
-
async function showHeader(ctx) {
|
|
82
|
-
if (ctx.env.renderHeader === null) return;
|
|
83
|
-
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
84
|
-
if (header) {
|
|
85
|
-
ctx.log(header);
|
|
86
|
-
ctx.log();
|
|
87
|
-
return header;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async function showValidationErrors(ctx, error) {
|
|
91
|
-
if (ctx.env.renderValidationErrors === null) return;
|
|
92
|
-
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
93
|
-
ctx.log(await render(ctx, error));
|
|
94
|
-
}
|
|
95
|
-
async function resolveCommand(sub, entry, options, needRunResolving = false) {
|
|
96
|
-
const omitted = !sub;
|
|
97
|
-
if (typeof entry === "function") return [
|
|
98
|
-
void 0,
|
|
99
|
-
{ run: entry },
|
|
100
|
-
"entry"
|
|
101
|
-
];
|
|
102
|
-
else if (omitted) return typeof entry === "object" ? [
|
|
103
|
-
resolveEntryName(entry),
|
|
104
|
-
await resolveLazyCommand(entry, "", needRunResolving),
|
|
105
|
-
"entry"
|
|
106
|
-
] : [
|
|
107
|
-
void 0,
|
|
108
|
-
void 0,
|
|
109
|
-
"unexpected"
|
|
110
|
-
];
|
|
111
|
-
else {
|
|
112
|
-
if (options.subCommands == null || options.subCommands.size === 0) return [
|
|
113
|
-
resolveEntryName(entry),
|
|
114
|
-
await resolveLazyCommand(entry, "", needRunResolving),
|
|
115
|
-
"entry"
|
|
116
|
-
];
|
|
117
|
-
const cmd = options.subCommands?.get(sub);
|
|
118
|
-
if (cmd == null) return [
|
|
119
|
-
sub,
|
|
120
|
-
void 0,
|
|
121
|
-
"unexpected"
|
|
122
|
-
];
|
|
123
|
-
return [
|
|
124
|
-
sub,
|
|
125
|
-
await resolveLazyCommand(cmd, sub, needRunResolving),
|
|
126
|
-
"subCommand"
|
|
127
|
-
];
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
function resolveEntryName(entry) {
|
|
131
|
-
return entry.name || "(anonymous)";
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
//#endregion
|
|
135
|
-
export { cli };
|