gunshi 0.6.2 → 0.7.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/README.md CHANGED
@@ -317,7 +317,7 @@ const customUsageRenderer = ctx => {
317
317
 
318
318
  for (const [key, option] of Object.entries(ctx.options || Object.create(null))) {
319
319
  const shortFlag = option.short ? `-${option.short}, ` : ' '
320
- lines.push(` ${shortFlag}--${key.padEnd(10)} ${ctx.translation(key)}`)
320
+ lines.push(` ${shortFlag}--${key.padEnd(10)} ${ctx.translate(key)}`)
321
321
  }
322
322
 
323
323
  return Promise.resolve(lines.join('\n'))
@@ -365,7 +365,7 @@ const command = {
365
365
  },
366
366
  run: ctx => {
367
367
  const { name = 'World', formal } = ctx.values
368
- const greeting = formal ? ctx.translation('formal') : ctx.translation('informal')
368
+ const greeting = formal ? ctx.translate('formal_greeting') : ctx.translate('informal_greeting')
369
369
  console.log(`${greeting}, ${name}!`)
370
370
  }
371
371
  }
@@ -1,4 +1,4 @@
1
- import { create, deepFreeze, resolveLazyCommand } from "./utils-NHs5DuHk.js";
1
+ import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE, create, deepFreeze, mapResourceWithBuiltinKey, resolveLazyCommand } from "./utils-B39blQOf.js";
2
2
 
3
3
  //#region locales/en-US.json
4
4
  var COMMAND = "COMMAND";
@@ -22,49 +22,14 @@ var en_US_default = {
22
22
  version
23
23
  };
24
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
25
  //#endregion
61
26
  //#region src/context.ts
