gunshi 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,178 @@
1
+ import { create, deepFreeze, resolveLazyCommand } from "./utils-NHs5DuHk.js";
2
+
3
+ //#region locales/en-US.json
4
+ var COMMAND = "COMMAND";
5
+ var COMMANDS = "COMMANDS";
6
+ var SUBCOMMAND = "SUBCOMMAND";
7
+ var USAGE = "USAGE";
8
+ var OPTIONS = "OPTIONS";
9
+ var EXAMPLES = "EXAMPLES";
10
+ var FORMORE = "For more info, run any command with the `--help` flag:";
11
+ var help = "Display this help message";
12
+ var version = "Display this version";
13
+ var en_US_default = {
14
+ COMMAND,
15
+ COMMANDS,
16
+ SUBCOMMAND,
17
+ USAGE,
18
+ OPTIONS,
19
+ EXAMPLES,
20
+ FORMORE,
21
+ help,
22
+ version
23
+ };
24
+
25
+ //#endregion
26
+ //#region src/constants.ts
27
+ const COMMON_OPTIONS = {
28
+ help: {
29
+ type: "boolean",
30
+ short: "h"
31
+ },
32
+ version: {
33
+ type: "boolean",
34
+ short: "v"
35
+ }
36
+ };
37
+ const COMMAND_OPTIONS_DEFAULT = {
38
+ name: undefined,
39
+ description: undefined,
40
+ version: undefined,
41
+ cwd: undefined,
42
+ subCommands: undefined,
43
+ leftMargin: 2,
44
+ middleMargin: 10,
45
+ usageOptionType: false,
46
+ renderHeader: undefined,
47
+ renderUsage: undefined,
48
+ renderValidationErrors: undefined
49
+ };
50
+ const COMMAND_I18N_RESOURCE_KEYS = [
51
+ "USAGE",
52
+ "COMMAND",
53
+ "SUBCOMMAND",
54
+ "COMMANDS",
55
+ "OPTIONS",
56
+ "EXAMPLES",
57
+ "FORMORE"
58
+ ];
59
+
60
+ //#endregion
61
+ //#region src/context.ts
62
+ const DEFAULT_LOCALE = "en-US";
63
+ async function createCommandContext({ options, values, positionals, command, commandOptions, omitted = false }) {
64
+ /**
65
+ * tweak the options and values
66
+ */
67
+ const _options = options == null ? undefined : Object.entries(options).reduce((acc, [key, value]) => {
68
+ acc[key] = Object.assign(create(), value);
69
+ return acc;
70
+ }, create());
71
+ const _values = Object.assign(create(), values);
72
+ /**
73
+ * normalize the usage
74
+ */
75
+ const usage = Object.assign(create(), command.usage);
76
+ const { help: help$1, version: version$1 } = en_US_default;
77
+ usage.options = Object.assign(create(), usage.options, {
78
+ help: help$1,
79
+ version: version$1
80
+ });
81
+ /**
82
+ * setup the environment
83
+ */
84
+ const env = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, commandOptions);
85
+ const locale = resolveLocale(commandOptions.locale);
86
+ const localeResources = new Map();
87
+ const commandResources = new Map();
88
+ let builtInLoadedResources;
89
+ /**
90
+ * load the built-in locale resources
91
+ */
92
+ localeResources.set(DEFAULT_LOCALE, en_US_default);
93
+ if (DEFAULT_LOCALE !== locale.toString()) try {
94
+ builtInLoadedResources = await import(`../locales/${locale.toString()}.json`, { with: { type: "json" } });
95
+ localeResources.set(locale.toString(), builtInLoadedResources);
96
+ } catch {}
97
+ /**
98
+ * define the translation function, which is used to {@link CommandContext.translation}.
99
+ *
100
+ */
101
+ function translation(key) {
102
+ if (COMMAND_I18N_RESOURCE_KEYS.includes(key)) {
103
+ const resource = localeResources.get(locale.toString()) || localeResources.get(DEFAULT_LOCALE);
104
+ return resource[key] || key;
105
+ } else {
106
+ const resource = commandResources.get(locale.toString()) || commandResources.get(DEFAULT_LOCALE);
107
+ return resource[key] || "";
108
+ }
109
+ }
110
+ /**
111
+ * load the sub commands
112
+ */
113
+ let cachedCommands;
114
+ async function loadCommands() {
115
+ if (cachedCommands) return cachedCommands;
116
+ const subCommands = [...env.subCommands || []];
117
+ return cachedCommands = await Promise.all(subCommands.map(async ([name, cmd]) => await resolveLazyCommand(cmd, name)));
118
+ }
119
+ /**
120
+ * create the context
121
+ */
122
+ const ctx = deepFreeze(Object.assign(create(), {
123
+ name: command.name,
124
+ description: command.description,
125
+ omitted,
126
+ locale,
127
+ env,
128
+ options: _options,
129
+ values: _values,
130
+ positionals,
131
+ usage,
132
+ loadCommands,
133
+ translation
134
+ }));
135
+ /**
136
+ * load the command resources
137
+ */
138
+ const loadedOptionsResources = Object.entries(usage.options || create()).map(([key, _]) => {
139
+ const option = usage.options[key];
140
+ return [key, option];
141
+ });
142
+ const defaultCommandResource = loadedOptionsResources.reduce((res, [key, value]) => {
143
+ res[key] = value;
144
+ return res;
145
+ }, create());
146
+ defaultCommandResource.description = command.description || "";
147
+ defaultCommandResource.examples = usage.examples || "";
148
+ commandResources.set(DEFAULT_LOCALE, defaultCommandResource);
149
+ const originalResource = await loadCommandResource(ctx, command);
150
+ if (originalResource) {
151
+ const resource = Object.entries(originalResource.options).reduce((res, [key, value]) => {
152
+ res[key] = value;
153
+ return res;
154
+ }, Object.assign(create(), {
155
+ description: originalResource.description,
156
+ examples: originalResource.examples
157
+ }));
158
+ if (builtInLoadedResources) {
159
+ resource.help = builtInLoadedResources.help;
160
+ resource.version = builtInLoadedResources.version;
161
+ }
162
+ commandResources.set(locale.toString(), resource);
163
+ }
164
+ return ctx;
165
+ }
166
+ function resolveLocale(locale) {
167
+ return locale instanceof Intl.Locale ? locale : typeof locale === "string" ? new Intl.Locale(locale) : new Intl.Locale(DEFAULT_LOCALE);
168
+ }
169
+ async function loadCommandResource(ctx, command) {
170
+ let resource;
171
+ try {
172
+ resource = await command.resource?.(ctx);
173
+ } catch {}
174
+ return resource;
175
+ }
176
+
177
+ //#endregion
178
+ export { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, createCommandContext };
package/lib/context.d.ts CHANGED
@@ -1,14 +1,50 @@
1
- import type { ArgOptions, ArgValues } from "args-tokens";
2
- import type { Command, CommandContext, CommandOptions } from "./types.js";
3
- export declare const DEFAULT_LOCALE = "en-US";
4
- export declare function createCommandContext<
1
+ import { ArgOptions, ArgValues } from 'args-tokens';
2
+ import { C as Command, a as CommandOptions, b as CommandContext } from './types.d-00BVt8hZ.js';
3
+
4
+ /**
5
+ * The default locale string, which format is BCP 47 language tag
6
+ */
7
+ declare const DEFAULT_LOCALE = "en-US";
8
+ /**
9
+ * Parameters of {@link createCommandContext}
10
+ */
11
+ interface CommandContextParams<
12
+ Options extends ArgOptions,
13
+ Values
14
+ > {
15
+ /**
16
+ * An options of target command
17
+ */
18
+ options: Options | undefined;
19
+ /**
20
+ * A values of target command
21
+ */
22
+ values: Values;
23
+ /**
24
+ * A positionals arguments, which passed to the target command
25
+ */
26
+ positionals: string[];
27
+ /**
28
+ * Whether the command is omitted
29
+ */
30
+ omitted: boolean;
31
+ /**
32
+ * A target {@link Command | command}
33
+ */
34
+ command: Command<Options>;
35
+ /**
36
+ * A command options, which is spicialized from `cli` function
37
+ */
38
+ commandOptions: CommandOptions<Options>;
39
+ }
40
+ /**
41
+ * Create a {@link CommandContext | command context}
42
+ * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
43
+ * @returns A {@link CommandContext | command context}, which is readonly
44
+ */
45
+ declare function createCommandContext<
5
46
  Options extends ArgOptions,
