bob-core 1.2.2 → 1.2.4
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/bob-core.cjs +8 -0
- package/dist/bob-core.js +515 -0
- package/dist/package-B3ICsvyj.js +27 -0
- package/dist/package-sv16L6xn.cjs +1 -0
- package/dist/{cjs → types}/src/Cli.d.ts +4 -4
- package/dist/{esm → types}/src/Command.d.ts +2 -2
- package/dist/{cjs → types}/src/CommandParser.d.ts +1 -1
- package/dist/{cjs → types}/src/CommandRegistry.d.ts +1 -1
- package/dist/{cjs → types}/src/ExceptionHandler.d.ts +1 -1
- package/dist/{esm → types}/src/commands/HelpCommand.d.ts +2 -2
- package/dist/{esm → types}/src/errors/BadCommandOption.d.ts +1 -1
- package/dist/{cjs → types}/src/errors/BadCommandParameter.d.ts +1 -1
- package/dist/{cjs → types}/src/errors/CommandNotFoundError.d.ts +1 -1
- package/dist/{esm → types}/src/errors/InvalidOption.d.ts +2 -2
- package/dist/{cjs → types}/src/errors/MissingRequiredArgumentValue.d.ts +2 -2
- package/dist/{cjs → types}/src/errors/MissingSignatureArgument.d.ts +2 -2
- package/dist/{cjs → types}/src/errors/MissingSignatureOption.d.ts +2 -2
- package/dist/{cjs → types}/src/options/HelpOption.d.ts +2 -2
- package/package.json +12 -20
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/src/Cli.js +0 -66
- package/dist/cjs/src/Command.d.ts +0 -34
- package/dist/cjs/src/Command.js +0 -97
- package/dist/cjs/src/Command.test.d.ts +0 -1
- package/dist/cjs/src/Command.test.js +0 -52
- package/dist/cjs/src/CommandParser.js +0 -239
- package/dist/cjs/src/CommandParser.test.d.ts +0 -1
- package/dist/cjs/src/CommandParser.test.js +0 -177
- package/dist/cjs/src/CommandRegistry.js +0 -149
- package/dist/cjs/src/ExceptionHandler.js +0 -14
- package/dist/cjs/src/commands/HelpCommand.d.ts +0 -14
- package/dist/cjs/src/commands/HelpCommand.js +0 -90
- package/dist/cjs/src/contracts/CommandOption.js +0 -2
- package/dist/cjs/src/contracts/index.js +0 -17
- package/dist/cjs/src/errors/BadCommandOption.d.ts +0 -11
- package/dist/cjs/src/errors/BadCommandOption.js +0 -36
- package/dist/cjs/src/errors/BadCommandParameter.js +0 -36
- package/dist/cjs/src/errors/BobError.js +0 -6
- package/dist/cjs/src/errors/CommandNotFoundError.js +0 -30
- package/dist/cjs/src/errors/InvalidOption.d.ts +0 -8
- package/dist/cjs/src/errors/InvalidOption.js +0 -32
- package/dist/cjs/src/errors/MissingRequiredArgumentValue.js +0 -23
- package/dist/cjs/src/errors/MissingSignatureArgument.js +0 -31
- package/dist/cjs/src/errors/MissingSignatureOption.js +0 -31
- package/dist/cjs/src/errors/index.js +0 -19
- package/dist/cjs/src/index.js +0 -21
- package/dist/cjs/src/lib/string.js +0 -6
- package/dist/cjs/src/options/HelpOption.js +0 -77
- package/dist/cjs/src/options/index.js +0 -17
- package/dist/esm/package.json +0 -50
- package/dist/esm/src/Cli.d.ts +0 -26
- package/dist/esm/src/Cli.js +0 -59
- package/dist/esm/src/Command.js +0 -93
- package/dist/esm/src/Command.test.d.ts +0 -1
- package/dist/esm/src/Command.test.js +0 -50
- package/dist/esm/src/CommandParser.d.ts +0 -46
- package/dist/esm/src/CommandParser.js +0 -232
- package/dist/esm/src/CommandParser.test.d.ts +0 -1
- package/dist/esm/src/CommandParser.test.js +0 -175
- package/dist/esm/src/CommandRegistry.d.ts +0 -17
- package/dist/esm/src/CommandRegistry.js +0 -109
- package/dist/esm/src/ExceptionHandler.d.ts +0 -4
- package/dist/esm/src/ExceptionHandler.js +0 -10
- package/dist/esm/src/commands/HelpCommand.js +0 -51
- package/dist/esm/src/contracts/CommandOption.d.ts +0 -7
- package/dist/esm/src/contracts/CommandOption.js +0 -1
- package/dist/esm/src/contracts/index.d.ts +0 -1
- package/dist/esm/src/contracts/index.js +0 -1
- package/dist/esm/src/errors/BadCommandOption.js +0 -29
- package/dist/esm/src/errors/BadCommandParameter.d.ts +0 -11
- package/dist/esm/src/errors/BadCommandParameter.js +0 -29
- package/dist/esm/src/errors/BobError.d.ts +0 -3
- package/dist/esm/src/errors/BobError.js +0 -2
- package/dist/esm/src/errors/CommandNotFoundError.d.ts +0 -7
- package/dist/esm/src/errors/CommandNotFoundError.js +0 -23
- package/dist/esm/src/errors/InvalidOption.js +0 -25
- package/dist/esm/src/errors/MissingRequiredArgumentValue.d.ts +0 -7
- package/dist/esm/src/errors/MissingRequiredArgumentValue.js +0 -16
- package/dist/esm/src/errors/MissingSignatureArgument.d.ts +0 -8
- package/dist/esm/src/errors/MissingSignatureArgument.js +0 -24
- package/dist/esm/src/errors/MissingSignatureOption.d.ts +0 -8
- package/dist/esm/src/errors/MissingSignatureOption.js +0 -24
- package/dist/esm/src/errors/index.d.ts +0 -3
- package/dist/esm/src/errors/index.js +0 -3
- package/dist/esm/src/index.d.ts +0 -5
- package/dist/esm/src/index.js +0 -5
- package/dist/esm/src/lib/string.d.ts +0 -1
- package/dist/esm/src/lib/string.js +0 -3
- package/dist/esm/src/options/HelpOption.d.ts +0 -9
- package/dist/esm/src/options/HelpOption.js +0 -70
- package/dist/esm/src/options/index.d.ts +0 -1
- package/dist/esm/src/options/index.js +0 -1
- /package/dist/{cjs → types}/src/contracts/CommandOption.d.ts +0 -0
- /package/dist/{cjs → types}/src/contracts/index.d.ts +0 -0
- /package/dist/{cjs → types}/src/errors/BobError.d.ts +0 -0
- /package/dist/{cjs → types}/src/errors/index.d.ts +0 -0
- /package/dist/{cjs → types}/src/index.d.ts +0 -0
- /package/dist/{cjs → types}/src/lib/string.d.ts +0 -0
- /package/dist/{cjs → types}/src/options/index.d.ts +0 -0
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const CommandParser_js_1 = require("./CommandParser.js");
|
|
4
|
-
const MissingRequiredArgumentValue_js_1 = require("./errors/MissingRequiredArgumentValue.js");
|
|
5
|
-
class TestCommandOptions {
|
|
6
|
-
option = 'testOption';
|
|
7
|
-
description = 'Test option';
|
|
8
|
-
defaultValue = 'default';
|
|
9
|
-
alias = ['t'];
|
|
10
|
-
async handler() {
|
|
11
|
-
return 0;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
describe('CommandParser', () => {
|
|
15
|
-
let commandParser;
|
|
16
|
-
const parseCommand = (signature, args, helperDefinition = {}, defaultCommandOptions = []) => {
|
|
17
|
-
return new CommandParser_js_1.CommandParser(signature, helperDefinition, defaultCommandOptions, ...args);
|
|
18
|
-
};
|
|
19
|
-
it('should parse signature without arguments & options', () => {
|
|
20
|
-
commandParser = parseCommand('test', []);
|
|
21
|
-
expect(commandParser.command).toBe('test');
|
|
22
|
-
});
|
|
23
|
-
describe('Arguments', () => {
|
|
24
|
-
it('should parse signature with arguments', () => {
|
|
25
|
-
commandParser = parseCommand('test {arg1} {arg2}', ['value1', 'value2']);
|
|
26
|
-
expect(commandParser.argument('arg1')).toBe('value1');
|
|
27
|
-
expect(commandParser.argument('arg2')).toBe('value2');
|
|
28
|
-
});
|
|
29
|
-
it('should parse signature with optional arguments', () => {
|
|
30
|
-
commandParser = parseCommand('test {arg1?} {arg2?}', ['value1']);
|
|
31
|
-
expect(commandParser.argument('arg1')).toBe('value1');
|
|
32
|
-
expect(commandParser.argument('arg2')).toBeNull();
|
|
33
|
-
});
|
|
34
|
-
it('should parse signature with optional arguments with default value', () => {
|
|
35
|
-
commandParser = parseCommand('test {arg1?} {arg2=defaultValue1}', ['value1']);
|
|
36
|
-
expect(commandParser.argument('arg1')).toBe('value1');
|
|
37
|
-
expect(commandParser.argument('arg2')).toBe('defaultValue1');
|
|
38
|
-
});
|
|
39
|
-
it('should parse signature with variadic arguments', () => {
|
|
40
|
-
commandParser = parseCommand('test {arg1*}', ['value1', 'value2']);
|
|
41
|
-
expect(commandParser.argument('arg1')).toEqual(['value1', 'value2']);
|
|
42
|
-
});
|
|
43
|
-
it('should parse signature with optional variadic arguments', () => {
|
|
44
|
-
commandParser = parseCommand('test {arg1*?}', ['value1', 'value2']);
|
|
45
|
-
expect(commandParser.argument('arg1')).toEqual(['value1', 'value2']);
|
|
46
|
-
});
|
|
47
|
-
it('should parse signature with optional variadic arguments without value', () => {
|
|
48
|
-
commandParser = parseCommand('test {arg1*?}', []);
|
|
49
|
-
expect(commandParser.argument('arg1')).toEqual([]);
|
|
50
|
-
});
|
|
51
|
-
it('should set argument value', () => {
|
|
52
|
-
commandParser = parseCommand('test {arg1}', ['value1']);
|
|
53
|
-
commandParser.setArgument('arg1', 'newValue');
|
|
54
|
-
expect(commandParser.argument('arg1')).toBe('newValue');
|
|
55
|
-
});
|
|
56
|
-
it('should throw error when argument is missing with setArgument', () => {
|
|
57
|
-
commandParser = parseCommand('test {arg1}', []);
|
|
58
|
-
expect(() => commandParser.setArgument('arg2', 'newValue')).toThrowError(Error);
|
|
59
|
-
});
|
|
60
|
-
it('calling validate method should throw error when argument is missing', () => {
|
|
61
|
-
commandParser = parseCommand('test {arg1}', []);
|
|
62
|
-
expect(() => commandParser.validate()).toThrowError(MissingRequiredArgumentValue_js_1.MissingRequiredArgumentValue);
|
|
63
|
-
});
|
|
64
|
-
it('calling validate should throw with variadic argument is missing', () => {
|
|
65
|
-
commandParser = parseCommand('test {arg1*}', []);
|
|
66
|
-
expect(() => commandParser.validate()).toThrowError(MissingRequiredArgumentValue_js_1.MissingRequiredArgumentValue);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
describe('Options', () => {
|
|
70
|
-
it('boolean option should be false when not provided', () => {
|
|
71
|
-
commandParser = parseCommand('test {--option}', []);
|
|
72
|
-
expect(commandParser.option('option')).toBeFalsy();
|
|
73
|
-
});
|
|
74
|
-
it('boolean option should be true when provided', () => {
|
|
75
|
-
commandParser = parseCommand('test {--option}', ['--option']);
|
|
76
|
-
expect(commandParser.option('option')).toBeTruthy();
|
|
77
|
-
});
|
|
78
|
-
it('boolean option should be true when provided with value', () => {
|
|
79
|
-
commandParser = parseCommand('test {--option}', ['--option=true']);
|
|
80
|
-
expect(commandParser.option('option')).toBeTruthy();
|
|
81
|
-
});
|
|
82
|
-
it('boolean option should be false when provided with value', () => {
|
|
83
|
-
commandParser = parseCommand('test {--option}', ['--option=false']);
|
|
84
|
-
expect(commandParser.option('option')).toBeFalsy();
|
|
85
|
-
});
|
|
86
|
-
it('string option should be null when not provided', () => {
|
|
87
|
-
commandParser = parseCommand('test {--option=}', []);
|
|
88
|
-
expect(commandParser.option('option')).toBeNull();
|
|
89
|
-
});
|
|
90
|
-
it('string option should be value when provided', () => {
|
|
91
|
-
commandParser = parseCommand('test {--option=}', ['--option=value']);
|
|
92
|
-
expect(commandParser.option('option')).toBe('value');
|
|
93
|
-
});
|
|
94
|
-
it('string option should take the default value when not provided', () => {
|
|
95
|
-
commandParser = parseCommand('test {--option=default}', []);
|
|
96
|
-
expect(commandParser.option('option')).toBe('default');
|
|
97
|
-
});
|
|
98
|
-
it('string option should take the provided value with default value', () => {
|
|
99
|
-
commandParser = parseCommand('test {--option=default}', ['--option=value']);
|
|
100
|
-
expect(commandParser.option('option')).toBe('value');
|
|
101
|
-
});
|
|
102
|
-
it('array option should be empty when not provided', () => {
|
|
103
|
-
commandParser = parseCommand('test {--option=*}', []);
|
|
104
|
-
expect(commandParser.option('option')).toEqual([]);
|
|
105
|
-
});
|
|
106
|
-
it('array option should be value when provided', () => {
|
|
107
|
-
commandParser = parseCommand('test {--option=*}', ['--option=value1', '--option=value2']);
|
|
108
|
-
expect(commandParser.option('option')).toEqual(['value1', 'value2']);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
describe('Mixed', () => {
|
|
112
|
-
it('should parse signature with arguments and options', () => {
|
|
113
|
-
commandParser = parseCommand('test {arg1} {arg2} {--option}', ['value1', 'value2', '--option']);
|
|
114
|
-
expect(commandParser.argument('arg1')).toBe('value1');
|
|
115
|
-
expect(commandParser.argument('arg2')).toBe('value2');
|
|
116
|
-
expect(commandParser.option('option')).toBeTruthy();
|
|
117
|
-
});
|
|
118
|
-
it('should parse signature with optional arguments and options', () => {
|
|
119
|
-
commandParser = parseCommand('test {arg1?} {arg2?} {--option}', ['value1', '--option']);
|
|
120
|
-
expect(commandParser.argument('arg1')).toBe('value1');
|
|
121
|
-
expect(commandParser.argument('arg2')).toBeNull();
|
|
122
|
-
expect(commandParser.option('option')).toBeTruthy();
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
describe('Helper', () => {
|
|
126
|
-
it('should parse help signature', () => {
|
|
127
|
-
commandParser = parseCommand('test {arg1} {arg2:help 1} {--option : option help 2 }', ['value1', 'value2', '--option']);
|
|
128
|
-
expect(commandParser.argumentHelp('arg1')).toBeUndefined();
|
|
129
|
-
expect(commandParser.argumentHelp('arg2')).toBe('help 1');
|
|
130
|
-
expect(commandParser.optionHelp('option')).toBe('option help 2');
|
|
131
|
-
});
|
|
132
|
-
it('should define help with helperDefinition', () => {
|
|
133
|
-
commandParser = parseCommand('test {arg1} {arg2} {--option} {--option2}', ['value1', 'value2', '--option'], {
|
|
134
|
-
arg1: 'arg1 help',
|
|
135
|
-
arg2: 'arg2 help',
|
|
136
|
-
'--option': 'option help'
|
|
137
|
-
});
|
|
138
|
-
expect(commandParser.argumentHelp('arg1')).toBe('arg1 help');
|
|
139
|
-
expect(commandParser.argumentHelp('arg2')).toBe('arg2 help');
|
|
140
|
-
expect(commandParser.optionHelp('option')).toBe('option help');
|
|
141
|
-
expect(commandParser.optionHelp('option2')).toBeUndefined();
|
|
142
|
-
});
|
|
143
|
-
it('should define help with helperDefinition with default value', () => {
|
|
144
|
-
commandParser = parseCommand('test {arg1:arg1 help} {arg2} {--option=default}', ['value1', 'value2'], {
|
|
145
|
-
arg2: 'arg2 help',
|
|
146
|
-
'--option': 'option help'
|
|
147
|
-
});
|
|
148
|
-
expect(commandParser.argumentHelp('arg1')).toBe('arg1 help');
|
|
149
|
-
expect(commandParser.argumentHelp('arg2')).toBe('arg2 help');
|
|
150
|
-
expect(commandParser.optionHelp('option')).toBe('option help');
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
describe('DefaultCommandOptions', () => {
|
|
154
|
-
it('should parse default command options', () => {
|
|
155
|
-
commandParser = parseCommand('test', [], {}, [new TestCommandOptions()]);
|
|
156
|
-
expect(commandParser.option('testOption')).toBe('default');
|
|
157
|
-
});
|
|
158
|
-
it('should parse default command options with provided value', () => {
|
|
159
|
-
commandParser = parseCommand('test', ['--testOption=value'], {}, [new TestCommandOptions()]);
|
|
160
|
-
expect(commandParser.option('testOption')).toBe('value');
|
|
161
|
-
});
|
|
162
|
-
it('should parse default command options with provided value with alias', () => {
|
|
163
|
-
commandParser = parseCommand('test', ['-t=value'], {}, [new TestCommandOptions()]);
|
|
164
|
-
expect(commandParser.option('testOption')).toBe('value');
|
|
165
|
-
});
|
|
166
|
-
it('should parse default command option help', () => {
|
|
167
|
-
commandParser = parseCommand('test', [], {}, [new TestCommandOptions()]);
|
|
168
|
-
expect(commandParser.optionHelp('testOption')).toBe('Test option');
|
|
169
|
-
});
|
|
170
|
-
it('should handle null default value', () => {
|
|
171
|
-
const option = new TestCommandOptions();
|
|
172
|
-
option.defaultValue = null;
|
|
173
|
-
commandParser = parseCommand('test', ['--testOption=value'], {}, [option]);
|
|
174
|
-
expect(commandParser.option('testOption')).toBe('value');
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
});
|
|
@@ -1,149 +0,0 @@
|
|
|
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 () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.CommandRegistry = void 0;
|
|
40
|
-
const fs = __importStar(require("node:fs"));
|
|
41
|
-
const path_1 = __importDefault(require("path"));
|
|
42
|
-
const SS = __importStar(require("string-similarity"));
|
|
43
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
44
|
-
const CommandNotFoundError_js_1 = require("./errors/CommandNotFoundError.js");
|
|
45
|
-
class CommandRegistry {
|
|
46
|
-
commands = {};
|
|
47
|
-
get commandSuffix() {
|
|
48
|
-
return "Command";
|
|
49
|
-
}
|
|
50
|
-
constructor() { }
|
|
51
|
-
getAvailableCommands() {
|
|
52
|
-
return Object.keys(this.commands);
|
|
53
|
-
}
|
|
54
|
-
getCommands() {
|
|
55
|
-
return Object.values(this.commands);
|
|
56
|
-
}
|
|
57
|
-
commandResolver = async (path) => {
|
|
58
|
-
const CommandClass = (await Promise.resolve(`${path}`).then(s => __importStar(require(s)))).default;
|
|
59
|
-
let command;
|
|
60
|
-
if (CommandClass?.default) {
|
|
61
|
-
command = new CommandClass.default();
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
command = new CommandClass();
|
|
65
|
-
}
|
|
66
|
-
return command;
|
|
67
|
-
};
|
|
68
|
-
setCommandResolver(resolver) {
|
|
69
|
-
this.commandResolver = resolver;
|
|
70
|
-
}
|
|
71
|
-
registerCommand(command, force = false) {
|
|
72
|
-
const commandName = command.signature.split(' ')[0];
|
|
73
|
-
if (!commandName) {
|
|
74
|
-
throw new Error('Command signature is invalid, it must have a command name.');
|
|
75
|
-
}
|
|
76
|
-
if (!force && this.commands[commandName]) {
|
|
77
|
-
throw new Error(`Command ${commandName} already registered.`);
|
|
78
|
-
}
|
|
79
|
-
this.commands[commandName] = command;
|
|
80
|
-
}
|
|
81
|
-
async loadCommandsPath(commandsPath) {
|
|
82
|
-
for await (const file of this.listCommandsFiles(commandsPath)) {
|
|
83
|
-
try {
|
|
84
|
-
const command = await this.commandResolver(file);
|
|
85
|
-
this.registerCommand(command);
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
throw new Error(`Command ${file} failed to load. ${e}`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
async runCommand(ctx, command, ...args) {
|
|
93
|
-
const commandToRun = typeof command === 'string' ? this.commands[command] : command;
|
|
94
|
-
const commandSignature = typeof command === 'string' ? command : commandToRun.command;
|
|
95
|
-
if (!commandToRun) {
|
|
96
|
-
const suggestedCommand = await this.suggestCommand(commandSignature);
|
|
97
|
-
if (suggestedCommand) {
|
|
98
|
-
return await this.runCommand(ctx, suggestedCommand, ...args);
|
|
99
|
-
}
|
|
100
|
-
return 1;
|
|
101
|
-
}
|
|
102
|
-
return await commandToRun.run(ctx, ...args);
|
|
103
|
-
}
|
|
104
|
-
async suggestCommand(command) {
|
|
105
|
-
const availableCommands = this.getAvailableCommands();
|
|
106
|
-
const { bestMatch, bestMatchIndex, ratings } = SS.findBestMatch(command, availableCommands);
|
|
107
|
-
const similarCommands = ratings.filter(r => r.rating > 0.3).map(r => r.target);
|
|
108
|
-
if ((bestMatch.rating > 0 && similarCommands.length <= 1) || (bestMatch.rating > 0.7 && similarCommands.length > 1)) {
|
|
109
|
-
const commandToAsk = availableCommands[bestMatchIndex];
|
|
110
|
-
const runCommand = await this.askRunSimilarCommand(command, commandToAsk);
|
|
111
|
-
if (runCommand) {
|
|
112
|
-
return commandToAsk;
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
throw new CommandNotFoundError_js_1.CommandNotFoundError(command, similarCommands);
|
|
119
|
-
}
|
|
120
|
-
async askRunSimilarCommand(command, commandToAsk) {
|
|
121
|
-
const readline = require('readline').createInterface({
|
|
122
|
-
input: process.stdin,
|
|
123
|
-
output: process.stdout
|
|
124
|
-
});
|
|
125
|
-
console.log((0, chalk_1.default) ` {bgRed ERROR } Command {yellow ${command}} not found.\n`);
|
|
126
|
-
return new Promise((resolve) => {
|
|
127
|
-
readline.question((0, chalk_1.default) `{green Do you want to run {yellow ${commandToAsk}} instead?} {white (yes/no)} [{yellow no}]\n > `, (answer) => {
|
|
128
|
-
resolve(answer === 'yes' || answer === 'y');
|
|
129
|
-
readline.close();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
async *listCommandsFiles(basePath) {
|
|
134
|
-
const dirEntry = fs.readdirSync(basePath, { withFileTypes: true });
|
|
135
|
-
for (const dirent of dirEntry) {
|
|
136
|
-
const direntPath = path_1.default.resolve(basePath, dirent.name);
|
|
137
|
-
if (dirent.isDirectory()) {
|
|
138
|
-
yield* this.listCommandsFiles(path_1.default.resolve(basePath, dirent.name));
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
if (!direntPath.endsWith(`${this.commandSuffix}.ts`) && !direntPath.endsWith(`${this.commandSuffix}.js`)) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
yield direntPath;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.CommandRegistry = CommandRegistry;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ExceptionHandler = void 0;
|
|
4
|
-
const index_js_1 = require("./errors/index.js");
|
|
5
|
-
class ExceptionHandler {
|
|
6
|
-
handle(err) {
|
|
7
|
-
if (err instanceof index_js_1.BobError) {
|
|
8
|
-
err.pretty();
|
|
9
|
-
return -1;
|
|
10
|
-
}
|
|
11
|
-
throw err;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
exports.ExceptionHandler = ExceptionHandler;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Command } from "../Command.js";
|
|
2
|
-
import { CommandRegistry } from "../CommandRegistry.js";
|
|
3
|
-
export type HelpCommandOptions = {
|
|
4
|
-
commandRegistry: CommandRegistry;
|
|
5
|
-
cliName?: string;
|
|
6
|
-
cliVersion?: string;
|
|
7
|
-
};
|
|
8
|
-
export default class HelpCommand extends Command {
|
|
9
|
-
private opts;
|
|
10
|
-
signature: string;
|
|
11
|
-
description: string;
|
|
12
|
-
constructor(opts: HelpCommandOptions);
|
|
13
|
-
protected handle(): Promise<void>;
|
|
14
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
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 () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
40
|
-
const Command_js_1 = require("../Command.js");
|
|
41
|
-
const string_js_1 = require("../lib/string.js");
|
|
42
|
-
class HelpCommand extends Command_js_1.Command {
|
|
43
|
-
opts;
|
|
44
|
-
signature = 'help';
|
|
45
|
-
description = 'Show help';
|
|
46
|
-
constructor(opts) {
|
|
47
|
-
super();
|
|
48
|
-
this.opts = opts;
|
|
49
|
-
}
|
|
50
|
-
async handle() {
|
|
51
|
-
const commands = this.opts.commandRegistry.getCommands();
|
|
52
|
-
const cliName = this.opts.cliName ?? 'Bob CLI';
|
|
53
|
-
const version = this.opts.cliVersion ?? '0.0.0';
|
|
54
|
-
const coreVersion = (await Promise.resolve().then(() => __importStar(require('../../package.json'))))?.default?.version ?? '0.0.0';
|
|
55
|
-
console.log((0, chalk_1.default) `${cliName} {green ${version}} (core: {yellow ${coreVersion}})
|
|
56
|
-
|
|
57
|
-
{yellow Usage}:
|
|
58
|
-
command [options] [arguments]
|
|
59
|
-
|
|
60
|
-
{yellow Available commands}:
|
|
61
|
-
`);
|
|
62
|
-
const maxCommandLength = Math.max(...commands.map(command => command.command.length)) ?? 0;
|
|
63
|
-
const commandByGroups = {};
|
|
64
|
-
for (const command of commands) {
|
|
65
|
-
const commandGroup = command.command.split(':')[0];
|
|
66
|
-
if (!commandByGroups[commandGroup]) {
|
|
67
|
-
commandByGroups[commandGroup] = [];
|
|
68
|
-
}
|
|
69
|
-
commandByGroups[commandGroup].push(command);
|
|
70
|
-
}
|
|
71
|
-
const sortedCommandsByGroups = Object.entries(commandByGroups)
|
|
72
|
-
.sort(([groupA], [groupB]) => groupA.toLowerCase().localeCompare(groupB.toLowerCase()))
|
|
73
|
-
.sort(([, commandsA], [, commandsB]) => commandsA.length - commandsB.length);
|
|
74
|
-
for (const [group, groupCommands] of sortedCommandsByGroups) {
|
|
75
|
-
const isGrouped = groupCommands.length > 1;
|
|
76
|
-
if (isGrouped) {
|
|
77
|
-
console.log((0, chalk_1.default) `{yellow ${group}}:`);
|
|
78
|
-
}
|
|
79
|
-
const sortedGroupCommands = groupCommands.sort((a, b) => a.command.toLowerCase().localeCompare(b.command.toLowerCase()));
|
|
80
|
-
for (const command of sortedGroupCommands) {
|
|
81
|
-
let spaces = (0, string_js_1.generateSpace)(maxCommandLength - command.command.length);
|
|
82
|
-
if (isGrouped) {
|
|
83
|
-
spaces = spaces.slice(2);
|
|
84
|
-
}
|
|
85
|
-
console.log((0, chalk_1.default) `${isGrouped ? ' ' : ''}{green ${command.command}} ${spaces} ${command.description}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.default = HelpCommand;
|
|
@@ -1,17 +0,0 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./CommandOption.js"), exports);
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { BobError } from "../errors/BobError.js";
|
|
2
|
-
export type OptionProps = {
|
|
3
|
-
option: string;
|
|
4
|
-
value?: string;
|
|
5
|
-
reason?: string;
|
|
6
|
-
};
|
|
7
|
-
export declare class BadCommandOption extends BobError {
|
|
8
|
-
readonly param: OptionProps;
|
|
9
|
-
constructor(param: OptionProps);
|
|
10
|
-
pretty(): void;
|
|
11
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.BadCommandOption = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const BobError_js_1 = require("../errors/BobError.js");
|
|
9
|
-
class BadCommandOption extends BobError_js_1.BobError {
|
|
10
|
-
param;
|
|
11
|
-
constructor(param) {
|
|
12
|
-
let message = `Option "${param.option}" value is invalid.`;
|
|
13
|
-
if (param.reason) {
|
|
14
|
-
message += ` Reason: ${param.reason}`;
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
message += ` Value: "${param.value}"`;
|
|
18
|
-
}
|
|
19
|
-
super(message);
|
|
20
|
-
this.param = param;
|
|
21
|
-
}
|
|
22
|
-
pretty() {
|
|
23
|
-
const log = console.log;
|
|
24
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `);
|
|
25
|
-
if (this.param.value || this.param.reason) {
|
|
26
|
-
log('');
|
|
27
|
-
}
|
|
28
|
-
if (this.param.value) {
|
|
29
|
-
log((0, chalk_1.default) ` {blue Value}: ${this.param.value}`);
|
|
30
|
-
}
|
|
31
|
-
if (this.param.reason) {
|
|
32
|
-
log((0, chalk_1.default) ` {yellow Reason}: ${this.param.reason}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.BadCommandOption = BadCommandOption;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.BadCommandParameter = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const BobError_js_1 = require("../errors/BobError.js");
|
|
9
|
-
class BadCommandParameter extends BobError_js_1.BobError {
|
|
10
|
-
param;
|
|
11
|
-
constructor(param) {
|
|
12
|
-
let message = `Argument "${param.param}" value is invalid.`;
|
|
13
|
-
if (param.reason) {
|
|
14
|
-
message += ` Reason: ${param.reason}`;
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
message += ` Value: "${param.value}"`;
|
|
18
|
-
}
|
|
19
|
-
super(message);
|
|
20
|
-
this.param = param;
|
|
21
|
-
}
|
|
22
|
-
pretty() {
|
|
23
|
-
const log = console.log;
|
|
24
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `);
|
|
25
|
-
if (this.param.value || this.param.reason) {
|
|
26
|
-
log('');
|
|
27
|
-
}
|
|
28
|
-
if (this.param.value) {
|
|
29
|
-
log((0, chalk_1.default) ` {blue Value}: ${this.param.value}`);
|
|
30
|
-
}
|
|
31
|
-
if (this.param.reason) {
|
|
32
|
-
log((0, chalk_1.default) ` {yellow Reason}: ${this.param.reason}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.BadCommandParameter = BadCommandParameter;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.CommandNotFoundError = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const BobError_js_1 = require("../errors/BobError.js");
|
|
9
|
-
class CommandNotFoundError extends BobError_js_1.BobError {
|
|
10
|
-
command;
|
|
11
|
-
similarCommands;
|
|
12
|
-
constructor(command, similarCommands) {
|
|
13
|
-
super(`Command "${command}" not found.`);
|
|
14
|
-
this.command = command;
|
|
15
|
-
this.similarCommands = similarCommands;
|
|
16
|
-
}
|
|
17
|
-
pretty() {
|
|
18
|
-
const log = console.log;
|
|
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) {
|
|
22
|
-
log((0, chalk_1.default) ` {gray ⇂ ${cmd}}`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Command "${this.command}" not found.`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.CommandNotFoundError = CommandNotFoundError;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { BobError } from "../errors/BobError.js";
|
|
2
|
-
import { ArgSignature } from "../CommandParser.js";
|
|
3
|
-
export declare class InvalidOption extends BobError {
|
|
4
|
-
private option;
|
|
5
|
-
private optionsSignature;
|
|
6
|
-
constructor(option: string, optionsSignature: ArgSignature[]);
|
|
7
|
-
pretty(): void;
|
|
8
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.InvalidOption = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const BobError_js_1 = require("../errors/BobError.js");
|
|
9
|
-
class InvalidOption extends BobError_js_1.BobError {
|
|
10
|
-
option;
|
|
11
|
-
optionsSignature;
|
|
12
|
-
constructor(option, optionsSignature) {
|
|
13
|
-
super(`Invalid option ${option} in not recognized`);
|
|
14
|
-
this.option = option;
|
|
15
|
-
this.optionsSignature = optionsSignature;
|
|
16
|
-
}
|
|
17
|
-
pretty() {
|
|
18
|
-
const log = console.log;
|
|
19
|
-
if (this.optionsSignature.length > 0) {
|
|
20
|
-
log((0, chalk_1.default) `\n{yellow Available options}:`);
|
|
21
|
-
for (const option of this.optionsSignature) {
|
|
22
|
-
const type = option.type ? (0, chalk_1.default) `{white (${option.type})}` : '';
|
|
23
|
-
const nameWithAlias = `--${option.name}${option.alias?.map(a => `, -${a}`).join('') ?? ''}`;
|
|
24
|
-
const spaces = ' '.repeat(30 - nameWithAlias.length);
|
|
25
|
-
log((0, chalk_1.default) ` {green ${nameWithAlias}} ${spaces} ${option.help ?? '\b'} ${type}`);
|
|
26
|
-
}
|
|
27
|
-
log('');
|
|
28
|
-
}
|
|
29
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Option {bold.yellow ${this.option}} is not recognized.`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.InvalidOption = InvalidOption;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.MissingRequiredArgumentValue = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const BobError_js_1 = require("../errors/BobError.js");
|
|
9
|
-
class MissingRequiredArgumentValue extends BobError_js_1.BobError {
|
|
10
|
-
paramSignature;
|
|
11
|
-
constructor(paramSignature) {
|
|
12
|
-
super(`Argument "${paramSignature.name}" is required.`);
|
|
13
|
-
this.paramSignature = paramSignature;
|
|
14
|
-
}
|
|
15
|
-
pretty() {
|
|
16
|
-
const log = console.log;
|
|
17
|
-
if (this.paramSignature.help) {
|
|
18
|
-
log((0, chalk_1.default) `{yellow Help}: ${this.paramSignature.help}\n`);
|
|
19
|
-
}
|
|
20
|
-
log((0, chalk_1.default) ` {white.bgRed ERROR } Argument {bold.yellow ${this.paramSignature.name}} is required.`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.MissingRequiredArgumentValue = MissingRequiredArgumentValue;
|