breadc 0.4.2 → 0.4.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 +17 -2
- package/dist/index.d.ts +5 -3
- package/dist/index.mjs +17 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -167,6 +167,11 @@ const _Command = class {
|
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
|
+
for (const key of Object.keys(options)) {
|
|
171
|
+
if (!fullOptions.has(key)) {
|
|
172
|
+
delete options[key];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
170
175
|
return {
|
|
171
176
|
command: this,
|
|
172
177
|
arguments: argumentss,
|
|
@@ -179,7 +184,7 @@ const _Command = class {
|
|
|
179
184
|
}
|
|
180
185
|
async run(...args) {
|
|
181
186
|
if (this.actionFn) {
|
|
182
|
-
this.actionFn(...args, { logger: this.logger });
|
|
187
|
+
return await this.actionFn(...args, { logger: this.logger });
|
|
183
188
|
} else {
|
|
184
189
|
this.logger.warn(`You may miss action function in "${this.format}"`);
|
|
185
190
|
}
|
|
@@ -235,6 +240,10 @@ class Breadc {
|
|
|
235
240
|
constructor(name, option) {
|
|
236
241
|
this.options = [];
|
|
237
242
|
this.commands = [];
|
|
243
|
+
this.callbacks = {
|
|
244
|
+
pre: [],
|
|
245
|
+
post: []
|
|
246
|
+
};
|
|
238
247
|
this.name = name;
|
|
239
248
|
this._version = option.version ?? "unknown";
|
|
240
249
|
this.description = option.description;
|
|
@@ -376,10 +385,16 @@ class Breadc {
|
|
|
376
385
|
options
|
|
377
386
|
};
|
|
378
387
|
}
|
|
388
|
+
on(event, fn) {
|
|
389
|
+
this.callbacks[event].push(fn);
|
|
390
|
+
}
|
|
379
391
|
async run(args) {
|
|
380
392
|
const parsed = this.parse(args);
|
|
381
393
|
if (parsed.command) {
|
|
382
|
-
|
|
394
|
+
await Promise.all(this.callbacks.pre.map((fn) => fn(parsed.options)));
|
|
395
|
+
const returnValue = await parsed.command.run(...parsed.arguments, parsed.options);
|
|
396
|
+
await Promise.all(this.callbacks.post.map((fn) => fn(parsed.options)));
|
|
397
|
+
return returnValue;
|
|
383
398
|
}
|
|
384
399
|
}
|
|
385
400
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -52,7 +52,7 @@ declare class Command<F extends string = string, CommandOption extends object =
|
|
|
52
52
|
shouldRun(args: ParsedArgs): boolean;
|
|
53
53
|
parseArgs(args: ParsedArgs, globalOptions: Option[]): ParseResult;
|
|
54
54
|
action(fn: ActionFn<ExtractCommand<F>, CommandOption>): this;
|
|
55
|
-
run(...args: any[]): Promise<
|
|
55
|
+
run(...args: any[]): Promise<any>;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
interface AppOption {
|
|
@@ -93,7 +93,7 @@ declare type Push<T extends any[], U, R> = [...T, U, R];
|
|
|
93
93
|
declare type Context = {
|
|
94
94
|
logger: Logger;
|
|
95
95
|
};
|
|
96
|
-
declare type ActionFn<T extends any[], Option extends object = {}> = (...arg: Push<T, Option, Context>) =>
|
|
96
|
+
declare type ActionFn<T extends any[], Option extends object = {}, R = any> = (...arg: Push<T, Option, Context>) => R | Promise<R>;
|
|
97
97
|
/**
|
|
98
98
|
* Max Dep: 5
|
|
99
99
|
*
|
|
@@ -117,7 +117,9 @@ declare class Breadc<GlobalOption extends object = {}> {
|
|
|
117
117
|
command<F extends string>(format: F, description: string, config?: Omit<CommandConfig, 'description'>): Command<F, GlobalOption>;
|
|
118
118
|
command<F extends string>(format: F, config?: CommandConfig): Command<F, GlobalOption>;
|
|
119
119
|
parse(args: string[]): ParseResult;
|
|
120
|
-
|
|
120
|
+
private readonly callbacks;
|
|
121
|
+
on(event: 'pre' | 'post', fn: (option: GlobalOption) => void | Promise<void>): void;
|
|
122
|
+
run(args: string[]): Promise<any>;
|
|
121
123
|
}
|
|
122
124
|
|
|
123
125
|
declare function breadc(name: string, option?: AppOption): Breadc<{}>;
|
package/dist/index.mjs
CHANGED
|
@@ -160,6 +160,11 @@ const _Command = class {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
+
for (const key of Object.keys(options)) {
|
|
164
|
+
if (!fullOptions.has(key)) {
|
|
165
|
+
delete options[key];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
163
168
|
return {
|
|
164
169
|
command: this,
|
|
165
170
|
arguments: argumentss,
|
|
@@ -172,7 +177,7 @@ const _Command = class {
|
|
|
172
177
|
}
|
|
173
178
|
async run(...args) {
|
|
174
179
|
if (this.actionFn) {
|
|
175
|
-
this.actionFn(...args, { logger: this.logger });
|
|
180
|
+
return await this.actionFn(...args, { logger: this.logger });
|
|
176
181
|
} else {
|
|
177
182
|
this.logger.warn(`You may miss action function in "${this.format}"`);
|
|
178
183
|
}
|
|
@@ -228,6 +233,10 @@ class Breadc {
|
|
|
228
233
|
constructor(name, option) {
|
|
229
234
|
this.options = [];
|
|
230
235
|
this.commands = [];
|
|
236
|
+
this.callbacks = {
|
|
237
|
+
pre: [],
|
|
238
|
+
post: []
|
|
239
|
+
};
|
|
231
240
|
this.name = name;
|
|
232
241
|
this._version = option.version ?? "unknown";
|
|
233
242
|
this.description = option.description;
|
|
@@ -369,10 +378,16 @@ class Breadc {
|
|
|
369
378
|
options
|
|
370
379
|
};
|
|
371
380
|
}
|
|
381
|
+
on(event, fn) {
|
|
382
|
+
this.callbacks[event].push(fn);
|
|
383
|
+
}
|
|
372
384
|
async run(args) {
|
|
373
385
|
const parsed = this.parse(args);
|
|
374
386
|
if (parsed.command) {
|
|
375
|
-
|
|
387
|
+
await Promise.all(this.callbacks.pre.map((fn) => fn(parsed.options)));
|
|
388
|
+
const returnValue = await parsed.command.run(...parsed.arguments, parsed.options);
|
|
389
|
+
await Promise.all(this.callbacks.post.map((fn) => fn(parsed.options)));
|
|
390
|
+
return returnValue;
|
|
376
391
|
}
|
|
377
392
|
}
|
|
378
393
|
}
|