@bemoje/cli 4.0.0 → 4.0.1
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 +372 -372
- package/lib/index.cjs +32 -70
- package/lib/index.d.cts +207 -18
- package/lib/index.d.ts +207 -18
- package/lib/index.js +960 -957
- package/package.json +2 -1
package/lib/index.js
CHANGED
|
@@ -1,1437 +1,1445 @@
|
|
|
1
|
-
import C from 'ansi-colors'
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
1
|
+
import C from 'ansi-colors'
|
|
2
|
+
import humanizeDuration from 'humanize-duration'
|
|
3
|
+
import { inspect } from 'util'
|
|
4
|
+
import { isFunction } from 'es-toolkit/predicate'
|
|
5
|
+
import { isPrimitive } from 'es-toolkit/predicate'
|
|
6
|
+
import { isPromise } from 'util/types'
|
|
7
|
+
import { isString } from 'es-toolkit/predicate'
|
|
8
|
+
import { kebabCase } from 'es-toolkit/string'
|
|
9
|
+
import { last } from 'es-toolkit/array'
|
|
10
|
+
import memoizee from 'memoizee'
|
|
11
|
+
import { ms } from 'enhanced-ms'
|
|
12
|
+
import { parseArgs } from 'util'
|
|
13
|
+
import { uniq } from 'es-toolkit/array'
|
|
14
|
+
import { upperFirst } from 'es-toolkit/string'
|
|
10
15
|
|
|
11
|
-
var __defProp = Object.defineProperty
|
|
12
|
-
var __name = (target, value) => __defProp(target,
|
|
16
|
+
var __defProp = Object.defineProperty
|
|
17
|
+
var __name = (target, value) => __defProp(target, 'name', { value, configurable: true })
|
|
13
18
|
|
|
14
19
|
// src/lib/helpers/parseOptionFlags.ts
|
|
15
20
|
function parseOptionFlags(flags) {
|
|
16
|
-
const match = flags.match(/^-(.+?), --([A-Za-z][\w-]*)(?:\s*(<(.+?)>|\[(.+?)]))?$/)
|
|
21
|
+
const match = flags.match(/^-(.+?), --([A-Za-z][\w-]*)(?:\s*(<(.+?)>|\[(.+?)]))?$/)
|
|
17
22
|
if (!match) {
|
|
18
|
-
throw new Error(`Invalid option format: ${flags}`)
|
|
23
|
+
throw new Error(`Invalid option format: ${flags}`)
|
|
19
24
|
}
|
|
20
|
-
const short = match[1]
|
|
25
|
+
const short = match[1]
|
|
21
26
|
if (short.length !== 1) {
|
|
22
|
-
throw new Error(`Expected short name to be a single character. Got: -${short}`)
|
|
27
|
+
throw new Error(`Expected short name to be a single character. Got: -${short}`)
|
|
23
28
|
}
|
|
24
|
-
const long = match[2]
|
|
25
|
-
const argName = (match[4] || match[5])?.replace(/\.{3}$/,
|
|
26
|
-
const name = long.split(
|
|
27
|
-
return str + word[0].toUpperCase() + word.slice(1);
|
|
28
|
-
});
|
|
29
|
+
const long = match[2]
|
|
30
|
+
const argName = (match[4] || match[5])?.replace(/\.{3}$/, '') || void 0
|
|
31
|
+
const name = long.split('-').reduce((str, word) => str + word[0].toUpperCase() + word.slice(1))
|
|
29
32
|
return {
|
|
30
33
|
short,
|
|
31
34
|
long,
|
|
32
35
|
name,
|
|
33
|
-
argName
|
|
34
|
-
}
|
|
36
|
+
argName,
|
|
37
|
+
}
|
|
35
38
|
}
|
|
36
|
-
__name(parseOptionFlags,
|
|
39
|
+
__name(parseOptionFlags, 'parseOptionFlags')
|
|
37
40
|
|
|
38
41
|
// src/lib/helpers/getCommandAndAncestors.ts
|
|
39
42
|
function getCommandAndAncestors(cmd) {
|
|
40
|
-
const result = []
|
|
41
|
-
let command = cmd
|
|
43
|
+
const result = []
|
|
44
|
+
let command = cmd
|
|
42
45
|
for (; command; command = command.parent) {
|
|
43
|
-
result.push(command)
|
|
46
|
+
result.push(command)
|
|
44
47
|
}
|
|
45
|
-
return result
|
|
48
|
+
return result
|
|
46
49
|
}
|
|
47
|
-
__name(getCommandAndAncestors,
|
|
50
|
+
__name(getCommandAndAncestors, 'getCommandAndAncestors')
|
|
48
51
|
|
|
49
52
|
// src/lib/helpers/getCommandAncestors.ts
|
|
50
53
|
function getCommandAncestors(cmd) {
|
|
51
|
-
return getCommandAndAncestors(cmd).slice(1)
|
|
54
|
+
return getCommandAndAncestors(cmd).slice(1)
|
|
52
55
|
}
|
|
53
|
-
__name(getCommandAncestors,
|
|
56
|
+
__name(getCommandAncestors, 'getCommandAncestors')
|
|
54
57
|
|
|
55
58
|
// src/lib/helpers/findOption.ts
|
|
56
59
|
function findOption(cmd, nameOrShortOrLong) {
|
|
57
|
-
return nameOrShortOrLong.startsWith(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return o.name === nameOrShortOrLong;
|
|
63
|
-
});
|
|
60
|
+
return nameOrShortOrLong.startsWith('--')
|
|
61
|
+
? cmd.options.find((o) => o.long === nameOrShortOrLong.slice(2))
|
|
62
|
+
: nameOrShortOrLong.startsWith('-')
|
|
63
|
+
? cmd.options.find((o) => o.short === nameOrShortOrLong.slice(1))
|
|
64
|
+
: cmd.options.find((o) => o.name === nameOrShortOrLong)
|
|
64
65
|
}
|
|
65
|
-
__name(findOption,
|
|
66
|
+
__name(findOption, 'findOption')
|
|
66
67
|
|
|
67
68
|
// ../object/src/entriesOf.ts
|
|
68
69
|
function entriesOf(obj) {
|
|
69
|
-
return Object.entries(obj)
|
|
70
|
+
return Object.entries(obj)
|
|
70
71
|
}
|
|
71
|
-
__name(entriesOf,
|
|
72
|
+
__name(entriesOf, 'entriesOf')
|
|
72
73
|
|
|
73
74
|
// ../object/src/keysOf.ts
|
|
74
75
|
function keysOf(obj) {
|
|
75
|
-
return Object.keys(obj)
|
|
76
|
+
return Object.keys(obj)
|
|
76
77
|
}
|
|
77
|
-
__name(keysOf,
|
|
78
|
+
__name(keysOf, 'keysOf')
|
|
78
79
|
|
|
79
80
|
// ../object/src/filterObject.ts
|
|
80
81
|
function filterObject(obj, predicate) {
|
|
81
|
-
const accum = {}
|
|
82
|
+
const accum = {}
|
|
82
83
|
for (const key of keysOf(obj)) {
|
|
83
84
|
if (predicate(obj[key], key, obj)) {
|
|
84
|
-
accum[key] = obj[key]
|
|
85
|
+
accum[key] = obj[key]
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
|
-
return accum
|
|
88
|
+
return accum
|
|
88
89
|
}
|
|
89
|
-
__name(filterObject,
|
|
90
|
+
__name(filterObject, 'filterObject')
|
|
90
91
|
|
|
91
92
|
// ../object/src/objSortKeys.ts
|
|
92
93
|
function objSortKeys(o, compare) {
|
|
93
|
-
const entries = Object.entries(o)
|
|
94
|
+
const entries = Object.entries(o)
|
|
94
95
|
if (compare) {
|
|
95
|
-
entries.sort(compare)
|
|
96
|
+
entries.sort(compare)
|
|
96
97
|
} else {
|
|
97
|
-
entries.sort((a, b) =>
|
|
98
|
-
return a[0].localeCompare(b[0]);
|
|
99
|
-
});
|
|
98
|
+
entries.sort((a, b) => a[0].localeCompare(b[0]))
|
|
100
99
|
}
|
|
101
|
-
return Object.fromEntries(entries)
|
|
100
|
+
return Object.fromEntries(entries)
|
|
102
101
|
}
|
|
103
|
-
__name(objSortKeys,
|
|
102
|
+
__name(objSortKeys, 'objSortKeys')
|
|
104
103
|
|
|
105
104
|
// ../object/src/valuesOf.ts
|
|
106
105
|
function valuesOf(obj) {
|
|
107
|
-
return Object.values(obj)
|
|
106
|
+
return Object.values(obj)
|
|
108
107
|
}
|
|
109
|
-
__name(valuesOf,
|
|
108
|
+
__name(valuesOf, 'valuesOf')
|
|
110
109
|
|
|
111
110
|
// src/lib/helpers/findCommand.ts
|
|
112
111
|
function findCommand(cmd, nameOrAlias) {
|
|
113
|
-
return cmd.commands[nameOrAlias] ?? valuesOf(cmd.commands).find((c) =>
|
|
114
|
-
return c.aliases.includes(nameOrAlias);
|
|
115
|
-
});
|
|
112
|
+
return cmd.commands[nameOrAlias] ?? valuesOf(cmd.commands).find((c) => c.aliases.includes(nameOrAlias))
|
|
116
113
|
}
|
|
117
|
-
__name(findCommand,
|
|
114
|
+
__name(findCommand, 'findCommand')
|
|
118
115
|
|
|
119
116
|
// ../decorators/src/assertDescriptorValueIsFunction.ts
|
|
120
117
|
function assertDescriptorValueIsFunction(key, descriptor) {
|
|
121
|
-
if (!(typeof descriptor.value ===
|
|
122
|
-
throw new TypeError(`"value" not a function for ${key} with descriptor: ${JSON.stringify(descriptor)}.`)
|
|
118
|
+
if (!(typeof descriptor.value === 'function' && descriptor.value !== Function.prototype)) {
|
|
119
|
+
throw new TypeError(`"value" not a function for ${key} with descriptor: ${JSON.stringify(descriptor)}.`)
|
|
123
120
|
}
|
|
124
121
|
}
|
|
125
|
-
__name(assertDescriptorValueIsFunction,
|
|
122
|
+
__name(assertDescriptorValueIsFunction, 'assertDescriptorValueIsFunction')
|
|
126
123
|
|
|
127
124
|
// ../map/src/mapGetOrDefault.ts
|
|
128
125
|
function mapGetOrDefault(map, key, factory) {
|
|
129
|
-
let value = map.get(key)
|
|
126
|
+
let value = map.get(key)
|
|
130
127
|
if (value !== void 0 || map.has(key)) {
|
|
131
|
-
return value
|
|
128
|
+
return value
|
|
132
129
|
}
|
|
133
|
-
value = factory(key, map)
|
|
134
|
-
map.set(key, value)
|
|
135
|
-
return value
|
|
130
|
+
value = factory(key, map)
|
|
131
|
+
map.set(key, value)
|
|
132
|
+
return value
|
|
136
133
|
}
|
|
137
|
-
__name(mapGetOrDefault,
|
|
134
|
+
__name(mapGetOrDefault, 'mapGetOrDefault')
|
|
138
135
|
|
|
139
136
|
// ../map/src/TimeoutWeakMap.ts
|
|
140
137
|
var TimeoutWeakMap = class {
|
|
141
138
|
static {
|
|
142
|
-
__name(this,
|
|
139
|
+
__name(this, 'TimeoutWeakMap')
|
|
143
140
|
}
|
|
144
|
-
timeoutMs
|
|
145
|
-
wmap = /* @__PURE__ */ new WeakMap()
|
|
141
|
+
timeoutMs
|
|
142
|
+
wmap = /* @__PURE__ */ new WeakMap()
|
|
146
143
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
144
|
+
* @param timeoutMs Default timeout in milliseconds for entries
|
|
145
|
+
*/
|
|
149
146
|
constructor(timeoutMs) {
|
|
150
|
-
this.timeoutMs = timeoutMs
|
|
147
|
+
this.timeoutMs = timeoutMs
|
|
151
148
|
}
|
|
152
149
|
/**
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
150
|
+
* Retrieves the value for the given key and refreshes its timeout.
|
|
151
|
+
*
|
|
152
|
+
* @param key The key to retrieve
|
|
153
|
+
* @returns The value if found, undefined otherwise
|
|
154
|
+
*/
|
|
158
155
|
get(key) {
|
|
159
|
-
const vt = this.wmap.get(key)
|
|
156
|
+
const vt = this.wmap.get(key)
|
|
160
157
|
if (!vt) {
|
|
161
|
-
return void 0
|
|
158
|
+
return void 0
|
|
162
159
|
}
|
|
163
|
-
const [value, timeout] = vt
|
|
164
|
-
timeout.refresh()
|
|
165
|
-
return value
|
|
160
|
+
const [value, timeout] = vt
|
|
161
|
+
timeout.refresh()
|
|
162
|
+
return value
|
|
166
163
|
}
|
|
167
164
|
/**
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
165
|
+
* Sets a key-value pair with an optional custom timeout.
|
|
166
|
+
*
|
|
167
|
+
* @param key The key to set
|
|
168
|
+
* @param value The value to associate with the key
|
|
169
|
+
* @param timeoutMs Optional timeout override for this entry
|
|
170
|
+
* @returns This instance for chaining
|
|
171
|
+
*/
|
|
175
172
|
set(key, value, timeoutMs) {
|
|
176
173
|
this.wmap.set(key, [
|
|
177
174
|
value,
|
|
178
175
|
setTimeout(() => {
|
|
179
|
-
this.delete(key)
|
|
180
|
-
}, timeoutMs ?? this.timeoutMs).unref()
|
|
181
|
-
])
|
|
182
|
-
return this
|
|
176
|
+
this.delete(key)
|
|
177
|
+
}, timeoutMs ?? this.timeoutMs).unref(),
|
|
178
|
+
])
|
|
179
|
+
return this
|
|
183
180
|
}
|
|
184
181
|
/**
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
182
|
+
* Removes a key-value pair and clears its timeout.
|
|
183
|
+
*
|
|
184
|
+
* @param key The key to delete
|
|
185
|
+
* @returns True if the key existed and was deleted, false otherwise
|
|
186
|
+
*/
|
|
190
187
|
delete(key) {
|
|
191
|
-
const vt = this.wmap.get(key)
|
|
188
|
+
const vt = this.wmap.get(key)
|
|
192
189
|
if (!vt) {
|
|
193
|
-
return false
|
|
190
|
+
return false
|
|
194
191
|
}
|
|
195
|
-
const timeout = vt[1]
|
|
196
|
-
clearTimeout(timeout)
|
|
197
|
-
return this.wmap.delete(key)
|
|
192
|
+
const timeout = vt[1]
|
|
193
|
+
clearTimeout(timeout)
|
|
194
|
+
return this.wmap.delete(key)
|
|
198
195
|
}
|
|
199
196
|
/**
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
197
|
+
* Checks if a key exists in the map.
|
|
198
|
+
* Does not refresh the timeout.
|
|
199
|
+
*
|
|
200
|
+
* @param key The key to check
|
|
201
|
+
* @returns True if the key exists, false otherwise
|
|
202
|
+
*/
|
|
206
203
|
has(key) {
|
|
207
|
-
return this.wmap.has(key)
|
|
204
|
+
return this.wmap.has(key)
|
|
208
205
|
}
|
|
209
206
|
/**
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
207
|
+
* Loads multiple key-value pairs into the map.
|
|
208
|
+
*
|
|
209
|
+
* @param entries Iterable of key-value pairs to load
|
|
210
|
+
* @param timeoutMs Optional timeout override for all entries
|
|
211
|
+
* @returns This instance for chaining
|
|
212
|
+
*/
|
|
216
213
|
load(entries, timeoutMs) {
|
|
217
214
|
for (const [key, value] of entries) {
|
|
218
|
-
this.set(key, value, timeoutMs)
|
|
215
|
+
this.set(key, value, timeoutMs)
|
|
219
216
|
}
|
|
220
|
-
return this
|
|
217
|
+
return this
|
|
221
218
|
}
|
|
222
219
|
/**
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
220
|
+
* Updates an existing value or creates a new one using an update function.
|
|
221
|
+
*
|
|
222
|
+
* @param key The key to update
|
|
223
|
+
* @param update Function that receives the current value and returns the new value
|
|
224
|
+
* @returns This instance for chaining
|
|
225
|
+
*/
|
|
229
226
|
update(key, update) {
|
|
230
|
-
return this.set(key, update(this.get(key), key, this))
|
|
227
|
+
return this.set(key, update(this.get(key), key, this))
|
|
231
228
|
}
|
|
232
229
|
/**
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
230
|
+
* Gets a value or creates it using a factory function if it doesn't exist.
|
|
231
|
+
* Refreshes timeout if the value exists.
|
|
232
|
+
*
|
|
233
|
+
* @param key The key to get or create
|
|
234
|
+
* @param factory Function to create the value if it doesn't exist
|
|
235
|
+
* @param timeoutMs Optional timeout override for new entries
|
|
236
|
+
* @returns The existing or newly created value
|
|
237
|
+
*/
|
|
241
238
|
getOrDefault(key, factory, timeoutMs) {
|
|
242
|
-
const vt = this.wmap.get(key)
|
|
239
|
+
const vt = this.wmap.get(key)
|
|
243
240
|
if (vt) {
|
|
244
|
-
const [value, timeout] = vt
|
|
245
|
-
timeout.refresh()
|
|
246
|
-
return value
|
|
241
|
+
const [value, timeout] = vt
|
|
242
|
+
timeout.refresh()
|
|
243
|
+
return value
|
|
247
244
|
} else {
|
|
248
|
-
const value = factory(key, this)
|
|
249
|
-
this.set(key, value, timeoutMs)
|
|
250
|
-
return value
|
|
245
|
+
const value = factory(key, this)
|
|
246
|
+
this.set(key, value, timeoutMs)
|
|
247
|
+
return value
|
|
251
248
|
}
|
|
252
249
|
}
|
|
253
|
-
}
|
|
250
|
+
}
|
|
254
251
|
function memoizeSync(arg = {}) {
|
|
255
|
-
const opts =
|
|
256
|
-
|
|
257
|
-
|
|
252
|
+
const opts =
|
|
253
|
+
typeof arg === 'object'
|
|
254
|
+
? arg
|
|
255
|
+
: {
|
|
256
|
+
maxAge: typeof arg === 'number' ? arg : ms(arg),
|
|
257
|
+
}
|
|
258
258
|
return /* @__PURE__ */ __name(function decorator(target, key, descriptor) {
|
|
259
259
|
if (!descriptor) {
|
|
260
|
-
throw new TypeError(
|
|
260
|
+
throw new TypeError('descriptor is undefined')
|
|
261
261
|
}
|
|
262
|
-
const orig = descriptor.value
|
|
263
|
-
assertDescriptorValueIsFunction(key, descriptor)
|
|
262
|
+
const orig = descriptor.value
|
|
263
|
+
assertDescriptorValueIsFunction(key, descriptor)
|
|
264
264
|
const options = {
|
|
265
265
|
length: false,
|
|
266
|
-
...opts
|
|
267
|
-
}
|
|
266
|
+
...opts,
|
|
267
|
+
}
|
|
268
268
|
if (opts.instancesShareCache) {
|
|
269
|
-
Reflect.deleteProperty(options,
|
|
270
|
-
descriptor.value = memoizee(orig, options)
|
|
269
|
+
Reflect.deleteProperty(options, 'instancesShareCache')
|
|
270
|
+
descriptor.value = memoizee(orig, options)
|
|
271
271
|
} else {
|
|
272
|
-
const wmap = /* @__PURE__ */ new WeakMap()
|
|
273
|
-
descriptor.value = function(...args) {
|
|
274
|
-
const memoized = mapGetOrDefault(wmap, this, () =>
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
return memoized.apply(this, args);
|
|
278
|
-
};
|
|
272
|
+
const wmap = /* @__PURE__ */ new WeakMap()
|
|
273
|
+
descriptor.value = function (...args) {
|
|
274
|
+
const memoized = mapGetOrDefault(wmap, this, () => memoizee(orig, options))
|
|
275
|
+
return memoized.apply(this, args)
|
|
276
|
+
}
|
|
279
277
|
}
|
|
280
|
-
return descriptor
|
|
281
|
-
},
|
|
278
|
+
return descriptor
|
|
279
|
+
}, 'decorator')
|
|
282
280
|
}
|
|
283
|
-
__name(memoizeSync,
|
|
281
|
+
__name(memoizeSync, 'memoizeSync')
|
|
284
282
|
function lazyProp(targetOrTimeout, key, descriptor) {
|
|
285
|
-
if (typeof targetOrTimeout ===
|
|
286
|
-
const maxAge = typeof targetOrTimeout ===
|
|
287
|
-
return createLazyPropDecorator(new TimeoutWeakMap(maxAge))
|
|
283
|
+
if (typeof targetOrTimeout === 'number' || typeof targetOrTimeout === 'string') {
|
|
284
|
+
const maxAge = typeof targetOrTimeout === 'number' ? targetOrTimeout : ms(targetOrTimeout)
|
|
285
|
+
return createLazyPropDecorator(new TimeoutWeakMap(maxAge))
|
|
288
286
|
} else {
|
|
289
|
-
const decorator = createLazyPropDecorator(/* @__PURE__ */ new WeakMap())
|
|
290
|
-
return decorator(targetOrTimeout, key, descriptor)
|
|
287
|
+
const decorator = createLazyPropDecorator(/* @__PURE__ */ new WeakMap())
|
|
288
|
+
return decorator(targetOrTimeout, key, descriptor)
|
|
291
289
|
}
|
|
292
290
|
}
|
|
293
|
-
__name(lazyProp,
|
|
291
|
+
__name(lazyProp, 'lazyProp')
|
|
294
292
|
function createLazyPropDecorator(map) {
|
|
295
|
-
return function(target, key, descriptor) {
|
|
296
|
-
const { get, value } = descriptor
|
|
293
|
+
return function (target, key, descriptor) {
|
|
294
|
+
const { get, value } = descriptor
|
|
297
295
|
if (isFunction(get)) {
|
|
298
|
-
descriptor.get = function() {
|
|
299
|
-
return mapGetOrDefault(map, this, () =>
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
};
|
|
303
|
-
return descriptor;
|
|
296
|
+
descriptor.get = function () {
|
|
297
|
+
return mapGetOrDefault(map, this, () => get.call(this))
|
|
298
|
+
}
|
|
299
|
+
return descriptor
|
|
304
300
|
}
|
|
305
301
|
if (isFunction(value)) {
|
|
306
|
-
return memoizeSync()(target, key, descriptor)
|
|
302
|
+
return memoizeSync()(target, key, descriptor)
|
|
307
303
|
}
|
|
308
|
-
throw new TypeError('neither "get" nor "value" are functions')
|
|
309
|
-
}
|
|
304
|
+
throw new TypeError('neither "get" nor "value" are functions')
|
|
305
|
+
}
|
|
310
306
|
}
|
|
311
|
-
__name(createLazyPropDecorator,
|
|
307
|
+
__name(createLazyPropDecorator, 'createLazyPropDecorator')
|
|
312
308
|
|
|
313
309
|
// src/lib/Help.ts
|
|
314
310
|
function _ts_decorate(decorators, target, key, desc) {
|
|
315
|
-
var c = arguments.length,
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
311
|
+
var c = arguments.length,
|
|
312
|
+
r = c < 3 ? target : desc === null ? (desc = Object.getOwnPropertyDescriptor(target, key)) : desc,
|
|
313
|
+
d
|
|
314
|
+
if (typeof Reflect === 'object' && typeof Reflect.decorate === 'function')
|
|
315
|
+
r = Reflect.decorate(decorators, target, key, desc)
|
|
316
|
+
else
|
|
317
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
318
|
+
if ((d = decorators[i])) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r
|
|
319
|
+
return (c > 3 && r && Object.defineProperty(target, key, r), r)
|
|
319
320
|
}
|
|
320
|
-
__name(_ts_decorate,
|
|
321
|
+
__name(_ts_decorate, '_ts_decorate')
|
|
321
322
|
function _ts_metadata(k, v) {
|
|
322
|
-
if (typeof Reflect ===
|
|
323
|
+
if (typeof Reflect === 'object' && typeof Reflect.metadata === 'function') return Reflect.metadata(k, v)
|
|
323
324
|
}
|
|
324
|
-
__name(_ts_metadata,
|
|
325
|
+
__name(_ts_metadata, '_ts_metadata')
|
|
325
326
|
var Help = class {
|
|
326
327
|
static {
|
|
327
|
-
__name(this,
|
|
328
|
+
__name(this, 'Help')
|
|
328
329
|
}
|
|
329
|
-
cmd
|
|
330
|
+
cmd
|
|
330
331
|
/** output helpWidth, long lines are wrapped to fit */
|
|
331
|
-
helpWidth = process.stdout.isTTY ? process.stdout.columns : 80
|
|
332
|
-
minWidthToWrap = 40
|
|
333
|
-
sortSubcommands = true
|
|
334
|
-
sortOptions = true
|
|
335
|
-
usageDisplayOptionsAs =
|
|
336
|
-
usageDisplaySubcommandAs =
|
|
332
|
+
helpWidth = process.stdout.isTTY ? process.stdout.columns : 80
|
|
333
|
+
minWidthToWrap = 40
|
|
334
|
+
sortSubcommands = true
|
|
335
|
+
sortOptions = true
|
|
336
|
+
usageDisplayOptionsAs = '[opts]'
|
|
337
|
+
usageDisplaySubcommandAs = '[cmd]'
|
|
337
338
|
constructor(cmd) {
|
|
338
|
-
this.cmd = cmd
|
|
339
|
-
Object.defineProperty(this,
|
|
340
|
-
enumerable: false
|
|
341
|
-
})
|
|
339
|
+
this.cmd = cmd
|
|
340
|
+
Object.defineProperty(this, 'cmd', {
|
|
341
|
+
enumerable: false,
|
|
342
|
+
})
|
|
342
343
|
}
|
|
343
344
|
/**
|
|
344
|
-
|
|
345
|
-
|
|
345
|
+
* Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
|
|
346
|
+
*/
|
|
346
347
|
visibleCommands() {
|
|
347
|
-
const res = Object.values(this.cmd.commands).filter((c) =>
|
|
348
|
-
return !c.hidden;
|
|
349
|
-
});
|
|
348
|
+
const res = Object.values(this.cmd.commands).filter((c) => !c.hidden)
|
|
350
349
|
if (this.sortSubcommands) {
|
|
351
|
-
res.sort((a, b) =>
|
|
352
|
-
return a.name.localeCompare(b.name);
|
|
353
|
-
});
|
|
350
|
+
res.sort((a, b) => a.name.localeCompare(b.name))
|
|
354
351
|
}
|
|
355
|
-
return res
|
|
352
|
+
return res
|
|
356
353
|
}
|
|
357
354
|
/**
|
|
358
|
-
|
|
359
|
-
|
|
355
|
+
* Compare options for sort.
|
|
356
|
+
*/
|
|
360
357
|
compareOptions(a, b) {
|
|
361
|
-
const getSortKey = /* @__PURE__ */ __name(
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
358
|
+
const getSortKey = /* @__PURE__ */ __name(
|
|
359
|
+
(option) => (option.short ? option.short.replace(/^-/, '') : option.long.replace(/^--/, '')),
|
|
360
|
+
'getSortKey'
|
|
361
|
+
)
|
|
362
|
+
return getSortKey(a).localeCompare(getSortKey(b))
|
|
365
363
|
}
|
|
366
364
|
/**
|
|
367
|
-
|
|
368
|
-
|
|
365
|
+
* Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
|
|
366
|
+
*/
|
|
369
367
|
visibleOptions() {
|
|
370
|
-
const res = this.cmd.options.filter((option) =>
|
|
371
|
-
return !option.hidden;
|
|
372
|
-
});
|
|
368
|
+
const res = this.cmd.options.filter((option) => !option.hidden)
|
|
373
369
|
if (this.sortOptions) {
|
|
374
|
-
res.sort(this.compareOptions)
|
|
370
|
+
res.sort(this.compareOptions)
|
|
375
371
|
}
|
|
376
|
-
return res
|
|
372
|
+
return res
|
|
377
373
|
}
|
|
378
374
|
/**
|
|
379
|
-
|
|
380
|
-
|
|
375
|
+
* Get an array of the arguments if any have a description.
|
|
376
|
+
*/
|
|
381
377
|
visibleArguments() {
|
|
382
|
-
return this.cmd.arguments.slice()
|
|
378
|
+
return this.cmd.arguments.slice()
|
|
383
379
|
}
|
|
384
380
|
/**
|
|
385
|
-
|
|
386
|
-
|
|
381
|
+
* Get the command term to show in the list of subcommands.
|
|
382
|
+
*/
|
|
387
383
|
subcommandTerm(sub) {
|
|
388
|
-
const args = sub.arguments.map((arg) =>
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
384
|
+
const args = sub.arguments.map((arg) => arg.usage).join(' ')
|
|
385
|
+
return (
|
|
386
|
+
(sub.aliases[0] ? `${sub.aliases[0].padEnd(this.longestSubcommandAliasLength(), ' ')} | ` : '') +
|
|
387
|
+
sub.name +
|
|
388
|
+
(sub.options.length ? ` ${this.usageDisplayOptionsAs}` : '') + // simplistic check for non-help option
|
|
389
|
+
(args ? ` ${args}` : '')
|
|
390
|
+
)
|
|
393
391
|
}
|
|
394
392
|
/**
|
|
395
|
-
|
|
396
|
-
|
|
393
|
+
* Get the option term to show in the list of options.
|
|
394
|
+
*/
|
|
397
395
|
optionTerm(option) {
|
|
398
|
-
return option.flags
|
|
396
|
+
return option.flags
|
|
399
397
|
}
|
|
400
398
|
/**
|
|
401
|
-
|
|
402
|
-
|
|
399
|
+
* Get the argument term to show in the list of arguments.
|
|
400
|
+
*/
|
|
403
401
|
argumentTerm(argument) {
|
|
404
|
-
return argument.usage
|
|
402
|
+
return argument.usage
|
|
405
403
|
}
|
|
406
404
|
/**
|
|
407
|
-
|
|
408
|
-
|
|
405
|
+
* Get the longest subcommand primary alias length.
|
|
406
|
+
*/
|
|
409
407
|
longestSubcommandAliasLength() {
|
|
410
|
-
return Math.max(0, ...this.visibleCommands().map((c) =>
|
|
411
|
-
return c.aliases[0]?.length || 0;
|
|
412
|
-
}));
|
|
408
|
+
return Math.max(0, ...this.visibleCommands().map((c) => c.aliases[0]?.length || 0))
|
|
413
409
|
}
|
|
414
410
|
/**
|
|
415
|
-
|
|
416
|
-
|
|
411
|
+
* Get the longest subcommand term length.
|
|
412
|
+
*/
|
|
417
413
|
longestSubcommandTermLength() {
|
|
418
|
-
return this.visibleCommands().reduce(
|
|
419
|
-
|
|
420
|
-
|
|
414
|
+
return this.visibleCommands().reduce(
|
|
415
|
+
(max, command) => Math.max(max, this.displayWidth(this.styleSubcommandTerm(this.subcommandTerm(command)))),
|
|
416
|
+
0
|
|
417
|
+
)
|
|
421
418
|
}
|
|
422
419
|
/**
|
|
423
|
-
|
|
424
|
-
|
|
420
|
+
* Get the longest option term length.
|
|
421
|
+
*/
|
|
425
422
|
longestOptionTermLength() {
|
|
426
|
-
return this.visibleOptions().reduce(
|
|
427
|
-
|
|
428
|
-
|
|
423
|
+
return this.visibleOptions().reduce(
|
|
424
|
+
(max, option) => Math.max(max, this.displayWidth(this.styleOptionTerm(this.optionTerm(option)))),
|
|
425
|
+
0
|
|
426
|
+
)
|
|
429
427
|
}
|
|
430
428
|
/**
|
|
431
|
-
|
|
432
|
-
|
|
429
|
+
* Get the longest argument term length.
|
|
430
|
+
*/
|
|
433
431
|
longestArgumentTermLength() {
|
|
434
|
-
return this.visibleArguments().reduce(
|
|
435
|
-
|
|
436
|
-
|
|
432
|
+
return this.visibleArguments().reduce(
|
|
433
|
+
(max, argument) => Math.max(max, this.displayWidth(this.styleArgumentTerm(this.argumentTerm(argument)))),
|
|
434
|
+
0
|
|
435
|
+
)
|
|
437
436
|
}
|
|
438
437
|
/**
|
|
439
|
-
|
|
440
|
-
|
|
438
|
+
* Get the command usage to be displayed at the top of the built-in help.
|
|
439
|
+
*/
|
|
441
440
|
commandUsage() {
|
|
442
|
-
let path =
|
|
441
|
+
let path = ''
|
|
443
442
|
for (let ancestor = this.cmd.parent; ancestor; ancestor = ancestor.parent) {
|
|
444
|
-
path = `${ancestor.name} ${path}
|
|
443
|
+
path = `${ancestor.name} ${path}`
|
|
445
444
|
}
|
|
446
445
|
return `${path + this.cmd.name} ${[
|
|
447
|
-
...Object.keys(this.cmd.commands).length ? [
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
446
|
+
...(Object.keys(this.cmd.commands).length ? [this.usageDisplaySubcommandAs] : []),
|
|
447
|
+
...(this.cmd.options.length ? [this.usageDisplayOptionsAs] : []),
|
|
448
|
+
...this.cmd.arguments.map((arg) =>
|
|
449
|
+
arg.required
|
|
450
|
+
? arg.variadic
|
|
451
|
+
? `<${arg.name}...>`
|
|
452
|
+
: `<${arg.name}>`
|
|
453
|
+
: arg.variadic
|
|
454
|
+
? `[${arg.name}...]`
|
|
455
|
+
: `[${arg.name}]`
|
|
456
|
+
),
|
|
457
|
+
].join(' ')}`.trim()
|
|
457
458
|
}
|
|
458
459
|
/**
|
|
459
|
-
|
|
460
|
-
|
|
460
|
+
* Get the description for the command.
|
|
461
|
+
*/
|
|
461
462
|
commandDescription() {
|
|
462
|
-
let res =
|
|
463
|
+
let res = ''
|
|
463
464
|
if (this.cmd.aliases.length) {
|
|
464
|
-
res += `Aliases: ${this.cmd.aliases.join(
|
|
465
|
-
res +=
|
|
465
|
+
res += `Aliases: ${this.cmd.aliases.join(', ')}`
|
|
466
|
+
res += '\n\n'
|
|
466
467
|
}
|
|
467
|
-
res += this.cmd.description
|
|
468
|
-
return res
|
|
468
|
+
res += this.cmd.description
|
|
469
|
+
return res
|
|
469
470
|
}
|
|
470
471
|
/**
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
472
|
+
* Get the subcommand summary to show in the list of subcommands.
|
|
473
|
+
* (Fallback to description for backwards compatibility.)
|
|
474
|
+
*/
|
|
474
475
|
subcommandDescription(sub) {
|
|
475
|
-
return
|
|
476
|
+
return (
|
|
477
|
+
sub.summary ||
|
|
478
|
+
(sub.description?.includes('\n') ? sub.description.trim().split('\n')[0] : sub.description.trim())
|
|
479
|
+
)
|
|
476
480
|
}
|
|
477
481
|
/**
|
|
478
|
-
|
|
479
|
-
|
|
482
|
+
* Get the option description to show in the list of options.
|
|
483
|
+
*/
|
|
480
484
|
optionDescription(option) {
|
|
481
|
-
const extraInfo = []
|
|
485
|
+
const extraInfo = []
|
|
482
486
|
if (option.choices) {
|
|
483
|
-
const choices = option.choices.length > 5 ? option.choices.slice(0, 5).concat([
|
|
484
|
-
"..."
|
|
485
|
-
]) : option.choices;
|
|
487
|
+
const choices = option.choices.length > 5 ? option.choices.slice(0, 5).concat(['...']) : option.choices
|
|
486
488
|
extraInfo.push(
|
|
487
489
|
// use stringify to match the display of the default value
|
|
488
|
-
`choices: ${choices.join(
|
|
489
|
-
)
|
|
490
|
+
`choices: ${choices.join(', ')}`
|
|
491
|
+
)
|
|
490
492
|
}
|
|
491
493
|
if (option.defaultValue && !(Array.isArray(option.defaultValue) && option.defaultValue.length === 0)) {
|
|
492
|
-
extraInfo.push(
|
|
494
|
+
extraInfo.push(
|
|
495
|
+
`default: ${option.defaultValueDescription || JSON.stringify(option.defaultValue).replaceAll('"', '')}`
|
|
496
|
+
)
|
|
493
497
|
}
|
|
494
498
|
if (option.env !== void 0) {
|
|
495
|
-
extraInfo.push(`env: ${option.env}`)
|
|
499
|
+
extraInfo.push(`env: ${option.env}`)
|
|
496
500
|
}
|
|
497
501
|
if (extraInfo.length > 0) {
|
|
498
|
-
const extraDescription = `(${extraInfo.join(
|
|
502
|
+
const extraDescription = `(${extraInfo.join(', ')})`
|
|
499
503
|
if (option.description) {
|
|
500
|
-
return `${option.description} ${extraDescription}
|
|
504
|
+
return `${option.description} ${extraDescription}`
|
|
501
505
|
}
|
|
502
|
-
return extraDescription
|
|
506
|
+
return extraDescription
|
|
503
507
|
}
|
|
504
|
-
return option.description ??
|
|
508
|
+
return option.description ?? ''
|
|
505
509
|
}
|
|
506
510
|
/**
|
|
507
|
-
|
|
508
|
-
|
|
511
|
+
* Get the argument description to show in the list of arguments.
|
|
512
|
+
*/
|
|
509
513
|
argumentDescription(argument) {
|
|
510
|
-
const extraInfo = []
|
|
514
|
+
const extraInfo = []
|
|
511
515
|
if (argument.choices) {
|
|
512
|
-
const choices = argument.choices.length > 5 ? argument.choices.slice(0, 5).concat([
|
|
513
|
-
"..."
|
|
514
|
-
]) : argument.choices;
|
|
516
|
+
const choices = argument.choices.length > 5 ? argument.choices.slice(0, 5).concat(['...']) : argument.choices
|
|
515
517
|
extraInfo.push(
|
|
516
518
|
// use stringify to match the display of the default value
|
|
517
|
-
`choices: ${choices.join(
|
|
518
|
-
)
|
|
519
|
+
`choices: ${choices.join(', ')}`
|
|
520
|
+
)
|
|
519
521
|
}
|
|
520
522
|
if (argument.defaultValue && !(Array.isArray(argument.defaultValue) && argument.defaultValue.length === 0)) {
|
|
521
|
-
extraInfo.push(
|
|
523
|
+
extraInfo.push(
|
|
524
|
+
`default: ${argument.defaultValueDescription || JSON.stringify(argument.defaultValue).replaceAll('"', '')}`
|
|
525
|
+
)
|
|
522
526
|
}
|
|
523
527
|
if (extraInfo.length > 0) {
|
|
524
|
-
const extraDescription = `(${extraInfo.join(
|
|
528
|
+
const extraDescription = `(${extraInfo.join(', ')})`
|
|
525
529
|
if (argument.description) {
|
|
526
|
-
return `${argument.description} ${extraDescription}
|
|
530
|
+
return `${argument.description} ${extraDescription}`
|
|
527
531
|
}
|
|
528
|
-
return extraDescription
|
|
532
|
+
return extraDescription
|
|
529
533
|
}
|
|
530
|
-
return argument.description ??
|
|
534
|
+
return argument.description ?? ''
|
|
531
535
|
}
|
|
532
536
|
/**
|
|
533
|
-
|
|
534
|
-
|
|
537
|
+
* Format a list of items, given a heading and an array of formatted items.
|
|
538
|
+
*/
|
|
535
539
|
formatItemList(heading, items) {
|
|
536
540
|
if (items.length === 0) {
|
|
537
|
-
return []
|
|
541
|
+
return []
|
|
538
542
|
}
|
|
539
|
-
return [
|
|
540
|
-
this.styleTitle(heading),
|
|
541
|
-
...items,
|
|
542
|
-
""
|
|
543
|
-
];
|
|
543
|
+
return [this.styleTitle(heading), ...items, '']
|
|
544
544
|
}
|
|
545
545
|
/**
|
|
546
|
-
|
|
547
|
-
|
|
546
|
+
* Group items by their help group heading.
|
|
547
|
+
*/
|
|
548
548
|
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
549
|
-
const result = /* @__PURE__ */ new Map()
|
|
549
|
+
const result = /* @__PURE__ */ new Map()
|
|
550
550
|
unsortedItems.forEach((item) => {
|
|
551
|
-
const group = getGroup(item)
|
|
551
|
+
const group = getGroup(item)
|
|
552
552
|
if (!result.has(group)) {
|
|
553
|
-
result.set(group, [])
|
|
553
|
+
result.set(group, [])
|
|
554
554
|
}
|
|
555
|
-
})
|
|
555
|
+
})
|
|
556
556
|
visibleItems.forEach((item) => {
|
|
557
|
-
const group = getGroup(item)
|
|
557
|
+
const group = getGroup(item)
|
|
558
558
|
if (!result.has(group)) {
|
|
559
|
-
result.set(group, [])
|
|
559
|
+
result.set(group, [])
|
|
560
560
|
}
|
|
561
|
-
result.get(group).push(item)
|
|
562
|
-
})
|
|
563
|
-
return result
|
|
561
|
+
result.get(group).push(item)
|
|
562
|
+
})
|
|
563
|
+
return result
|
|
564
564
|
}
|
|
565
565
|
/**
|
|
566
|
-
|
|
567
|
-
|
|
566
|
+
* Return display width of string, ignoring ANSI escape sequences. Used in padding and wrapping calculations.
|
|
567
|
+
*/
|
|
568
568
|
displayWidth(str) {
|
|
569
|
-
const sgrPattern = /\u001b\[\d*(;\d*)*m/g
|
|
570
|
-
return str.replaceAll(sgrPattern,
|
|
569
|
+
const sgrPattern = /\u001b\[\d*(;\d*)*m/g
|
|
570
|
+
return str.replaceAll(sgrPattern, '').length
|
|
571
571
|
}
|
|
572
572
|
/**
|
|
573
|
-
|
|
574
|
-
|
|
573
|
+
* Style the title for displaying in the help. Called with 'Usage:', 'Options:', etc.
|
|
574
|
+
*/
|
|
575
575
|
styleTitle(str) {
|
|
576
|
-
return C.yellow(str)
|
|
576
|
+
return C.yellow(str)
|
|
577
577
|
}
|
|
578
578
|
/**
|
|
579
|
-
|
|
580
|
-
|
|
579
|
+
* Style the usage line for displaying in the help. Applies specific styling to different parts like options, commands, and arguments.
|
|
580
|
+
*/
|
|
581
581
|
styleUsage(str) {
|
|
582
|
-
return str
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
582
|
+
return str
|
|
583
|
+
.split(' ')
|
|
584
|
+
.map((word, index, arr) => {
|
|
585
|
+
if (word === this.usageDisplaySubcommandAs) {
|
|
586
|
+
return C.green(word)
|
|
587
|
+
}
|
|
588
|
+
if (word === this.usageDisplayOptionsAs) {
|
|
589
|
+
return C.blue(word)
|
|
590
|
+
}
|
|
591
|
+
if (word[0] === '<') {
|
|
592
|
+
return C.red(word)
|
|
593
|
+
}
|
|
594
|
+
if (word[0] === '[') {
|
|
595
|
+
return C.cyan(word)
|
|
596
|
+
}
|
|
597
|
+
if (arr[index + 1]?.startsWith('[')) {
|
|
598
|
+
return C.magenta(word)
|
|
599
|
+
}
|
|
600
|
+
return this.styleCommandText(word)
|
|
601
|
+
})
|
|
602
|
+
.join(' ')
|
|
600
603
|
}
|
|
601
604
|
/**
|
|
602
|
-
|
|
603
|
-
|
|
605
|
+
* Style command descriptions for display in help output.
|
|
606
|
+
*/
|
|
604
607
|
styleCommandDescription(str) {
|
|
605
|
-
return this.styleDescriptionText(str)
|
|
608
|
+
return this.styleDescriptionText(str)
|
|
606
609
|
}
|
|
607
610
|
/**
|
|
608
|
-
|
|
609
|
-
|
|
611
|
+
* Style option descriptions for display in help output.
|
|
612
|
+
*/
|
|
610
613
|
styleOptionDescription(str) {
|
|
611
|
-
return C.gray(this.styleDescriptionText(str))
|
|
614
|
+
return C.gray(this.styleDescriptionText(str))
|
|
612
615
|
}
|
|
613
616
|
/**
|
|
614
|
-
|
|
615
|
-
|
|
617
|
+
* Style subcommand descriptions for display in help output.
|
|
618
|
+
*/
|
|
616
619
|
styleSubcommandDescription(str) {
|
|
617
|
-
return this.styleDescriptionText(str)
|
|
620
|
+
return this.styleDescriptionText(str)
|
|
618
621
|
}
|
|
619
622
|
/**
|
|
620
|
-
|
|
621
|
-
|
|
623
|
+
* Style argument descriptions for display in help output.
|
|
624
|
+
*/
|
|
622
625
|
styleArgumentDescription(str) {
|
|
623
|
-
return C.gray(this.styleDescriptionText(str))
|
|
626
|
+
return C.gray(this.styleDescriptionText(str))
|
|
624
627
|
}
|
|
625
628
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
629
|
+
* Base style used by descriptions. Override in subclass to apply custom formatting.
|
|
630
|
+
*/
|
|
628
631
|
styleDescriptionText(str) {
|
|
629
|
-
return C.gray(str)
|
|
632
|
+
return C.gray(str)
|
|
630
633
|
}
|
|
631
634
|
/**
|
|
632
|
-
|
|
633
|
-
|
|
635
|
+
* Style option terms (flags) for display in help output.
|
|
636
|
+
*/
|
|
634
637
|
styleOptionTerm(str) {
|
|
635
|
-
return this.styleOptionText(str)
|
|
638
|
+
return this.styleOptionText(str)
|
|
636
639
|
}
|
|
637
640
|
/**
|
|
638
|
-
|
|
639
|
-
|
|
641
|
+
* Style subcommand terms for display in help output. Applies specific styling to different parts like options and arguments.
|
|
642
|
+
*/
|
|
640
643
|
styleSubcommandTerm(str) {
|
|
641
|
-
const res = str
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
644
|
+
const res = str
|
|
645
|
+
.split(' ')
|
|
646
|
+
.map((word) => {
|
|
647
|
+
if (word === this.usageDisplayOptionsAs) {
|
|
648
|
+
return C.dim(word)
|
|
649
|
+
}
|
|
650
|
+
if (word[0] === '[' || word[0] === '<') {
|
|
651
|
+
return C.dim(word)
|
|
652
|
+
}
|
|
653
|
+
return this.styleSubcommandText(word)
|
|
654
|
+
})
|
|
655
|
+
.join(' ')
|
|
656
|
+
const split = res.split('|')
|
|
651
657
|
if (split.length === 1) {
|
|
652
|
-
return res
|
|
658
|
+
return res
|
|
653
659
|
}
|
|
654
|
-
split[0] = C.green(split[0])
|
|
655
|
-
return split.join(
|
|
660
|
+
split[0] = C.green(split[0])
|
|
661
|
+
return split.join('|')
|
|
656
662
|
}
|
|
657
663
|
/**
|
|
658
|
-
|
|
659
|
-
|
|
664
|
+
* Style argument terms for display in help output.
|
|
665
|
+
*/
|
|
660
666
|
styleArgumentTerm(str) {
|
|
661
|
-
return this.styleArgumentText(str)
|
|
667
|
+
return this.styleArgumentText(str)
|
|
662
668
|
}
|
|
663
669
|
/**
|
|
664
|
-
|
|
665
|
-
|
|
670
|
+
* Base style used in terms and usage for options. Override in subclass to apply custom formatting.
|
|
671
|
+
*/
|
|
666
672
|
styleOptionText(str) {
|
|
667
|
-
return str
|
|
673
|
+
return str
|
|
668
674
|
}
|
|
669
675
|
/**
|
|
670
|
-
|
|
671
|
-
|
|
676
|
+
* Base style used in terms and usage for arguments. Override in subclass to apply custom formatting.
|
|
677
|
+
*/
|
|
672
678
|
styleArgumentText(str) {
|
|
673
|
-
return str
|
|
679
|
+
return str
|
|
674
680
|
}
|
|
675
681
|
/**
|
|
676
|
-
|
|
677
|
-
|
|
682
|
+
* Base style used in terms and usage for subcommands. Override in subclass to apply custom formatting.
|
|
683
|
+
*/
|
|
678
684
|
styleSubcommandText(str) {
|
|
679
|
-
return str
|
|
685
|
+
return str
|
|
680
686
|
}
|
|
681
687
|
/**
|
|
682
|
-
|
|
683
|
-
|
|
688
|
+
* Base style used in terms and usage for commands. Override in subclass to apply custom formatting.
|
|
689
|
+
*/
|
|
684
690
|
styleCommandText(str) {
|
|
685
|
-
return str
|
|
691
|
+
return str
|
|
686
692
|
}
|
|
687
693
|
/**
|
|
688
|
-
|
|
689
|
-
|
|
694
|
+
* Calculate the pad width from the maximum term length.
|
|
695
|
+
*/
|
|
690
696
|
padWidth() {
|
|
691
|
-
return Math.max(
|
|
697
|
+
return Math.max(
|
|
698
|
+
this.longestOptionTermLength(),
|
|
699
|
+
this.longestSubcommandTermLength(),
|
|
700
|
+
this.longestArgumentTermLength()
|
|
701
|
+
)
|
|
692
702
|
}
|
|
693
703
|
/**
|
|
694
|
-
|
|
695
|
-
|
|
704
|
+
* Detect manually wrapped and indented strings by checking for line break followed by whitespace.
|
|
705
|
+
*/
|
|
696
706
|
preformatted(str) {
|
|
697
|
-
return /\n[^\S\n\r]/.test(str)
|
|
707
|
+
return /\n[^\S\n\r]/.test(str)
|
|
698
708
|
}
|
|
699
709
|
/**
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
710
|
+
* Format the "item", which consists of a term and description. Pad the term and wrap the description, indenting the following lines.
|
|
711
|
+
*
|
|
712
|
+
* So "TTT", 5, "DDD DDDD DD DDD" might be formatted for this.helpWidth=17 like so:
|
|
713
|
+
* TTT DDD DDDD
|
|
714
|
+
* DD DDD
|
|
715
|
+
*/
|
|
706
716
|
formatItem(term, termWidth, description) {
|
|
707
|
-
const itemIndent = 2
|
|
708
|
-
const itemIndentStr =
|
|
717
|
+
const itemIndent = 2
|
|
718
|
+
const itemIndentStr = ' '.repeat(itemIndent)
|
|
709
719
|
if (!description) {
|
|
710
|
-
return itemIndentStr + term
|
|
720
|
+
return itemIndentStr + term
|
|
711
721
|
}
|
|
712
|
-
const paddedTerm = term.padEnd(termWidth + term.length - this.displayWidth(term))
|
|
713
|
-
const spacerWidth = 2
|
|
714
|
-
const helpWidth = this.helpWidth
|
|
715
|
-
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent
|
|
716
|
-
let formattedDescription
|
|
722
|
+
const paddedTerm = term.padEnd(termWidth + term.length - this.displayWidth(term))
|
|
723
|
+
const spacerWidth = 2
|
|
724
|
+
const helpWidth = this.helpWidth
|
|
725
|
+
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent
|
|
726
|
+
let formattedDescription
|
|
717
727
|
if (remainingWidth < this.minWidthToWrap || this.preformatted(description)) {
|
|
718
|
-
formattedDescription = description
|
|
728
|
+
formattedDescription = description
|
|
719
729
|
} else {
|
|
720
|
-
const wrappedDescription = this.boxWrap(description, remainingWidth)
|
|
721
|
-
formattedDescription = wrappedDescription.replaceAll(
|
|
722
|
-
|
|
730
|
+
const wrappedDescription = this.boxWrap(description, remainingWidth)
|
|
731
|
+
formattedDescription = wrappedDescription.replaceAll(
|
|
732
|
+
'\n',
|
|
733
|
+
`
|
|
734
|
+
${' '.repeat(termWidth + spacerWidth)}`
|
|
735
|
+
)
|
|
723
736
|
}
|
|
724
|
-
return
|
|
725
|
-
|
|
737
|
+
return (
|
|
738
|
+
itemIndentStr +
|
|
739
|
+
paddedTerm +
|
|
740
|
+
' '.repeat(spacerWidth) +
|
|
741
|
+
formattedDescription.replaceAll(
|
|
742
|
+
'\n',
|
|
743
|
+
`
|
|
744
|
+
${itemIndentStr}`
|
|
745
|
+
)
|
|
746
|
+
)
|
|
726
747
|
}
|
|
727
748
|
/**
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
749
|
+
* Wrap a string at whitespace, preserving existing line breaks.
|
|
750
|
+
* Wrapping is skipped if the width is less than `minWidthToWrap`.
|
|
751
|
+
*/
|
|
731
752
|
boxWrap(str, width) {
|
|
732
753
|
if (width < this.minWidthToWrap) {
|
|
733
|
-
return str
|
|
754
|
+
return str
|
|
734
755
|
}
|
|
735
|
-
const rawLines = str.split(/\r\n|\n/)
|
|
736
|
-
const chunkPattern = /\s*\S+/g
|
|
737
|
-
const wrappedLines = []
|
|
756
|
+
const rawLines = str.split(/\r\n|\n/)
|
|
757
|
+
const chunkPattern = /\s*\S+/g
|
|
758
|
+
const wrappedLines = []
|
|
738
759
|
rawLines.forEach((line) => {
|
|
739
|
-
const chunks = line.match(chunkPattern)
|
|
760
|
+
const chunks = line.match(chunkPattern)
|
|
740
761
|
if (chunks === null) {
|
|
741
|
-
wrappedLines.push(
|
|
742
|
-
return
|
|
762
|
+
wrappedLines.push('')
|
|
763
|
+
return
|
|
743
764
|
}
|
|
744
|
-
let sumChunks = [
|
|
745
|
-
|
|
746
|
-
];
|
|
747
|
-
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
765
|
+
let sumChunks = [chunks.shift()]
|
|
766
|
+
let sumWidth = this.displayWidth(sumChunks[0])
|
|
748
767
|
chunks.forEach((chunk) => {
|
|
749
|
-
const visibleWidth = this.displayWidth(chunk)
|
|
768
|
+
const visibleWidth = this.displayWidth(chunk)
|
|
750
769
|
if (sumWidth + visibleWidth <= width) {
|
|
751
|
-
sumChunks.push(chunk)
|
|
752
|
-
sumWidth += visibleWidth
|
|
753
|
-
return
|
|
770
|
+
sumChunks.push(chunk)
|
|
771
|
+
sumWidth += visibleWidth
|
|
772
|
+
return
|
|
754
773
|
}
|
|
755
|
-
wrappedLines.push(sumChunks.join(
|
|
756
|
-
const nextChunk = chunk.trimStart()
|
|
757
|
-
sumChunks = [
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
});
|
|
764
|
-
return wrappedLines.join("\n");
|
|
774
|
+
wrappedLines.push(sumChunks.join(''))
|
|
775
|
+
const nextChunk = chunk.trimStart()
|
|
776
|
+
sumChunks = [nextChunk]
|
|
777
|
+
sumWidth = this.displayWidth(nextChunk)
|
|
778
|
+
})
|
|
779
|
+
wrappedLines.push(sumChunks.join(''))
|
|
780
|
+
})
|
|
781
|
+
return wrappedLines.join('\n')
|
|
765
782
|
}
|
|
766
783
|
/**
|
|
767
|
-
|
|
768
|
-
|
|
784
|
+
* Generate the built-in help text.
|
|
785
|
+
*/
|
|
769
786
|
render() {
|
|
770
|
-
let output = [
|
|
771
|
-
|
|
772
|
-
""
|
|
773
|
-
];
|
|
774
|
-
const des = this.commandDescription();
|
|
787
|
+
let output = [`${this.styleTitle('Usage:')} ${this.styleUsage(this.commandUsage())}`, '']
|
|
788
|
+
const des = this.commandDescription()
|
|
775
789
|
if (des.length > 0) {
|
|
776
|
-
output = output.concat([
|
|
777
|
-
this.boxWrap(this.styleCommandDescription(des), this.helpWidth),
|
|
778
|
-
""
|
|
779
|
-
]);
|
|
790
|
+
output = output.concat([this.boxWrap(this.styleCommandDescription(des), this.helpWidth), ''])
|
|
780
791
|
}
|
|
781
|
-
const argumentList = this.visibleArguments().map((argument) =>
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
792
|
+
const argumentList = this.visibleArguments().map((argument) =>
|
|
793
|
+
this.formatItem(
|
|
794
|
+
this.styleArgumentTerm(this.argumentTerm(argument)),
|
|
795
|
+
this.padWidth(),
|
|
796
|
+
this.styleArgumentDescription(this.argumentDescription(argument))
|
|
797
|
+
)
|
|
798
|
+
)
|
|
799
|
+
output = output.concat(this.formatItemList('Arguments:', argumentList))
|
|
800
|
+
const optionGroups = this.groupItems(
|
|
801
|
+
this.cmd.options,
|
|
802
|
+
this.visibleOptions(),
|
|
803
|
+
(option) => option.group ?? 'Options:'
|
|
804
|
+
)
|
|
788
805
|
optionGroups.forEach((options, group) => {
|
|
789
|
-
const optionList = options.map((option) =>
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
806
|
+
const optionList = options.map((option) =>
|
|
807
|
+
this.formatItem(
|
|
808
|
+
this.styleOptionTerm(this.optionTerm(option)),
|
|
809
|
+
this.padWidth(),
|
|
810
|
+
this.styleOptionDescription(this.optionDescription(option))
|
|
811
|
+
)
|
|
812
|
+
)
|
|
813
|
+
output = output.concat(this.formatItemList(group, optionList))
|
|
814
|
+
})
|
|
815
|
+
const commandGroups = this.groupItems(
|
|
816
|
+
Object.values(this.cmd.commands),
|
|
817
|
+
this.visibleCommands(),
|
|
818
|
+
(sub) => sub.group || 'Commands:'
|
|
819
|
+
)
|
|
797
820
|
commandGroups.forEach((commands, group) => {
|
|
798
|
-
const commandList = commands.map((sub) =>
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
821
|
+
const commandList = commands.map((sub) =>
|
|
822
|
+
this.formatItem(
|
|
823
|
+
this.styleSubcommandTerm(this.subcommandTerm(sub)),
|
|
824
|
+
this.padWidth(),
|
|
825
|
+
this.styleSubcommandDescription(this.subcommandDescription(sub))
|
|
826
|
+
)
|
|
827
|
+
)
|
|
828
|
+
output = output.concat(this.formatItemList(group, commandList))
|
|
829
|
+
})
|
|
830
|
+
return output.join('\n')
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
_ts_decorate(
|
|
834
|
+
[
|
|
835
|
+
lazyProp,
|
|
836
|
+
_ts_metadata('design:type', Function),
|
|
837
|
+
_ts_metadata('design:paramtypes', []),
|
|
838
|
+
_ts_metadata('design:returntype', Array),
|
|
839
|
+
],
|
|
840
|
+
Help.prototype,
|
|
841
|
+
'visibleCommands',
|
|
842
|
+
null
|
|
843
|
+
)
|
|
844
|
+
_ts_decorate(
|
|
845
|
+
[
|
|
846
|
+
lazyProp,
|
|
847
|
+
_ts_metadata('design:type', Function),
|
|
848
|
+
_ts_metadata('design:paramtypes', []),
|
|
849
|
+
_ts_metadata('design:returntype', Array),
|
|
850
|
+
],
|
|
851
|
+
Help.prototype,
|
|
852
|
+
'visibleOptions',
|
|
853
|
+
null
|
|
854
|
+
)
|
|
855
|
+
_ts_decorate(
|
|
856
|
+
[
|
|
857
|
+
lazyProp,
|
|
858
|
+
_ts_metadata('design:type', Function),
|
|
859
|
+
_ts_metadata('design:paramtypes', []),
|
|
860
|
+
_ts_metadata('design:returntype', Array),
|
|
861
|
+
],
|
|
862
|
+
Help.prototype,
|
|
863
|
+
'visibleArguments',
|
|
864
|
+
null
|
|
865
|
+
)
|
|
866
|
+
_ts_decorate(
|
|
867
|
+
[
|
|
868
|
+
lazyProp,
|
|
869
|
+
_ts_metadata('design:type', Function),
|
|
870
|
+
_ts_metadata('design:paramtypes', []),
|
|
871
|
+
_ts_metadata('design:returntype', Number),
|
|
872
|
+
],
|
|
873
|
+
Help.prototype,
|
|
874
|
+
'longestSubcommandAliasLength',
|
|
875
|
+
null
|
|
876
|
+
)
|
|
877
|
+
_ts_decorate(
|
|
878
|
+
[
|
|
879
|
+
lazyProp,
|
|
880
|
+
_ts_metadata('design:type', Function),
|
|
881
|
+
_ts_metadata('design:paramtypes', []),
|
|
882
|
+
_ts_metadata('design:returntype', Number),
|
|
883
|
+
],
|
|
884
|
+
Help.prototype,
|
|
885
|
+
'longestSubcommandTermLength',
|
|
886
|
+
null
|
|
887
|
+
)
|
|
888
|
+
_ts_decorate(
|
|
889
|
+
[
|
|
890
|
+
lazyProp,
|
|
891
|
+
_ts_metadata('design:type', Function),
|
|
892
|
+
_ts_metadata('design:paramtypes', []),
|
|
893
|
+
_ts_metadata('design:returntype', Number),
|
|
894
|
+
],
|
|
895
|
+
Help.prototype,
|
|
896
|
+
'longestOptionTermLength',
|
|
897
|
+
null
|
|
898
|
+
)
|
|
899
|
+
_ts_decorate(
|
|
900
|
+
[
|
|
901
|
+
lazyProp,
|
|
902
|
+
_ts_metadata('design:type', Function),
|
|
903
|
+
_ts_metadata('design:paramtypes', []),
|
|
904
|
+
_ts_metadata('design:returntype', Number),
|
|
905
|
+
],
|
|
906
|
+
Help.prototype,
|
|
907
|
+
'longestArgumentTermLength',
|
|
908
|
+
null
|
|
909
|
+
)
|
|
910
|
+
_ts_decorate(
|
|
911
|
+
[
|
|
912
|
+
lazyProp,
|
|
913
|
+
_ts_metadata('design:type', Function),
|
|
914
|
+
_ts_metadata('design:paramtypes', []),
|
|
915
|
+
_ts_metadata('design:returntype', Number),
|
|
916
|
+
],
|
|
917
|
+
Help.prototype,
|
|
918
|
+
'padWidth',
|
|
919
|
+
null
|
|
920
|
+
)
|
|
854
921
|
|
|
855
922
|
// src/lib/internal/collectVariadicOptionValues.ts
|
|
856
923
|
function collectVariadicOptionValues(parsed, options) {
|
|
857
924
|
for (let i = 0; i < parsed.tokens.length; i++) {
|
|
858
|
-
const token = parsed.tokens[i]
|
|
859
|
-
if (token.kind !==
|
|
860
|
-
continue
|
|
925
|
+
const token = parsed.tokens[i]
|
|
926
|
+
if (token.kind !== 'option') {
|
|
927
|
+
continue
|
|
861
928
|
}
|
|
862
|
-
const def = options.find((o) =>
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
if (!def?.variadic || def.type !== "string") {
|
|
866
|
-
continue;
|
|
929
|
+
const def = options.find((o) => o.name === token.name)
|
|
930
|
+
if (!def?.variadic || def.type !== 'string') {
|
|
931
|
+
continue
|
|
867
932
|
}
|
|
868
|
-
const values = [
|
|
869
|
-
|
|
870
|
-
]
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
const
|
|
874
|
-
values.push(positionalToken.value);
|
|
875
|
-
const posIndex = parsed.positionals.indexOf(positionalToken.value);
|
|
933
|
+
const values = [token.value]
|
|
934
|
+
let j = i + 1
|
|
935
|
+
while (j < parsed.tokens.length && parsed.tokens[j].kind === 'positional') {
|
|
936
|
+
const positionalToken = parsed.tokens[j]
|
|
937
|
+
values.push(positionalToken.value)
|
|
938
|
+
const posIndex = parsed.positionals.indexOf(positionalToken.value)
|
|
876
939
|
if (posIndex !== -1) {
|
|
877
|
-
parsed.positionals.splice(posIndex, 1)
|
|
940
|
+
parsed.positionals.splice(posIndex, 1)
|
|
878
941
|
}
|
|
879
|
-
j
|
|
942
|
+
j++
|
|
880
943
|
}
|
|
881
|
-
Reflect.set(
|
|
882
|
-
|
|
883
|
-
|
|
944
|
+
Reflect.set(
|
|
945
|
+
parsed.values,
|
|
946
|
+
token.name,
|
|
947
|
+
values.filter((v) => v !== void 0)
|
|
948
|
+
)
|
|
884
949
|
}
|
|
885
950
|
}
|
|
886
|
-
__name(collectVariadicOptionValues,
|
|
951
|
+
__name(collectVariadicOptionValues, 'collectVariadicOptionValues')
|
|
887
952
|
|
|
888
953
|
// src/lib/internal/mergeOptionDefaults.ts
|
|
889
954
|
function mergeOptionDefaults(values, options) {
|
|
890
955
|
for (const option of options) {
|
|
891
956
|
if (option.defaultValue !== void 0 && option.name in values) {
|
|
892
|
-
values[option.name] ??= option.defaultValue
|
|
957
|
+
values[option.name] ??= option.defaultValue
|
|
893
958
|
}
|
|
894
959
|
}
|
|
895
960
|
}
|
|
896
|
-
__name(mergeOptionDefaults,
|
|
961
|
+
__name(mergeOptionDefaults, 'mergeOptionDefaults')
|
|
897
962
|
|
|
898
963
|
// src/lib/internal/normalizeArgv.ts
|
|
899
964
|
function normalizeArgv(argv, options) {
|
|
900
965
|
for (const o of options) {
|
|
901
966
|
if (o.long === o.name) {
|
|
902
|
-
continue
|
|
967
|
+
continue
|
|
903
968
|
}
|
|
904
969
|
argv = argv.map((a) => {
|
|
905
970
|
if (a === `--${o.long}`) {
|
|
906
|
-
return `--${o.name}
|
|
971
|
+
return `--${o.name}`
|
|
907
972
|
}
|
|
908
973
|
if (a === `--no-${o.long}`) {
|
|
909
|
-
return `--no-${o.name}
|
|
974
|
+
return `--no-${o.name}`
|
|
910
975
|
}
|
|
911
|
-
return a
|
|
912
|
-
})
|
|
976
|
+
return a
|
|
977
|
+
})
|
|
913
978
|
}
|
|
914
|
-
return argv
|
|
979
|
+
return argv
|
|
915
980
|
}
|
|
916
|
-
__name(normalizeArgv,
|
|
981
|
+
__name(normalizeArgv, 'normalizeArgv')
|
|
917
982
|
function resolveArguments(positionals, args) {
|
|
918
983
|
const result = args.map((arg, index) => {
|
|
919
984
|
if (arg.variadic) {
|
|
920
|
-
const remaining = positionals.slice(index)
|
|
921
|
-
return remaining.length > 0 ? remaining : arg.defaultValue
|
|
985
|
+
const remaining = positionals.slice(index)
|
|
986
|
+
return remaining.length > 0 ? remaining : arg.defaultValue
|
|
922
987
|
}
|
|
923
|
-
return positionals[index] ?? arg.defaultValue
|
|
924
|
-
})
|
|
988
|
+
return positionals[index] ?? arg.defaultValue
|
|
989
|
+
})
|
|
925
990
|
while (result.length && last(result) === void 0) {
|
|
926
|
-
result.pop()
|
|
991
|
+
result.pop()
|
|
927
992
|
}
|
|
928
|
-
return result
|
|
993
|
+
return result
|
|
929
994
|
}
|
|
930
|
-
__name(resolveArguments,
|
|
995
|
+
__name(resolveArguments, 'resolveArguments')
|
|
931
996
|
|
|
932
997
|
// ../fn/src/setName.ts
|
|
933
998
|
function setName(name, target) {
|
|
934
|
-
return Object.defineProperty(target,
|
|
935
|
-
value: typeof name ===
|
|
999
|
+
return Object.defineProperty(target, 'name', {
|
|
1000
|
+
value: typeof name === 'string' ? name : name.name,
|
|
936
1001
|
configurable: true,
|
|
937
|
-
enumerable: false
|
|
938
|
-
})
|
|
1002
|
+
enumerable: false,
|
|
1003
|
+
})
|
|
939
1004
|
}
|
|
940
|
-
__name(setName,
|
|
1005
|
+
__name(setName, 'setName')
|
|
941
1006
|
function createLogger(name) {
|
|
942
|
-
const NAME = name ? C.dim.cyan(name) : name
|
|
943
|
-
const START = [
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
].filter(Boolean)
|
|
947
|
-
const
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
const
|
|
952
|
-
NAME,
|
|
953
|
-
C.dim.gray("[INFO]")
|
|
954
|
-
].filter(Boolean);
|
|
955
|
-
const DONE = [
|
|
956
|
-
NAME,
|
|
957
|
-
C.dim.green("[DONE] ")
|
|
958
|
-
].filter(Boolean);
|
|
959
|
-
const WARN = [
|
|
960
|
-
NAME,
|
|
961
|
-
C.dim.yellow("[WARN] ")
|
|
962
|
-
].filter(Boolean);
|
|
963
|
-
const ERROR = [
|
|
964
|
-
NAME,
|
|
965
|
-
C.dim.red("[ERROR]")
|
|
966
|
-
].filter(Boolean);
|
|
967
|
-
const grayArgs = createColoredArgs(C.gray);
|
|
968
|
-
const cyanArgs = createColoredArgs(C.cyan);
|
|
969
|
-
const yellowArgs = createColoredArgs(C.yellow);
|
|
1007
|
+
const NAME = name ? C.dim.cyan(name) : name
|
|
1008
|
+
const START = [NAME, C.dim.gray('[START]')].filter(Boolean)
|
|
1009
|
+
const DEBUG = [NAME, C.dim.magenta('[DEBUG]')].filter(Boolean)
|
|
1010
|
+
const INFO = [NAME, C.dim.gray('[INFO]')].filter(Boolean)
|
|
1011
|
+
const DONE = [NAME, C.dim.green('[DONE] ')].filter(Boolean)
|
|
1012
|
+
const WARN = [NAME, C.dim.yellow('[WARN] ')].filter(Boolean)
|
|
1013
|
+
const ERROR = [NAME, C.dim.red('[ERROR]')].filter(Boolean)
|
|
1014
|
+
const grayArgs = createColoredArgs(C.gray)
|
|
1015
|
+
const cyanArgs = createColoredArgs(C.cyan)
|
|
1016
|
+
const yellowArgs = createColoredArgs(C.yellow)
|
|
970
1017
|
return {
|
|
971
|
-
start: /* @__PURE__ */ __name((...args) =>
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
}, "info"),
|
|
980
|
-
log: /* @__PURE__ */ __name((...args) => {
|
|
981
|
-
return console.log(...NAME ? [
|
|
982
|
-
NAME,
|
|
983
|
-
...args
|
|
984
|
-
] : args);
|
|
985
|
-
}, "log"),
|
|
986
|
-
warn: /* @__PURE__ */ __name((...args) => {
|
|
987
|
-
return console.warn(...WARN, ...yellowArgs(args));
|
|
988
|
-
}, "warn"),
|
|
989
|
-
debug: /* @__PURE__ */ __name((...args) => {
|
|
990
|
-
return console.debug(...DEBUG, ...cyanArgs(args));
|
|
991
|
-
}, "debug"),
|
|
992
|
-
error: /* @__PURE__ */ __name((...args) => {
|
|
993
|
-
return args.forEach((arg) => {
|
|
994
|
-
return console.error(...ERROR, arg);
|
|
995
|
-
});
|
|
996
|
-
}, "error")
|
|
997
|
-
};
|
|
1018
|
+
start: /* @__PURE__ */ __name((...args) => console.info(...START, ...args), 'start'),
|
|
1019
|
+
done: /* @__PURE__ */ __name((...args) => console.info(...DONE, ...args), 'done'),
|
|
1020
|
+
info: /* @__PURE__ */ __name((...args) => console.info(...INFO, ...grayArgs(args)), 'info'),
|
|
1021
|
+
log: /* @__PURE__ */ __name((...args) => console.log(...(NAME ? [NAME, ...args] : args)), 'log'),
|
|
1022
|
+
warn: /* @__PURE__ */ __name((...args) => console.warn(...WARN, ...yellowArgs(args)), 'warn'),
|
|
1023
|
+
debug: /* @__PURE__ */ __name((...args) => console.debug(...DEBUG, ...cyanArgs(args)), 'debug'),
|
|
1024
|
+
error: /* @__PURE__ */ __name((...args) => args.forEach((arg) => console.error(...ERROR, arg)), 'error'),
|
|
1025
|
+
}
|
|
998
1026
|
}
|
|
999
|
-
__name(createLogger,
|
|
1027
|
+
__name(createLogger, 'createLogger')
|
|
1000
1028
|
function createColoredArgs(colorFn) {
|
|
1001
|
-
return (args) =>
|
|
1002
|
-
|
|
1029
|
+
return (args) =>
|
|
1030
|
+
args.map((arg) => {
|
|
1003
1031
|
if (isString(arg)) {
|
|
1004
|
-
return arg === C.stripColor(arg) ? colorFn(arg) : arg
|
|
1032
|
+
return arg === C.stripColor(arg) ? colorFn(arg) : arg
|
|
1005
1033
|
} else if (isPrimitive(arg)) {
|
|
1006
|
-
return colorFn(String(arg))
|
|
1034
|
+
return colorFn(String(arg))
|
|
1007
1035
|
} else {
|
|
1008
|
-
return arg
|
|
1036
|
+
return arg
|
|
1009
1037
|
}
|
|
1010
|
-
})
|
|
1011
|
-
};
|
|
1038
|
+
})
|
|
1012
1039
|
}
|
|
1013
|
-
__name(createColoredArgs,
|
|
1040
|
+
__name(createColoredArgs, 'createColoredArgs')
|
|
1014
1041
|
function timer(arg, task) {
|
|
1015
|
-
const t0 = process.hrtime.bigint()
|
|
1016
|
-
const [name, description] = Array.isArray(arg) ? arg : [
|
|
1017
|
-
|
|
1018
|
-
""
|
|
1019
|
-
];
|
|
1020
|
-
const log = createLogger(name);
|
|
1042
|
+
const t0 = process.hrtime.bigint()
|
|
1043
|
+
const [name, description] = Array.isArray(arg) ? arg : [arg, '']
|
|
1044
|
+
const log = createLogger(name)
|
|
1021
1045
|
if (name && description) {
|
|
1022
|
-
log.start(description)
|
|
1046
|
+
log.start(description)
|
|
1023
1047
|
}
|
|
1024
|
-
const retval = task(log, name)
|
|
1048
|
+
const retval = task(log, name)
|
|
1025
1049
|
if (!isPromise(retval)) {
|
|
1026
|
-
return done(retval)
|
|
1050
|
+
return done(retval)
|
|
1027
1051
|
}
|
|
1028
|
-
return retval.then((result) =>
|
|
1029
|
-
return done(result);
|
|
1030
|
-
});
|
|
1052
|
+
return retval.then((result) => done(result))
|
|
1031
1053
|
function done(retval2) {
|
|
1032
1054
|
if (process.exitCode) {
|
|
1033
|
-
return retval2
|
|
1055
|
+
return retval2
|
|
1034
1056
|
}
|
|
1035
|
-
const ns = process.hrtime.bigint() - t0
|
|
1036
|
-
const ms3 = Math.floor(Number(ns) / 1e6)
|
|
1037
|
-
log.done(C.dim(humanizeDuration(ms3)))
|
|
1038
|
-
return retval2
|
|
1057
|
+
const ns = process.hrtime.bigint() - t0
|
|
1058
|
+
const ms3 = Math.floor(Number(ns) / 1e6)
|
|
1059
|
+
log.done(C.dim(humanizeDuration(ms3)))
|
|
1060
|
+
return retval2
|
|
1039
1061
|
}
|
|
1040
1062
|
}
|
|
1041
|
-
__name(timer,
|
|
1063
|
+
__name(timer, 'timer')
|
|
1042
1064
|
|
|
1043
1065
|
// src/lib/internal/validateParsed.ts
|
|
1044
1066
|
function validateParsed(args, optionValues, argDefs, optionDefs) {
|
|
1045
|
-
return argDefs
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
value
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
})
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
return `\`${c}\``;
|
|
1073
|
-
}).join(",")}]`;
|
|
1074
|
-
}
|
|
1075
|
-
})).filter((s) => {
|
|
1076
|
-
return s !== void 0;
|
|
1077
|
-
}).reduce((acc, curr) => {
|
|
1078
|
-
return (acc ?? []).concat(curr);
|
|
1079
|
-
}, void 0);
|
|
1067
|
+
return argDefs
|
|
1068
|
+
.map((def, index) => {
|
|
1069
|
+
const value = args[index]
|
|
1070
|
+
if (def.required && (def.variadic ? Array.isArray(value) && value.length === 0 : value === void 0)) {
|
|
1071
|
+
return `Missing argument [${index}] ${def.usage}`
|
|
1072
|
+
}
|
|
1073
|
+
if (def.choices && value !== void 0 && ![value].flat().every((v) => def.choices.includes(v))) {
|
|
1074
|
+
return `Invalid argument [${index}] ${def.usage}: Got \`${value}\`. Accepted values: [${def.choices.map((c) => `\`${c}\``).join(',')}]`
|
|
1075
|
+
}
|
|
1076
|
+
})
|
|
1077
|
+
.concat(
|
|
1078
|
+
entriesOf(optionValues).map(([key, value]) => {
|
|
1079
|
+
const def = optionDefs.find((o) => o.name === key)
|
|
1080
|
+
if (!def) {
|
|
1081
|
+
return `Unknown option --${key}`
|
|
1082
|
+
}
|
|
1083
|
+
if (
|
|
1084
|
+
def.choices &&
|
|
1085
|
+
value !== void 0 &&
|
|
1086
|
+
!(def.variadic ? value : [value]).every((v) => def.choices.includes(v))
|
|
1087
|
+
) {
|
|
1088
|
+
return `Invalid option value ${def.flags}: Got \`${value}\`. Accepted values: [${def.choices.map((c) => `\`${c}\``).join(',')}]`
|
|
1089
|
+
}
|
|
1090
|
+
})
|
|
1091
|
+
)
|
|
1092
|
+
.filter((s) => s !== void 0)
|
|
1093
|
+
.reduce((acc, curr) => (acc ?? []).concat(curr), void 0)
|
|
1080
1094
|
}
|
|
1081
|
-
__name(validateParsed,
|
|
1095
|
+
__name(validateParsed, 'validateParsed')
|
|
1082
1096
|
|
|
1083
1097
|
// src/lib/Command.ts
|
|
1084
1098
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
1085
|
-
var c = arguments.length,
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1099
|
+
var c = arguments.length,
|
|
1100
|
+
r = c < 3 ? target : desc === null ? (desc = Object.getOwnPropertyDescriptor(target, key)) : desc,
|
|
1101
|
+
d
|
|
1102
|
+
if (typeof Reflect === 'object' && typeof Reflect.decorate === 'function')
|
|
1103
|
+
r = Reflect.decorate(decorators, target, key, desc)
|
|
1104
|
+
else
|
|
1105
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
1106
|
+
if ((d = decorators[i])) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r
|
|
1107
|
+
return (c > 3 && r && Object.defineProperty(target, key, r), r)
|
|
1089
1108
|
}
|
|
1090
|
-
__name(_ts_decorate2,
|
|
1109
|
+
__name(_ts_decorate2, '_ts_decorate')
|
|
1091
1110
|
function _ts_metadata2(k, v) {
|
|
1092
|
-
if (typeof Reflect ===
|
|
1111
|
+
if (typeof Reflect === 'object' && typeof Reflect.metadata === 'function') return Reflect.metadata(k, v)
|
|
1093
1112
|
}
|
|
1094
|
-
__name(_ts_metadata2,
|
|
1113
|
+
__name(_ts_metadata2, '_ts_metadata')
|
|
1095
1114
|
var Command = class _Command {
|
|
1096
1115
|
static {
|
|
1097
|
-
__name(this,
|
|
1116
|
+
__name(this, 'Command')
|
|
1098
1117
|
}
|
|
1099
1118
|
/** parent command in the hierarchy, undefined for root command */
|
|
1100
|
-
parent
|
|
1119
|
+
parent
|
|
1101
1120
|
/** the command name used to invoke it */
|
|
1102
|
-
name
|
|
1121
|
+
name
|
|
1103
1122
|
/** semantic version string displayed by --version flag */
|
|
1104
|
-
version
|
|
1123
|
+
version
|
|
1105
1124
|
/** alternative names for invoking this command */
|
|
1106
|
-
aliases
|
|
1125
|
+
aliases
|
|
1107
1126
|
/** brief one-line description shown in command lists */
|
|
1108
|
-
summary
|
|
1127
|
+
summary
|
|
1109
1128
|
/** full description displayed in help text */
|
|
1110
|
-
description
|
|
1129
|
+
description
|
|
1111
1130
|
/** whether to exclude from help listings */
|
|
1112
|
-
hidden
|
|
1131
|
+
hidden
|
|
1113
1132
|
/** category for organizing related commands in help output */
|
|
1114
|
-
group
|
|
1133
|
+
group
|
|
1115
1134
|
/** positional arguments this command accepts */
|
|
1116
|
-
arguments
|
|
1135
|
+
arguments
|
|
1117
1136
|
/** cLI options (flags) this command recognizes */
|
|
1118
|
-
options
|
|
1137
|
+
options
|
|
1119
1138
|
/** subcommands registered with this command */
|
|
1120
|
-
commands
|
|
1139
|
+
commands
|
|
1121
1140
|
/** main action handler executed when command is invoked */
|
|
1122
|
-
action
|
|
1141
|
+
action
|
|
1123
1142
|
/** option-driven actions (e.g., --help, --version) executed when their conditions match */
|
|
1124
|
-
hooks
|
|
1143
|
+
hooks
|
|
1125
1144
|
constructor(name, parent) {
|
|
1126
|
-
this.name = name
|
|
1127
|
-
this.aliases = []
|
|
1128
|
-
this.description =
|
|
1129
|
-
this.arguments = []
|
|
1130
|
-
this.options = []
|
|
1131
|
-
this.commands = {}
|
|
1132
|
-
this.hooks = []
|
|
1133
|
-
Object.defineProperty(this,
|
|
1145
|
+
this.name = name
|
|
1146
|
+
this.aliases = []
|
|
1147
|
+
this.description = ''
|
|
1148
|
+
this.arguments = []
|
|
1149
|
+
this.options = []
|
|
1150
|
+
this.commands = {}
|
|
1151
|
+
this.hooks = []
|
|
1152
|
+
Object.defineProperty(this, 'parent', {
|
|
1134
1153
|
value: parent,
|
|
1135
|
-
enumerable: false
|
|
1136
|
-
})
|
|
1154
|
+
enumerable: false,
|
|
1155
|
+
})
|
|
1137
1156
|
if (!parent) {
|
|
1138
|
-
this.addOption(
|
|
1139
|
-
description:
|
|
1140
|
-
}).addOptionHook(
|
|
1141
|
-
console.debug(
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1157
|
+
this.addOption('-D, --debug', {
|
|
1158
|
+
description: 'Display debug information',
|
|
1159
|
+
}).addOptionHook('debug', ({ cmd, ...data }) => {
|
|
1160
|
+
console.debug(
|
|
1161
|
+
inspect(cmd, {
|
|
1162
|
+
depth: 1,
|
|
1163
|
+
colors: true,
|
|
1164
|
+
})
|
|
1165
|
+
)
|
|
1166
|
+
console.debug(
|
|
1167
|
+
inspect(data, {
|
|
1168
|
+
depth: 3,
|
|
1169
|
+
colors: true,
|
|
1170
|
+
})
|
|
1171
|
+
)
|
|
1172
|
+
})
|
|
1173
|
+
this.addOption('-h, --help', {
|
|
1174
|
+
description: 'Display help information',
|
|
1175
|
+
}).addOptionHook('help', ({ cmd }) => {
|
|
1176
|
+
console.log(cmd.renderHelp())
|
|
1177
|
+
process.exitCode = 0
|
|
1178
|
+
})
|
|
1156
1179
|
}
|
|
1157
1180
|
}
|
|
1158
1181
|
get help() {
|
|
1159
|
-
return new Help(this)
|
|
1182
|
+
return new Help(this)
|
|
1160
1183
|
}
|
|
1161
1184
|
/** configure how the help is rendered */
|
|
1162
1185
|
helpConfiguration(cb) {
|
|
1163
|
-
const help = this.help
|
|
1164
|
-
cb?.(help)
|
|
1165
|
-
return this
|
|
1186
|
+
const help = this.help
|
|
1187
|
+
cb?.(help)
|
|
1188
|
+
return this
|
|
1166
1189
|
}
|
|
1167
1190
|
/** renders formatted help text using provided help definition */
|
|
1168
1191
|
renderHelp(config = {}) {
|
|
1169
|
-
const result = this.help.render()
|
|
1170
|
-
return config.noColor ? C.stripColor(result) : result
|
|
1192
|
+
const result = this.help.render()
|
|
1193
|
+
return config.noColor ? C.stripColor(result) : result
|
|
1171
1194
|
}
|
|
1172
1195
|
/** sets the command name */
|
|
1173
1196
|
setName(name) {
|
|
1174
|
-
this.name = name
|
|
1197
|
+
this.name = name
|
|
1175
1198
|
}
|
|
1176
1199
|
/** sets command aliases, flattening nested arrays */
|
|
1177
1200
|
setAliases(...aliases) {
|
|
1178
|
-
this.aliases = []
|
|
1179
|
-
this.addAliases(...aliases)
|
|
1180
|
-
return this
|
|
1201
|
+
this.aliases = []
|
|
1202
|
+
this.addAliases(...aliases)
|
|
1203
|
+
return this
|
|
1181
1204
|
}
|
|
1182
1205
|
/** adds aliases to existing ones */
|
|
1183
1206
|
addAliases(...aliases) {
|
|
1184
|
-
const taken = this.parent ? valuesOf(this.parent.commands).flatMap((c) =>
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
});
|
|
1198
|
-
this.aliases.sort((a, b) => {
|
|
1199
|
-
return a.length - b.length;
|
|
1200
|
-
});
|
|
1201
|
-
return this;
|
|
1207
|
+
const taken = this.parent ? valuesOf(this.parent.commands).flatMap((c) => [c.name, ...c.aliases]) : []
|
|
1208
|
+
uniq(aliases.flat())
|
|
1209
|
+
.filter((a) => !this.aliases.includes(a) && a !== this.name)
|
|
1210
|
+
.forEach((a) => {
|
|
1211
|
+
if (taken.includes(a)) {
|
|
1212
|
+
throw new Error(
|
|
1213
|
+
`Alias "${a}" is already used by a sibling command: ${findCommand(this.parent, a)?.name}`
|
|
1214
|
+
)
|
|
1215
|
+
}
|
|
1216
|
+
this.aliases.push(a)
|
|
1217
|
+
})
|
|
1218
|
+
this.aliases.sort((a, b) => a.length - b.length)
|
|
1219
|
+
return this
|
|
1202
1220
|
}
|
|
1203
1221
|
/** sets the command version */
|
|
1204
1222
|
setVersion(version) {
|
|
1205
|
-
this.version = version
|
|
1206
|
-
if (findOption(this,
|
|
1207
|
-
return this
|
|
1223
|
+
this.version = version
|
|
1224
|
+
if (findOption(this, 'version')) {
|
|
1225
|
+
return this
|
|
1208
1226
|
}
|
|
1209
|
-
return this.addOption(
|
|
1210
|
-
description:
|
|
1211
|
-
}).addOptionHook(
|
|
1212
|
-
console.log(getCommandAndAncestors(cmd).find((c) =>
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
process.exitCode = 0;
|
|
1216
|
-
});
|
|
1227
|
+
return this.addOption('-V, --version', {
|
|
1228
|
+
description: 'Display semver version',
|
|
1229
|
+
}).addOptionHook('version', ({ cmd }) => {
|
|
1230
|
+
console.log(getCommandAndAncestors(cmd).find((c) => c.version)?.version)
|
|
1231
|
+
process.exitCode = 0
|
|
1232
|
+
})
|
|
1217
1233
|
}
|
|
1218
1234
|
/** sets the command summary */
|
|
1219
1235
|
setSummary(summary) {
|
|
1220
|
-
this.summary = summary
|
|
1221
|
-
return this
|
|
1236
|
+
this.summary = summary
|
|
1237
|
+
return this
|
|
1222
1238
|
}
|
|
1223
1239
|
/** sets command description, joining variadic lines */
|
|
1224
1240
|
setDescription(...lines) {
|
|
1225
|
-
this.description = lines.join(
|
|
1226
|
-
return this
|
|
1241
|
+
this.description = lines.join('\n')
|
|
1242
|
+
return this
|
|
1227
1243
|
}
|
|
1228
1244
|
/** sets whether command is hidden from help */
|
|
1229
1245
|
setHidden(hidden = true) {
|
|
1230
|
-
this.hidden = hidden
|
|
1231
|
-
return this
|
|
1246
|
+
this.hidden = hidden
|
|
1247
|
+
return this
|
|
1232
1248
|
}
|
|
1233
1249
|
/** sets the command group for help organization */
|
|
1234
1250
|
setGroup(group) {
|
|
1235
|
-
this.group = group
|
|
1236
|
-
return this
|
|
1251
|
+
this.group = group
|
|
1252
|
+
return this
|
|
1237
1253
|
}
|
|
1238
1254
|
/** add a subcommand and return the subcommand. All options are inherited by the subcommand. */
|
|
1239
|
-
|
|
1255
|
+
|
|
1240
1256
|
command(name, cb) {
|
|
1241
1257
|
if (this.arguments.length) {
|
|
1242
|
-
throw new Error(
|
|
1258
|
+
throw new Error(
|
|
1259
|
+
`Cannot add subcommand "${name}" to "${this.name}" because it already has arguments defined.`
|
|
1260
|
+
)
|
|
1243
1261
|
}
|
|
1244
|
-
const sub = this.createSubcommand(name)
|
|
1245
|
-
const inherit = this.options
|
|
1246
|
-
sub.options.push(...inherit)
|
|
1247
|
-
const inheritHooks = this.hooks.filter((t) =>
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
});
|
|
1251
|
-
});
|
|
1252
|
-
sub.hooks.push(...inheritHooks);
|
|
1253
|
-
const taken = valuesOf(this.commands).flatMap((c) => {
|
|
1254
|
-
return [
|
|
1255
|
-
c.name,
|
|
1256
|
-
...c.aliases
|
|
1257
|
-
];
|
|
1258
|
-
});
|
|
1262
|
+
const sub = this.createSubcommand(name)
|
|
1263
|
+
const inherit = this.options
|
|
1264
|
+
sub.options.push(...inherit)
|
|
1265
|
+
const inheritHooks = this.hooks.filter((t) => inherit.some((i) => i.name === t.name))
|
|
1266
|
+
sub.hooks.push(...inheritHooks)
|
|
1267
|
+
const taken = valuesOf(this.commands).flatMap((c) => [c.name, ...c.aliases])
|
|
1259
1268
|
if (taken.includes(name)) {
|
|
1260
|
-
throw new Error(
|
|
1261
|
-
|
|
1262
|
-
|
|
1269
|
+
throw new Error(
|
|
1270
|
+
`Command name "${getCommandAndAncestors(sub).map((c) => c.name)}" is already used by this command or its aliases: ${taken.join(', ')}`
|
|
1271
|
+
)
|
|
1263
1272
|
}
|
|
1264
|
-
const kebab = kebabCase(name)
|
|
1265
|
-
const words = kebab.split(
|
|
1266
|
-
const initials = words.map((s) =>
|
|
1267
|
-
return s[0];
|
|
1268
|
-
}).join("");
|
|
1273
|
+
const kebab = kebabCase(name)
|
|
1274
|
+
const words = kebab.split('-')
|
|
1275
|
+
const initials = words.map((s) => s[0]).join('')
|
|
1269
1276
|
if (taken.includes(initials)) {
|
|
1270
|
-
const initials2 = words.map((s) =>
|
|
1271
|
-
return s[0] + s[1];
|
|
1272
|
-
}).join("");
|
|
1277
|
+
const initials2 = words.map((s) => s[0] + s[1]).join('')
|
|
1273
1278
|
if (!taken.includes(initials2)) {
|
|
1274
|
-
sub.addAliases(initials2)
|
|
1279
|
+
sub.addAliases(initials2)
|
|
1275
1280
|
}
|
|
1276
1281
|
} else {
|
|
1277
|
-
sub.addAliases(initials)
|
|
1282
|
+
sub.addAliases(initials)
|
|
1278
1283
|
}
|
|
1279
|
-
this.commands[name] = sub
|
|
1280
|
-
return cb ? cb(sub, this) : sub
|
|
1284
|
+
this.commands[name] = sub
|
|
1285
|
+
return cb ? cb(sub, this) : sub
|
|
1281
1286
|
}
|
|
1282
1287
|
/** add a subcommand and return the subcommand. All options are inherited by the subcommand. */
|
|
1283
|
-
|
|
1288
|
+
|
|
1284
1289
|
addCommand(name, cb) {
|
|
1285
|
-
this.command(name, cb)
|
|
1286
|
-
return this
|
|
1290
|
+
this.command(name, cb)
|
|
1291
|
+
return this
|
|
1287
1292
|
}
|
|
1288
1293
|
// implementation
|
|
1289
1294
|
addArgument(usage, options = {}) {
|
|
1290
1295
|
if (!/^<(.*?)>$|^\[(.*?)]$/.test(usage)) {
|
|
1291
|
-
throw new Error(`Invalid argument format: ${usage}`)
|
|
1296
|
+
throw new Error(`Invalid argument format: ${usage}`)
|
|
1292
1297
|
}
|
|
1293
|
-
const name = usage.slice(1, -1).replace(/\.{3}$/,
|
|
1294
|
-
const required = usage.startsWith(
|
|
1295
|
-
const variadic = usage.slice(0, -1).endsWith(
|
|
1296
|
-
const prevArg = this.arguments[this.arguments.length - 1]
|
|
1298
|
+
const name = usage.slice(1, -1).replace(/\.{3}$/, '')
|
|
1299
|
+
const required = usage.startsWith('<')
|
|
1300
|
+
const variadic = usage.slice(0, -1).endsWith('...')
|
|
1301
|
+
const prevArg = this.arguments[this.arguments.length - 1]
|
|
1297
1302
|
if (prevArg?.variadic) {
|
|
1298
|
-
throw new Error(`Cannot add argument ${usage} after variadic argument ${prevArg.usage}`)
|
|
1303
|
+
throw new Error(`Cannot add argument ${usage} after variadic argument ${prevArg.usage}`)
|
|
1299
1304
|
}
|
|
1300
1305
|
if (required && prevArg && !prevArg?.required) {
|
|
1301
|
-
throw new Error(`Cannot add required argument ${usage} after optional argument ${prevArg?.usage}`)
|
|
1306
|
+
throw new Error(`Cannot add required argument ${usage} after optional argument ${prevArg?.usage}`)
|
|
1302
1307
|
}
|
|
1303
1308
|
if (required && prevArg?.defaultValue) {
|
|
1304
|
-
throw new Error(
|
|
1309
|
+
throw new Error(
|
|
1310
|
+
`Cannot add required argument ${usage} after optional argument with default value ${prevArg.usage}`
|
|
1311
|
+
)
|
|
1305
1312
|
}
|
|
1306
1313
|
const arg = {
|
|
1307
1314
|
usage,
|
|
1308
1315
|
name,
|
|
1309
1316
|
required,
|
|
1310
1317
|
variadic,
|
|
1311
|
-
...options
|
|
1312
|
-
}
|
|
1318
|
+
...options,
|
|
1319
|
+
}
|
|
1313
1320
|
if (variadic && !arg.defaultValue) {
|
|
1314
|
-
arg.defaultValue = []
|
|
1321
|
+
arg.defaultValue = []
|
|
1315
1322
|
}
|
|
1316
|
-
this.arguments.push(arg)
|
|
1317
|
-
return this
|
|
1323
|
+
this.arguments.push(arg)
|
|
1324
|
+
return this
|
|
1318
1325
|
}
|
|
1319
1326
|
/**
|
|
1320
|
-
|
|
1321
|
-
|
|
1327
|
+
* adds command-line option with type inference. Parses format: `-s, --long [<value>|[value]|<value...>|[value...]]`
|
|
1328
|
+
*/
|
|
1322
1329
|
addOption(flags, opts = {}) {
|
|
1323
|
-
const ins = {}
|
|
1324
|
-
const { short, long, name, argName } = parseOptionFlags(flags)
|
|
1330
|
+
const ins = {}
|
|
1331
|
+
const { short, long, name, argName } = parseOptionFlags(flags)
|
|
1325
1332
|
for (const opt of this.options) {
|
|
1326
1333
|
if (opt.long === long) {
|
|
1327
|
-
throw new Error(`Option long name, --${long} already in use by: ${opt.flags}`)
|
|
1334
|
+
throw new Error(`Option long name, --${long} already in use by: ${opt.flags}`)
|
|
1328
1335
|
}
|
|
1329
1336
|
if (opt.short === short) {
|
|
1330
|
-
throw new Error(`Option short name, -${short} already in use by: ${opt.flags}`)
|
|
1337
|
+
throw new Error(`Option short name, -${short} already in use by: ${opt.flags}`)
|
|
1331
1338
|
}
|
|
1332
1339
|
}
|
|
1333
|
-
ins.flags = flags
|
|
1334
|
-
ins.short = short
|
|
1335
|
-
ins.long = long
|
|
1336
|
-
ins.name = name
|
|
1337
|
-
ins.description =
|
|
1340
|
+
ins.flags = flags
|
|
1341
|
+
ins.short = short
|
|
1342
|
+
ins.long = long
|
|
1343
|
+
ins.name = name
|
|
1344
|
+
ins.description = ''
|
|
1338
1345
|
if (argName) {
|
|
1339
|
-
ins.type =
|
|
1340
|
-
ins.argName = argName
|
|
1341
|
-
if (flags.endsWith(
|
|
1342
|
-
if (flags.endsWith(
|
|
1343
|
-
ins.required = true
|
|
1344
|
-
ins.variadic = true
|
|
1346
|
+
ins.type = 'string'
|
|
1347
|
+
ins.argName = argName
|
|
1348
|
+
if (flags.endsWith('>')) {
|
|
1349
|
+
if (flags.endsWith('...>')) {
|
|
1350
|
+
ins.required = true
|
|
1351
|
+
ins.variadic = true
|
|
1345
1352
|
} else {
|
|
1346
|
-
ins.required = true
|
|
1353
|
+
ins.required = true
|
|
1347
1354
|
}
|
|
1348
|
-
} else if (flags.endsWith(
|
|
1349
|
-
ins.variadic = true
|
|
1350
|
-
ins.defaultValue = opts.defaultValue ?? []
|
|
1355
|
+
} else if (flags.endsWith(']') && flags.endsWith('...]')) {
|
|
1356
|
+
ins.variadic = true
|
|
1357
|
+
ins.defaultValue = opts.defaultValue ?? []
|
|
1351
1358
|
}
|
|
1352
1359
|
} else {
|
|
1353
|
-
ins.type =
|
|
1360
|
+
ins.type = 'boolean'
|
|
1354
1361
|
}
|
|
1355
1362
|
for (const [key, value] of Object.entries(opts)) {
|
|
1356
1363
|
if (value !== void 0) {
|
|
1357
|
-
Reflect.set(ins, key, value)
|
|
1364
|
+
Reflect.set(ins, key, value)
|
|
1358
1365
|
}
|
|
1359
1366
|
}
|
|
1360
|
-
if (ins.env && ins.defaultValue === void 0 && typeof process.env[ins.env] ===
|
|
1361
|
-
ins.required = false
|
|
1362
|
-
ins.flags = ins.flags.replace(
|
|
1363
|
-
if (ins.type ===
|
|
1364
|
-
ins.defaultValue = /^(t(rue)?|y(es)?|1)$/i.test(process.env[ins.env])
|
|
1367
|
+
if (ins.env && ins.defaultValue === void 0 && typeof process.env[ins.env] === 'string') {
|
|
1368
|
+
ins.required = false
|
|
1369
|
+
ins.flags = ins.flags.replace('<', '[').replace('>', ']')
|
|
1370
|
+
if (ins.type === 'boolean') {
|
|
1371
|
+
ins.defaultValue = /^(t(rue)?|y(es)?|1)$/i.test(process.env[ins.env])
|
|
1365
1372
|
} else if (ins.variadic) {
|
|
1366
|
-
ins.defaultValue = process.env[ins.env]
|
|
1367
|
-
|
|
1368
|
-
|
|
1373
|
+
ins.defaultValue = process.env[ins.env]
|
|
1374
|
+
.replace(/\[]/, '')
|
|
1375
|
+
.split(',')
|
|
1376
|
+
.map((v) => v.trim())
|
|
1369
1377
|
} else {
|
|
1370
|
-
ins.defaultValue = process.env[ins.env]
|
|
1378
|
+
ins.defaultValue = process.env[ins.env]
|
|
1371
1379
|
}
|
|
1372
1380
|
}
|
|
1373
|
-
this.options.push(ins)
|
|
1374
|
-
return this
|
|
1381
|
+
this.options.push(ins)
|
|
1382
|
+
return this
|
|
1375
1383
|
}
|
|
1376
1384
|
/**
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1385
|
+
* register an action to be invoked when an option is set to true or string value.
|
|
1386
|
+
*
|
|
1387
|
+
* Hooks execute in addition to or instead of the main action handler,
|
|
1388
|
+
* allowing for option-driven behavior. For example, `--help` and `--version`
|
|
1389
|
+
* are implemented as hooks.
|
|
1390
|
+
*/
|
|
1383
1391
|
addOptionHook(optionName, action) {
|
|
1384
|
-
const def = findOption(this, optionName)
|
|
1392
|
+
const def = findOption(this, optionName)
|
|
1385
1393
|
if (!def.group && /process\.exitCode ?= ?.+;?\s*}$/.test(action.toString())) {
|
|
1386
|
-
def.group =
|
|
1394
|
+
def.group = 'Command Options'
|
|
1387
1395
|
}
|
|
1388
1396
|
this.hooks.push({
|
|
1389
1397
|
name: optionName,
|
|
1390
|
-
predicate: setName(
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1398
|
+
predicate: setName(
|
|
1399
|
+
`has${upperFirst(optionName)}`,
|
|
1400
|
+
({ opts }) =>
|
|
1401
|
+
opts[optionName] !== void 0 &&
|
|
1402
|
+
opts[optionName] !== false &&
|
|
1403
|
+
!(Array.isArray(opts[optionName]) && opts[optionName].length === 0)
|
|
1404
|
+
),
|
|
1405
|
+
action: setName(optionName, action),
|
|
1406
|
+
})
|
|
1407
|
+
return this
|
|
1396
1408
|
}
|
|
1397
1409
|
/** parses command-line arguments with subcommand support and type-safe validation. */
|
|
1398
1410
|
parseArgv(argv = process.argv.slice(2)) {
|
|
1399
|
-
const sub = findCommand(this, argv[0])
|
|
1411
|
+
const sub = findCommand(this, argv[0])
|
|
1400
1412
|
if (sub) {
|
|
1401
|
-
return sub.parseArgv(argv.slice(1))
|
|
1413
|
+
return sub.parseArgv(argv.slice(1))
|
|
1402
1414
|
}
|
|
1403
|
-
argv = normalizeArgv(argv, this.options)
|
|
1415
|
+
argv = normalizeArgv(argv, this.options)
|
|
1404
1416
|
const parsed = parseArgs({
|
|
1405
1417
|
args: argv,
|
|
1406
|
-
options: Object.fromEntries(
|
|
1407
|
-
|
|
1418
|
+
options: Object.fromEntries(
|
|
1419
|
+
this.options.map((o) => [
|
|
1408
1420
|
o.name,
|
|
1409
1421
|
{
|
|
1410
1422
|
type: o.type,
|
|
1411
1423
|
short: o.short,
|
|
1412
1424
|
default: o.defaultValue,
|
|
1413
|
-
multiple: !!o.variadic
|
|
1414
|
-
}
|
|
1415
|
-
]
|
|
1416
|
-
|
|
1425
|
+
multiple: !!o.variadic,
|
|
1426
|
+
},
|
|
1427
|
+
])
|
|
1428
|
+
),
|
|
1417
1429
|
allowPositionals: true,
|
|
1418
1430
|
tokens: true,
|
|
1419
1431
|
strict: false,
|
|
1420
|
-
allowNegative: true
|
|
1421
|
-
})
|
|
1422
|
-
collectVariadicOptionValues(parsed, this.options)
|
|
1423
|
-
mergeOptionDefaults(parsed.values, this.options)
|
|
1424
|
-
parsed.values = objSortKeys(parsed.values, (a, b) =>
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
const args = resolveArguments(parsed.positionals, this.arguments)
|
|
1428
|
-
const opts = filterObject(parsed.values, (value) =>
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
const errors = validateParsed(args, parsed.values, this.arguments, this.options);
|
|
1432
|
-
const path = getCommandAncestors(this).map((c) => {
|
|
1433
|
-
return c.name;
|
|
1434
|
-
});
|
|
1432
|
+
allowNegative: true,
|
|
1433
|
+
})
|
|
1434
|
+
collectVariadicOptionValues(parsed, this.options)
|
|
1435
|
+
mergeOptionDefaults(parsed.values, this.options)
|
|
1436
|
+
parsed.values = objSortKeys(parsed.values, (a, b) =>
|
|
1437
|
+
a[1] === false ? 1 : b[1] === false ? -1 : a[1] === true ? 1 : b[1] === true ? -1 : 0
|
|
1438
|
+
)
|
|
1439
|
+
const args = resolveArguments(parsed.positionals, this.arguments)
|
|
1440
|
+
const opts = filterObject(parsed.values, (value) => value !== void 0)
|
|
1441
|
+
const errors = validateParsed(args, parsed.values, this.arguments, this.options)
|
|
1442
|
+
const path = getCommandAncestors(this).map((c) => c.name)
|
|
1435
1443
|
const data = {
|
|
1436
1444
|
path,
|
|
1437
1445
|
name: this.name,
|
|
@@ -1439,31 +1447,21 @@ var Command = class _Command {
|
|
|
1439
1447
|
args,
|
|
1440
1448
|
opts,
|
|
1441
1449
|
errors,
|
|
1442
|
-
cmd: this
|
|
1443
|
-
}
|
|
1444
|
-
const hooks = this.hooks.filter((t) =>
|
|
1445
|
-
return t.predicate(data);
|
|
1446
|
-
});
|
|
1450
|
+
cmd: this,
|
|
1451
|
+
}
|
|
1452
|
+
const hooks = this.hooks.filter((t) => t.predicate(data))
|
|
1447
1453
|
const execute = /* @__PURE__ */ __name(async () => {
|
|
1448
1454
|
for (const hook of hooks) {
|
|
1449
|
-
await hook.action(data)
|
|
1455
|
+
await hook.action(data)
|
|
1450
1456
|
if (process.exitCode !== void 0) {
|
|
1451
|
-
return
|
|
1457
|
+
return
|
|
1452
1458
|
}
|
|
1453
1459
|
}
|
|
1454
|
-
await timer([
|
|
1455
|
-
[
|
|
1456
|
-
...path,
|
|
1457
|
-
this.name
|
|
1458
|
-
].join(" "),
|
|
1459
|
-
this.description
|
|
1460
|
-
], async (logger) => {
|
|
1460
|
+
await timer([[...path, this.name].join(' '), this.description], async (logger) => {
|
|
1461
1461
|
if (errors) {
|
|
1462
|
-
errors.forEach((msg) =>
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
process.exitCode = 1;
|
|
1466
|
-
return;
|
|
1462
|
+
errors.forEach((msg) => logger.error(C.red(msg)))
|
|
1463
|
+
process.exitCode = 1
|
|
1464
|
+
return
|
|
1467
1465
|
}
|
|
1468
1466
|
if (this.action) {
|
|
1469
1467
|
return await this.action(...args, opts, {
|
|
@@ -1471,35 +1469,40 @@ var Command = class _Command {
|
|
|
1471
1469
|
args,
|
|
1472
1470
|
opts,
|
|
1473
1471
|
cmd: this,
|
|
1474
|
-
logger
|
|
1475
|
-
})
|
|
1472
|
+
logger,
|
|
1473
|
+
})
|
|
1476
1474
|
}
|
|
1477
|
-
console.log(this.renderHelp())
|
|
1478
|
-
})
|
|
1479
|
-
},
|
|
1475
|
+
console.log(this.renderHelp())
|
|
1476
|
+
})
|
|
1477
|
+
}, 'execute')
|
|
1480
1478
|
return {
|
|
1481
1479
|
...data,
|
|
1482
1480
|
hooks,
|
|
1483
|
-
execute
|
|
1484
|
-
}
|
|
1481
|
+
execute,
|
|
1482
|
+
}
|
|
1485
1483
|
}
|
|
1486
1484
|
/**
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1485
|
+
* sets the main action handler for this command, which is executed after any matching option hooks when the command is invoked.
|
|
1486
|
+
* The handler receives parsed arguments and options with correct typings.
|
|
1487
|
+
*/
|
|
1490
1488
|
setAction(fn) {
|
|
1491
|
-
this.action = setName(this.name, fn)
|
|
1492
|
-
return this
|
|
1489
|
+
this.action = setName(this.name, fn)
|
|
1490
|
+
return this
|
|
1493
1491
|
}
|
|
1494
1492
|
/** returns a new Command instance. Override this method in subclasses. */
|
|
1495
1493
|
createSubcommand(name) {
|
|
1496
|
-
return new _Command(name, this)
|
|
1497
|
-
}
|
|
1498
|
-
}
|
|
1499
|
-
_ts_decorate2(
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1494
|
+
return new _Command(name, this)
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
_ts_decorate2(
|
|
1498
|
+
[
|
|
1499
|
+
lazyProp,
|
|
1500
|
+
_ts_metadata2('design:type', typeof Help === 'undefined' ? Object : Help),
|
|
1501
|
+
_ts_metadata2('design:paramtypes', []),
|
|
1502
|
+
],
|
|
1503
|
+
Command.prototype,
|
|
1504
|
+
'help',
|
|
1505
|
+
null
|
|
1506
|
+
)
|
|
1504
1507
|
|
|
1505
|
-
export { Command, Help, findCommand, findOption, getCommandAncestors, getCommandAndAncestors, parseOptionFlags }
|
|
1508
|
+
export { Command, Help, findCommand, findOption, getCommandAncestors, getCommandAndAncestors, parseOptionFlags }
|