@wooksjs/event-cli 0.2.19 → 0.2.21
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 +18 -10
- package/dist/index.d.ts +6 -2
- package/dist/index.mjs +18 -11
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -54,16 +54,16 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
54
54
|
return this.on('CLI', path, handler);
|
|
55
55
|
}
|
|
56
56
|
run(_argv) {
|
|
57
|
-
var _a;
|
|
57
|
+
var _a, _b;
|
|
58
58
|
return __awaiter(this, void 0, void 0, function* () {
|
|
59
59
|
const argv = process.argv.slice(2) || _argv;
|
|
60
60
|
const firstFlagIndex = argv.findIndex((a) => a.startsWith('-')) + 1;
|
|
61
|
-
const pathParams = firstFlagIndex
|
|
61
|
+
const pathParams = (firstFlagIndex
|
|
62
62
|
? argv.slice(0, firstFlagIndex - 1)
|
|
63
|
-
: argv;
|
|
63
|
+
: argv);
|
|
64
64
|
const path = '/' + pathParams.map((v) => encodeURIComponent(v)).join('/');
|
|
65
|
-
const { restoreCtx, clearCtx } = createCliContext({ argv }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
|
|
66
|
-
const handlers = this.wooks.lookup('CLI', path);
|
|
65
|
+
const { restoreCtx, clearCtx } = createCliContext({ argv, pathParams }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
|
|
66
|
+
const handlers = this.wooks.lookup('CLI', path) || ((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound] || null;
|
|
67
67
|
if (handlers) {
|
|
68
68
|
try {
|
|
69
69
|
for (const handler of handlers) {
|
|
@@ -93,7 +93,8 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
93
93
|
clearCtx();
|
|
94
94
|
}
|
|
95
95
|
else {
|
|
96
|
-
this.
|
|
96
|
+
this.onUnknownCommand(pathParams);
|
|
97
|
+
clearCtx();
|
|
97
98
|
}
|
|
98
99
|
});
|
|
99
100
|
}
|
|
@@ -107,14 +108,14 @@ class WooksCli extends wooks.WooksAdapterBase {
|
|
|
107
108
|
process.exit(1);
|
|
108
109
|
}
|
|
109
110
|
}
|
|
110
|
-
|
|
111
|
+
onUnknownCommand(pathParams) {
|
|
111
112
|
var _a;
|
|
112
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.
|
|
113
|
-
this.opts.
|
|
113
|
+
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
|
|
114
|
+
this.opts.onUnknownCommand(pathParams);
|
|
114
115
|
}
|
|
115
116
|
else {
|
|
116
117
|
this.error('[0m' +
|
|
117
|
-
'Unknown command
|
|
118
|
+
'Unknown command: ' +
|
|
118
119
|
'[31m' +
|
|
119
120
|
pathParams.join(' '));
|
|
120
121
|
process.exit(1);
|
|
@@ -145,6 +146,12 @@ function useFlag(name) {
|
|
|
145
146
|
return useFlags()[name];
|
|
146
147
|
}
|
|
147
148
|
|
|
149
|
+
function usePathParams() {
|
|
150
|
+
const { store } = useCliContext();
|
|
151
|
+
const event = store('event');
|
|
152
|
+
return event.get('pathParams');
|
|
153
|
+
}
|
|
154
|
+
|
|
148
155
|
exports.WooksCli = WooksCli;
|
|
149
156
|
exports.cliShortcuts = cliShortcuts;
|
|
150
157
|
exports.createCliApp = createCliApp;
|
|
@@ -152,3 +159,4 @@ exports.createCliContext = createCliContext;
|
|
|
152
159
|
exports.useCliContext = useCliContext;
|
|
153
160
|
exports.useFlag = useFlag;
|
|
154
161
|
exports.useFlags = useFlags;
|
|
162
|
+
exports.usePathParams = usePathParams;
|
package/dist/index.d.ts
CHANGED
|
@@ -43,12 +43,14 @@ export declare interface TCliContextStore {
|
|
|
43
43
|
|
|
44
44
|
export declare interface TCliEventData {
|
|
45
45
|
argv: string[];
|
|
46
|
+
pathParams: string[];
|
|
46
47
|
type: 'CLI';
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
export declare interface TWooksCliOptions {
|
|
50
51
|
onError?(e: Error): void;
|
|
51
|
-
|
|
52
|
+
onNotFound?: TWooksHandler<unknown>;
|
|
53
|
+
onUnknownCommand?: ((params: string[]) => unknown);
|
|
52
54
|
logger?: TConsoleBase;
|
|
53
55
|
eventOptions?: TEventOptions;
|
|
54
56
|
}
|
|
@@ -81,6 +83,8 @@ export declare function useFlags(): {
|
|
|
81
83
|
[name: string]: string | boolean;
|
|
82
84
|
};
|
|
83
85
|
|
|
86
|
+
export declare function usePathParams(): string[];
|
|
87
|
+
|
|
84
88
|
export declare class WooksCli extends WooksAdapterBase {
|
|
85
89
|
protected opts?: TWooksCliOptions | undefined;
|
|
86
90
|
protected logger: TConsoleBase;
|
|
@@ -88,7 +92,7 @@ export declare class WooksCli extends WooksAdapterBase {
|
|
|
88
92
|
cli<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): TProstoRouterPathHandle<ParamsType>;
|
|
89
93
|
run(_argv?: string[]): Promise<void>;
|
|
90
94
|
onError(e: Error): void;
|
|
91
|
-
|
|
95
|
+
onUnknownCommand(pathParams: string[]): void;
|
|
92
96
|
error(e: string | Error): void;
|
|
93
97
|
}
|
|
94
98
|
|
package/dist/index.mjs
CHANGED
|
@@ -52,16 +52,16 @@ class WooksCli extends WooksAdapterBase {
|
|
|
52
52
|
return this.on('CLI', path, handler);
|
|
53
53
|
}
|
|
54
54
|
run(_argv) {
|
|
55
|
-
var _a;
|
|
55
|
+
var _a, _b;
|
|
56
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
57
|
const argv = process.argv.slice(2) || _argv;
|
|
58
58
|
const firstFlagIndex = argv.findIndex((a) => a.startsWith('-')) + 1;
|
|
59
|
-
const pathParams = firstFlagIndex
|
|
59
|
+
const pathParams = (firstFlagIndex
|
|
60
60
|
? argv.slice(0, firstFlagIndex - 1)
|
|
61
|
-
: argv;
|
|
61
|
+
: argv);
|
|
62
62
|
const path = '/' + pathParams.map((v) => encodeURIComponent(v)).join('/');
|
|
63
|
-
const { restoreCtx, clearCtx } = createCliContext({ argv }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
|
|
64
|
-
const handlers = this.wooks.lookup('CLI', path);
|
|
63
|
+
const { restoreCtx, clearCtx } = createCliContext({ argv, pathParams }, this.mergeEventOptions((_a = this.opts) === null || _a === void 0 ? void 0 : _a.eventOptions));
|
|
64
|
+
const handlers = this.wooks.lookup('CLI', path) || ((_b = this.opts) === null || _b === void 0 ? void 0 : _b.onNotFound) && [this.opts.onNotFound] || null;
|
|
65
65
|
if (handlers) {
|
|
66
66
|
try {
|
|
67
67
|
for (const handler of handlers) {
|
|
@@ -91,7 +91,8 @@ class WooksCli extends WooksAdapterBase {
|
|
|
91
91
|
clearCtx();
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
94
|
-
this.
|
|
94
|
+
this.onUnknownCommand(pathParams);
|
|
95
|
+
clearCtx();
|
|
95
96
|
}
|
|
96
97
|
});
|
|
97
98
|
}
|
|
@@ -105,14 +106,14 @@ class WooksCli extends WooksAdapterBase {
|
|
|
105
106
|
process.exit(1);
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
+
onUnknownCommand(pathParams) {
|
|
109
110
|
var _a;
|
|
110
|
-
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.
|
|
111
|
-
this.opts.
|
|
111
|
+
if ((_a = this.opts) === null || _a === void 0 ? void 0 : _a.onUnknownCommand) {
|
|
112
|
+
this.opts.onUnknownCommand(pathParams);
|
|
112
113
|
}
|
|
113
114
|
else {
|
|
114
115
|
this.error('[0m' +
|
|
115
|
-
'Unknown command
|
|
116
|
+
'Unknown command: ' +
|
|
116
117
|
'[31m' +
|
|
117
118
|
pathParams.join(' '));
|
|
118
119
|
process.exit(1);
|
|
@@ -143,4 +144,10 @@ function useFlag(name) {
|
|
|
143
144
|
return useFlags()[name];
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
+
function usePathParams() {
|
|
148
|
+
const { store } = useCliContext();
|
|
149
|
+
const event = store('event');
|
|
150
|
+
return event.get('pathParams');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export { WooksCli, cliShortcuts, createCliApp, createCliContext, useCliContext, useFlag, useFlags, usePathParams };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.21",
|
|
4
4
|
"description": "@wooksjs/event-cli",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"wooks": "0.2.
|
|
35
|
-
"@wooksjs/event-core": "0.2.
|
|
34
|
+
"wooks": "0.2.21",
|
|
35
|
+
"@wooksjs/event-core": "0.2.21"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"minimist": "^1.2.6",
|