bob-core 0.5.0 → 0.6.0
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/Cli.d.ts +1 -0
- package/dist/Cli.js +3 -0
- package/dist/Command.d.ts +1 -1
- package/dist/CommandRegistry.d.ts +3 -0
- package/dist/CommandRegistry.js +10 -10
- package/package.json +1 -1
package/dist/Cli.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare class Cli<C> {
|
|
|
10
10
|
get HelpCommandClass(): typeof HelpCommand;
|
|
11
11
|
get ExceptionHandlerClass(): typeof ExceptionHandler;
|
|
12
12
|
constructor(ctx?: C);
|
|
13
|
+
setCommandResolver(resolver: (path: string) => Promise<Command>): Promise<void>;
|
|
13
14
|
loadCommandsPath(commandsPath: string): Promise<void>;
|
|
14
15
|
registerCommand(command: Command): void;
|
|
15
16
|
runCommand(command: string, ...args: any[]): Promise<number>;
|
package/dist/Cli.js
CHANGED
|
@@ -26,6 +26,9 @@ class Cli {
|
|
|
26
26
|
this.exceptionHandler = new this.ExceptionHandlerClass();
|
|
27
27
|
this.registerCommand(new this.HelpCommandClass(this.commandRegistry));
|
|
28
28
|
}
|
|
29
|
+
async setCommandResolver(resolver) {
|
|
30
|
+
this.commandRegistry.setCommandResolver(resolver);
|
|
31
|
+
}
|
|
29
32
|
async loadCommandsPath(commandsPath) {
|
|
30
33
|
await this.commandRegistry.loadCommandsPath(commandsPath);
|
|
31
34
|
}
|
package/dist/Command.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type CommandExample = {
|
|
|
4
4
|
description: string;
|
|
5
5
|
command: string;
|
|
6
6
|
};
|
|
7
|
-
export declare abstract class Command<C =
|
|
7
|
+
export declare abstract class Command<C = undefined> extends CommandHelper {
|
|
8
8
|
abstract signature: string;
|
|
9
9
|
abstract description: string;
|
|
10
10
|
protected ctx: C;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { Command } from "./Command";
|
|
2
|
+
export type CommandResolver = (path: string) => Promise<Command>;
|
|
2
3
|
export declare class CommandRegistry {
|
|
3
4
|
private readonly commands;
|
|
4
5
|
get commandSuffix(): string;
|
|
5
6
|
constructor();
|
|
6
7
|
getAvailableCommands(): string[];
|
|
7
8
|
getCommands(): Command[];
|
|
9
|
+
private commandResolver;
|
|
10
|
+
setCommandResolver(resolver: (path: string) => Promise<Command>): void;
|
|
8
11
|
registerCommand(command: Command, force?: boolean): void;
|
|
9
12
|
loadCommandsPath(commandsPath: string): Promise<void>;
|
|
10
13
|
runCommand(ctx: any, command: string, ...args: any[]): Promise<number>;
|
package/dist/CommandRegistry.js
CHANGED
|
@@ -42,6 +42,14 @@ class CommandRegistry {
|
|
|
42
42
|
getCommands() {
|
|
43
43
|
return Object.values(this.commands);
|
|
44
44
|
}
|
|
45
|
+
commandResolver = async (path) => {
|
|
46
|
+
let CommandClass;
|
|
47
|
+
CommandClass = (await import(path)).default;
|
|
48
|
+
return new CommandClass();
|
|
49
|
+
};
|
|
50
|
+
setCommandResolver(resolver) {
|
|
51
|
+
this.commandResolver = resolver;
|
|
52
|
+
}
|
|
45
53
|
registerCommand(command, force = false) {
|
|
46
54
|
const commandName = command.signature.split(' ')[0];
|
|
47
55
|
if (!commandName) {
|
|
@@ -54,20 +62,12 @@ class CommandRegistry {
|
|
|
54
62
|
}
|
|
55
63
|
async loadCommandsPath(commandsPath) {
|
|
56
64
|
for await (const file of this.listCommandsFiles(commandsPath)) {
|
|
57
|
-
let CommandClass;
|
|
58
|
-
try {
|
|
59
|
-
CommandClass = (await import(file)).default;
|
|
60
|
-
}
|
|
61
|
-
catch (e) {
|
|
62
|
-
console.error(`Failed to load command ${file}. ${e}`);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
65
|
try {
|
|
66
|
-
const command =
|
|
66
|
+
const command = await this.commandResolver(file);
|
|
67
67
|
this.registerCommand(command);
|
|
68
68
|
}
|
|
69
69
|
catch (e) {
|
|
70
|
-
throw new Error(`Command ${file} failed to
|
|
70
|
+
throw new Error(`Command ${file} failed to load. ${e}`);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
}
|