62
- const DEFAULT_LOCALE = "en-US";
27
+ const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
63
28
  async function createCommandContext({ options, values, positionals, command, commandOptions, omitted = false }) {
64
29
  /**
65
30
  * tweak the options and values
66
31
  */
67
- const _options = options == null ? undefined : Object.entries(options).reduce((acc, [key, value]) => {
32
+ const _options = Object.entries(options).reduce((acc, [key, value]) => {
68
33
  acc[key] = Object.assign(create(), value);
69
34
  return acc;
70
35
  }, create());
@@ -89,17 +54,17 @@ async function createCommandContext({ options, values, positionals, command, com
89
54
  /**
90
55
  * load the built-in locale resources
91
56
  */
92
- localeResources.set(DEFAULT_LOCALE, en_US_default);
57
+ localeResources.set(DEFAULT_LOCALE, mapResourceWithBuiltinKey(en_US_default));
93
58
  if (DEFAULT_LOCALE !== locale.toString()) try {
94
59
  builtInLoadedResources = await import(`../locales/${locale.toString()}.json`, { with: { type: "json" } });
95
- localeResources.set(locale.toString(), builtInLoadedResources);
60
+ localeResources.set(locale.toString(), mapResourceWithBuiltinKey(builtInLoadedResources));
96
61
  } catch {}
97
62
  /**
98
- * define the translation function, which is used to {@link CommandContext.translation}.
63
+ * define the translation function, which is used to {@link CommandContext.translate}.
99
64
  *
100
65
  */
101
- function translation(key) {
102
- if (COMMAND_I18N_RESOURCE_KEYS.includes(key)) {
66
+ function translate(key) {
67
+ if (key.codePointAt(0) === BUILT_IN_PREFIX_CODE) {
103
68
  const resource = localeResources.get(locale.toString()) || localeResources.get(DEFAULT_LOCALE);
104
69
  return resource[key] || key;
105
70
  } else {
@@ -130,7 +95,7 @@ async function createCommandContext({ options, values, positionals, command, com
130
95
  positionals,
131
96
  usage,
132
97
  loadCommands,
133
- translation
98
+ translate
134
99
  }));
135
100
  /**
136
101
  * load the command resources
@@ -148,13 +113,10 @@ async function createCommandContext({ options, values, positionals, command, com
148
113
  commandResources.set(DEFAULT_LOCALE, defaultCommandResource);
149
114
  const originalResource = await loadCommandResource(ctx, command);
150
115
  if (originalResource) {
151
- const resource = Object.entries(originalResource.options).reduce((res, [key, value]) => {
152
- res[key] = value;
153
- return res;
154
- }, Object.assign(create(), {
116
+ const resource = Object.assign(create(), {
155
117
  description: originalResource.description,
156
118
  examples: originalResource.examples
157
- }));
119
+ }, originalResource);
158
120
  if (builtInLoadedResources) {
159
121
  resource.help = builtInLoadedResources.help;
160
122
  resource.version = builtInLoadedResources.version;
@@ -175,4 +137,4 @@ async function loadCommandResource(ctx, command) {
175
137
  }
176
138
 
177
139
  //#endregion
178
- export { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, createCommandContext };
140
+ export { createCommandContext };
package/lib/context.d.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  import { ArgOptions, ArgValues } from 'args-tokens';
2
- import { C as Command, a as CommandOptions, b as CommandContext } from './types.d-CxaX4FVV.js';
2
+ import { C as Command, a as CommandOptions, b as CommandContext } from './types.d-BvzTOO8I.js';
3
3
 
4
- /**
5
- * The default locale string, which format is BCP 47 language tag
6
- */
7
- declare const DEFAULT_LOCALE = "en-US";
8
4
  /**
9
5
  * Parameters of {@link createCommandContext}
10
6
  */
@@ -15,7 +11,7 @@ interface CommandContextParams<
15
11
  /**
16
12
  * An options of target command
17
13
  */
18
- options: Options | undefined;
14
+ options: Options;
19
15
  /**
20
16
  * A values of target command
21
17
  */
@@ -47,4 +43,4 @@ declare function createCommandContext<
47
43
  Values = ArgValues<Options>
48
44
  >({ options, values, positionals, command, commandOptions, omitted }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>;
49
45
 
50
- export { DEFAULT_LOCALE, createCommandContext };
46
+ export { createCommandContext };
package/lib/context.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DEFAULT_LOCALE, createCommandContext } from "./context-DmZAeiph.js";
2
- import "./utils-NHs5DuHk.js";
1
+ import { createCommandContext } from "./context-CEZVz8Dg.js";
2
+ import "./utils-B39blQOf.js";
3
3
 
4
- export { DEFAULT_LOCALE, createCommandContext };
4
+ export { createCommandContext };
package/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
2
  export { ArgOptionSchema, ArgOptions, ArgValues } from 'args-tokens';
3
- import { C as Command, c as CommandRunner, a as CommandOptions } from './types.d-CxaX4FVV.js';
4
- export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, b as CommandContext, g as CommandEnvironment, h as CommandResource, i as CommandResourceFetcher, j as Commandable, L as LazyCommand } from './types.d-CxaX4FVV.js';
3
+ import { C as Command, c as CommandRunner, a as CommandOptions } from './types.d-BvzTOO8I.js';
4
+ export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, b as CommandContext, g as CommandEnvironment, h as CommandResource, i as CommandResourceFetcher, j as Commandable, G as GenerateNamespacedKey, L as LazyCommand } from './types.d-BvzTOO8I.js';
5
5
 
6
6
  /**
7
7
  * Run the command
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, createCommandContext } from "./context-DmZAeiph.js";
2
- import { create, log, resolveLazyCommand } from "./utils-NHs5DuHk.js";
3
- import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-v5Uq0km9.js";
1
+ import { createCommandContext } from "./context-CEZVz8Dg.js";
2
+ import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, create, log, resolveLazyCommand } from "./utils-B39blQOf.js";
3
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-CD-yc-xk.js";
4
4
  import { parseArgs, resolveArgs } from "args-tokens";
5
5
 
6
6
  //#region src/cli.ts
@@ -45,7 +45,8 @@ function resolveArgOptions(options) {
45
45
  function resolveCommandOptions(options, entry) {
46
46
  const subCommands = new Map(options.subCommands);
47
47
  if (typeof entry === "object" && entry.name) subCommands.set(entry.name, entry);
48
- return Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
48
+ const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
49
+ return resolvedOptions;
49
50
  }
50
51
  function getSubCommand(tokens) {
51
52
  const firstToken = tokens[0];
@@ -1,5 +1,5 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
- import { b as CommandContext } from '../types.d-CxaX4FVV.js';
2
+ import { b as CommandContext } from '../types.d-BvzTOO8I.js';
3
3
 
4
4
  /**
5
5
  * Render the header
@@ -1,4 +1,4 @@
1
- import "../utils-NHs5DuHk.js";
2
- import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-v5Uq0km9.js";
1
+ import "../utils-B39blQOf.js";
2
+ import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-CD-yc-xk.js";
3
3
 
4
4
  export { renderHeader, renderUsage, renderValidationErrors };
@@ -1,4 +1,4 @@
1
- import { create } from "./utils-NHs5DuHk.js";
1
+ import { create, resolveBuiltInKey } from "./utils-B39blQOf.js";
2
2
 
3
3
  //#region src/renderer/header.ts
4
4
  function renderHeader(ctx) {
@@ -27,9 +27,8 @@ async function renderUsage(ctx) {
27
27
  */
28
28
  async function renderOptionsSection(ctx) {
29
29
  const messages = [];
30
- messages.push(`${ctx.translation("OPTIONS")}:`);
31
- const optionsPairs = getOptionsPairs(ctx);
32
- messages.push(await generateOptionsUsage(ctx, optionsPairs));
30
+ messages.push(`${ctx.translate(resolveBuiltInKey("OPTIONS"))}:`);
31
+ messages.push(await generateOptionsUsage(ctx, getOptionsPairs(ctx)));
33
32
  return messages;
34
33
  }
35
34
  /**
@@ -40,7 +39,7 @@ async function renderOptionsSection(ctx) {
40
39
  function renderExamplesSection(ctx) {
41
40
  const messages = [];
42
41
  const examples = ctx.usage.examples.split("\n").map((example) => example.padStart(ctx.env.leftMargin + example.length));
43
- messages.push(`${ctx.translation("EXAMPLES")}:`, ...examples);
42
+ messages.push(`${ctx.translate(resolveBuiltInKey("EXAMPLES"))}:`, ...examples);
44
43
  return messages;
45
44
  }
46
45
  /**
@@ -49,12 +48,12 @@ function renderExamplesSection(ctx) {
49
48
  * @returns A rendered usage section
50
49
  */
51
50
  async function renderUsageSection(ctx) {
52
- const messages = [`${ctx.translation("USAGE")}:`];
51
+ const messages = [`${ctx.translate(resolveBuiltInKey("USAGE"))}:`];
53
52
  if (ctx.omitted) {
54
- const defaultCommand = `${resolveEntry(ctx)}${await hasCommands(ctx) ? ` [${resolveSubCommand(ctx)}]` : ""} ${hasOptions(ctx) ? `<${ctx.translation("OPTIONS")}>` : ""} `;
53
+ const defaultCommand = `${resolveEntry(ctx)}${await hasCommands(ctx) ? ` [${resolveSubCommand(ctx)}]` : ""} ${hasOptions(ctx) ? `<${ctx.translate(resolveBuiltInKey("OPTIONS"))}>` : ""} `;
55
54
  messages.push(defaultCommand.padStart(ctx.env.leftMargin + defaultCommand.length));
56
55
  if (await hasCommands(ctx)) {
57
- const commandsUsage = `${resolveEntry(ctx)} <${ctx.translation("COMMANDS")}>`;
56
+ const commandsUsage = `${resolveEntry(ctx)} <${ctx.translate(resolveBuiltInKey("COMMANDS"))}>`;
58
57
  messages.push(commandsUsage.padStart(ctx.env.leftMargin + commandsUsage.length));
59
58
  }
60
59
  } else {
@@ -69,7 +68,7 @@ async function renderUsageSection(ctx) {
69
68
  * @returns A rendered commands section
70
69
  */
71
70
  async function renderCommandsSection(ctx) {
72
- const messages = [`${ctx.translation("COMMANDS")}:`];
71
+ const messages = [`${ctx.translate(resolveBuiltInKey("COMMANDS"))}:`];
73
72
  const loadedCommands = await ctx.loadCommands();
74
73
  const commandMaxLength = Math.max(...loadedCommands.map((cmd) => (cmd.name || "").length));
75
74
  const commandsStr = await Promise.all(loadedCommands.map((cmd) => {
@@ -78,7 +77,7 @@ async function renderCommandsSection(ctx) {
78
77
  const command = `${key.padEnd(commandMaxLength + ctx.env.middleMargin)}${desc} `;
79
78
  return `${command.padStart(ctx.env.leftMargin + command.length)} `;
80
79
  }));
81
- messages.push(...commandsStr, "", ctx.translation("FORMORE"));
80
+ messages.push(...commandsStr, "", ctx.translate(resolveBuiltInKey("FORMORE")));
82
81
  messages.push(...loadedCommands.map((cmd) => {
83
82
  const commandHelp = `${ctx.env.name} ${cmd.name} --help`;
84
83
  return `${commandHelp.padStart(ctx.env.leftMargin + commandHelp.length)}`;
@@ -91,7 +90,7 @@ async function renderCommandsSection(ctx) {
91
90
  * @returns The entry command name
92
91
  */
93
92
  function resolveEntry(ctx) {
94
- return ctx.env.name || ctx.translation("COMMAND");
93
+ return ctx.env.name || ctx.translate(resolveBuiltInKey("COMMAND"));
95
94
  }
96
95
  /**
97
96
  * Resolve the sub command name
@@ -99,7 +98,7 @@ function resolveEntry(ctx) {
99
98
  * @returns The sub command name
100
99
  */
101
100
  function resolveSubCommand(ctx) {
102
- return ctx.name || ctx.translation("SUBCOMMAND");
101
+ return ctx.name || ctx.translate(resolveBuiltInKey("SUBCOMMAND"));
103
102
  }
104
103
  /**
105
104
  * Resolve the command description
@@ -107,7 +106,7 @@ function resolveSubCommand(ctx) {
107
106
  * @returns resolved command description
108
107
  */
109
108
  function resolveDescription(ctx) {
110
- return ctx.translation("description") || ctx.description || "";
109
+ return ctx.translate("description") || ctx.description || "";
111
110
  }
112
111
  /**
113
112
  * Check if the command has sub commands
@@ -148,7 +147,7 @@ function hasAllDefaultOptions(ctx) {
148
147
  * @returns Options symbols for usage
149
148
  */
150
149
  function generateOptionsSymbols(ctx) {
151
- return hasOptions(ctx) ? hasAllDefaultOptions(ctx) ? `[${ctx.translation("OPTIONS")}]` : `<${ctx.translation("OPTIONS")}>` : "";
150
+ return hasOptions(ctx) ? hasAllDefaultOptions(ctx) ? `[${ctx.translate("_:OPTIONS")}]` : `<${ctx.translate("_:OPTIONS")}>` : "";
152
151
  }
153
152
  /**
154
153
  * Get options pairs for usage
@@ -174,7 +173,7 @@ async function generateOptionsUsage(ctx, optionsPairs) {
174
173
  const optionsMaxLength = Math.max(...Object.entries(optionsPairs).map(([_, value]) => value.length));
175
174
  const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key, _]) => ctx.options[key].type.length)) : 0;
176
175
  const usages = await Promise.all(Object.entries(optionsPairs).map(([key, value]) => {
177
- const rawDesc = ctx.translation(key);
176
+ const rawDesc = ctx.translate(key);
178
177
  const optionsSchema = ctx.env.usageOptionType ? `[${ctx.options[key].type}] ` : "";
179
178
  const desc = `${optionsSchema ? optionsSchema.padEnd(optionSchemaMaxLength + 3) : ""}${rawDesc}`;
180
179
  const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}`;
@@ -1,5 +1,11 @@
1
1
  import { ArgOptions, ArgValues } from 'args-tokens';
2
2
 
3
+ /**
4
+ * The default locale string, which format is BCP 47 language tag
5
+ */
6
+ declare const DEFAULT_LOCALE = "en-US";
7
+ declare const BUILT_IN_PREFIX = "_";
8
+ declare const BUILT_IN_KEY_SEPARATOR = ":";
3
9
  type CommonOptionType = {
4
10
  readonly help: {
5
11
  readonly type: "boolean"
@@ -12,19 +18,26 @@ type CommonOptionType = {
12
18
  };
13
19
  declare const COMMON_OPTIONS: CommonOptionType;
14
20
  declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<ArgOptions>;
15
- declare const COMMAND_I18N_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE"];
21
+ declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE"];
16
22
 
17
- declare const __constants_COMMAND_I18N_RESOURCE_KEYS: typeof COMMAND_I18N_RESOURCE_KEYS;
23
+ declare const __constants_BUILT_IN_KEY_SEPARATOR: typeof BUILT_IN_KEY_SEPARATOR;
24
+ declare const __constants_BUILT_IN_PREFIX: typeof BUILT_IN_PREFIX;
25
+ declare const __constants_COMMAND_BUILTIN_RESOURCE_KEYS: typeof COMMAND_BUILTIN_RESOURCE_KEYS;
18
26
  declare const __constants_COMMAND_OPTIONS_DEFAULT: typeof COMMAND_OPTIONS_DEFAULT;
19
27
  declare const __constants_COMMON_OPTIONS: typeof COMMON_OPTIONS;
28
+ declare const __constants_DEFAULT_LOCALE: typeof DEFAULT_LOCALE;
20
29
  declare namespace __constants {
21
- export { __constants_COMMAND_I18N_RESOURCE_KEYS as COMMAND_I18N_RESOURCE_KEYS, __constants_COMMAND_OPTIONS_DEFAULT as COMMAND_OPTIONS_DEFAULT, __constants_COMMON_OPTIONS as COMMON_OPTIONS };
30
+ export { __constants_BUILT_IN_KEY_SEPARATOR as BUILT_IN_KEY_SEPARATOR, __constants_BUILT_IN_PREFIX as BUILT_IN_PREFIX, __constants_COMMAND_BUILTIN_RESOURCE_KEYS as COMMAND_BUILTIN_RESOURCE_KEYS, __constants_COMMAND_OPTIONS_DEFAULT as COMMAND_OPTIONS_DEFAULT, __constants_COMMON_OPTIONS as COMMON_OPTIONS, __constants_DEFAULT_LOCALE as DEFAULT_LOCALE };
22
31
  }
23
32
 
24
33
  /**
25
34
  * Define a promise type that can be await from T
26
35
  */
27
36
  type Awaitable<T> = T | Promise<T>;
37
+ type GenerateNamespacedKey<
38
+ Key extends string,
39
+ Prefixed extends string = typeof BUILT_IN_PREFIX
40
+ > = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
28
41
  /**
29
42
  * Command i18n built-in options keys
30
43
  * @experimental
@@ -34,13 +47,13 @@ type CommandBuiltinOptionsKeys = keyof (typeof __constants)["COMMON_OPTIONS"];
34
47
  * Command i18n built-in resource keys
35
48
  * @experimental
36
49
  */
37
- type CommandBuiltinResourceKeys = (typeof __constants)["COMMAND_I18N_RESOURCE_KEYS"][number];
50
+ type CommandBuiltinResourceKeys = (typeof __constants)["COMMAND_BUILTIN_RESOURCE_KEYS"][number];
38
51
  /**
39
52
  * Command i18n built-in keys
40
- * @description The command i18n built-in keys are used to {@link CommandContext.translation | translate} function
53
+ * @description The command i18n built-in keys are used to {@link CommandContext.translate | translate} function
41
54
  * @experimental
42
55
  */
43
- type CommandBuiltinKeys = CommandBuiltinOptionsKeys | CommandBuiltinResourceKeys | "description" | "examples";
56
+ type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | "description" | "examples";
44
57
  /**
45
58
  * Command environment
46
59
  */
@@ -187,7 +200,7 @@ interface CommandContext<
187
200
  * Command options, that is the options of the command that is executed
188
201
  * @description The command options is same {@link Command.options}
189
202
  */
190
- options: Options | undefined;
203
+ options: Options;
191
204
  /**
192
205
  * Command values, that is the values of the command that is executed
193
206
  * @description Resolve values with `resolveArgs` from command arguments and {@link Command.options}
@@ -214,14 +227,14 @@ interface CommandContext<
214
227
  */
215
228
  loadCommands: () => Promise<Command<Options>[]>;
216
229
  /**
217
- * Translation function
230
+ * Translate function
218
231
  * @param key the key to be translated
219
232
  * @returns A translated string
220
233
  * @experimental
221
234
  */
222
- translation: <
223
- T = CommandBuiltinKeys,
224
- Key = CommandBuiltinKeys | T
235
+ translate: <
236
+ T extends string = CommandBuiltinKeys,
237
+ Key = CommandBuiltinKeys | keyof Options | T
225
238
  >(key: Key) => string;
226
239
  }
227
240
  /**
@@ -282,20 +295,18 @@ interface Command<Options extends ArgOptions = ArgOptions> {
282
295
  * Command resource
283
296
  * @experimental
284
297
  */
285
- interface CommandResource<Options extends ArgOptions = ArgOptions> {
298
+ type CommandResource<Options extends ArgOptions = ArgOptions> = {
286
299
  /**
287
300
  * Command description
288
301
  */
289
- description: string;
290
- /**
291
- * Options usage
292
- */
293
- options: { [Option in keyof Options] : string };
302
+ description: string
294
303
  /**
295
304
  * Examples usage
296
305
  */
297
- examples: string;
298
- }
306
+ examples: string
307
+ } & { [Option in keyof Options] : string } & {
308
+ [key: string]: string
309
+ };
299
310
  /**
300
311
  * Command resource fetcher
301
312
  * @param ctx A {@link CommandContext | command context}
@@ -318,4 +329,4 @@ type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Comm
318
329
  */
319
330
  type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>;
320
331
 
321
- export type { Command as C, LazyCommand as L, CommandOptions as a, CommandContext as b, CommandRunner as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandEnvironment as g, CommandResource as h, CommandResourceFetcher as i, Commandable as j };
332
+ export type { Command as C, GenerateNamespacedKey as G, LazyCommand as L, CommandOptions as a, CommandContext as b, CommandRunner as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandEnvironment as g, CommandResource as h, CommandResourceFetcher as i, Commandable as j };
@@ -0,0 +1,62 @@
1
+
2
+ //#region src/constants.ts
3
+ const DEFAULT_LOCALE = "en-US";
4
+ const BUILT_IN_PREFIX = "_";
5
+ const BUILT_IN_KEY_SEPARATOR = ":";
6
+ const COMMON_OPTIONS = {
7
+ help: {
8
+ type: "boolean",
9
+ short: "h"
10
+ },
11
+ version: {
12
+ type: "boolean",
13
+ short: "v"
14
+ }
15
+ };
16
+ const COMMAND_OPTIONS_DEFAULT = {
17
+ name: undefined,
18
+ description: undefined,
19
+ version: undefined,
20
+ cwd: undefined,
21
+ subCommands: undefined,
22
+ leftMargin: 2,
23
+ middleMargin: 10,
24
+ usageOptionType: false,
25
+ renderHeader: undefined,
26
+ renderUsage: undefined,
27
+ renderValidationErrors: undefined
28
+ };
29
+
30
+ //#endregion
31
+ //#region src/utils.ts
32
+ async function resolveLazyCommand(cmd, name, entry = false) {
33
+ const resolved = Object.assign(create(), typeof cmd == "function" ? await cmd() : cmd, { default: entry });
34
+ if (resolved.name == null && name) resolved.name = name;
35
+ return deepFreeze(resolved);
36
+ }
37
+ function resolveBuiltInKey(key) {
38
+ return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
39
+ }
40
+ function mapResourceWithBuiltinKey(resource) {
41
+ return Object.entries(resource).reduce((acc, [key, value]) => {
42
+ acc[resolveBuiltInKey(key)] = value;
43
+ return acc;
44
+ }, create());
45
+ }
46
+ function create(obj = null) {
47
+ return Object.create(obj);
48
+ }
49
+ function log(...args) {
50
+ console.log(...args);
51
+ }
52
+ function deepFreeze(obj) {
53
+ if (obj === null || typeof obj !== "object") return obj;
54
+ for (const key of Object.keys(obj)) {
55
+ const value = obj[key];
56
+ if (typeof value === "object" && value !== null) deepFreeze(value);
57
+ }
58
+ return Object.freeze(obj);
59
+ }
60
+
61
+ //#endregion
62
+ export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveBuiltInKey, 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.6.2",
4
+ "version": "0.7.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -1,24 +0,0 @@
1
-
2
- //#region src/utils.ts
3
- async function resolveLazyCommand(cmd, name, entry = false) {
4
- const resolved = Object.assign(create(), typeof cmd == "function" ? await cmd() : cmd, { default: entry });
5
- if (resolved.name == null && name) resolved.name = name;
6
- return deepFreeze(resolved);
7
- }
8
- function create(obj = null) {
9
- return Object.create(obj);
10
- }
11
- function log(...args) {
12
- console.log(...args);
13
- }
14
- function deepFreeze(obj) {
15
- if (obj === null || typeof obj !== "object") return obj;
16
- for (const key of Object.keys(obj)) {
17
- const value = obj[key];
18
- if (typeof value === "object" && value !== null) deepFreeze(value);
19
- }
20
- return Object.freeze(obj);
21
- }
22
-
23
- //#endregion
24
- export { create, deepFreeze, log, resolveLazyCommand };