breadc 0.6.2 → 0.6.5
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/dist/index.cjs +68 -22
- package/dist/index.d.ts +6 -6
- package/dist/index.mjs +56 -21
- package/package.json +13 -9
package/dist/index.cjs
CHANGED
|
@@ -5,8 +5,20 @@ const kolorist = require('kolorist');
|
|
|
5
5
|
|
|
6
6
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
7
7
|
|
|
8
|
+
function _interopNamespace(e) {
|
|
9
|
+
if (e && e.__esModule) return e;
|
|
10
|
+
const n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
for (const k in e) {
|
|
13
|
+
n[k] = e[k];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
n["default"] = e;
|
|
17
|
+
return n;
|
|
18
|
+
}
|
|
19
|
+
|
|
8
20
|
const minimist__default = /*#__PURE__*/_interopDefaultLegacy(minimist);
|
|
9
|
-
const
|
|
21
|
+
const kolorist__namespace = /*#__PURE__*/_interopNamespace(kolorist);
|
|
10
22
|
|
|
11
23
|
function twoColumn(texts, split = " ") {
|
|
12
24
|
const left = padRight(texts.map((t) => t[0]));
|
|
@@ -82,7 +94,9 @@ const _Command = class {
|
|
|
82
94
|
{
|
|
83
95
|
const restArgs = this.arguments.findIndex((a) => a.startsWith("[..."));
|
|
84
96
|
if (restArgs !== -1 && restArgs !== this.arguments.length - 1) {
|
|
85
|
-
this.logger.warn(
|
|
97
|
+
this.logger.warn(
|
|
98
|
+
`Expand arguments ${this.arguments[restArgs]} should be placed at the last position`
|
|
99
|
+
);
|
|
86
100
|
}
|
|
87
101
|
if (pieces.length > _Command.MaxDep) {
|
|
88
102
|
this.logger.warn(`Command format string "${format}" is too long`);
|
|
@@ -156,7 +170,9 @@ const _Command = class {
|
|
|
156
170
|
}
|
|
157
171
|
} else {
|
|
158
172
|
if (this.arguments[i].startsWith("<")) {
|
|
159
|
-
this.logger.warn(
|
|
173
|
+
this.logger.warn(
|
|
174
|
+
`You should provide the argument "${this.arguments[i]}"`
|
|
175
|
+
);
|
|
160
176
|
args.push("");
|
|
161
177
|
} else if (this.arguments[i].startsWith("[...")) {
|
|
162
178
|
args.push([]);
|
|
@@ -174,6 +190,8 @@ const _Command = class {
|
|
|
174
190
|
const options = argv;
|
|
175
191
|
delete options["_"];
|
|
176
192
|
for (const [name, rawOption] of fullOptions) {
|
|
193
|
+
if (rawOption.type === "boolean")
|
|
194
|
+
continue;
|
|
177
195
|
if (rawOption.required) {
|
|
178
196
|
if (options[name] === void 0) {
|
|
179
197
|
options[name] = false;
|
|
@@ -187,13 +205,12 @@ const _Command = class {
|
|
|
187
205
|
options[name] = void 0;
|
|
188
206
|
}
|
|
189
207
|
}
|
|
190
|
-
if (rawOption.default !== void 0) {
|
|
191
|
-
if (options[name] === void 0 || options[name] === false) {
|
|
192
|
-
options[name] = rawOption.default;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
208
|
if (rawOption.construct !== void 0) {
|
|
196
209
|
options[name] = rawOption.construct(options[name]);
|
|
210
|
+
} else if (rawOption.default !== void 0) {
|
|
211
|
+
if (options[name] === void 0 || options[name] === false || options[name] === "") {
|
|
212
|
+
options[name] = rawOption.default;
|
|
213
|
+
}
|
|
197
214
|
}
|
|
198
215
|
}
|
|
199
216
|
for (const key of Object.keys(options)) {
|
|
@@ -215,10 +232,12 @@ const _Command = class {
|
|
|
215
232
|
if (this.actionFn) {
|
|
216
233
|
return await this.actionFn(...args, {
|
|
217
234
|
logger: this.logger,
|
|
218
|
-
color:
|
|
235
|
+
color: kolorist__namespace
|
|
219
236
|
});
|
|
220
237
|
} else {
|
|
221
|
-
this.logger.warn(
|
|
238
|
+
this.logger.warn(
|
|
239
|
+
`You may miss action function in ${this.format ? `"${this.format}"` : "<default command>"}`
|
|
240
|
+
);
|
|
222
241
|
return void 0;
|
|
223
242
|
}
|
|
224
243
|
}
|
|
@@ -314,7 +333,10 @@ class Breadc {
|
|
|
314
333
|
this._version = option.version ?? "unknown";
|
|
315
334
|
this.description = option.description;
|
|
316
335
|
this.logger = createDefaultLogger(name, option.logger);
|
|
317
|
-
this.commands.push(
|
|
336
|
+
this.commands.push(
|
|
337
|
+
new VersionCommand(this.version(), this.logger),
|
|
338
|
+
new HelpCommand(this.commands, this.help.bind(this), this.logger)
|
|
339
|
+
);
|
|
318
340
|
}
|
|
319
341
|
version() {
|
|
320
342
|
return `${this.name}/${this._version}`;
|
|
@@ -350,10 +372,14 @@ class Breadc {
|
|
|
350
372
|
println(` $ ${this.name} ${command.format}`);
|
|
351
373
|
}
|
|
352
374
|
if (commands.length !== 1) {
|
|
353
|
-
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
375
|
+
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
376
|
+
(c) => !c.isInternal
|
|
377
|
+
);
|
|
354
378
|
println(``);
|
|
355
379
|
println(`Commands:`);
|
|
356
|
-
const commandHelps = cmdList.map(
|
|
380
|
+
const commandHelps = cmdList.map(
|
|
381
|
+
(c) => [` $ ${this.name} ${c.format}`, c.description]
|
|
382
|
+
);
|
|
357
383
|
for (const line of twoColumn(commandHelps)) {
|
|
358
384
|
println(line);
|
|
359
385
|
}
|
|
@@ -361,8 +387,14 @@ class Breadc {
|
|
|
361
387
|
println(``);
|
|
362
388
|
println(`Options:`);
|
|
363
389
|
const optionHelps = [].concat([
|
|
364
|
-
...commands.length > 0 ? commands.flatMap(
|
|
365
|
-
|
|
390
|
+
...commands.length > 0 ? commands.flatMap(
|
|
391
|
+
(cmd) => cmd.options.map(
|
|
392
|
+
(o) => [` ${o.format}`, o.description]
|
|
393
|
+
)
|
|
394
|
+
) : [],
|
|
395
|
+
...this.options.map(
|
|
396
|
+
(o) => [` ${o.format}`, o.description]
|
|
397
|
+
),
|
|
366
398
|
[` -h, --help`, `Display this message`],
|
|
367
399
|
[` -v, --version`, `Display version number`]
|
|
368
400
|
]);
|
|
@@ -412,15 +444,25 @@ class Breadc {
|
|
|
412
444
|
}
|
|
413
445
|
}
|
|
414
446
|
}
|
|
415
|
-
const alias = allowOptions.reduce(
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
447
|
+
const alias = allowOptions.reduce(
|
|
448
|
+
(map, o) => {
|
|
449
|
+
if (o.shortcut) {
|
|
450
|
+
map[o.shortcut] = o.name;
|
|
451
|
+
}
|
|
452
|
+
return map;
|
|
453
|
+
},
|
|
454
|
+
{ h: "help", v: "version" }
|
|
455
|
+
);
|
|
456
|
+
const defaultValue = allowOptions.filter(
|
|
457
|
+
(o) => o.type === "boolean" && o.default !== void 0 && o.default !== null && typeof o.default === "boolean"
|
|
458
|
+
).reduce((map, o) => {
|
|
459
|
+
map[o.name] = o.default;
|
|
419
460
|
return map;
|
|
420
|
-
}, {
|
|
461
|
+
}, {});
|
|
421
462
|
const argv = minimist__default(args, {
|
|
422
463
|
string: allowOptions.filter((o) => o.type === "string").map((o) => o.name),
|
|
423
464
|
boolean: allowOptions.filter((o) => o.type === "boolean").map((o) => o.name).concat(["help", "version"]),
|
|
465
|
+
default: defaultValue,
|
|
424
466
|
alias,
|
|
425
467
|
"--": true,
|
|
426
468
|
unknown: (t) => {
|
|
@@ -467,12 +509,16 @@ class Breadc {
|
|
|
467
509
|
async run(args) {
|
|
468
510
|
const parsed = this.parse(args);
|
|
469
511
|
if (parsed.command) {
|
|
470
|
-
await Promise.all(
|
|
512
|
+
await Promise.all(
|
|
513
|
+
this.callbacks.pre.map((fn) => fn(parsed.options))
|
|
514
|
+
);
|
|
471
515
|
const returnValue = await parsed.command.run(...parsed.arguments, {
|
|
472
516
|
"--": parsed["--"],
|
|
473
517
|
...parsed.options
|
|
474
518
|
});
|
|
475
|
-
await Promise.all(
|
|
519
|
+
await Promise.all(
|
|
520
|
+
this.callbacks.post.map((fn) => fn(parsed.options))
|
|
521
|
+
);
|
|
476
522
|
return returnValue;
|
|
477
523
|
} else {
|
|
478
524
|
return void 0;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import kolorist from 'kolorist';
|
|
2
2
|
import { ParsedArgs } from 'minimist';
|
|
3
3
|
|
|
4
|
-
interface OptionConfig<F extends string, T = never
|
|
4
|
+
interface OptionConfig<F extends string, T = never, O = ExtractOptionType<F>> {
|
|
5
5
|
/**
|
|
6
6
|
* Option description
|
|
7
7
|
*/
|
|
@@ -9,7 +9,7 @@ interface OptionConfig<F extends string, T = never> {
|
|
|
9
9
|
/**
|
|
10
10
|
* Option string default value
|
|
11
11
|
*/
|
|
12
|
-
default?: string;
|
|
12
|
+
default?: O extends boolean ? boolean : string;
|
|
13
13
|
/**
|
|
14
14
|
* Transform option text
|
|
15
15
|
*/
|
|
@@ -24,17 +24,17 @@ interface OptionConfig<F extends string, T = never> {
|
|
|
24
24
|
* + --option <arg>
|
|
25
25
|
* + --option [arg]
|
|
26
26
|
*/
|
|
27
|
-
declare class Option<
|
|
27
|
+
declare class Option<F extends string = string, T = string, O = ExtractOptionType<F>> {
|
|
28
28
|
private static OptionRE;
|
|
29
29
|
readonly name: string;
|
|
30
30
|
readonly shortcut?: string;
|
|
31
|
-
readonly default?: string;
|
|
31
|
+
readonly default?: O extends boolean ? boolean : string;
|
|
32
32
|
readonly format: string;
|
|
33
33
|
readonly description: string;
|
|
34
34
|
readonly type: 'string' | 'boolean';
|
|
35
35
|
readonly required: boolean;
|
|
36
|
-
readonly construct?: (rawText: ExtractOptionType<
|
|
37
|
-
constructor(format:
|
|
36
|
+
readonly construct?: (rawText: ExtractOptionType<F>) => T;
|
|
37
|
+
constructor(format: F, config?: OptionConfig<F, T, O>);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
interface CommandConfig {
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import minimist from 'minimist';
|
|
2
|
-
import
|
|
2
|
+
import * as kolorist from 'kolorist';
|
|
3
|
+
import { blue, yellow, red, gray } from 'kolorist';
|
|
3
4
|
|
|
4
5
|
function twoColumn(texts, split = " ") {
|
|
5
6
|
const left = padRight(texts.map((t) => t[0]));
|
|
@@ -75,7 +76,9 @@ const _Command = class {
|
|
|
75
76
|
{
|
|
76
77
|
const restArgs = this.arguments.findIndex((a) => a.startsWith("[..."));
|
|
77
78
|
if (restArgs !== -1 && restArgs !== this.arguments.length - 1) {
|
|
78
|
-
this.logger.warn(
|
|
79
|
+
this.logger.warn(
|
|
80
|
+
`Expand arguments ${this.arguments[restArgs]} should be placed at the last position`
|
|
81
|
+
);
|
|
79
82
|
}
|
|
80
83
|
if (pieces.length > _Command.MaxDep) {
|
|
81
84
|
this.logger.warn(`Command format string "${format}" is too long`);
|
|
@@ -149,7 +152,9 @@ const _Command = class {
|
|
|
149
152
|
}
|
|
150
153
|
} else {
|
|
151
154
|
if (this.arguments[i].startsWith("<")) {
|
|
152
|
-
this.logger.warn(
|
|
155
|
+
this.logger.warn(
|
|
156
|
+
`You should provide the argument "${this.arguments[i]}"`
|
|
157
|
+
);
|
|
153
158
|
args.push("");
|
|
154
159
|
} else if (this.arguments[i].startsWith("[...")) {
|
|
155
160
|
args.push([]);
|
|
@@ -167,6 +172,8 @@ const _Command = class {
|
|
|
167
172
|
const options = argv;
|
|
168
173
|
delete options["_"];
|
|
169
174
|
for (const [name, rawOption] of fullOptions) {
|
|
175
|
+
if (rawOption.type === "boolean")
|
|
176
|
+
continue;
|
|
170
177
|
if (rawOption.required) {
|
|
171
178
|
if (options[name] === void 0) {
|
|
172
179
|
options[name] = false;
|
|
@@ -180,13 +187,12 @@ const _Command = class {
|
|
|
180
187
|
options[name] = void 0;
|
|
181
188
|
}
|
|
182
189
|
}
|
|
183
|
-
if (rawOption.default !== void 0) {
|
|
184
|
-
if (options[name] === void 0 || options[name] === false) {
|
|
185
|
-
options[name] = rawOption.default;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
190
|
if (rawOption.construct !== void 0) {
|
|
189
191
|
options[name] = rawOption.construct(options[name]);
|
|
192
|
+
} else if (rawOption.default !== void 0) {
|
|
193
|
+
if (options[name] === void 0 || options[name] === false || options[name] === "") {
|
|
194
|
+
options[name] = rawOption.default;
|
|
195
|
+
}
|
|
190
196
|
}
|
|
191
197
|
}
|
|
192
198
|
for (const key of Object.keys(options)) {
|
|
@@ -211,7 +217,9 @@ const _Command = class {
|
|
|
211
217
|
color: kolorist
|
|
212
218
|
});
|
|
213
219
|
} else {
|
|
214
|
-
this.logger.warn(
|
|
220
|
+
this.logger.warn(
|
|
221
|
+
`You may miss action function in ${this.format ? `"${this.format}"` : "<default command>"}`
|
|
222
|
+
);
|
|
215
223
|
return void 0;
|
|
216
224
|
}
|
|
217
225
|
}
|
|
@@ -307,7 +315,10 @@ class Breadc {
|
|
|
307
315
|
this._version = option.version ?? "unknown";
|
|
308
316
|
this.description = option.description;
|
|
309
317
|
this.logger = createDefaultLogger(name, option.logger);
|
|
310
|
-
this.commands.push(
|
|
318
|
+
this.commands.push(
|
|
319
|
+
new VersionCommand(this.version(), this.logger),
|
|
320
|
+
new HelpCommand(this.commands, this.help.bind(this), this.logger)
|
|
321
|
+
);
|
|
311
322
|
}
|
|
312
323
|
version() {
|
|
313
324
|
return `${this.name}/${this._version}`;
|
|
@@ -343,10 +354,14 @@ class Breadc {
|
|
|
343
354
|
println(` $ ${this.name} ${command.format}`);
|
|
344
355
|
}
|
|
345
356
|
if (commands.length !== 1) {
|
|
346
|
-
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
357
|
+
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
358
|
+
(c) => !c.isInternal
|
|
359
|
+
);
|
|
347
360
|
println(``);
|
|
348
361
|
println(`Commands:`);
|
|
349
|
-
const commandHelps = cmdList.map(
|
|
362
|
+
const commandHelps = cmdList.map(
|
|
363
|
+
(c) => [` $ ${this.name} ${c.format}`, c.description]
|
|
364
|
+
);
|
|
350
365
|
for (const line of twoColumn(commandHelps)) {
|
|
351
366
|
println(line);
|
|
352
367
|
}
|
|
@@ -354,8 +369,14 @@ class Breadc {
|
|
|
354
369
|
println(``);
|
|
355
370
|
println(`Options:`);
|
|
356
371
|
const optionHelps = [].concat([
|
|
357
|
-
...commands.length > 0 ? commands.flatMap(
|
|
358
|
-
|
|
372
|
+
...commands.length > 0 ? commands.flatMap(
|
|
373
|
+
(cmd) => cmd.options.map(
|
|
374
|
+
(o) => [` ${o.format}`, o.description]
|
|
375
|
+
)
|
|
376
|
+
) : [],
|
|
377
|
+
...this.options.map(
|
|
378
|
+
(o) => [` ${o.format}`, o.description]
|
|
379
|
+
),
|
|
359
380
|
[` -h, --help`, `Display this message`],
|
|
360
381
|
[` -v, --version`, `Display version number`]
|
|
361
382
|
]);
|
|
@@ -405,15 +426,25 @@ class Breadc {
|
|
|
405
426
|
}
|
|
406
427
|
}
|
|
407
428
|
}
|
|
408
|
-
const alias = allowOptions.reduce(
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
429
|
+
const alias = allowOptions.reduce(
|
|
430
|
+
(map, o) => {
|
|
431
|
+
if (o.shortcut) {
|
|
432
|
+
map[o.shortcut] = o.name;
|
|
433
|
+
}
|
|
434
|
+
return map;
|
|
435
|
+
},
|
|
436
|
+
{ h: "help", v: "version" }
|
|
437
|
+
);
|
|
438
|
+
const defaultValue = allowOptions.filter(
|
|
439
|
+
(o) => o.type === "boolean" && o.default !== void 0 && o.default !== null && typeof o.default === "boolean"
|
|
440
|
+
).reduce((map, o) => {
|
|
441
|
+
map[o.name] = o.default;
|
|
412
442
|
return map;
|
|
413
|
-
}, {
|
|
443
|
+
}, {});
|
|
414
444
|
const argv = minimist(args, {
|
|
415
445
|
string: allowOptions.filter((o) => o.type === "string").map((o) => o.name),
|
|
416
446
|
boolean: allowOptions.filter((o) => o.type === "boolean").map((o) => o.name).concat(["help", "version"]),
|
|
447
|
+
default: defaultValue,
|
|
417
448
|
alias,
|
|
418
449
|
"--": true,
|
|
419
450
|
unknown: (t) => {
|
|
@@ -460,12 +491,16 @@ class Breadc {
|
|
|
460
491
|
async run(args) {
|
|
461
492
|
const parsed = this.parse(args);
|
|
462
493
|
if (parsed.command) {
|
|
463
|
-
await Promise.all(
|
|
494
|
+
await Promise.all(
|
|
495
|
+
this.callbacks.pre.map((fn) => fn(parsed.options))
|
|
496
|
+
);
|
|
464
497
|
const returnValue = await parsed.command.run(...parsed.arguments, {
|
|
465
498
|
"--": parsed["--"],
|
|
466
499
|
...parsed.options
|
|
467
500
|
});
|
|
468
|
-
await Promise.all(
|
|
501
|
+
await Promise.all(
|
|
502
|
+
this.callbacks.post.map((fn) => fn(parsed.options))
|
|
503
|
+
);
|
|
469
504
|
return returnValue;
|
|
470
505
|
} else {
|
|
471
506
|
return void 0;
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "breadc",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.5",
|
|
4
4
|
"description": "Yet another Command Line Application Framework with fully strong TypeScript support",
|
|
5
5
|
"keywords": [
|
|
6
|
+
"breadc",
|
|
6
7
|
"cli",
|
|
7
8
|
"framework",
|
|
8
9
|
"command-line",
|
|
@@ -34,26 +35,29 @@
|
|
|
34
35
|
"dist"
|
|
35
36
|
],
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"kolorist": "^1.
|
|
38
|
+
"kolorist": "^1.6.0",
|
|
38
39
|
"minimist": "^1.2.6"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@types/minimist": "^1.2.2",
|
|
42
43
|
"@types/node": "^18.0.0",
|
|
43
|
-
"
|
|
44
|
+
"cac": "^6.7.14",
|
|
44
45
|
"prettier": "^2.7.1",
|
|
45
|
-
"typescript": "^4.
|
|
46
|
-
"unbuild": "^0.
|
|
47
|
-
"vite": "^3.0
|
|
48
|
-
"vitest": "^0.
|
|
46
|
+
"typescript": "^4.8.3",
|
|
47
|
+
"unbuild": "^0.8.10",
|
|
48
|
+
"vite": "^3.1.0",
|
|
49
|
+
"vitest": "^0.23.2"
|
|
49
50
|
},
|
|
50
51
|
"packageManager": "pnpm@7.5.2",
|
|
51
52
|
"scripts": {
|
|
52
53
|
"build": "unbuild",
|
|
54
|
+
"build:all": "unbuild && pnpm -r build",
|
|
53
55
|
"format": "prettier --write src/**/*.ts test/*.ts examples/*.ts",
|
|
54
|
-
"release": "bumpp --commit --push --tag && pnpm publish",
|
|
56
|
+
"release": "bumpp package.json packages/*/package.json --commit --push --tag && pnpm publish && pnpm -r publish --access public",
|
|
55
57
|
"test": "vitest",
|
|
58
|
+
"test:bench": "vitest bench",
|
|
56
59
|
"typecheck": "tsc --noEmit",
|
|
57
|
-
"
|
|
60
|
+
"typecheck:all": "tsc --noEmit && pnpm -r typecheck",
|
|
61
|
+
"preversion": "pnpm typecheck:all && pnpm build:all"
|
|
58
62
|
}
|
|
59
63
|
}
|