6
47
  Values = ArgValues<Options>
7
- >({ options, values, positionals, command, commandOptions, omitted }: {
8
- options: Options | undefined
9
- values: Values
10
- positionals: string[]
11
- omitted: boolean
12
- command: Command<Options>
13
- commandOptions: CommandOptions<Options>
14
- }): Promise<Readonly<CommandContext<Options, Values>>>;
48
+ >({ options, values, positionals, command, commandOptions, omitted }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>;
49
+
50
+ export { DEFAULT_LOCALE, createCommandContext };
package/lib/context.js ADDED
@@ -0,0 +1,4 @@
1
+ import { DEFAULT_LOCALE, createCommandContext } from "./context-DmZAeiph.js";
2
+ import "./utils-NHs5DuHk.js";
3
+
4
+ export { DEFAULT_LOCALE, createCommandContext };
package/lib/index.d.ts CHANGED
@@ -1,2 +1,14 @@
1
- export * from "./cli.js";
2
- export type * from "./types.js";
1
+ import { ArgOptions } from 'args-tokens';
2
+ export { ArgOptionSchema, ArgOptions, ArgValues } from 'args-tokens';
3
+ import { C as Command, c as CommandRunner, a as CommandOptions } from './types.d-00BVt8hZ.js';
4
+ export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, b as CommandContext, g as CommandEnvironment, h as CommandResource, i as CommandResourceFetcher, L as LazyCommand } from './types.d-00BVt8hZ.js';
5
+
6
+ /**
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
+ */
12
+ declare function cli<Options extends ArgOptions>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<void>;
13
+
14
+ export { Command, CommandOptions, CommandRunner, cli };