breadc 0.6.4 → 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 +59 -19
- package/dist/index.mjs +47 -18
- package/package.json +8 -6
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([]);
|
|
@@ -216,10 +232,12 @@ const _Command = class {
|
|
|
216
232
|
if (this.actionFn) {
|
|
217
233
|
return await this.actionFn(...args, {
|
|
218
234
|
logger: this.logger,
|
|
219
|
-
color:
|
|
235
|
+
color: kolorist__namespace
|
|
220
236
|
});
|
|
221
237
|
} else {
|
|
222
|
-
this.logger.warn(
|
|
238
|
+
this.logger.warn(
|
|
239
|
+
`You may miss action function in ${this.format ? `"${this.format}"` : "<default command>"}`
|
|
240
|
+
);
|
|
223
241
|
return void 0;
|
|
224
242
|
}
|
|
225
243
|
}
|
|
@@ -315,7 +333,10 @@ class Breadc {
|
|
|
315
333
|
this._version = option.version ?? "unknown";
|
|
316
334
|
this.description = option.description;
|
|
317
335
|
this.logger = createDefaultLogger(name, option.logger);
|
|
318
|
-
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
|
+
);
|
|
319
340
|
}
|
|
320
341
|
version() {
|
|
321
342
|
return `${this.name}/${this._version}`;
|
|
@@ -351,10 +372,14 @@ class Breadc {
|
|
|
351
372
|
println(` $ ${this.name} ${command.format}`);
|
|
352
373
|
}
|
|
353
374
|
if (commands.length !== 1) {
|
|
354
|
-
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
375
|
+
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
376
|
+
(c) => !c.isInternal
|
|
377
|
+
);
|
|
355
378
|
println(``);
|
|
356
379
|
println(`Commands:`);
|
|
357
|
-
const commandHelps = cmdList.map(
|
|
380
|
+
const commandHelps = cmdList.map(
|
|
381
|
+
(c) => [` $ ${this.name} ${c.format}`, c.description]
|
|
382
|
+
);
|
|
358
383
|
for (const line of twoColumn(commandHelps)) {
|
|
359
384
|
println(line);
|
|
360
385
|
}
|
|
@@ -362,8 +387,14 @@ class Breadc {
|
|
|
362
387
|
println(``);
|
|
363
388
|
println(`Options:`);
|
|
364
389
|
const optionHelps = [].concat([
|
|
365
|
-
...commands.length > 0 ? commands.flatMap(
|
|
366
|
-
|
|
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
|
+
),
|
|
367
398
|
[` -h, --help`, `Display this message`],
|
|
368
399
|
[` -v, --version`, `Display version number`]
|
|
369
400
|
]);
|
|
@@ -413,13 +444,18 @@ class Breadc {
|
|
|
413
444
|
}
|
|
414
445
|
}
|
|
415
446
|
}
|
|
416
|
-
const alias = allowOptions.reduce(
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
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) => {
|
|
423
459
|
map[o.name] = o.default;
|
|
424
460
|
return map;
|
|
425
461
|
}, {});
|
|
@@ -473,12 +509,16 @@ class Breadc {
|
|
|
473
509
|
async run(args) {
|
|
474
510
|
const parsed = this.parse(args);
|
|
475
511
|
if (parsed.command) {
|
|
476
|
-
await Promise.all(
|
|
512
|
+
await Promise.all(
|
|
513
|
+
this.callbacks.pre.map((fn) => fn(parsed.options))
|
|
514
|
+
);
|
|
477
515
|
const returnValue = await parsed.command.run(...parsed.arguments, {
|
|
478
516
|
"--": parsed["--"],
|
|
479
517
|
...parsed.options
|
|
480
518
|
});
|
|
481
|
-
await Promise.all(
|
|
519
|
+
await Promise.all(
|
|
520
|
+
this.callbacks.post.map((fn) => fn(parsed.options))
|
|
521
|
+
);
|
|
482
522
|
return returnValue;
|
|
483
523
|
} else {
|
|
484
524
|
return void 0;
|
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([]);
|
|
@@ -212,7 +217,9 @@ const _Command = class {
|
|
|
212
217
|
color: kolorist
|
|
213
218
|
});
|
|
214
219
|
} else {
|
|
215
|
-
this.logger.warn(
|
|
220
|
+
this.logger.warn(
|
|
221
|
+
`You may miss action function in ${this.format ? `"${this.format}"` : "<default command>"}`
|
|
222
|
+
);
|
|
216
223
|
return void 0;
|
|
217
224
|
}
|
|
218
225
|
}
|
|
@@ -308,7 +315,10 @@ class Breadc {
|
|
|
308
315
|
this._version = option.version ?? "unknown";
|
|
309
316
|
this.description = option.description;
|
|
310
317
|
this.logger = createDefaultLogger(name, option.logger);
|
|
311
|
-
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
|
+
);
|
|
312
322
|
}
|
|
313
323
|
version() {
|
|
314
324
|
return `${this.name}/${this._version}`;
|
|
@@ -344,10 +354,14 @@ class Breadc {
|
|
|
344
354
|
println(` $ ${this.name} ${command.format}`);
|
|
345
355
|
}
|
|
346
356
|
if (commands.length !== 1) {
|
|
347
|
-
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
357
|
+
const cmdList = (commands.length === 0 ? this.commands : commands).filter(
|
|
358
|
+
(c) => !c.isInternal
|
|
359
|
+
);
|
|
348
360
|
println(``);
|
|
349
361
|
println(`Commands:`);
|
|
350
|
-
const commandHelps = cmdList.map(
|
|
362
|
+
const commandHelps = cmdList.map(
|
|
363
|
+
(c) => [` $ ${this.name} ${c.format}`, c.description]
|
|
364
|
+
);
|
|
351
365
|
for (const line of twoColumn(commandHelps)) {
|
|
352
366
|
println(line);
|
|
353
367
|
}
|
|
@@ -355,8 +369,14 @@ class Breadc {
|
|
|
355
369
|
println(``);
|
|
356
370
|
println(`Options:`);
|
|
357
371
|
const optionHelps = [].concat([
|
|
358
|
-
...commands.length > 0 ? commands.flatMap(
|
|
359
|
-
|
|
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
|
+
),
|
|
360
380
|
[` -h, --help`, `Display this message`],
|
|
361
381
|
[` -v, --version`, `Display version number`]
|
|
362
382
|
]);
|
|
@@ -406,13 +426,18 @@ class Breadc {
|
|
|
406
426
|
}
|
|
407
427
|
}
|
|
408
428
|
}
|
|
409
|
-
const alias = allowOptions.reduce(
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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) => {
|
|
416
441
|
map[o.name] = o.default;
|
|
417
442
|
return map;
|
|
418
443
|
}, {});
|
|
@@ -466,12 +491,16 @@ class Breadc {
|
|
|
466
491
|
async run(args) {
|
|
467
492
|
const parsed = this.parse(args);
|
|
468
493
|
if (parsed.command) {
|
|
469
|
-
await Promise.all(
|
|
494
|
+
await Promise.all(
|
|
495
|
+
this.callbacks.pre.map((fn) => fn(parsed.options))
|
|
496
|
+
);
|
|
470
497
|
const returnValue = await parsed.command.run(...parsed.arguments, {
|
|
471
498
|
"--": parsed["--"],
|
|
472
499
|
...parsed.options
|
|
473
500
|
});
|
|
474
|
-
await Promise.all(
|
|
501
|
+
await Promise.all(
|
|
502
|
+
this.callbacks.post.map((fn) => fn(parsed.options))
|
|
503
|
+
);
|
|
475
504
|
return returnValue;
|
|
476
505
|
} else {
|
|
477
506
|
return void 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
"breadc",
|
|
@@ -35,17 +35,18 @@
|
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"kolorist": "^1.
|
|
38
|
+
"kolorist": "^1.6.0",
|
|
39
39
|
"minimist": "^1.2.6"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/minimist": "^1.2.2",
|
|
43
43
|
"@types/node": "^18.0.0",
|
|
44
|
+
"cac": "^6.7.14",
|
|
44
45
|
"prettier": "^2.7.1",
|
|
45
|
-
"typescript": "^4.8.
|
|
46
|
-
"unbuild": "^0.8.
|
|
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": {
|
|
@@ -54,6 +55,7 @@
|
|
|
54
55
|
"format": "prettier --write src/**/*.ts test/*.ts examples/*.ts",
|
|
55
56
|
"release": "bumpp package.json packages/*/package.json --commit --push --tag && pnpm publish && pnpm -r publish --access public",
|
|
56
57
|
"test": "vitest",
|
|
58
|
+
"test:bench": "vitest bench",
|
|
57
59
|
"typecheck": "tsc --noEmit",
|
|
58
60
|
"typecheck:all": "tsc --noEmit && pnpm -r typecheck",
|
|
59
61
|
"preversion": "pnpm typecheck:all && pnpm build:all"
|