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.
Files changed (99) hide show
  1. package/dist/bob-core.cjs +8 -0
  2. package/dist/bob-core.js +515 -0
  3. package/dist/package-B3ICsvyj.js +27 -0
  4. package/dist/package-sv16L6xn.cjs +1 -0
  5. package/dist/{cjs → types}/src/Cli.d.ts +4 -4
  6. package/dist/{esm → types}/src/Command.d.ts +2 -2
  7. package/dist/{cjs → types}/src/CommandParser.d.ts +1 -1
  8. package/dist/{cjs → types}/src/CommandRegistry.d.ts +1 -1
  9. package/dist/{cjs → types}/src/ExceptionHandler.d.ts +1 -1
  10. package/dist/{esm → types}/src/commands/HelpCommand.d.ts +2 -2
  11. package/dist/{esm → types}/src/errors/BadCommandOption.d.ts +1 -1
  12. package/dist/{cjs → types}/src/errors/BadCommandParameter.d.ts +1 -1
  13. package/dist/{cjs → types}/src/errors/CommandNotFoundError.d.ts +1 -1
  14. package/dist/{esm → types}/src/errors/InvalidOption.d.ts +2 -2
  15. package/dist/{cjs → types}/src/errors/MissingRequiredArgumentValue.d.ts +2 -2
  16. package/dist/{cjs → types}/src/errors/MissingSignatureArgument.d.ts +2 -2
  17. package/dist/{cjs → types}/src/errors/MissingSignatureOption.d.ts +2 -2
  18. package/dist/{cjs → types}/src/options/HelpOption.d.ts +2 -2
  19. package/package.json +12 -20
  20. package/dist/cjs/package.json +0 -1
  21. package/dist/cjs/src/Cli.js +0 -66
  22. package/dist/cjs/src/Command.d.ts +0 -34
  23. package/dist/cjs/src/Command.js +0 -97
  24. package/dist/cjs/src/Command.test.d.ts +0 -1
  25. package/dist/cjs/src/Command.test.js +0 -52
  26. package/dist/cjs/src/CommandParser.js +0 -239
  27. package/dist/cjs/src/CommandParser.test.d.ts +0 -1
  28. package/dist/cjs/src/CommandParser.test.js +0 -177
  29. package/dist/cjs/src/CommandRegistry.js +0 -149
  30. package/dist/cjs/src/ExceptionHandler.js +0 -14
  31. package/dist/cjs/src/commands/HelpCommand.d.ts +0 -14
  32. package/dist/cjs/src/commands/HelpCommand.js +0 -90
  33. package/dist/cjs/src/contracts/CommandOption.js +0 -2
  34. package/dist/cjs/src/contracts/index.js +0 -17
  35. package/dist/cjs/src/errors/BadCommandOption.d.ts +0 -11
  36. package/dist/cjs/src/errors/BadCommandOption.js +0 -36
  37. package/dist/cjs/src/errors/BadCommandParameter.js +0 -36
  38. package/dist/cjs/src/errors/BobError.js +0 -6
  39. package/dist/cjs/src/errors/CommandNotFoundError.js +0 -30
  40. package/dist/cjs/src/errors/InvalidOption.d.ts +0 -8
  41. package/dist/cjs/src/errors/InvalidOption.js +0 -32
  42. package/dist/cjs/src/errors/MissingRequiredArgumentValue.js +0 -23
  43. package/dist/cjs/src/errors/MissingSignatureArgument.js +0 -31
  44. package/dist/cjs/src/errors/MissingSignatureOption.js +0 -31
  45. package/dist/cjs/src/errors/index.js +0 -19
  46. package/dist/cjs/src/index.js +0 -21
  47. package/dist/cjs/src/lib/string.js +0 -6
  48. package/dist/cjs/src/options/HelpOption.js +0 -77
  49. package/dist/cjs/src/options/index.js +0 -17
  50. package/dist/esm/package.json +0 -50
  51. package/dist/esm/src/Cli.d.ts +0 -26
  52. package/dist/esm/src/Cli.js +0 -59
  53. package/dist/esm/src/Command.js +0 -93
  54. package/dist/esm/src/Command.test.d.ts +0 -1
  55. package/dist/esm/src/Command.test.js +0 -50
  56. package/dist/esm/src/CommandParser.d.ts +0 -46
  57. package/dist/esm/src/CommandParser.js +0 -232
  58. package/dist/esm/src/CommandParser.test.d.ts +0 -1
  59. package/dist/esm/src/CommandParser.test.js +0 -175
  60. package/dist/esm/src/CommandRegistry.d.ts +0 -17
  61. package/dist/esm/src/CommandRegistry.js +0 -109
  62. package/dist/esm/src/ExceptionHandler.d.ts +0 -4
  63. package/dist/esm/src/ExceptionHandler.js +0 -10
  64. package/dist/esm/src/commands/HelpCommand.js +0 -51
  65. package/dist/esm/src/contracts/CommandOption.d.ts +0 -7
  66. package/dist/esm/src/contracts/CommandOption.js +0 -1
  67. package/dist/esm/src/contracts/index.d.ts +0 -1
  68. package/dist/esm/src/contracts/index.js +0 -1
  69. package/dist/esm/src/errors/BadCommandOption.js +0 -29
  70. package/dist/esm/src/errors/BadCommandParameter.d.ts +0 -11
  71. package/dist/esm/src/errors/BadCommandParameter.js +0 -29
  72. package/dist/esm/src/errors/BobError.d.ts +0 -3
  73. package/dist/esm/src/errors/BobError.js +0 -2
  74. package/dist/esm/src/errors/CommandNotFoundError.d.ts +0 -7
  75. package/dist/esm/src/errors/CommandNotFoundError.js +0 -23
  76. package/dist/esm/src/errors/InvalidOption.js +0 -25
  77. package/dist/esm/src/errors/MissingRequiredArgumentValue.d.ts +0 -7
  78. package/dist/esm/src/errors/MissingRequiredArgumentValue.js +0 -16
  79. package/dist/esm/src/errors/MissingSignatureArgument.d.ts +0 -8
  80. package/dist/esm/src/errors/MissingSignatureArgument.js +0 -24
  81. package/dist/esm/src/errors/MissingSignatureOption.d.ts +0 -8
  82. package/dist/esm/src/errors/MissingSignatureOption.js +0 -24
  83. package/dist/esm/src/errors/index.d.ts +0 -3
  84. package/dist/esm/src/errors/index.js +0 -3
  85. package/dist/esm/src/index.d.ts +0 -5
  86. package/dist/esm/src/index.js +0 -5
  87. package/dist/esm/src/lib/string.d.ts +0 -1
  88. package/dist/esm/src/lib/string.js +0 -3
  89. package/dist/esm/src/options/HelpOption.d.ts +0 -9
  90. package/dist/esm/src/options/HelpOption.js +0 -70
  91. package/dist/esm/src/options/index.d.ts +0 -1
  92. package/dist/esm/src/options/index.js +0 -1
  93. /package/dist/{cjs → types}/src/contracts/CommandOption.d.ts +0 -0
  94. /package/dist/{cjs → types}/src/contracts/index.d.ts +0 -0
  95. /package/dist/{cjs → types}/src/errors/BobError.d.ts +0 -0
  96. /package/dist/{cjs → types}/src/errors/index.d.ts +0 -0
  97. /package/dist/{cjs → types}/src/index.d.ts +0 -0
  98. /package/dist/{cjs → types}/src/lib/string.d.ts +0 -0
  99. /package/dist/{cjs → types}/src/options/index.d.ts +0 -0
