gunshi 0.11.0 → 0.13.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 +13 -312
- package/lib/{context-ZNBQwQKN.js → cli-D_4MqIMP.js} +104 -14
- package/lib/generator.d.ts +3 -3
- package/lib/generator.js +2 -4
- package/lib/index.d.ts +8 -9
- package/lib/index.js +4 -5
- package/lib/renderer/index.d.ts +7 -7
- package/lib/renderer/index.js +1 -2
- package/lib/{renderer-Dm_6iF2f.js → renderer-DAUAIZxV.js} +66 -2
- package/lib/{types.d-VymWn7vz.d.ts → types.d-DqtZy3HN.d.ts} +97 -107
- package/package.json +17 -12
- package/lib/cli-CKcZ5eWi.js +0 -96
- package/lib/context.d.ts +0 -46
- package/lib/context.js +0 -4
- package/lib/utils-zZSVaYTy.js +0 -67
package/README.md
CHANGED
|
@@ -32,8 +32,6 @@ Gunshi is designed to simplify the creation of modern command-line interfaces:
|
|
|
32
32
|
|
|
33
33
|
## 💿 Installation
|
|
34
34
|
|
|
35
|
-
### 🐢 Node
|
|
36
|
-
|
|
37
35
|
```sh
|
|
38
36
|
# npm
|
|
39
37
|
npm install --save gunshi
|
|
@@ -43,41 +41,16 @@ pnpm add gunshi
|
|
|
43
41
|
|
|
44
42
|
## yarn
|
|
45
43
|
yarn add gunshi
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### 🦕 Deno
|
|
49
44
|
|
|
50
|
-
|
|
45
|
+
## deno
|
|
51
46
|
deno add jsr:@kazupon/gunshi
|
|
52
|
-
```
|
|
53
47
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
```sh
|
|
48
|
+
## bun
|
|
57
49
|
bun add gunshi
|
|
58
50
|
```
|
|
59
51
|
|
|
60
52
|
## 🚀 Usage
|
|
61
53
|
|
|
62
|
-
### 📏 Simple API
|
|
63
|
-
|
|
64
|
-
Gunshi has a simple API that is a facade:
|
|
65
|
-
|
|
66
|
-
```js
|
|
67
|
-
import { cli } from 'gunshi'
|
|
68
|
-
|
|
69
|
-
const args = process.argv.slice(2)
|
|
70
|
-
// run a simple command
|
|
71
|
-
await cli(args, () => {
|
|
72
|
-
// something logic ...
|
|
73
|
-
console.log('Hello from Gunshi!', args)
|
|
74
|
-
})
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### ⚙️ Declarative Configuration
|
|
78
|
-
|
|
79
|
-
Configure commands declaratively:
|
|
80
|
-
|
|
81
54
|
```js
|
|
82
55
|
import { cli } from 'gunshi'
|
|
83
56
|
|
|
@@ -121,289 +94,7 @@ await cli(process.argv.slice(2), command, {
|
|
|
121
94
|
})
|
|
122
95
|
```
|
|
123
96
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
### 🛡️ Type-Safe Arguments
|
|
127
|
-
|
|
128
|
-
Gunshi provides type-safe argument parsing with TypeScript:
|
|
129
|
-
|
|
130
|
-
```ts
|
|
131
|
-
import { cli } from 'gunshi'
|
|
132
|
-
import type { ArgOptions, Command, CommandContext } from 'gunshi'
|
|
133
|
-
|
|
134
|
-
// type-safe arguments parsing example
|
|
135
|
-
// this demonstrates how to define and use typed command options with `satisfies`
|
|
136
|
-
|
|
137
|
-
// define 'type-safe' command options with types
|
|
138
|
-
const options = {
|
|
139
|
-
// define string option with short alias
|
|
140
|
-
name: {
|
|
141
|
-
type: 'string',
|
|
142
|
-
short: 'n'
|
|
143
|
-
},
|
|
144
|
-
// define number option with default value
|
|
145
|
-
age: {
|
|
146
|
-
type: 'number',
|
|
147
|
-
short: 'a',
|
|
148
|
-
default: 25
|
|
149
|
-
},
|
|
150
|
-
// define boolean flag
|
|
151
|
-
verbose: {
|
|
152
|
-
type: 'boolean',
|
|
153
|
-
short: 'v'
|
|
154
|
-
}
|
|
155
|
-
} satisfies ArgOptions
|
|
156
|
-
|
|
157
|
-
// define 'type-safe' command
|
|
158
|
-
const command = {
|
|
159
|
-
name: 'type-safe',
|
|
160
|
-
options,
|
|
161
|
-
run: (ctx: CommandContext<UserOptions, UserValues>) => {
|
|
162
|
-
const { name, age, verbose } = ctx.values
|
|
163
|
-
console.log(`Hello, ${name || 'World'}! You are ${age} years old.`)
|
|
164
|
-
}
|
|
165
|
-
} satisfies Command<typeof options>
|
|
166
|
-
|
|
167
|
-
await cli(process.argv.slice(2), command)
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
For more detailed examples, check out the [playground/type-safe](https://github.com/kazupon/gunshi/tree/main/playground/type-safe) in the repository.
|
|
171
|
-
|
|
172
|
-
### 🧩 Composable Sub-commands
|
|
173
|
-
|
|
174
|
-
Run a CLI with composable sub-commands:
|
|
175
|
-
|
|
176
|
-
```js
|
|
177
|
-
import { cli } from 'gunshi'
|
|
178
|
-
|
|
179
|
-
// define 'create' command
|
|
180
|
-
const createCommand = {
|
|
181
|
-
name: 'create',
|
|
182
|
-
description: 'Create a new resource',
|
|
183
|
-
options: {
|
|
184
|
-
name: { type: 'string', short: 'n' }
|
|
185
|
-
},
|
|
186
|
-
run: ctx => {
|
|
187
|
-
console.log(`Creating resource: ${ctx.values.name}`)
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// define 'list' command
|
|
192
|
-
const listCommand = {
|
|
193
|
-
name: 'list',
|
|
194
|
-
description: 'List all resources',
|
|
195
|
-
run: () => {
|
|
196
|
-
console.log('Listing all resources...')
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// prepare a Map of sub-commands
|
|
201
|
-
const subCommands = new Map()
|
|
202
|
-
subCommands.set('create', createCommand)
|
|
203
|
-
subCommands.set('list', listCommand)
|
|
204
|
-
|
|
205
|
-
// define the main ('resource-manager') command
|
|
206
|
-
const mainCommand = {
|
|
207
|
-
name: 'resource-manager',
|
|
208
|
-
description: 'Manage resources',
|
|
209
|
-
run: () => {
|
|
210
|
-
console.log('Use one of the sub-commands: create, list')
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// run the CLI with composable sub-commands
|
|
215
|
-
await cli(process.argv.slice(2), mainCommand, {
|
|
216
|
-
name: 'my-app',
|
|
217
|
-
version: '1.0.0',
|
|
218
|
-
subCommands
|
|
219
|
-
})
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
For more detailed examples, check out the [playground/composable](https://github.com/kazupon/gunshi/tree/main/playground/composable) in the repository.
|
|
223
|
-
|
|
224
|
-
### ⏳ Lazy & Async Command Loading
|
|
225
|
-
|
|
226
|
-
Load commands lazily and execute them asynchronously:
|
|
227
|
-
|
|
228
|
-
```js
|
|
229
|
-
import { cli } from 'gunshi'
|
|
230
|
-
|
|
231
|
-
// define a command that will be loaded lazily
|
|
232
|
-
const lazyCommand = async () => {
|
|
233
|
-
// simulate async loading
|
|
234
|
-
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
235
|
-
|
|
236
|
-
// return the actual command
|
|
237
|
-
return {
|
|
238
|
-
name: 'lazy',
|
|
239
|
-
description: 'A command that is loaded lazily',
|
|
240
|
-
run: async ctx => {
|
|
241
|
-
// async execution
|
|
242
|
-
await new Promise(resolve => setTimeout(resolve, 500))
|
|
243
|
-
console.log('Command executed!')
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// prepare a Map of sub-commands with lazy-loaded commands
|
|
249
|
-
const subCommands = new Map()
|
|
250
|
-
subCommands.set('lazy', lazyCommand)
|
|
251
|
-
|
|
252
|
-
// run the CLI with lazy-loaded commands
|
|
253
|
-
await cli(
|
|
254
|
-
process.argv.slice(2),
|
|
255
|
-
{ name: 'main', run: () => {} },
|
|
256
|
-
{
|
|
257
|
-
name: 'my-app',
|
|
258
|
-
subCommands
|
|
259
|
-
}
|
|
260
|
-
)
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
For more detailed examples, check out the [playground/lazy-async](https://github.com/kazupon/gunshi/tree/main/playground/lazy-async) in the repository.
|
|
264
|
-
|
|
265
|
-
### 📜 Auto Usage Generation
|
|
266
|
-
|
|
267
|
-
Gunshi automatically generates usage information:
|
|
268
|
-
|
|
269
|
-
```js
|
|
270
|
-
import { cli } from 'gunshi'
|
|
271
|
-
|
|
272
|
-
const command = {
|
|
273
|
-
name: 'app',
|
|
274
|
-
description: 'My application',
|
|
275
|
-
options: {
|
|
276
|
-
path: {
|
|
277
|
-
type: 'string',
|
|
278
|
-
short: 'p',
|
|
279
|
-
description: 'File or directory path'
|
|
280
|
-
},
|
|
281
|
-
recursive: {
|
|
282
|
-
type: 'boolean',
|
|
283
|
-
short: 'r',
|
|
284
|
-
description: 'Operate recursively on directories'
|
|
285
|
-
},
|
|
286
|
-
operation: {
|
|
287
|
-
type: 'string',
|
|
288
|
-
short: 'o',
|
|
289
|
-
required: true,
|
|
290
|
-
description: 'Operation to perform (list, copy, move, delete)'
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
// define examples
|
|
294
|
-
examples: '# Example\n$ my-app --operation list --path ./src',
|
|
295
|
-
run: ctx => {
|
|
296
|
-
// command implementation
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// run with --help to see the automatically generated usage information
|
|
301
|
-
await cli(process.argv.slice(2), command, {
|
|
302
|
-
name: 'my-app',
|
|
303
|
-
version: '1.0.0'
|
|
304
|
-
})
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
For more detailed examples, check out the [playground/auto-usage](https://github.com/kazupon/gunshi/tree/main/playground/auto-usage) in the repository.
|
|
308
|
-
|
|
309
|
-
### 🎨 Custom Usage Generation
|
|
310
|
-
|
|
311
|
-
Customize the usage message generation:
|
|
312
|
-
|
|
313
|
-
```js
|
|
314
|
-
import { cli } from 'gunshi'
|
|
315
|
-
|
|
316
|
-
// define custom header renderer
|
|
317
|
-
const customHeaderRenderer = ctx => {
|
|
318
|
-
return Promise.resolve(`
|
|
319
|
-
╔═══════════════════════╗
|
|
320
|
-
║ ${ctx.env.name.toUpperCase()} ║
|
|
321
|
-
╚═══════════════════════╝
|
|
322
|
-
${ctx.env.description}
|
|
323
|
-
Version: ${ctx.env.version}
|
|
324
|
-
`)
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// define custom usage renderer
|
|
328
|
-
const customUsageRenderer = ctx => {
|
|
329
|
-
const lines = []
|
|
330
|
-
lines.push('USAGE:')
|
|
331
|
-
lines.push(` $ ${ctx.env.name} [options]`)
|
|
332
|
-
lines.push('')
|
|
333
|
-
lines.push('OPTIONS:')
|
|
334
|
-
|
|
335
|
-
for (const [key, option] of Object.entries(ctx.options || Object.create(null))) {
|
|
336
|
-
const shortFlag = option.short ? `-${option.short}, ` : ' '
|
|
337
|
-
lines.push(` ${shortFlag}--${key.padEnd(10)} ${ctx.translate(key)}`)
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
return Promise.resolve(lines.join('\n'))
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
// run with custom renderers
|
|
344
|
-
await cli(
|
|
345
|
-
process.argv.slice(2),
|
|
346
|
-
{ name: 'app', run: () => {} },
|
|
347
|
-
{
|
|
348
|
-
name: 'my-app',
|
|
349
|
-
version: '1.0.0',
|
|
350
|
-
description: 'My application',
|
|
351
|
-
renderHeader: customHeaderRenderer,
|
|
352
|
-
renderUsage: customUsageRenderer
|
|
353
|
-
}
|
|
354
|
-
)
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
For more detailed examples, check out the [playground/custom-usage](https://github.com/kazupon/gunshi/tree/main/playground/custom-usage) in the repository.
|
|
358
|
-
|
|
359
|
-
### 🌍 Internationalization
|
|
360
|
-
|
|
361
|
-
Support internationalization:
|
|
362
|
-
|
|
363
|
-
```js
|
|
364
|
-
import { cli } from 'gunshi'
|
|
365
|
-
import enUS from './locales/en-US.json' with { type: 'json' }
|
|
366
|
-
|
|
367
|
-
const command = {
|
|
368
|
-
name: 'greeter',
|
|
369
|
-
options: {
|
|
370
|
-
name: {
|
|
371
|
-
type: 'string',
|
|
372
|
-
short: 'n'
|
|
373
|
-
},
|
|
374
|
-
formal: {
|
|
375
|
-
type: 'boolean',
|
|
376
|
-
short: 'f'
|
|
377
|
-
}
|
|
378
|
-
},
|
|
379
|
-
// resource fetcher for translations
|
|
380
|
-
resource: async ctx => {
|
|
381
|
-
if (ctx.locale.toString() === 'ja-JP') {
|
|
382
|
-
const resource = await import('./locales/ja-JP.json', { with: { type: 'json' } })
|
|
383
|
-
return resource.default
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// default to English
|
|
387
|
-
return enUS
|
|
388
|
-
},
|
|
389
|
-
run: ctx => {
|
|
390
|
-
const { name = 'World', formal } = ctx.values
|
|
391
|
-
const greeting = formal ? ctx.translate('formal_greeting') : ctx.translate('informal_greeting')
|
|
392
|
-
console.log(`${greeting}, ${name}!`)
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// run with locale support
|
|
397
|
-
await cli(process.argv.slice(2), command, {
|
|
398
|
-
name: 'my-app',
|
|
399
|
-
version: '1.0.0',
|
|
400
|
-
// set the locale via an environment variable
|
|
401
|
-
// if Node v21 or later is used, you can use the built-in `navigator.language` instead)
|
|
402
|
-
locale: new Intl.Locale(process.env.MY_LOCALE || 'en-US')
|
|
403
|
-
})
|
|
404
|
-
```
|
|
405
|
-
|
|
406
|
-
For more detailed examples, check out the [playground/i18n](https://github.com/kazupon/gunshi/tree/main/playground/i18n) in the repository.
|
|
97
|
+
About more details and usage, see [documentations](https://gunshi.dev)
|
|
407
98
|
|
|
408
99
|
## 💁♀️ Showcases
|
|
409
100
|
|
|
@@ -423,6 +114,16 @@ This project is inspired and powered by:
|
|
|
423
114
|
|
|
424
115
|
Thank you!
|
|
425
116
|
|
|
117
|
+
## 🤝 Sponsors
|
|
118
|
+
|
|
119
|
+
The development of Gunish is supported by my OSS sponsors!
|
|
120
|
+
|
|
121
|
+
<p align="center">
|
|
122
|
+
<a href="https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg">
|
|
123
|
+
<img src='https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg'/>
|
|
124
|
+
</a>
|
|
125
|
+
</p>
|
|
126
|
+
|
|
426
127
|
## ©️ License
|
|
427
128
|
|
|
428
129
|
[MIT](http://opensource.org/licenses/MIT)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveLazyCommand } from "./
|
|
1
|
+
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand } from "./renderer-DAUAIZxV.js";
|
|
2
|
+
import { parseArgs, resolveArgs } from "args-tokens";
|
|
2
3
|
|
|
3
4
|
//#region src/locales/en-US.json
|
|
4
5
|
var COMMAND = "COMMAND";
|
|
@@ -29,10 +30,11 @@ function createTranslationAdapter(options) {
|
|
|
29
30
|
}
|
|
30
31
|
var DefaultTranslation = class {
|
|
31
32
|
#resources = new Map();
|
|
32
|
-
options;
|
|
33
|
+
#options;
|
|
33
34
|
constructor(options) {
|
|
34
|
-
this
|
|
35
|
-
this.#resources
|
|
35
|
+
this.#options = options;
|
|
36
|
+
this.#resources.set(options.locale, create());
|
|
37
|
+
if (options.locale !== options.fallbackLocale) this.#resources.set(options.fallbackLocale, create());
|
|
36
38
|
}
|
|
37
39
|
getResource(locale) {
|
|
38
40
|
return this.#resources.get(locale);
|
|
@@ -45,21 +47,20 @@ var DefaultTranslation = class {
|
|
|
45
47
|
if (resource) return resource[key];
|
|
46
48
|
return void 0;
|
|
47
49
|
}
|
|
48
|
-
translate(locale, key,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return this.getMessage(locale, key) || this.getMessage(this.options.fallbackLocale, key);
|
|
50
|
+
translate(locale, key, values = create()) {
|
|
51
|
+
let message = this.getMessage(locale, key);
|
|
52
|
+
if (message === void 0 && locale !== this.#options.fallbackLocale) message = this.getMessage(this.#options.fallbackLocale, key);
|
|
53
|
+
if (message === void 0) return;
|
|
54
|
+
return message.replaceAll(/\{\{(\w+)\}\}/g, (_, name) => {
|
|
55
|
+
return values[name] == null ? "" : values[name].toString();
|
|
56
|
+
});
|
|
56
57
|
}
|
|
57
58
|
};
|
|
58
59
|
|
|
59
60
|
//#endregion
|
|
60
61
|
//#region src/context.ts
|
|
61
62
|
const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
|
|
62
|
-
async function createCommandContext({ options, values, positionals, command, commandOptions, omitted = false }) {
|
|
63
|
+
async function createCommandContext({ options, values, positionals, args, command, commandOptions, omitted = false }) {
|
|
63
64
|
/**
|
|
64
65
|
* normailize the options schema and values, to avoid prototype pollution
|
|
65
66
|
*/
|
|
@@ -120,6 +121,7 @@ async function createCommandContext({ options, values, positionals, command, com
|
|
|
120
121
|
options: _options,
|
|
121
122
|
values,
|
|
122
123
|
positionals,
|
|
124
|
+
_: args,
|
|
123
125
|
log: commandOptions.usageSilent ? NOOP : log,
|
|
124
126
|
loadCommands,
|
|
125
127
|
translate
|
|
@@ -164,4 +166,92 @@ async function loadCommandResource(ctx, command) {
|
|
|
164
166
|
}
|
|
165
167
|
|
|
166
168
|
//#endregion
|
|
167
|
-
|
|
169
|
+
//#region src/cli.ts
|
|
170
|
+
async function cli(args, entry, opts = {}) {
|
|
171
|
+
const tokens = parseArgs(args);
|
|
172
|
+
const subCommand = getSubCommand(tokens);
|
|
173
|
+
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
174
|
+
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
|
|
175
|
+
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
176
|
+
const options = resolveArgOptions(command.options);
|
|
177
|
+
const { values, positionals, error } = resolveArgs(options, tokens);
|
|
178
|
+
const omitted = !subCommand;
|
|
179
|
+
const ctx = await createCommandContext({
|
|
180
|
+
options,
|
|
181
|
+
values,
|
|
182
|
+
positionals,
|
|
183
|
+
args,
|
|
184
|
+
omitted,
|
|
185
|
+
command,
|
|
186
|
+
commandOptions: resolvedCommandOptions
|
|
187
|
+
});
|
|
188
|
+
if (values.version) {
|
|
189
|
+
showVersion(ctx);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const usageBuffer = [];
|
|
193
|
+
const header = await showHeader(ctx);
|
|
194
|
+
if (header) usageBuffer.push(header);
|
|
195
|
+
if (values.help) {
|
|
196
|
+
const usage = await showUsage(ctx);
|
|
197
|
+
if (usage) usageBuffer.push(usage);
|
|
198
|
+
return usageBuffer.join("\n");
|
|
199
|
+
}
|
|
200
|
+
if (error) {
|
|
201
|
+
await showValidationErrors(ctx, error);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
await command.run(ctx);
|
|
205
|
+
}
|
|
206
|
+
function resolveArgOptions(options) {
|
|
207
|
+
return Object.assign(create(), options, COMMON_OPTIONS);
|
|
208
|
+
}
|
|
209
|
+
function resolveCommandOptions(options, entry) {
|
|
210
|
+
const subCommands = new Map(options.subCommands);
|
|
211
|
+
if (typeof entry === "object" && entry.name) subCommands.set(entry.name, entry);
|
|
212
|
+
const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
|
|
213
|
+
return resolvedOptions;
|
|
214
|
+
}
|
|
215
|
+
function getSubCommand(tokens) {
|
|
216
|
+
const firstToken = tokens[0];
|
|
217
|
+
return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
|
|
218
|
+
}
|
|
219
|
+
async function showUsage(ctx) {
|
|
220
|
+
if (ctx.env.renderUsage === null) return;
|
|
221
|
+
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
222
|
+
if (usage) {
|
|
223
|
+
ctx.log(usage);
|
|
224
|
+
return usage;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function showVersion(ctx) {
|
|
228
|
+
ctx.log(ctx.env.version);
|
|
229
|
+
}
|
|
230
|
+
async function showHeader(ctx) {
|
|
231
|
+
if (ctx.env.renderHeader === null) return;
|
|
232
|
+
const header = await (ctx.env.renderHeader || renderHeader)(ctx);
|
|
233
|
+
if (header) {
|
|
234
|
+
ctx.log(header);
|
|
235
|
+
ctx.log();
|
|
236
|
+
return header;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
async function showValidationErrors(ctx, error) {
|
|
240
|
+
if (ctx.env.renderValidationErrors === null) return;
|
|
241
|
+
const render = ctx.env.renderValidationErrors || renderValidationErrors;
|
|
242
|
+
ctx.log(await render(ctx, error));
|
|
243
|
+
}
|
|
244
|
+
async function resolveCommand(sub, entry, options) {
|
|
245
|
+
const omitted = !sub;
|
|
246
|
+
if (typeof entry === "function") return [void 0, { run: entry }];
|
|
247
|
+
else if (omitted) return typeof entry === "object" ? [entry.name, await resolveLazyCommand(entry)] : [void 0, void 0];
|
|
248
|
+
else {
|
|
249
|
+
if (options.subCommands == null) return [sub, void 0];
|
|
250
|
+
const cmd = options.subCommands?.get(sub);
|
|
251
|
+
if (cmd == null) return [sub, void 0];
|
|
252
|
+
return [sub, await resolveLazyCommand(cmd, sub)];
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
export { DefaultTranslation, cli };
|
package/lib/generator.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { ArgOptions } from 'args-tokens';
|
|
2
|
-
import { C as Command, a as CommandOptions } from './types.d-
|
|
2
|
+
import { C as Command, a as CommandOptions } from './types.d-DqtZy3HN.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Generate the command usage
|
|
5
|
+
* Generate the command usage.
|
|
6
6
|
* @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`.
|
|
7
7
|
* @param entry - A {@link Command | entry command}
|
|
8
8
|
* @param opts - A {@link CommandOptions | command options}
|
|
9
|
-
* @returns A rendered usage
|
|
9
|
+
* @returns A rendered usage.
|
|
10
10
|
*/
|
|
11
11
|
declare function generate<Options extends ArgOptions = ArgOptions>(command: string | null, entry: Command<Options>, opts?: CommandOptions<Options>): Promise<string>;
|
|
12
12
|
|
package/lib/generator.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { create } from "./
|
|
2
|
-
import "./
|
|
3
|
-
import "./renderer-Dm_6iF2f.js";
|
|
4
|
-
import { cli } from "./cli-CKcZ5eWi.js";
|
|
1
|
+
import { create } from "./renderer-DAUAIZxV.js";
|
|
2
|
+
import { cli } from "./cli-D_4MqIMP.js";
|
|
5
3
|
|
|
6
4
|
//#region src/generator.ts
|
|
7
5
|
async function generate(command, entry, opts = {}) {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
import { ArgOptions } from 'args-tokens';
|
|
2
|
-
export { ArgOptionSchema, ArgOptions, ArgValues } from 'args-tokens';
|
|
3
|
-
import { C as Command,
|
|
4
|
-
export {
|
|
2
|
+
export { ArgOptionSchema, ArgOptions, ArgValues, parseArgs, resolveArgs } from 'args-tokens';
|
|
3
|
+
import { C as Command, b as CommandRunner, a as CommandOptions, T as TranslationAdapter, c as TranslationAdapterFactoryOptions } from './types.d-DqtZy3HN.js';
|
|
4
|
+
export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, h as CommandContext, g as CommandEnvironment, i as CommandResource, j as CommandResourceFetcher, l as Commandable, D as DEFAULT_LOCALE, G as GenerateNamespacedKey, L as LazyCommand, k as TranslationAdapterFactory } from './types.d-DqtZy3HN.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Run the command
|
|
8
|
-
* @param args
|
|
9
|
-
* @param entry
|
|
10
|
-
* @param opts
|
|
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
11
|
* @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
|
|
12
12
|
*/
|
|
13
13
|
declare function cli<Options extends ArgOptions = ArgOptions>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
|
|
14
14
|
|
|
15
15
|
declare class DefaultTranslation implements TranslationAdapter {
|
|
16
16
|
#private;
|
|
17
|
-
options: TranslationAdapterFactoryOptions;
|
|
18
17
|
constructor(options: TranslationAdapterFactoryOptions);
|
|
19
18
|
getResource(locale: string): Record<string, string> | undefined;
|
|
20
19
|
setResource(locale: string, resource: Record<string, string>): void;
|
|
21
20
|
getMessage(locale: string, key: string): string | undefined;
|
|
22
|
-
translate(locale: string, key: string,
|
|
21
|
+
translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
export { Command, CommandOptions, CommandRunner, DefaultTranslation, TranslationAdapter, TranslationAdapterFactoryOptions, cli };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import { DefaultTranslation } from "./
|
|
3
|
-
import "
|
|
4
|
-
import { cli } from "./cli-CKcZ5eWi.js";
|
|
1
|
+
import { DEFAULT_LOCALE } from "./renderer-DAUAIZxV.js";
|
|
2
|
+
import { DefaultTranslation, cli } from "./cli-D_4MqIMP.js";
|
|
3
|
+
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
4
|
|
|
6
|
-
export { DefaultTranslation, cli };
|
|
5
|
+
export { DEFAULT_LOCALE, DefaultTranslation, cli, parseArgs, resolveArgs };
|
package/lib/renderer/index.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import { ArgOptions } from 'args-tokens';
|
|
2
|
-
import {
|
|
2
|
+
import { h as CommandContext } from '../types.d-DqtZy3HN.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Render the header
|
|
5
|
+
* Render the header.
|
|
6
6
|
* @param ctx A {@link CommandContext | command context}
|
|
7
|
-
* @returns A rendered header
|
|
7
|
+
* @returns A rendered header.
|
|
8
8
|
*/
|
|
9
9
|
declare function renderHeader<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Render the usage
|
|
12
|
+
* Render the usage.
|
|
13
13
|
* @param ctx A {@link CommandContext | command context}
|
|
14
|
-
* @returns A rendered usage
|
|
14
|
+
* @returns A rendered usage.
|
|
15
15
|
*/
|
|
16
16
|
declare function renderUsage<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Render the validation errors
|
|
19
|
+
* Render the validation errors.
|
|
20
20
|
* @param ctx A {@link CommandContext | command context}
|
|
21
21
|
* @param error An {@link AggregateError} of option in `args-token` validation
|
|
22
|
-
* @returns A rendered validation error
|
|
22
|
+
* @returns A rendered validation error.
|
|
23
23
|
*/
|
|
24
24
|
declare function renderValidationErrors<Options extends ArgOptions = ArgOptions>(_ctx: CommandContext<Options>, error: AggregateError): Promise<string>;
|
|
25
25
|
|
package/lib/renderer/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "../
|
|
2
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-Dm_6iF2f.js";
|
|
1
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-DAUAIZxV.js";
|
|
3
2
|
|
|
4
3
|
export { renderHeader, renderUsage, renderValidationErrors };
|