@breadc/core 1.0.0-beta.3 → 1.0.0-beta.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.d.mts +3 -1
- package/dist/index.mjs +49 -24
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -614,7 +614,7 @@ type Command<Spec extends string = string, Init extends CommandInit<Spec> = Comm
|
|
|
614
614
|
*/
|
|
615
615
|
action<R extends unknown>(fn: (...args: [...Arguments, Prettify<Options & {
|
|
616
616
|
'--': string[];
|
|
617
|
-
}>]) => Promise<R> | R): Command<Spec, Init, Data, Options, Arguments, R>;
|
|
617
|
+
}>, Context<Data>]) => Promise<R> | R): Command<Spec, Init, Data, Options, Arguments, R>;
|
|
618
618
|
/**
|
|
619
619
|
* Run command directly
|
|
620
620
|
*/
|
|
@@ -657,6 +657,7 @@ declare function printVersion(context: Context): string;
|
|
|
657
657
|
//#region src/error.d.ts
|
|
658
658
|
declare abstract class BreadcError extends Error {}
|
|
659
659
|
type BreadcAppErrorCause = {
|
|
660
|
+
group?: InternalGroup;
|
|
660
661
|
command?: InternalCommand;
|
|
661
662
|
commands?: (InternalCommand | InternalGroup)[];
|
|
662
663
|
};
|
|
@@ -665,6 +666,7 @@ type BreadcAppErrorInput = BreadcAppErrorCause & {
|
|
|
665
666
|
};
|
|
666
667
|
declare class BreadcAppError extends BreadcError {
|
|
667
668
|
static DUPLICATED_DEFAULT_COMMAND: string;
|
|
669
|
+
static DUPLICATED_DEFAULT_GROUP_COMMAND: string;
|
|
668
670
|
static DUPLICATED_GROUP: string;
|
|
669
671
|
static DUPLICATED_COMMAND: string;
|
|
670
672
|
static NO_ACTION_BOUND: string;
|
package/dist/index.mjs
CHANGED
|
@@ -38,6 +38,7 @@ function rawOption(spec, type, long, short, init) {
|
|
|
38
38
|
//#region src/error.ts
|
|
39
39
|
var BreadcError = class extends Error {};
|
|
40
40
|
var RuntimeError = class extends BreadcError {
|
|
41
|
+
static UNEXPECTED_ARGUMENTS = "Detect unexpected redundant arguments";
|
|
41
42
|
static REQUIRED_ARGUMENT_MISSING = "Missing required argument";
|
|
42
43
|
static OPTIONAL_ARGUMENT_ACCEPT_ONCE = "Optional argument can only be assigned once";
|
|
43
44
|
static REQUIRED_ARGUMENT_ACCEPT_ONCE = "Required argument can only be assigned once";
|
|
@@ -60,6 +61,7 @@ var RuntimeError = class extends BreadcError {
|
|
|
60
61
|
};
|
|
61
62
|
var BreadcAppError = class extends BreadcError {
|
|
62
63
|
static DUPLICATED_DEFAULT_COMMAND = `Find duplicated default commands`;
|
|
64
|
+
static DUPLICATED_DEFAULT_GROUP_COMMAND = `Find duplicated default group commands`;
|
|
63
65
|
static DUPLICATED_GROUP = `Find duplicated groups`;
|
|
64
66
|
static DUPLICATED_COMMAND = `Find duplicated commands`;
|
|
65
67
|
static NO_ACTION_BOUND = `There is no action function bound in this command`;
|
|
@@ -117,12 +119,10 @@ var ResolveOptionError = class extends BreadcError {
|
|
|
117
119
|
function resolveOptionInput(spec, description, init) {
|
|
118
120
|
return typeof spec === "string" ? option(spec, description, init) : spec;
|
|
119
121
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
}
|
|
122
|
+
const defaultUnknownOptionMiddleware = (_ctx, key, value) => ({
|
|
123
|
+
name: key,
|
|
124
|
+
value
|
|
125
|
+
});
|
|
126
126
|
|
|
127
127
|
//#endregion
|
|
128
128
|
//#region src/breadc/command.ts
|
|
@@ -157,7 +157,7 @@ function command(spec, init) {
|
|
|
157
157
|
run.allowUnknownOption = (middleware) => {
|
|
158
158
|
if (!run._unknownOptionMiddlewares) run._unknownOptionMiddlewares = [];
|
|
159
159
|
if (typeof middleware === "function") run._unknownOptionMiddlewares.push(middleware);
|
|
160
|
-
else run._unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware
|
|
160
|
+
else run._unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware);
|
|
161
161
|
return run;
|
|
162
162
|
};
|
|
163
163
|
run.action = (fn) => {
|
|
@@ -783,6 +783,8 @@ function buildGroup(group) {
|
|
|
783
783
|
//#endregion
|
|
784
784
|
//#region src/breadc/builtin/i18n.ts
|
|
785
785
|
const en = {
|
|
786
|
+
OPTIONS: "OPTIONS",
|
|
787
|
+
COMMAND: "COMMAND",
|
|
786
788
|
"Usage:": "Usage:",
|
|
787
789
|
"Commands:": "Commands:",
|
|
788
790
|
"Options:": "Options:",
|
|
@@ -790,7 +792,9 @@ const en = {
|
|
|
790
792
|
"Print version": "Print version"
|
|
791
793
|
};
|
|
792
794
|
const zh = {
|
|
793
|
-
|
|
795
|
+
OPTIONS: "选项",
|
|
796
|
+
COMMAND: "子命令",
|
|
797
|
+
"Usage:": "用法:",
|
|
794
798
|
"Commands:": "命令:",
|
|
795
799
|
"Options:": "选项:",
|
|
796
800
|
"Print help": "显示帮助信息",
|
|
@@ -928,7 +932,7 @@ function buildHelpOption(context) {
|
|
|
928
932
|
function printHelp(context) {
|
|
929
933
|
const { breadc, pieces } = context;
|
|
930
934
|
const { allCommands, commands, options } = collect(context, pieces);
|
|
931
|
-
const usage = allCommands.length === 0 ? "
|
|
935
|
+
const usage = allCommands.length === 0 ? `[${i18n(context, "OPTIONS")}]` : allCommands.length === 1 ? `[${i18n(context, "OPTIONS")}] ${formatCommand(allCommands[0])}` : allCommands.some((command) => command._default) ? `[${i18n(context, "OPTIONS")}] [${i18n(context, "COMMAND")}]` : `[${i18n(context, "OPTIONS")}] <${i18n(context, "COMMAND")}>`;
|
|
932
936
|
const text = expandMessage([
|
|
933
937
|
`${breadc.name}/${breadc._init.version ?? "unknown"}`,
|
|
934
938
|
() => {
|
|
@@ -970,10 +974,14 @@ function parse(app, argv) {
|
|
|
970
974
|
commands: defaultCommands
|
|
971
975
|
});
|
|
972
976
|
const defaultCommand = defaultCommands[0];
|
|
973
|
-
doParse(context$1, defaultCommand !== void 0 && context$1.breadc._commands.length === 1 ? defaultCommand : void 0);
|
|
974
|
-
if (context$1.command || isVersion(context$1) || isHelp(context$1)) {} else if (
|
|
977
|
+
doParse(context$1, void 0, defaultCommand !== void 0 && context$1.breadc._commands.length === 1 ? defaultCommand : void 0);
|
|
978
|
+
if (context$1.command || isVersion(context$1) || isHelp(context$1)) {} else if (context$1.group) {
|
|
979
|
+
const matchedGroup = context$1.group;
|
|
980
|
+
reset(context$1);
|
|
981
|
+
doParse(context$1, matchedGroup, void 0);
|
|
982
|
+
} else if (defaultCommand) {
|
|
975
983
|
reset(context$1);
|
|
976
|
-
doParse(context$1, defaultCommand);
|
|
984
|
+
doParse(context$1, void 0, defaultCommand);
|
|
977
985
|
}
|
|
978
986
|
return context$1;
|
|
979
987
|
}
|
|
@@ -993,7 +1001,7 @@ function isVersion(context) {
|
|
|
993
1001
|
if (version && context.options.get(version.long)?.value()) return true;
|
|
994
1002
|
else return false;
|
|
995
1003
|
}
|
|
996
|
-
function doParse(context, defaultCommand) {
|
|
1004
|
+
function doParse(context, defaultGroup, defaultCommand) {
|
|
997
1005
|
const { breadc, tokens, options: matchedOptions } = context;
|
|
998
1006
|
let index = 0;
|
|
999
1007
|
let matchedGroup = void 0;
|
|
@@ -1030,10 +1038,8 @@ function doParse(context, defaultCommand) {
|
|
|
1030
1038
|
}
|
|
1031
1039
|
if (defaultCommand) {
|
|
1032
1040
|
matchedCommand = defaultCommand;
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
addPendingOptions(command._options);
|
|
1036
|
-
}
|
|
1041
|
+
buildCommand(defaultCommand);
|
|
1042
|
+
addPendingOptions(defaultCommand._options);
|
|
1037
1043
|
} else for (const command of breadc._commands) if (!command._default) for (let alias = 0; alias < command._pieces.length; alias++) addPendingCommand(command, alias);
|
|
1038
1044
|
while (!tokens.isEnd) {
|
|
1039
1045
|
const token = tokens.next();
|
|
@@ -1055,7 +1061,22 @@ function doParse(context, defaultCommand) {
|
|
|
1055
1061
|
});
|
|
1056
1062
|
buildGroup(group);
|
|
1057
1063
|
addPendingOptions(group._options);
|
|
1058
|
-
|
|
1064
|
+
if (matchedGroup && matchedGroup === defaultGroup) {
|
|
1065
|
+
const defaultCommands = matchedGroup._commands.filter((command) => command._pieces.some((p) => p.length === index));
|
|
1066
|
+
if (defaultCommands.length === 1) {
|
|
1067
|
+
const defaultCommand = defaultCommands[0];
|
|
1068
|
+
matchedCommand = defaultCommand;
|
|
1069
|
+
buildCommand(defaultCommand);
|
|
1070
|
+
addPendingOptions(defaultCommand._options);
|
|
1071
|
+
} else if (defaultCommands.length > 1) throw new BreadcAppError(BreadcAppError.DUPLICATED_DEFAULT_GROUP_COMMAND, {
|
|
1072
|
+
context,
|
|
1073
|
+
group: matchedGroup,
|
|
1074
|
+
commands: defaultCommands
|
|
1075
|
+
});
|
|
1076
|
+
} else for (const command of group._commands) for (let alias = 0; alias < command._pieces.length; alias++) {
|
|
1077
|
+
if (command._pieces[alias].length === index) continue;
|
|
1078
|
+
addPendingCommand(command, alias);
|
|
1079
|
+
}
|
|
1059
1080
|
} else {
|
|
1060
1081
|
if (!matchedCommand || matchedCommand === command) matchedCommand = command;
|
|
1061
1082
|
else throw new BreadcAppError(BreadcAppError.DUPLICATED_COMMAND, {
|
|
@@ -1095,6 +1116,7 @@ function doParse(context, defaultCommand) {
|
|
|
1095
1116
|
context.group = matchedGroup;
|
|
1096
1117
|
context.command = matchedCommand;
|
|
1097
1118
|
if (matchedCommand) {
|
|
1119
|
+
if (unknown.length > 0) throw new RuntimeError(RuntimeError.UNEXPECTED_ARGUMENTS, { context });
|
|
1098
1120
|
let i = 0;
|
|
1099
1121
|
for (; i < matchedCommand._arguments.length; i++) {
|
|
1100
1122
|
const argument = matchedCommand._arguments[i];
|
|
@@ -1144,7 +1166,7 @@ async function run(app, argv) {
|
|
|
1144
1166
|
options["--"] = context.remaining;
|
|
1145
1167
|
if (context.command._actionFn) {
|
|
1146
1168
|
const actionFn = context.command._actionFn;
|
|
1147
|
-
if (actionMiddlewares.length === 0) return await actionFn(...args, options);
|
|
1169
|
+
if (actionMiddlewares.length === 0) return await actionFn(...args, options, context);
|
|
1148
1170
|
else {
|
|
1149
1171
|
const invoked = [];
|
|
1150
1172
|
const makeNextFn = (index) => {
|
|
@@ -1154,7 +1176,7 @@ async function run(app, argv) {
|
|
|
1154
1176
|
...nextContext?.data
|
|
1155
1177
|
};
|
|
1156
1178
|
invoked[index] = true;
|
|
1157
|
-
if (index === actionMiddlewares.length) context.output = await actionFn(...args, options);
|
|
1179
|
+
if (index === actionMiddlewares.length) context.output = await actionFn(...args, options, context);
|
|
1158
1180
|
else {
|
|
1159
1181
|
const next = makeNextFn(index + 1);
|
|
1160
1182
|
await actionMiddlewares[index](context, next);
|
|
@@ -1192,8 +1214,11 @@ function group(spec, init) {
|
|
|
1192
1214
|
options.push(resolveOptionInput(spec, description, init));
|
|
1193
1215
|
return group;
|
|
1194
1216
|
},
|
|
1195
|
-
command(spec, init) {
|
|
1196
|
-
const command$2 = typeof spec === "string" ? command(spec, init
|
|
1217
|
+
command(spec, description, init) {
|
|
1218
|
+
const command$2 = typeof spec === "string" ? command(spec, description || init ? {
|
|
1219
|
+
description,
|
|
1220
|
+
...init
|
|
1221
|
+
} : void 0) : spec;
|
|
1197
1222
|
command$2._group = group;
|
|
1198
1223
|
commands.push(command$2);
|
|
1199
1224
|
return command$2;
|
|
@@ -1204,7 +1229,7 @@ function group(spec, init) {
|
|
|
1204
1229
|
},
|
|
1205
1230
|
allowUnknownOption(middleware) {
|
|
1206
1231
|
if (typeof middleware === "function") unknownOptionMiddlewares.push(middleware);
|
|
1207
|
-
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware
|
|
1232
|
+
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware);
|
|
1208
1233
|
return group;
|
|
1209
1234
|
}
|
|
1210
1235
|
};
|
|
@@ -1255,7 +1280,7 @@ function breadc(name, init = {}) {
|
|
|
1255
1280
|
},
|
|
1256
1281
|
allowUnknownOption(middleware) {
|
|
1257
1282
|
if (typeof middleware === "function") unknownOptionMiddlewares.push(middleware);
|
|
1258
|
-
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware
|
|
1283
|
+
else unknownOptionMiddlewares.push(defaultUnknownOptionMiddleware);
|
|
1259
1284
|
return app;
|
|
1260
1285
|
},
|
|
1261
1286
|
parse(argv) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breadc/core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.5",
|
|
4
4
|
"description": "Yet another Command Line Application Framework with fully strong TypeScript support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"breadc",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@breadc/color": "1.0.0-beta.
|
|
36
|
+
"@breadc/color": "1.0.0-beta.5"
|
|
37
37
|
},
|
|
38
38
|
"size-limit": [
|
|
39
39
|
{
|