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,5 +1,5 @@
1
- import { BobError } from "../errors/BobError.js";
2
- import { ArgSignature } from "../CommandParser.js";
1
+ import { BobError } from './BobError.js';
2
+ import { ArgSignature } from '../CommandParser.js';
3
3
  export declare class MissingRequiredArgumentValue extends BobError {
4
4
  readonly paramSignature: ArgSignature;
5
5
  constructor(paramSignature: ArgSignature);
@@ -1,5 +1,5 @@
1
- import { BobError } from "../errors/BobError.js";
2
- import { ArgSignature } from "../CommandParser.js";
1
+ import { BobError } from './BobError.js';
2
+ import { ArgSignature } from '../CommandParser.js';
3
3
  export declare class MissingSignatureArgument extends BobError {
4
4
  private argument;
5
5
  private argumentSignatures;
@@ -1,5 +1,5 @@
1
- import { BobError } from "../errors/BobError.js";
2
- import { ArgSignature } from "../CommandParser.js";
1
+ import { BobError } from './BobError.js';
2
+ import { ArgSignature } from '../CommandParser.js';
3
3
  export declare class MissingSignatureOption extends BobError {
4
4
  private option;
5
5
  private optionsSignature;
@@ -1,5 +1,5 @@
1
- import { Command } from "../Command.js";
2
- import { CommandOption } from "../contracts/index.js";
1
+ import { Command } from '../Command.js';
2
+ import { CommandOption } from '../contracts/index.js';
3
3
  export declare class HelpOption implements CommandOption<Command> {
4
4
  option: string;
5
5
  alias: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bob-core",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "BOB Core",
5
5
  "type": "module",
6
6
  "files": [
@@ -8,43 +8,35 @@
8
8
  ],
9
9
  "exports": {
10
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
- ]
11
+ "types": "./dist/types/src/index.d.ts",
12
+ "import": "./dist/bob-core.js",
13
+ "require": "./dist/bob-core.cjs"
20
14
  }
21
15
  },
22
16
  "scripts": {
23
17
  "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",
18
+ "build": "rimraf ./dist && vite build",
27
19
  "prepare": "npm run build",
28
- "test": "jest"
20
+ "test": "vitest run"
29
21
  },
30
22
  "author": "Léo Hubert",
31
23
  "license": "ISC",
32
24
  "devDependencies": {
33
25
  "@swc-node/register": "^1.11.1",
34
- "@types/jest": "^29.5.12",
35
- "@types/lodash": "^4.17.5",
36
26
  "@types/minimist": "^1.2.5",
37
27
  "@types/node": "^20.14.5",
28
+ "@types/prompts": "^2.4.9",
38
29
  "@types/string-similarity": "^4.0.2",
39
- "jest": "^29.7.0",
40
30
  "rimraf": "^6.0.1",
41
- "ts-jest": "^29.1.5",
42
- "tsc-alias": "^1.8.16",
43
- "typescript": "^5.7.3"
31
+ "typescript": "^5.7.3",
32
+ "vite": "^7.1.6",
33
+ "vite-plugin-dts": "^4.5.4",
34
+ "vitest": "^3.2.4"
44
35
  },
45
36
  "dependencies": {
46
37
  "chalk": "^4.1.2",
47
38
  "minimist": "^1.2.8",
39
+ "prompts": "^2.4.2",
48
40
  "string-similarity": "^4.0.4"
49
41
  }
50
42
  }
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1,66 +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.Cli = void 0;
7
- const CommandRegistry_js_1 = require("./CommandRegistry.js");
8
- const HelpCommand_js_1 = __importDefault(require("./commands/HelpCommand.js"));
9
- const ExceptionHandler_js_1 = require("./ExceptionHandler.js");
10
- class Cli {
11
- commandRegistry;
12
- exceptionHandler;
13
- ctx;
14
- helpCommand;
15
- get CommandRegistryClass() {
16
- return CommandRegistry_js_1.CommandRegistry;
17
- }
18
- get HelpCommandClass() {
19
- return HelpCommand_js_1.default;
20
- }
21
- get ExceptionHandlerClass() {
22
- return ExceptionHandler_js_1.ExceptionHandler;
23
- }
24
- constructor(opts = {}) {
25
- this.ctx = opts.ctx;
26
- this.commandRegistry = new this.CommandRegistryClass();
27
- this.exceptionHandler = new this.ExceptionHandlerClass();
28
- this.helpCommand = new this.HelpCommandClass({
29
- cliName: opts.name,
30
- cliVersion: opts.version,
31
- commandRegistry: this.commandRegistry
32
- });
33
- }
34
- setCommandResolver(resolver) {
35
- this.commandRegistry.setCommandResolver(resolver);
36
- }
37
- async withCommands(...commands) {
38
- for (const command of commands) {
39
- if (typeof command === 'string') {
40
- await this.commandRegistry.loadCommandsPath(command);
41
- }
42
- else {
43
- if (typeof command === 'function') {
44
- this.registerCommand(new command());
45
- }
46
- else {
47
- this.registerCommand(command);
48
- }
49
- }
50
- }
51
- }
52
- async runCommand(command, ...args) {
53
- if (!command) {
54
- return await this.runHelpCommand();
55
- }
56
- return await this.commandRegistry.runCommand(this.ctx, command, ...args)
57
- .catch(this.exceptionHandler.handle);
58
- }
59
- async runHelpCommand() {
60
- return await this.runCommand(this.helpCommand);
61
- }
62
- registerCommand(command) {
63
- this.commandRegistry.registerCommand(command);
64
- }
65
- }
66
- exports.Cli = Cli;
@@ -1,34 +0,0 @@
1
- import { CommandParser } from "./CommandParser.js";
2
- import { CommandOption } from "./contracts/index.js";
3
- export type CommandExample = {
4
- description: string;
5
- command: string;
6
- };
7
- export declare abstract class Command<C = any> {
8
- abstract signature: string;
9
- abstract description: string;
10
- protected ctx: C;
11
- protected helperDefinitions: {
12
- [key: string]: string;
13
- };
14
- protected commandsExamples: CommandExample[];
15
- protected parser: CommandParser;
16
- protected abstract handle(): Promise<void | number>;
17
- private get CommandParserClass();
18
- protected defaultOptions(): CommandOption<Command<C>>[];
19
- get command(): string;
20
- run(ctx: C, ...args: any[]): Promise<number>;
21
- protected setOption(name: string, value: any): void;
22
- protected setArgument(name: string, value: any): void;
23
- protected option<T = string>(key: string): T | null;
24
- protected option<T = string>(key: string, defaultValue: T): NoInfer<T>;
25
- protected optionBoolean(key: string, defaultValue?: boolean): boolean;
26
- protected optionArray<T = string>(key: string, defaultValue?: Array<T>): Array<NoInfer<T>>;
27
- protected optionNumber(key: string): number | null;
28
- protected optionNumber(key: string, defaultValue: number): number;
29
- protected argument<T = string>(key: string): T | null;
30
- protected argument<T = string>(key: string, defaultValue: T): NoInfer<T>;
31
- protected argumentArray<T = string>(key: string, defaultValue?: Array<any>): Array<T>;
32
- protected argumentBoolean(key: string, defaultValue?: boolean): boolean;
33
- protected argumentNumber(key: string, defaultValue?: number | null): number | null;
34
- }
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Command = void 0;
4
- const CommandParser_js_1 = require("./CommandParser.js");
5
- const index_js_1 = require("./options/index.js");
6
- class Command {
7
- ctx;
8
- helperDefinitions = {};
9
- commandsExamples = [];
10
- parser;
11
- get CommandParserClass() {
12
- return CommandParser_js_1.CommandParser;
13
- }
14
- defaultOptions() {
15
- return [new index_js_1.HelpOption];
16
- }
17
- get command() {
18
- if (this.parser) {
19
- return this.parser.command;
20
- }
21
- return this.signature.split(' ')[0];
22
- }
23
- async run(ctx, ...args) {
24
- this.ctx = ctx;
25
- const defaultOptions = this.defaultOptions();
26
- this.parser = new this.CommandParserClass(this.signature, this.helperDefinitions, defaultOptions, ...args);
27
- for (const option of defaultOptions) {
28
- if (this.parser.option(option.option)) {
29
- const code = await option.handler.call(this);
30
- if (code && code !== 0) {
31
- return code;
32
- }
33
- }
34
- }
35
- this.parser.validate();
36
- return (await this.handle()) ?? 0;
37
- }
38
- setOption(name, value) {
39
- this.parser.setOption(name, value);
40
- }
41
- setArgument(name, value) {
42
- this.parser.setArgument(name, value);
43
- }
44
- option(key, defaultValue = null) {
45
- return this.parser.option(key) ?? defaultValue;
46
- }
47
- optionBoolean(key, defaultValue = false) {
48
- return this.parser.option(key) ?? defaultValue;
49
- }
50
- optionArray(key, defaultValue = []) {
51
- const values = this.parser.option(key);
52
- if (!Array.isArray(values)) {
53
- throw new Error(`Option ${key} is not an array`);
54
- }
55
- if (values.length) {
56
- return values;
57
- }
58
- return defaultValue;
59
- }
60
- optionNumber(key, defaultValue = null) {
61
- const value = this.parser.option(key);
62
- if (!value) {
63
- return defaultValue;
64
- }
65
- if (typeof value === 'number') {
66
- return value;
67
- }
68
- return parseInt(value);
69
- }
70
- argument(key, defaultValue = null) {
71
- return this.parser.argument(key) ?? defaultValue;
72
- }
73
- argumentArray(key, defaultValue = []) {
74
- const values = this.parser.argument(key);
75
- if (!Array.isArray(values)) {
76
- throw new Error(`Argument ${key} is not an array`);
77
- }
78
- if (values?.length) {
79
- return values;
80
- }
81
- return defaultValue;
82
- }
83
- argumentBoolean(key, defaultValue = false) {
84
- return this.parser.argument(key) ?? defaultValue;
85
- }
86
- argumentNumber(key, defaultValue = null) {
87
- const value = this.parser.argument(key);
88
- if (!value) {
89
- return defaultValue;
90
- }
91
- if (typeof value === 'number') {
92
- return value;
93
- }
94
- return parseInt(value);
95
- }
96
- }
97
- exports.Command = Command;
@@ -1 +0,0 @@
1
- export {};
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const Command_js_1 = require("./Command.js");
4
- const MissingRequiredArgumentValue_js_1 = require("./errors/MissingRequiredArgumentValue.js");
5
- class MockCommand extends Command_js_1.Command {
6
- signature = 'mockCommand {argument} {--option}';
7
- description = 'This is a mock command for testing';
8
- handle() {
9
- const opts = this.option('option');
10
- const arg = this.argument('argument');
11
- if (opts) {
12
- return Promise.resolve(11);
13
- }
14
- if (arg === 'value') {
15
- return Promise.resolve(1);
16
- }
17
- else if (arg) {
18
- return Promise.resolve(-1);
19
- }
20
- return Promise.resolve(0);
21
- }
22
- }
23
- describe('Command', () => {
24
- let command;
25
- beforeEach(() => {
26
- command = new MockCommand();
27
- });
28
- it('should have a command', () => {
29
- expect(command.command).toBe('mockCommand');
30
- });
31
- it('should have a signature', () => {
32
- expect(command.signature).toBe('mockCommand {argument} {--option}');
33
- });
34
- it('should have a description', () => {
35
- expect(command.description).toBe('This is a mock command for testing');
36
- });
37
- it('should handle command with argument', async () => {
38
- const result = await command.run(undefined, 'value');
39
- expect(result).toBe(1);
40
- });
41
- it('should handle command with argument', async () => {
42
- const result = await command.run(undefined, 'badValue');
43
- expect(result).toBe(-1);
44
- });
45
- it('should throw error if argument is missing', async () => {
46
- await expect(command.run(undefined)).rejects.toThrowError(MissingRequiredArgumentValue_js_1.MissingRequiredArgumentValue);
47
- });
48
- it('should handle command with option', async () => {
49
- const result = await command.run(undefined, 'value', '--option');
50
- expect(result).toBe(11);
51
- });
52
- });
@@ -1,239 +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.CommandParser = void 0;
7
- const minimist_1 = __importDefault(require("minimist"));
8
- const MissingRequiredArgumentValue_js_1 = require("./errors/MissingRequiredArgumentValue.js");
9
- const MissingSignatureOption_js_1 = require("./errors/MissingSignatureOption.js");
10
- const MissingSignatureArgument_js_1 = require("./errors/MissingSignatureArgument.js");
11
- const InvalidOption_js_1 = require("./errors/InvalidOption.js");
12
- class CommandParser {
13
- signature;
14
- helperDefinitions;
15
- defaultCommandOptions;
16
- command;
17
- arguments = {};
18
- options = {};
19
- argumentsSignature = {};
20
- optionSignatures = {};
21
- optionAliases = {};
22
- constructor(signature, helperDefinitions, defaultCommandOptions, ...args) {
23
- this.signature = signature;
24
- this.helperDefinitions = helperDefinitions;
25
- this.defaultCommandOptions = defaultCommandOptions;
26
- const [command, ...signatureParams] = signature.split(/\{(.*?)\}/g).map(param => param.trim()).filter(Boolean);
27
- const { _: paramValues, ...optionValues } = (0, minimist_1.default)(args);
28
- this.command = command;
29
- this.parseSignature(signatureParams);
30
- this.parseDefaultOptions();
31
- this.handleArguments(paramValues);
32
- this.handleOptions(optionValues);
33
- }
34
- option(name) {
35
- if (!this.optionSignatures[name]) {
36
- throw new MissingSignatureOption_js_1.MissingSignatureOption(name, Object.values(this.optionSignatures));
37
- }
38
- return this.options[name];
39
- }
40
- setOption(name, value) {
41
- if (!this.optionSignatures[name]) {
42
- throw new MissingSignatureOption_js_1.MissingSignatureOption(name, Object.values(this.optionSignatures));
43
- }
44
- this.options[name] = value;
45
- }
46
- optionHelp(name) {
47
- const optionSignature = this.optionSignatures[name];
48
- if (!optionSignature) {
49
- throw new MissingSignatureOption_js_1.MissingSignatureOption(name, Object.values(this.optionSignatures));
50
- }
51
- return this.optionSignatures[name].help;
52
- }
53
- argument(name) {
54
- if (!this.argumentsSignature[name]) {
55
- throw new MissingSignatureArgument_js_1.MissingSignatureArgument(name, Object.values(this.argumentsSignature));
56
- }
57
- return this.arguments[name];
58
- }
59
- setArgument(name, value) {
60
- if (!this.argumentsSignature[name]) {
61
- throw new MissingSignatureArgument_js_1.MissingSignatureArgument(name, Object.values(this.argumentsSignature));
62
- }
63
- this.arguments[name] = value;
64
- }
65
- argumentHelp(name) {
66
- const argumentSignature = this.argumentsSignature[name];
67
- if (!argumentSignature) {
68
- throw new MissingSignatureArgument_js_1.MissingSignatureArgument(name, Object.values(this.argumentsSignature));
69
- }
70
- return this.argumentsSignature[name].help;
71
- }
72
- getArgumentSignatures() {
73
- return this.argumentsSignature;
74
- }
75
- getOptionSignatures() {
76
- return this.optionSignatures;
77
- }
78
- getParamValue(value, signature) {
79
- if (signature.type === 'boolean') {
80
- if (value === 'true' || value === '1') {
81
- return true;
82
- }
83
- else if (value === 'false' || value === '0') {
84
- return false;
85
- }
86
- return Boolean(value);
87
- }
88
- if (signature.type === 'array') {
89
- if (!value) {
90
- return [];
91
- }
92
- return Array.isArray(value) ? value : [value];
93
- }
94
- return value ?? signature.defaultValue;
95
- }
96
- handleArguments(paramValues) {
97
- for (const [argument, value] of Object.entries(this.arguments)) {
98
- const argSignature = this.argumentsSignature[argument];
99
- if (argSignature.variadic) {
100
- this.arguments[argument] = paramValues;
101
- }
102
- else {
103
- const paramValue = paramValues.shift();
104
- this.arguments[argument] = this.getParamValue(paramValue, argSignature);
105
- }
106
- }
107
- }
108
- handleOptions(optionValues) {
109
- for (const [option, value] of Object.entries(optionValues)) {
110
- const optionAlias = this.optionAliases[option];
111
- const optionSignature = this.optionSignatures[option] ?? this.optionSignatures[optionAlias];
112
- if (!optionSignature) {
113
- throw new InvalidOption_js_1.InvalidOption(option, Object.values(this.optionSignatures));
114
- }
115
- this.options[option] = this.getParamValue(value, optionSignature);
116
- for (const alias of optionSignature.alias ?? []) {
117
- if (optionValues[alias]) {
118
- this.options[optionSignature.name] = optionValues[alias];
119
- }
120
- }
121
- }
122
- }
123
- parseSignature(params) {
124
- for (const paramSignature of params) {
125
- const param = this.parseParamSignature(paramSignature);
126
- if (param.isOption) {
127
- this.options[param.name] = param.defaultValue ?? null;
128
- this.optionSignatures[param.name] = param;
129
- for (const alias of param.alias ?? []) {
130
- this.optionAliases[alias] = param.name;
131
- }
132
- }
133
- else {
134
- this.arguments[param.name] = param.defaultValue ?? null;
135
- this.argumentsSignature[param.name] = param;
136
- }
137
- }
138
- }
139
- parseDefaultOptions() {
140
- if (this.defaultCommandOptions.length) {
141
- for (const option of this.defaultCommandOptions) {
142
- this.optionSignatures[option.option] = {
143
- name: option.option,
144
- type: option.defaultValue == null ? 'string' : typeof option.defaultValue,
145
- optional: true,
146
- alias: option.alias,
147
- variadic: false,
148
- help: option.description,
149
- defaultValue: option.defaultValue ?? null,
150
- isOption: true
151
- };
152
- this.options[option.option] = option.defaultValue;
153
- if (option.alias) {
154
- for (const alias of option.alias) {
155
- this.optionAliases[alias] = option.option;
156
- }
157
- }
158
- }
159
- }
160
- }
161
- parseParamSignature(argument) {
162
- const arg = {
163
- name: argument,
164
- optional: false,
165
- type: 'string',
166
- help: undefined,
167
- defaultValue: null,
168
- variadic: false,
169
- isOption: false
170
- };
171
- if (arg.name.includes(':')) {
172
- const [name, help] = arg.name.split(':');
173
- arg.name = name.trim();
174
- arg.help = help.trim();
175
- }
176
- if (arg.name.includes('=')) {
177
- const [name, defaultValue] = arg.name.split('=');
178
- arg.name = name.trim();
179
- arg.defaultValue = defaultValue.trim();
180
- arg.optional = true;
181
- if (!arg.defaultValue.length) {
182
- arg.defaultValue = null;
183
- }
184
- else if (arg.defaultValue === 'true') {
185
- arg.defaultValue = true;
186
- arg.type = 'boolean';
187
- }
188
- else if (arg.defaultValue === 'false') {
189
- arg.defaultValue = false;
190
- arg.type = 'boolean';
191
- }
192
- }
193
- else {
194
- if (arg.name.startsWith('--')) {
195
- arg.optional = true;
196
- arg.defaultValue = false;
197
- arg.type = 'boolean';
198
- }
199
- }
200
- if (arg.name.includes('|')) {
201
- const [name, ...alias] = arg.name.split('|');
202
- arg.name = name.trim();
203
- arg.alias = alias.map(a => a.trim());
204
- }
205
- if (arg.name.startsWith('--')) {
206
- arg.isOption = true;
207
- arg.name = arg.name.slice(2);
208
- }
209
- if (arg.defaultValue === '*') {
210
- arg.defaultValue = [];
211
- arg.type = 'array';
212
- }
213
- if (arg.name.endsWith('?')) {
214
- arg.optional = true;
215
- arg.name = arg.name.slice(0, -1);
216
- }
217
- if (arg.name.endsWith('*')) {
218
- arg.type = 'array';
219
- arg.variadic = true;
220
- arg.defaultValue = [];
221
- arg.name = arg.name.slice(0, -1);
222
- }
223
- arg.help = arg.help ?? this.helperDefinitions[arg.name] ?? this.helperDefinitions[`--${arg.name}`];
224
- return arg;
225
- }
226
- validate() {
227
- // validate arguments
228
- for (const [argument, value] of Object.entries(this.arguments)) {
229
- const argSignature = this.argumentsSignature[argument];
230
- if (!value && !argSignature.optional) {
231
- throw new MissingRequiredArgumentValue_js_1.MissingRequiredArgumentValue(argSignature);
232
- }
233
- if (!value?.length && argSignature.variadic && !argSignature.optional) {
234
- throw new MissingRequiredArgumentValue_js_1.MissingRequiredArgumentValue(argSignature);
235
- }
236
- }
237
- }
238
- }
239
- exports.CommandParser = CommandParser;
@@ -1 +0,0 @@
1
- export {};