@@ -1,31 +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.MissingSignatureArgument = void 0;
7
- const chalk_1 = __importDefault(require("chalk"));
8
- const BobError_js_1 = require("../errors/BobError.js");
9
- class MissingSignatureArgument extends BobError_js_1.BobError {
10
- argument;
11
- argumentSignatures;
12
- constructor(argument, argumentSignatures) {
13
- super(`Missing ${argument} in the command signature`);
14
- this.argument = argument;
15
- this.argumentSignatures = argumentSignatures;
16
- }
17
- pretty() {
18
- const log = console.log;
19
- if (this.argumentSignatures.length) {
20
- log((0, chalk_1.default) `\n{yellow Available arguments}:`);
21
- for (const argument of this.argumentSignatures) {
22
- const type = argument.type ? (0, chalk_1.default) `{white (${argument.type})}` : '';
23
- const spaces = ' '.repeat(20 - argument.name.length);
24
- log((0, chalk_1.default) ` {green ${argument.name}} ${spaces} ${argument.help ?? '\b'} ${type}`);
25
- }
26
- log('');
27
- }
28
- log((0, chalk_1.default) ` {white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`);
29
- }
30
- }
31
- exports.MissingSignatureArgument = MissingSignatureArgument;
@@ -1,31 +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.MissingSignatureOption = void 0;
7
- const chalk_1 = __importDefault(require("chalk"));
8
- const BobError_js_1 = require("../errors/BobError.js");
9
- class MissingSignatureOption extends BobError_js_1.BobError {
10
- option;
11
- optionsSignature;
12
- constructor(option, optionsSignature) {
13
- super(`Missing ${option} in the command signature`);
14
- this.option = option;
15
- this.optionsSignature = optionsSignature;
16
- }
17
- pretty() {
18
- const log = console.log;
19
- if (this.optionsSignature.length) {
20
- log((0, chalk_1.default) `{yellow Available options}:`);
21
- for (const option of this.optionsSignature) {
22
- const type = option.type ? (0, chalk_1.default) `{white (${option.type})}` : '';
23
- const spaces = ' '.repeat(20 - option.name.length);
24
- log((0, chalk_1.default) ` {green ${option.name}} ${spaces} ${option.help ?? '\b'} ${type}`);
25
- }
26
- log('');
27
- }
28
- log((0, chalk_1.default) ` {white.bgRed ERROR } Option {bold.yellow ${this.option}} is missing in the signature.`);
29
- }
30
- }
31
- exports.MissingSignatureOption = MissingSignatureOption;
@@ -1,19 +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("./BobError.js"), exports);
18
- __exportStar(require("./BadCommandParameter.js"), exports);
19
- __exportStar(require("./BadCommandOption.js"), exports);
@@ -1,21 +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("./Command.js"), exports);
18
- __exportStar(require("./Cli.js"), exports);
19
- __exportStar(require("./errors/index.js"), exports);
20
- __exportStar(require("./contracts/index.js"), exports);
21
- __exportStar(require("./options/index.js"), exports);
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateSpace = generateSpace;
4
- function generateSpace(nb) {
5
- return new Array(nb + 5).join(' ');
6
- }
@@ -1,77 +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.HelpOption = void 0;
7
- const chalk_1 = __importDefault(require("chalk"));
8
- const string_js_1 = require("../lib/string.js");
9
- class HelpOption {
10
- option = 'help';
11
- alias = ['h'];
12
- defaultValue = false;
13
- description = (0, chalk_1.default) `Display help for the given command. When no command is given display help for the {green list} command`;
14
- async handler() {
15
- const log = console.log;
16
- const availableArguments = Object.values(this.parser.getArgumentSignatures());
17
- const availableOptions = Object.values(this.parser.getOptionSignatures())
18
- .map((signature) => ({
19
- ...signature,
20
- optionWithAlias: `--${signature.name}${signature.alias?.map(a => `, -${a}`).join('') ?? ''}`
21
- }));
22
- const requiredArguments = availableArguments.filter((signature) => !signature.optional);
23
- log((0, chalk_1.default) `{yellow Description}:`);
24
- log((0, chalk_1.default) ` ${this.description}\n`);
25
- log((0, chalk_1.default) `{yellow Usage}:`);
26
- log((0, chalk_1.default) ` ${this.command} ${requiredArguments.length > 0 ? requiredArguments.map((signature) => `<${signature.name}>`).join(' ') : '\b'} [options]`);
27
- const maxOptionLength = Math.max(...availableOptions.map((signature) => signature.optionWithAlias.length)) ?? 0;
28
- const maxArgumentLength = Math.max(...availableArguments.map((arg) => arg.name.length)) ?? 0;
29
- const maxLength = maxArgumentLength > maxOptionLength ? maxArgumentLength : maxOptionLength;
30
- if (availableArguments.length > 0) {
31
- log((0, chalk_1.default) `\n{yellow Arguments}:`);
32
- for (const signature of availableArguments) {
33
- const spaces = (0, string_js_1.generateSpace)(maxLength - signature.name.length);
34
- let message = (0, chalk_1.default) ` {green ${signature.name}} ${spaces} ${signature.help ?? '\b'}`;
35
- if (signature.defaultValue !== undefined && signature.optional) {
36
- const defaultValue = signature.type === 'array' ? JSON.stringify(signature.defaultValue) : signature.defaultValue;
37
- message += (0, chalk_1.default) ` {yellow [default: ${defaultValue}]}`;
38
- }
39
- if (signature.variadic) {
40
- message += (0, chalk_1.default) ` {white (variadic)}`;
41
- }
42
- log(message);
43
- }
44
- }
45
- if (availableOptions.length > 0) {
46
- log((0, chalk_1.default) `\n{yellow Options}:`);
47
- for (const signature of availableOptions) {
48
- const spaces = (0, string_js_1.generateSpace)(maxLength - signature.optionWithAlias.length);
49
- let message = (0, chalk_1.default) `{green ${signature.optionWithAlias}} ${spaces} ${signature.help ?? '\b'}`;
50
- if (signature.type) {
51
- message += (0, chalk_1.default) ` {white (${signature.type})}`;
52
- }
53
- if (signature.defaultValue !== undefined && signature.optional) {
54
- const defaultValue = signature.type === 'array' ? JSON.stringify(signature.defaultValue) : signature.defaultValue;
55
- message += (0, chalk_1.default) ` {yellow [default: ${defaultValue}]}`;
56
- }
57
- log(message);
58
- }
59
- }
60
- if (this.commandsExamples.length > 0) {
61
- log((0, chalk_1.default) `\n{yellow Examples}:`);
62
- let binaryName = process.argv[0].split('/').pop();
63
- if (binaryName === 'node') {
64
- binaryName += ' ' + process.argv[1].split('/').pop();
65
- }
66
- for (const [index, example] of this.commandsExamples.entries()) {
67
- if (index > 0) {
68
- log('');
69
- }
70
- log(` ${example.description}\n`);
71
- log((0, chalk_1.default) ` {green ${binaryName} ${example.command}}`);
72
- }
73
- }
74
- return -1;
75
- }
76
- }
77
- exports.HelpOption = HelpOption;
@@ -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("./HelpOption.js"), exports);
@@ -1,50 +0,0 @@
1
- {
2
- "name": "bob-core",
3
- "version": "1.2.2",
4
- "description": "BOB Core",
5
- "type": "module",
6
- "files": [
7
- "/dist"
8
- ],
9
- "exports": {
10
- ".": {
11
- "import": "./dist/esm/src/index.js",
12
- "require": "./dist/cjs/src/index.js"
13
- }
14
- },
15
- "typesVersions": {
16
- "*": {
17
- "*": [
18
- "./dist/cjs/index.d.ts"
19
- ]
20
- }
21
- },
22
- "scripts": {
23
- "start": "node -r @swc-node/register debug/main.ts",
24
- "build": "rimraf ./dist && npm run build:cjs && npm run build:esm",
25
- "build:cjs": "tsc --project tsconfig.json --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs && tsc-alias -p tsconfig.json --outDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
26
- "build:esm": "tsc --project tsconfig.json --outDir ./dist/esm && tsc-alias -p tsconfig.json --outDir ./dist/esm",
27
- "prepare": "npm run build",
28
- "test": "jest"
29
- },
30
- "author": "Léo Hubert",
31
- "license": "ISC",
32
- "devDependencies": {
33
- "@swc-node/register": "^1.11.1",
34
- "@types/jest": "^29.5.12",
35
- "@types/lodash": "^4.17.5",
36
- "@types/minimist": "^1.2.5",
37
- "@types/node": "^20.14.5",
38
- "@types/string-similarity": "^4.0.2",
39
- "jest": "^29.7.0",
40
- "rimraf": "^6.0.1",
41
- "ts-jest": "^29.1.5",
42
- "tsc-alias": "^1.8.16",
43
- "typescript": "^5.7.3"
44
- },
45
- "dependencies": {
46
- "chalk": "^4.1.2",
47
- "minimist": "^1.2.8",
48
- "string-similarity": "^4.0.4"
49
- }
50
- }
@@ -1,26 +0,0 @@
1
- import { CommandRegistry } from "./CommandRegistry.js";
2
- import { Command } from "./Command.js";
3
- import HelpCommand from "./commands/HelpCommand.js";
4
- import { ExceptionHandler } from "./ExceptionHandler.js";
5
- export type CliOptions<C> = {
6
- ctx?: C;
7
- name?: string;
8
- version?: string;
9
- };
10
- export declare class Cli<C> {
11
- readonly commandRegistry: CommandRegistry;
12
- private readonly exceptionHandler;
13
- private readonly ctx?;
14
- private readonly helpCommand;
15
- get CommandRegistryClass(): typeof CommandRegistry;
16
- get HelpCommandClass(): typeof HelpCommand;
17
- get ExceptionHandlerClass(): typeof ExceptionHandler;
18
- constructor(opts?: CliOptions<C>);
19
- setCommandResolver(resolver: (path: string) => Promise<Command<C>>): void;
20
- withCommands(...commands: Array<Command<C> | {
21
- new (): Command<C>;
22
- } | string>): Promise<void>;
23
- runCommand(command: string | Command, ...args: any[]): Promise<number>;
24
- runHelpCommand(): Promise<number>;
25
- protected registerCommand(command: Command<C>): void;
26
- }
@@ -1,59 +0,0 @@
1
- import { CommandRegistry } from "./CommandRegistry.js";
2
- import HelpCommand from "./commands/HelpCommand.js";
3
- import { ExceptionHandler } from "./ExceptionHandler.js";
4
- export class Cli {
5
- commandRegistry;
6
- exceptionHandler;
7
- ctx;
8
- helpCommand;
9
- get CommandRegistryClass() {
10
- return CommandRegistry;
11
- }
12
- get HelpCommandClass() {
13
- return HelpCommand;
14
- }
15
- get ExceptionHandlerClass() {
16
- return ExceptionHandler;
17
- }
18
- constructor(opts = {}) {
19
- this.ctx = opts.ctx;
20
- this.commandRegistry = new this.CommandRegistryClass();
21
- this.exceptionHandler = new this.ExceptionHandlerClass();
22
- this.helpCommand = new this.HelpCommandClass({
23
- cliName: opts.name,
24
- cliVersion: opts.version,
25
- commandRegistry: this.commandRegistry
26
- });
27
- }
28
- setCommandResolver(resolver) {
29
- this.commandRegistry.setCommandResolver(resolver);
30
- }
31
- async withCommands(...commands) {
32
- for (const command of commands) {
33
- if (typeof command === 'string') {
34
- await this.commandRegistry.loadCommandsPath(command);
35
- }
36
- else {
37
- if (typeof command === 'function') {
38
- this.registerCommand(new command());
39
- }
40
- else {
41
- this.registerCommand(command);
42
- }
43
- }
44
- }
45
- }
46
- async runCommand(command, ...args) {
47
- if (!command) {
48
- return await this.runHelpCommand();
49
- }
50
- return await this.commandRegistry.runCommand(this.ctx, command, ...args)
51
- .catch(this.exceptionHandler.handle);
52
- }
53
- async runHelpCommand() {
54
- return await this.runCommand(this.helpCommand);
55
- }
56
- registerCommand(command) {
57
- this.commandRegistry.registerCommand(command);
58
- }
59
- }
@@ -1,93 +0,0 @@
1
- import { CommandParser } from "./CommandParser.js";
2
- import { HelpOption } from "./options/index.js";
3
- export class Command {
4
- ctx;
5
- helperDefinitions = {};
6
- commandsExamples = [];
7
- parser;
8
- get CommandParserClass() {
9
- return CommandParser;
10
- }
11
- defaultOptions() {
12
- return [new HelpOption];
13
- }
14
- get command() {
15
- if (this.parser) {
16
- return this.parser.command;
17
- }
18
- return this.signature.split(' ')[0];
19
- }
20
- async run(ctx, ...args) {
21
- this.ctx = ctx;
22
- const defaultOptions = this.defaultOptions();
23
- this.parser = new this.CommandParserClass(this.signature, this.helperDefinitions, defaultOptions, ...args);
24
- for (const option of defaultOptions) {
25
- if (this.parser.option(option.option)) {
26
- const code = await option.handler.call(this);
27
- if (code && code !== 0) {
28
- return code;
29
- }
30
- }
31
- }
32
- this.parser.validate();
33
- return (await this.handle()) ?? 0;
34
- }
35
- setOption(name, value) {
36
- this.parser.setOption(name, value);
37
- }
38
- setArgument(name, value) {
39
- this.parser.setArgument(name, value);
40
- }
41
- option(key, defaultValue = null) {
42
- return this.parser.option(key) ?? defaultValue;
43
- }
44
- optionBoolean(key, defaultValue = false) {
45
- return this.parser.option(key) ?? defaultValue;
46
- }
47
- optionArray(key, defaultValue = []) {
48
- const values = this.parser.option(key);
49
- if (!Array.isArray(values)) {
50
- throw new Error(`Option ${key} is not an array`);
51
- }
52
- if (values.length) {
53
- return values;
54
- }
55
- return defaultValue;
56
- }
57
- optionNumber(key, defaultValue = null) {
58
- const value = this.parser.option(key);
59
- if (!value) {
60
- return defaultValue;
61
- }
62
- if (typeof value === 'number') {
63
- return value;
64
- }
65
- return parseInt(value);
66
- }
67
- argument(key, defaultValue = null) {
68
- return this.parser.argument(key) ?? defaultValue;
69
- }
70
- argumentArray(key, defaultValue = []) {
71
- const values = this.parser.argument(key);
72
- if (!Array.isArray(values)) {
73
- throw new Error(`Argument ${key} is not an array`);
74
- }
75
- if (values?.length) {
76
- return values;
77
- }
78
- return defaultValue;
79
- }
80
- argumentBoolean(key, defaultValue = false) {
81
- return this.parser.argument(key) ?? defaultValue;
82
- }
83
- argumentNumber(key, defaultValue = null) {
84
- const value = this.parser.argument(key);
85
- if (!value) {
86
- return defaultValue;
87
- }
88
- if (typeof value === 'number') {
89
- return value;
90
- }
91
- return parseInt(value);
92
- }
93
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,50 +0,0 @@
1
- import { Command } from './Command.js';
2
- import { MissingRequiredArgumentValue } from "./errors/MissingRequiredArgumentValue.js";
3
- class MockCommand extends Command {
4
- signature = 'mockCommand {argument} {--option}';
5
- description = 'This is a mock command for testing';
6
- handle() {
7
- const opts = this.option('option');
8
- const arg = this.argument('argument');
9
- if (opts) {
10
- return Promise.resolve(11);
11
- }
12
- if (arg === 'value') {
13
- return Promise.resolve(1);
14
- }
15
- else if (arg) {
16
- return Promise.resolve(-1);
17
- }
18
- return Promise.resolve(0);
19
- }
20
- }
21
- describe('Command', () => {
22
- let command;
23
- beforeEach(() => {
24
- command = new MockCommand();
25
- });
26
- it('should have a command', () => {
27
- expect(command.command).toBe('mockCommand');
28
- });
29
- it('should have a signature', () => {
30
- expect(command.signature).toBe('mockCommand {argument} {--option}');
31
- });
32
- it('should have a description', () => {
33
- expect(command.description).toBe('This is a mock command for testing');
34
- });
35
- it('should handle command with argument', async () => {
36
- const result = await command.run(undefined, 'value');
37
- expect(result).toBe(1);
38
- });
39
- it('should handle command with argument', async () => {
40
- const result = await command.run(undefined, 'badValue');
41
- expect(result).toBe(-1);
42
- });
43
- it('should throw error if argument is missing', async () => {
44
- await expect(command.run(undefined)).rejects.toThrowError(MissingRequiredArgumentValue);
45
- });
46
- it('should handle command with option', async () => {
47
- const result = await command.run(undefined, 'value', '--option');
48
- expect(result).toBe(11);
49
- });
50
- });
@@ -1,46 +0,0 @@
1
- import { CommandOption } from "./contracts/index.js";
2
- export type ArgSignature = {
3
- name: string;
4
- type: string;
5
- optional?: boolean;
6
- variadic?: boolean;
7
- alias?: string[];
8
- help?: string;
9
- defaultValue: string | boolean | Array<string> | null;
10
- isOption?: boolean;
11
- };
12
- export declare class CommandParser {
13
- protected readonly signature: string;
14
- protected readonly helperDefinitions: {
15
- [key: string]: string;
16
- };
17
- protected readonly defaultCommandOptions: CommandOption<any>[];
18
- command: string;
19
- private arguments;
20
- private options;
21
- private argumentsSignature;
22
- private optionSignatures;
23
- private optionAliases;
24
- constructor(signature: string, helperDefinitions: {
25
- [key: string]: string;
26
- }, defaultCommandOptions: CommandOption<any>[], ...args: any[]);
27
- option(name: string): any;
28
- setOption(name: string, value: any): void;
29
- optionHelp(name: string): string | undefined;
30
- argument(name: string): any;
31
- setArgument(name: string, value: any): void;
32
- argumentHelp(name: string): string | undefined;
33
- getArgumentSignatures(): {
34
- [argument: string]: ArgSignature;
35
- };
36
- getOptionSignatures(): {
37
- [option: string]: ArgSignature;
38
- };
39
- private getParamValue;
40
- private handleArguments;
41
- private handleOptions;
42
- private parseSignature;
43
- private parseDefaultOptions;
44
- private parseParamSignature;
45
- validate(): void;
46
- }