bob-core 0.9.4 → 0.9.6
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
CHANGED
|
@@ -11,11 +11,11 @@ export declare class Cli<C> {
|
|
|
11
11
|
get HelpCommandClass(): typeof HelpCommand;
|
|
12
12
|
get ExceptionHandlerClass(): typeof ExceptionHandler;
|
|
13
13
|
constructor(ctx?: C);
|
|
14
|
-
setCommandResolver(resolver: (path: string) => Promise<Command
|
|
15
|
-
withCommands(...commands: Array<Command | {
|
|
16
|
-
new (): Command
|
|
14
|
+
setCommandResolver(resolver: (path: string) => Promise<Command<C>>): void;
|
|
15
|
+
withCommands(...commands: Array<Command<C> | {
|
|
16
|
+
new (): Command<C>;
|
|
17
17
|
} | string>): Promise<void>;
|
|
18
|
-
runCommand(command: string, ...args: any[]): Promise<number>;
|
|
18
|
+
runCommand(command: string | Command, ...args: any[]): Promise<number>;
|
|
19
19
|
runHelpCommand(): Promise<number>;
|
|
20
|
-
protected registerCommand(command: Command): void;
|
|
20
|
+
protected registerCommand(command: Command<C>): void;
|
|
21
21
|
}
|
package/dist/Cli.js
CHANGED
|
@@ -26,7 +26,6 @@ class Cli {
|
|
|
26
26
|
this.commandRegistry = new this.CommandRegistryClass();
|
|
27
27
|
this.exceptionHandler = new this.ExceptionHandlerClass();
|
|
28
28
|
this.helpCommand = new this.HelpCommandClass(this.commandRegistry);
|
|
29
|
-
this.registerCommand(this.helpCommand);
|
|
30
29
|
}
|
|
31
30
|
setCommandResolver(resolver) {
|
|
32
31
|
this.commandRegistry.setCommandResolver(resolver);
|
|
@@ -47,11 +46,14 @@ class Cli {
|
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
async runCommand(command, ...args) {
|
|
49
|
+
if (!command) {
|
|
50
|
+
return await this.runHelpCommand();
|
|
51
|
+
}
|
|
50
52
|
return await this.commandRegistry.runCommand(this.ctx, command, ...args)
|
|
51
53
|
.catch(this.exceptionHandler.handle);
|
|
52
54
|
}
|
|
53
55
|
async runHelpCommand() {
|
|
54
|
-
return await this.runCommand(this.helpCommand
|
|
56
|
+
return await this.runCommand(this.helpCommand);
|
|
55
57
|
}
|
|
56
58
|
registerCommand(command) {
|
|
57
59
|
this.commandRegistry.registerCommand(command);
|
|
@@ -10,6 +10,8 @@ export declare class CommandRegistry {
|
|
|
10
10
|
setCommandResolver(resolver: (path: string) => Promise<Command>): void;
|
|
11
11
|
registerCommand(command: Command, force?: boolean): void;
|
|
12
12
|
loadCommandsPath(commandsPath: string): Promise<void>;
|
|
13
|
-
runCommand(ctx: any, command: string, ...args: any[]): Promise<number>;
|
|
13
|
+
runCommand(ctx: any, command: string | Command, ...args: any[]): Promise<number>;
|
|
14
|
+
private suggestCommand;
|
|
15
|
+
private askRunSimilarCommand;
|
|
14
16
|
private listCommandsFiles;
|
|
15
17
|
}
|
package/dist/CommandRegistry.js
CHANGED
|
@@ -30,6 +30,8 @@ exports.CommandRegistry = void 0;
|
|
|
30
30
|
const path_1 = __importDefault(require("path"));
|
|
31
31
|
const fs = __importStar(require("node:fs"));
|
|
32
32
|
const CommandNotFoundError_1 = require("./errors/CommandNotFoundError");
|
|
33
|
+
const SS = __importStar(require("string-similarity"));
|
|
34
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
33
35
|
class CommandRegistry {
|
|
34
36
|
commands = {};
|
|
35
37
|
get commandSuffix() {
|
|
@@ -78,12 +80,45 @@ class CommandRegistry {
|
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
82
|
async runCommand(ctx, command, ...args) {
|
|
81
|
-
const commandToRun = this.commands[command];
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
const commandToRun = typeof command === 'string' ? this.commands[command] : command;
|
|
84
|
+
const commandSignature = typeof command === 'string' ? command : commandToRun.command;
|
|
85
|
+
if (!commandToRun) {
|
|
86
|
+
const suggestedCommand = await this.suggestCommand(commandSignature);
|
|
87
|
+
if (suggestedCommand) {
|
|
88
|
+
return await this.runCommand(ctx, suggestedCommand, ...args);
|
|
89
|
+
}
|
|
90
|
+
return 1;
|
|
84
91
|
}
|
|
85
92
|
return await commandToRun.run(ctx, ...args);
|
|
86
93
|
}
|
|
94
|
+
async suggestCommand(command) {
|
|
95
|
+
const availableCommands = this.getAvailableCommands();
|
|
96
|
+
const similarCommands = SS.findBestMatch(command, availableCommands).ratings.filter(r => r.rating > 0.2).map(r => r.target);
|
|
97
|
+
if (similarCommands.length === 1) {
|
|
98
|
+
const commandToAsk = similarCommands[0];
|
|
99
|
+
const runCommand = await this.askRunSimilarCommand(command, commandToAsk);
|
|
100
|
+
if (runCommand) {
|
|
101
|
+
return commandToAsk;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
throw new CommandNotFoundError_1.CommandNotFoundError(command, similarCommands);
|
|
108
|
+
}
|
|
109
|
+
async askRunSimilarCommand(command, commandToAsk) {
|
|
110
|
+
const readline = require('readline').createInterface({
|
|
111
|
+
input: process.stdin,
|
|
112
|
+
output: process.stdout
|
|
113
|
+
});
|
|
114
|
+
console.log((0, chalk_1.default) ` {bgRed ERROR } Command {yellow ${command}} not found.\n`);
|
|
115
|
+
return new Promise((resolve) => {
|
|
116
|
+
readline.question((0, chalk_1.default) `{green Do you want to run {yellow ${commandToAsk}} instead?} {white (yes/no)} [{yellow no}]\n > `, (answer) => {
|
|
117
|
+
resolve(answer === 'yes' || answer === 'y');
|
|
118
|
+
readline.close();
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
87
122
|
async *listCommandsFiles(basePath) {
|
|
88
123
|
const dirEntry = fs.readdirSync(basePath, { withFileTypes: true });
|
|
89
124
|
for (const dirent of dirEntry) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BobError } from "./BobError";
|
|
2
2
|
export declare class CommandNotFoundError extends BobError {
|
|
3
3
|
readonly command: string;
|
|
4
|
-
readonly
|
|
5
|
-
constructor(command: string,
|
|
4
|
+
readonly similarCommands: string[];
|
|
5
|
+
constructor(command: string, similarCommands: string[]);
|
|
6
6
|
pretty(): void;
|
|
7
7
|
}
|
|
@@ -1,56 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
6
|
exports.CommandNotFoundError = void 0;
|
|
30
7
|
const BobError_1 = require("./BobError");
|
|
31
|
-
const SS = __importStar(require("string-similarity"));
|
|
32
8
|
const chalk_1 = __importDefault(require("chalk"));
|
|
33
9
|
class CommandNotFoundError extends BobError_1.BobError {
|
|
34
10
|
command;
|
|
35
|
-
|
|
36
|
-
constructor(command,
|
|
11
|
+
similarCommands;
|
|
12
|
+
constructor(command, similarCommands) {
|
|
37
13
|
super(`Command "${command}" not found.`);
|
|
38
14
|
this.command = command;
|
|
39
|
-
this.
|
|
15
|
+
this.similarCommands = similarCommands;
|
|
40
16
|
}
|
|
41
17
|
pretty() {
|
|
42
18
|
const log = console.log;
|
|
43
|
-
|
|
44
|
-
.
|
|
45
|
-
|
|
46
|
-
if (similarCommands.length) {
|
|
47
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Command "${this.command}" is not defined, Did you mean one of these?`);
|
|
48
|
-
for (const cmd of similarCommands) {
|
|
19
|
+
if (this.similarCommands.length) {
|
|
20
|
+
log((0, chalk_1.default) ` {white.bgRed ERROR } Command "${this.command}" not found, Did you mean one of these?`);
|
|
21
|
+
for (const cmd of this.similarCommands) {
|
|
49
22
|
log((0, chalk_1.default) ` {gray ⇂ ${cmd}}`);
|
|
50
23
|
}
|
|
51
24
|
}
|
|
52
25
|
else {
|
|
53
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Command "${this.command}"
|
|
26
|
+
log((0, chalk_1.default) ` {white.bgRed ERROR } Command "${this.command}" not found.`);
|
|
54
27
|
}
|
|
55
28
|
}
|
|
56
29
|
}
|