@nu-art/commando 0.401.0 → 0.401.2
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/{shell/core → core}/BaseCommando.d.ts +37 -6
- package/{shell/core → core}/BaseCommando.js +40 -6
- package/core/CliError.d.ts +49 -0
- package/core/CliError.js +58 -0
- package/{shell/core → core}/CommandBuilder.d.ts +27 -2
- package/{shell/core → core}/CommandBuilder.js +32 -3
- package/core/CommandoPool.d.ts +42 -0
- package/core/CommandoPool.js +48 -0
- package/core/class-merger.d.ts +39 -0
- package/core/class-merger.js +50 -0
- package/index.d.ts +11 -0
- package/index.js +16 -0
- package/{shell/interactive → interactive}/CommandoInteractive.d.ts +67 -7
- package/{shell/interactive → interactive}/CommandoInteractive.js +69 -6
- package/{shell/interactive → interactive}/InteractiveShell.d.ts +38 -0
- package/{shell/interactive → interactive}/InteractiveShell.js +25 -0
- package/package.json +24 -15
- package/plugins/basic.d.ts +140 -0
- package/plugins/basic.js +179 -0
- package/plugins/git.d.ts +169 -0
- package/plugins/git.js +219 -0
- package/plugins/nvm.d.ts +59 -0
- package/{shell/plugins → plugins}/nvm.js +47 -0
- package/plugins/pnpm.d.ts +42 -0
- package/{shell/plugins → plugins}/pnpm.js +31 -0
- package/{shell/plugins → plugins}/programming.d.ts +23 -3
- package/{shell/plugins → plugins}/programming.js +23 -3
- package/plugins/python.d.ts +41 -0
- package/plugins/python.js +58 -0
- package/services/nvm.d.ts +76 -0
- package/{shell/services → services}/nvm.js +67 -6
- package/services/pnpm.d.ts +67 -0
- package/{shell/services → services}/pnpm.js +41 -0
- package/simple/Commando.d.ts +92 -0
- package/simple/Commando.js +122 -0
- package/simple/SimpleShell.d.ts +48 -0
- package/{shell/simple → simple}/SimpleShell.js +32 -2
- package/tools.d.ts +33 -0
- package/tools.js +47 -0
- package/types.d.ts +17 -0
- package/cli-params/CLIParamsResolver.d.ts +0 -27
- package/cli-params/CLIParamsResolver.js +0 -97
- package/cli-params/consts.d.ts +0 -6
- package/cli-params/consts.js +0 -27
- package/cli-params/types.d.ts +0 -28
- package/shell/core/CliError.d.ts +0 -14
- package/shell/core/CliError.js +0 -23
- package/shell/core/CommandoPool.d.ts +0 -9
- package/shell/core/CommandoPool.js +0 -14
- package/shell/core/class-merger.d.ts +0 -20
- package/shell/core/class-merger.js +0 -35
- package/shell/index.d.ts +0 -2
- package/shell/index.js +0 -2
- package/shell/plugins/basic.d.ts +0 -59
- package/shell/plugins/basic.js +0 -98
- package/shell/plugins/git.d.ts +0 -54
- package/shell/plugins/git.js +0 -104
- package/shell/plugins/nvm.d.ts +0 -12
- package/shell/plugins/pnpm.d.ts +0 -11
- package/shell/plugins/python.d.ts +0 -10
- package/shell/plugins/python.js +0 -26
- package/shell/services/nvm.d.ts +0 -18
- package/shell/services/pnpm.d.ts +0 -26
- package/shell/simple/Commando.d.ts +0 -17
- package/shell/simple/Commando.js +0 -47
- package/shell/simple/SimpleShell.d.ts +0 -21
- package/shell/tools.d.ts +0 -3
- package/shell/tools.js +0 -17
- package/shell/types.d.ts +0 -3
- package/shell/types.js +0 -1
- /package/{cli-params/types.js → types.js} +0 -0
|
@@ -1,12 +1,39 @@
|
|
|
1
1
|
import { Constructor } from '@nu-art/ts-common';
|
|
2
2
|
import { CommandBuilder } from './CommandBuilder.js';
|
|
3
|
+
/**
|
|
4
|
+
* Base class for shell command execution with plugin support.
|
|
5
|
+
*
|
|
6
|
+
* Provides a fluent API for building shell commands with indentation support.
|
|
7
|
+
* Uses a plugin system that merges multiple classes into a single instance
|
|
8
|
+
* using the class-merger utility.
|
|
9
|
+
*
|
|
10
|
+
* **Plugin System**: The `_create()` method uses class merging to combine
|
|
11
|
+
* BaseCommando with plugin classes, creating a single instance with methods
|
|
12
|
+
* from all merged classes.
|
|
13
|
+
*
|
|
14
|
+
* **Command Building**: Commands are built using the CommandBuilder, which
|
|
15
|
+
* supports indentation and newline handling. The builder accumulates commands
|
|
16
|
+
* until `execute()` is called.
|
|
17
|
+
*
|
|
18
|
+
* **Note**: The `builder` field is marked readonly but is actually set via
|
|
19
|
+
* `@ts-ignore` in `_create()`. This is a type safety issue.
|
|
20
|
+
*/
|
|
3
21
|
export declare class BaseCommando {
|
|
22
|
+
/** Command builder for accumulating shell commands */
|
|
4
23
|
protected readonly builder: CommandBuilder;
|
|
24
|
+
/** Debug mode flag (enables verbose logging) */
|
|
5
25
|
protected _debug: boolean;
|
|
6
26
|
/**
|
|
7
|
-
* Creates a new instance
|
|
8
|
-
*
|
|
9
|
-
*
|
|
27
|
+
* Creates a new BaseCommando instance merged with provided plugins.
|
|
28
|
+
*
|
|
29
|
+
* Uses class merging to combine BaseCommando with plugin classes into
|
|
30
|
+
* a single instance. The builder is initialized after merging.
|
|
31
|
+
*
|
|
32
|
+
* **Note**: Uses `@ts-ignore` to set the readonly `builder` field.
|
|
33
|
+
*
|
|
34
|
+
* @template T - Array of constructor types to merge
|
|
35
|
+
* @param plugins - Plugin classes to merge with BaseCommando
|
|
36
|
+
* @returns Merged instance with BaseCommando and all plugin methods
|
|
10
37
|
*/
|
|
11
38
|
static _create<T extends Constructor<any>[]>(...plugins: T): import("./class-merger.js").MergeTypes<[typeof BaseCommando, ...T]> & BaseCommando;
|
|
12
39
|
/**
|
|
@@ -14,9 +41,12 @@ export declare class BaseCommando {
|
|
|
14
41
|
*/
|
|
15
42
|
constructor();
|
|
16
43
|
/**
|
|
17
|
-
* Toggles or sets
|
|
18
|
-
*
|
|
19
|
-
*
|
|
44
|
+
* Toggles or sets debug mode.
|
|
45
|
+
*
|
|
46
|
+
* When debug is enabled, shell execution provides verbose logging.
|
|
47
|
+
*
|
|
48
|
+
* @param debug - Optional value to set (if omitted, toggles current state)
|
|
49
|
+
* @returns This instance for method chaining
|
|
20
50
|
*/
|
|
21
51
|
debug(debug?: boolean): this;
|
|
22
52
|
/**
|
|
@@ -25,6 +55,7 @@ export declare class BaseCommando {
|
|
|
25
55
|
* @returns {this} - The BaseCommando instance for method chaining.
|
|
26
56
|
*/
|
|
27
57
|
append(command: string): this;
|
|
58
|
+
mark(): this;
|
|
28
59
|
/**
|
|
29
60
|
* Increases the current indentation level by one.
|
|
30
61
|
* @returns {this} - The BaseCommando instance for method chaining.
|
|
@@ -1,13 +1,40 @@
|
|
|
1
1
|
import { ImplementationMissingException } from '@nu-art/ts-common';
|
|
2
2
|
import { CommandBuilder } from './CommandBuilder.js';
|
|
3
3
|
import { CreateMergedInstance } from './class-merger.js';
|
|
4
|
+
/**
|
|
5
|
+
* Base class for shell command execution with plugin support.
|
|
6
|
+
*
|
|
7
|
+
* Provides a fluent API for building shell commands with indentation support.
|
|
8
|
+
* Uses a plugin system that merges multiple classes into a single instance
|
|
9
|
+
* using the class-merger utility.
|
|
10
|
+
*
|
|
11
|
+
* **Plugin System**: The `_create()` method uses class merging to combine
|
|
12
|
+
* BaseCommando with plugin classes, creating a single instance with methods
|
|
13
|
+
* from all merged classes.
|
|
14
|
+
*
|
|
15
|
+
* **Command Building**: Commands are built using the CommandBuilder, which
|
|
16
|
+
* supports indentation and newline handling. The builder accumulates commands
|
|
17
|
+
* until `execute()` is called.
|
|
18
|
+
*
|
|
19
|
+
* **Note**: The `builder` field is marked readonly but is actually set via
|
|
20
|
+
* `@ts-ignore` in `_create()`. This is a type safety issue.
|
|
21
|
+
*/
|
|
4
22
|
export class BaseCommando {
|
|
23
|
+
/** Command builder for accumulating shell commands */
|
|
5
24
|
builder;
|
|
25
|
+
/** Debug mode flag (enables verbose logging) */
|
|
6
26
|
_debug = false;
|
|
7
27
|
/**
|
|
8
|
-
* Creates a new instance
|
|
9
|
-
*
|
|
10
|
-
*
|
|
28
|
+
* Creates a new BaseCommando instance merged with provided plugins.
|
|
29
|
+
*
|
|
30
|
+
* Uses class merging to combine BaseCommando with plugin classes into
|
|
31
|
+
* a single instance. The builder is initialized after merging.
|
|
32
|
+
*
|
|
33
|
+
* **Note**: Uses `@ts-ignore` to set the readonly `builder` field.
|
|
34
|
+
*
|
|
35
|
+
* @template T - Array of constructor types to merge
|
|
36
|
+
* @param plugins - Plugin classes to merge with BaseCommando
|
|
37
|
+
* @returns Merged instance with BaseCommando and all plugin methods
|
|
11
38
|
*/
|
|
12
39
|
static _create(...plugins) {
|
|
13
40
|
const _commando = CreateMergedInstance(BaseCommando, ...plugins);
|
|
@@ -23,9 +50,12 @@ export class BaseCommando {
|
|
|
23
50
|
this.builder = new CommandBuilder();
|
|
24
51
|
}
|
|
25
52
|
/**
|
|
26
|
-
* Toggles or sets
|
|
27
|
-
*
|
|
28
|
-
*
|
|
53
|
+
* Toggles or sets debug mode.
|
|
54
|
+
*
|
|
55
|
+
* When debug is enabled, shell execution provides verbose logging.
|
|
56
|
+
*
|
|
57
|
+
* @param debug - Optional value to set (if omitted, toggles current state)
|
|
58
|
+
* @returns This instance for method chaining
|
|
29
59
|
*/
|
|
30
60
|
debug(debug) {
|
|
31
61
|
this._debug = debug ?? !this._debug;
|
|
@@ -40,6 +70,10 @@ export class BaseCommando {
|
|
|
40
70
|
this.builder.append(command);
|
|
41
71
|
return this;
|
|
42
72
|
}
|
|
73
|
+
mark() {
|
|
74
|
+
this.builder.setMark();
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
43
77
|
/**
|
|
44
78
|
* Increases the current indentation level by one.
|
|
45
79
|
* @returns {this} - The BaseCommando instance for method chaining.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { CustomException } from '@nu-art/ts-common';
|
|
2
|
+
import { ExecException } from 'child_process';
|
|
3
|
+
/**
|
|
4
|
+
* Exception thrown when a shell command execution fails.
|
|
5
|
+
*
|
|
6
|
+
* Contains the command output (stdout/stderr) and the underlying
|
|
7
|
+
* ExecException from Node.js child_process.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CliError extends CustomException {
|
|
10
|
+
/** Standard output from the failed command */
|
|
11
|
+
stdout: string;
|
|
12
|
+
/** Standard error from the failed command */
|
|
13
|
+
stderr: string;
|
|
14
|
+
/** Underlying Node.js ExecException */
|
|
15
|
+
cause: ExecException;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a CliError instance.
|
|
18
|
+
*
|
|
19
|
+
* @param message - Error message
|
|
20
|
+
* @param stdout - Standard output from command
|
|
21
|
+
* @param stderr - Standard error from command
|
|
22
|
+
* @param cause - Underlying ExecException
|
|
23
|
+
*/
|
|
24
|
+
constructor(message: string, stdout: string, stderr: string, cause: ExecException);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Exception for commando-specific errors with exit code.
|
|
28
|
+
*
|
|
29
|
+
* Similar to CliError but includes an explicit exit code rather than
|
|
30
|
+
* extracting it from the ExecException.
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export declare class CommandoException extends CustomException {
|
|
34
|
+
/** Standard output from the command */
|
|
35
|
+
stdout: string;
|
|
36
|
+
/** Standard error from the command */
|
|
37
|
+
stderr: string;
|
|
38
|
+
/** Exit code from the command */
|
|
39
|
+
exitCode: number;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a CommandoException instance.
|
|
42
|
+
*
|
|
43
|
+
* @param message - Error message
|
|
44
|
+
* @param stdout - Standard output
|
|
45
|
+
* @param stderr - Standard error
|
|
46
|
+
* @param exitCode - Command exit code
|
|
47
|
+
*/
|
|
48
|
+
constructor(message: string, stdout: string, stderr: string, exitCode: number);
|
|
49
|
+
}
|
package/core/CliError.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { CustomException } from '@nu-art/ts-common';
|
|
2
|
+
/**
|
|
3
|
+
* Exception thrown when a shell command execution fails.
|
|
4
|
+
*
|
|
5
|
+
* Contains the command output (stdout/stderr) and the underlying
|
|
6
|
+
* ExecException from Node.js child_process.
|
|
7
|
+
*/
|
|
8
|
+
export class CliError extends CustomException {
|
|
9
|
+
/** Standard output from the failed command */
|
|
10
|
+
stdout;
|
|
11
|
+
/** Standard error from the failed command */
|
|
12
|
+
stderr;
|
|
13
|
+
/** Underlying Node.js ExecException */
|
|
14
|
+
cause;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a CliError instance.
|
|
17
|
+
*
|
|
18
|
+
* @param message - Error message
|
|
19
|
+
* @param stdout - Standard output from command
|
|
20
|
+
* @param stderr - Standard error from command
|
|
21
|
+
* @param cause - Underlying ExecException
|
|
22
|
+
*/
|
|
23
|
+
constructor(message, stdout, stderr, cause) {
|
|
24
|
+
super(CliError, message, cause);
|
|
25
|
+
this.stdout = stdout;
|
|
26
|
+
this.stderr = stderr;
|
|
27
|
+
this.cause = cause;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Exception for commando-specific errors with exit code.
|
|
32
|
+
*
|
|
33
|
+
* Similar to CliError but includes an explicit exit code rather than
|
|
34
|
+
* extracting it from the ExecException.
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
export class CommandoException extends CustomException {
|
|
38
|
+
/** Standard output from the command */
|
|
39
|
+
stdout;
|
|
40
|
+
/** Standard error from the command */
|
|
41
|
+
stderr;
|
|
42
|
+
/** Exit code from the command */
|
|
43
|
+
exitCode;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a CommandoException instance.
|
|
46
|
+
*
|
|
47
|
+
* @param message - Error message
|
|
48
|
+
* @param stdout - Standard output
|
|
49
|
+
* @param stderr - Standard error
|
|
50
|
+
* @param exitCode - Command exit code
|
|
51
|
+
*/
|
|
52
|
+
constructor(message, stdout, stderr, exitCode) {
|
|
53
|
+
super(CommandoException, message);
|
|
54
|
+
this.stdout = stdout;
|
|
55
|
+
this.stderr = stderr;
|
|
56
|
+
this.exitCode = exitCode;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -5,9 +5,26 @@ type Options = {
|
|
|
5
5
|
newlineDelimiter: string;
|
|
6
6
|
indentation: number;
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Builds shell commands with indentation and formatting support.
|
|
10
|
+
*
|
|
11
|
+
* Accumulates commands in an array and formats them with proper indentation.
|
|
12
|
+
* Supports custom newline delimiters and indentation levels. Commands can
|
|
13
|
+
* be split across multiple lines using the newline delimiter.
|
|
14
|
+
*
|
|
15
|
+
* **Behavior**:
|
|
16
|
+
* - Commands are trimmed before adding
|
|
17
|
+
* - Empty commands are preserved (for spacing)
|
|
18
|
+
* - Indentation is applied per line when commands contain newlines
|
|
19
|
+
* - `reset()` returns the accumulated command and clears the builder
|
|
20
|
+
*/
|
|
8
21
|
export declare class CommandBuilder {
|
|
22
|
+
private initialCommands;
|
|
23
|
+
/** Array of accumulated command strings */
|
|
9
24
|
commands: string[];
|
|
25
|
+
/** Current indentation level (number of indent steps) */
|
|
10
26
|
private indentation;
|
|
27
|
+
/** Configuration options for formatting */
|
|
11
28
|
private option;
|
|
12
29
|
/**
|
|
13
30
|
* Constructs a CommandBuilder instance with given options.
|
|
@@ -19,6 +36,7 @@ export declare class CommandBuilder {
|
|
|
19
36
|
* @returns {string} - A string containing spaces for the current indentation level.
|
|
20
37
|
*/
|
|
21
38
|
protected getIndentation: () => string;
|
|
39
|
+
setMark(): void;
|
|
22
40
|
/**
|
|
23
41
|
* Increases the current indentation level by one.
|
|
24
42
|
*/
|
|
@@ -34,8 +52,15 @@ export declare class CommandBuilder {
|
|
|
34
52
|
emptyLine(): this;
|
|
35
53
|
/**
|
|
36
54
|
* Appends a command to the command list with proper indentation.
|
|
37
|
-
*
|
|
38
|
-
*
|
|
55
|
+
*
|
|
56
|
+
* **Behavior**:
|
|
57
|
+
* - Splits the command by the newline delimiter (allows multi-line commands)
|
|
58
|
+
* - Trims each line
|
|
59
|
+
* - Applies current indentation to non-empty lines
|
|
60
|
+
* - Preserves empty lines as-is (for spacing)
|
|
61
|
+
*
|
|
62
|
+
* @param command - Command string to append (can contain newlines)
|
|
63
|
+
* @returns This instance for method chaining
|
|
39
64
|
*/
|
|
40
65
|
readonly append: (command: string) => this;
|
|
41
66
|
/**
|
|
@@ -5,9 +5,26 @@ const defaultOptions = {
|
|
|
5
5
|
newlineDelimiter: '\n',
|
|
6
6
|
indentation: 2,
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Builds shell commands with indentation and formatting support.
|
|
10
|
+
*
|
|
11
|
+
* Accumulates commands in an array and formats them with proper indentation.
|
|
12
|
+
* Supports custom newline delimiters and indentation levels. Commands can
|
|
13
|
+
* be split across multiple lines using the newline delimiter.
|
|
14
|
+
*
|
|
15
|
+
* **Behavior**:
|
|
16
|
+
* - Commands are trimmed before adding
|
|
17
|
+
* - Empty commands are preserved (for spacing)
|
|
18
|
+
* - Indentation is applied per line when commands contain newlines
|
|
19
|
+
* - `reset()` returns the accumulated command and clears the builder
|
|
20
|
+
*/
|
|
8
21
|
export class CommandBuilder {
|
|
9
|
-
|
|
22
|
+
initialCommands = [];
|
|
23
|
+
/** Array of accumulated command strings */
|
|
24
|
+
commands;
|
|
25
|
+
/** Current indentation level (number of indent steps) */
|
|
10
26
|
indentation = 0;
|
|
27
|
+
/** Configuration options for formatting */
|
|
11
28
|
option = defaultOptions;
|
|
12
29
|
/**
|
|
13
30
|
* Constructs a CommandBuilder instance with given options.
|
|
@@ -15,6 +32,7 @@ export class CommandBuilder {
|
|
|
15
32
|
*/
|
|
16
33
|
constructor(options = defaultOptions) {
|
|
17
34
|
this.option = options;
|
|
35
|
+
this.commands = [...this.initialCommands];
|
|
18
36
|
}
|
|
19
37
|
/**
|
|
20
38
|
* Generates a string of spaces for indentation based on the current indentation level.
|
|
@@ -23,6 +41,9 @@ export class CommandBuilder {
|
|
|
23
41
|
getIndentation = () => {
|
|
24
42
|
return ' '.repeat(this.option.indentation * this.indentation);
|
|
25
43
|
};
|
|
44
|
+
setMark() {
|
|
45
|
+
this.initialCommands = [...this.commands];
|
|
46
|
+
}
|
|
26
47
|
/**
|
|
27
48
|
* Increases the current indentation level by one.
|
|
28
49
|
*/
|
|
@@ -45,8 +66,15 @@ export class CommandBuilder {
|
|
|
45
66
|
}
|
|
46
67
|
/**
|
|
47
68
|
* Appends a command to the command list with proper indentation.
|
|
48
|
-
*
|
|
49
|
-
*
|
|
69
|
+
*
|
|
70
|
+
* **Behavior**:
|
|
71
|
+
* - Splits the command by the newline delimiter (allows multi-line commands)
|
|
72
|
+
* - Trims each line
|
|
73
|
+
* - Applies current indentation to non-empty lines
|
|
74
|
+
* - Preserves empty lines as-is (for spacing)
|
|
75
|
+
*
|
|
76
|
+
* @param command - Command string to append (can contain newlines)
|
|
77
|
+
* @returns This instance for method chaining
|
|
50
78
|
*/
|
|
51
79
|
append = (command) => {
|
|
52
80
|
const commands = command.split(this.option.newlineDelimiter);
|
|
@@ -73,6 +101,7 @@ export class CommandBuilder {
|
|
|
73
101
|
reset() {
|
|
74
102
|
const command = this.getCommand();
|
|
75
103
|
this.commands.length = 0;
|
|
104
|
+
this.commands.push(...this.initialCommands);
|
|
76
105
|
return command;
|
|
77
106
|
}
|
|
78
107
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Constructor } from '@nu-art/ts-common';
|
|
2
|
+
import { CommandoInteractive } from '../interactive/CommandoInteractive.js';
|
|
3
|
+
import { BaseCommando } from './BaseCommando.js';
|
|
4
|
+
import { Commando_Basic } from '../plugins/basic.js';
|
|
5
|
+
import { MergeTypes } from './class-merger.js';
|
|
6
|
+
/**
|
|
7
|
+
* Pool manager for Commando instances with lifecycle management.
|
|
8
|
+
*
|
|
9
|
+
* Tracks all allocated Commando instances and provides a way to kill
|
|
10
|
+
* all of them at once. Useful for cleanup in long-running processes
|
|
11
|
+
* or test teardown.
|
|
12
|
+
*
|
|
13
|
+
* **Behavior**:
|
|
14
|
+
* - All allocated commandos are stored in an internal pool
|
|
15
|
+
* - `allocateCommando()` always includes Commando_Basic plugin
|
|
16
|
+
* - `killAll()` terminates all tracked commandos asynchronously
|
|
17
|
+
*
|
|
18
|
+
* **Use Case**: Managing multiple interactive commandos that need
|
|
19
|
+
* to be cleaned up together (e.g., test suites, long-running scripts).
|
|
20
|
+
*/
|
|
21
|
+
export declare const CommandoPool: {
|
|
22
|
+
/**
|
|
23
|
+
* Allocates a new Commando instance and adds it to the pool.
|
|
24
|
+
*
|
|
25
|
+
* Creates a CommandoInteractive instance with the provided plugins
|
|
26
|
+
* plus Commando_Basic (always included). The instance is tracked
|
|
27
|
+
* in the pool for later cleanup.
|
|
28
|
+
*
|
|
29
|
+
* @template T - Array of plugin constructor types
|
|
30
|
+
* @param uid - Unique identifier for this commando instance
|
|
31
|
+
* @param plugins - Plugin classes to merge with the commando
|
|
32
|
+
* @returns Merged commando instance with all plugins
|
|
33
|
+
*/
|
|
34
|
+
allocateCommando: <T extends Constructor<any>[]>(uid: string, ...plugins: T) => MergeTypes<[...T]> & CommandoInteractive & BaseCommando & Commando_Basic;
|
|
35
|
+
/**
|
|
36
|
+
* Kills all allocated commando instances.
|
|
37
|
+
*
|
|
38
|
+
* Calls `kill()` on all commandos in the pool asynchronously.
|
|
39
|
+
* Useful for cleanup in test teardown or application shutdown.
|
|
40
|
+
*/
|
|
41
|
+
killAll: () => Promise<void>;
|
|
42
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { CommandoInteractive } from '../interactive/CommandoInteractive.js';
|
|
2
|
+
import { Commando_Basic } from '../plugins/basic.js';
|
|
3
|
+
/** Internal pool of allocated commando instances */
|
|
4
|
+
const commandoPool = [];
|
|
5
|
+
/**
|
|
6
|
+
* Pool manager for Commando instances with lifecycle management.
|
|
7
|
+
*
|
|
8
|
+
* Tracks all allocated Commando instances and provides a way to kill
|
|
9
|
+
* all of them at once. Useful for cleanup in long-running processes
|
|
10
|
+
* or test teardown.
|
|
11
|
+
*
|
|
12
|
+
* **Behavior**:
|
|
13
|
+
* - All allocated commandos are stored in an internal pool
|
|
14
|
+
* - `allocateCommando()` always includes Commando_Basic plugin
|
|
15
|
+
* - `killAll()` terminates all tracked commandos asynchronously
|
|
16
|
+
*
|
|
17
|
+
* **Use Case**: Managing multiple interactive commandos that need
|
|
18
|
+
* to be cleaned up together (e.g., test suites, long-running scripts).
|
|
19
|
+
*/
|
|
20
|
+
export const CommandoPool = {
|
|
21
|
+
/**
|
|
22
|
+
* Allocates a new Commando instance and adds it to the pool.
|
|
23
|
+
*
|
|
24
|
+
* Creates a CommandoInteractive instance with the provided plugins
|
|
25
|
+
* plus Commando_Basic (always included). The instance is tracked
|
|
26
|
+
* in the pool for later cleanup.
|
|
27
|
+
*
|
|
28
|
+
* @template T - Array of plugin constructor types
|
|
29
|
+
* @param uid - Unique identifier for this commando instance
|
|
30
|
+
* @param plugins - Plugin classes to merge with the commando
|
|
31
|
+
* @returns Merged commando instance with all plugins
|
|
32
|
+
*/
|
|
33
|
+
allocateCommando: (uid, ...plugins) => {
|
|
34
|
+
const commando = CommandoInteractive.create(...plugins, Commando_Basic);
|
|
35
|
+
commando.setUID(uid);
|
|
36
|
+
commandoPool.push(commando);
|
|
37
|
+
return commando;
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* Kills all allocated commando instances.
|
|
41
|
+
*
|
|
42
|
+
* Calls `kill()` on all commandos in the pool asynchronously.
|
|
43
|
+
* Useful for cleanup in test teardown or application shutdown.
|
|
44
|
+
*/
|
|
45
|
+
killAll: async () => {
|
|
46
|
+
await Promise.all(commandoPool.map(c => c.kill()));
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Constructor } from '@nu-art/ts-common';
|
|
2
|
+
/**
|
|
3
|
+
* Recursively merges the instance types of multiple constructors.
|
|
4
|
+
*
|
|
5
|
+
* Creates an intersection type of all constructor instance types.
|
|
6
|
+
* Used for the plugin system to combine multiple classes into one.
|
|
7
|
+
*
|
|
8
|
+
* @template T - Array of constructor types
|
|
9
|
+
*/
|
|
10
|
+
export type MergeTypes<T extends Constructor<any>[]> = T extends [a: Constructor<infer A>, ...rest: infer R] ? R extends Constructor<any>[] ? A & MergeTypes<R> : {} : {};
|
|
11
|
+
/**
|
|
12
|
+
* Merges multiple classes into a single class constructor.
|
|
13
|
+
*
|
|
14
|
+
* Creates a new class that combines all properties and methods from
|
|
15
|
+
* the provided plugin classes. Properties are copied from prototype
|
|
16
|
+
* descriptors during construction.
|
|
17
|
+
*
|
|
18
|
+
* **Use Case**: Plugin system for Commando - allows combining
|
|
19
|
+
* BaseCommando with plugin classes (e.g., Commando_Git, Commando_NVM).
|
|
20
|
+
*
|
|
21
|
+
* **Limitation**: Only copies own properties from prototypes, not
|
|
22
|
+
* inherited properties or static members.
|
|
23
|
+
*
|
|
24
|
+
* @template T - Array of constructor types
|
|
25
|
+
* @param plugins - Constructor classes to merge
|
|
26
|
+
* @returns New constructor that merges all plugin classes
|
|
27
|
+
*/
|
|
28
|
+
export declare function MergeClass<T extends Constructor<any>[]>(...plugins: T): Constructor<MergeTypes<T>>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates an instance of a merged class from multiple constructors.
|
|
31
|
+
*
|
|
32
|
+
* Convenience function that merges classes and immediately instantiates
|
|
33
|
+
* the result. Used by BaseCommando._create() to create plugin instances.
|
|
34
|
+
*
|
|
35
|
+
* @template T - Array of constructor types
|
|
36
|
+
* @param plugins - Constructor classes to merge and instantiate
|
|
37
|
+
* @returns Instance of the merged class
|
|
38
|
+
*/
|
|
39
|
+
export declare function CreateMergedInstance<T extends Constructor<any>[]>(...plugins: T): MergeTypes<T>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merges multiple classes into a single class constructor.
|
|
3
|
+
*
|
|
4
|
+
* Creates a new class that combines all properties and methods from
|
|
5
|
+
* the provided plugin classes. Properties are copied from prototype
|
|
6
|
+
* descriptors during construction.
|
|
7
|
+
*
|
|
8
|
+
* **Use Case**: Plugin system for Commando - allows combining
|
|
9
|
+
* BaseCommando with plugin classes (e.g., Commando_Git, Commando_NVM).
|
|
10
|
+
*
|
|
11
|
+
* **Limitation**: Only copies own properties from prototypes, not
|
|
12
|
+
* inherited properties or static members.
|
|
13
|
+
*
|
|
14
|
+
* @template T - Array of constructor types
|
|
15
|
+
* @param plugins - Constructor classes to merge
|
|
16
|
+
* @returns New constructor that merges all plugin classes
|
|
17
|
+
*/
|
|
18
|
+
export function MergeClass(...plugins) {
|
|
19
|
+
class SuperClass {
|
|
20
|
+
/**
|
|
21
|
+
* Constructs an instance of SuperClass, merging properties from all plugins.
|
|
22
|
+
* @param {...any[]} args - The arguments to pass to the constructors.
|
|
23
|
+
*/
|
|
24
|
+
constructor(...args) {
|
|
25
|
+
plugins.forEach(plugin => {
|
|
26
|
+
Object.getOwnPropertyNames(plugin.prototype).forEach(name => {
|
|
27
|
+
const prop = Object.getOwnPropertyDescriptor(plugin.prototype, name);
|
|
28
|
+
if (prop) {
|
|
29
|
+
Object.defineProperty(this, name, prop);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return SuperClass;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates an instance of a merged class from multiple constructors.
|
|
39
|
+
*
|
|
40
|
+
* Convenience function that merges classes and immediately instantiates
|
|
41
|
+
* the result. Used by BaseCommando._create() to create plugin instances.
|
|
42
|
+
*
|
|
43
|
+
* @template T - Array of constructor types
|
|
44
|
+
* @param plugins - Constructor classes to merge and instantiate
|
|
45
|
+
* @returns Instance of the merged class
|
|
46
|
+
*/
|
|
47
|
+
export function CreateMergedInstance(...plugins) {
|
|
48
|
+
const SuperClass = MergeClass(...plugins);
|
|
49
|
+
return new SuperClass();
|
|
50
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './core/BaseCommando.js';
|
|
2
|
+
export * from './core/CliError.js';
|
|
3
|
+
export * from './core/CommandoPool.js';
|
|
4
|
+
export * from './core/class-merger.js';
|
|
5
|
+
export * from './interactive/CommandoInteractive.js';
|
|
6
|
+
export * from './simple/Commando.js';
|
|
7
|
+
export * from './plugins/basic.js';
|
|
8
|
+
export * from './plugins/nvm.js';
|
|
9
|
+
export * from './plugins/pnpm.js';
|
|
10
|
+
export * from './services/pnpm.js';
|
|
11
|
+
export * from './types.js';
|
package/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Core exports
|
|
2
|
+
export * from './core/BaseCommando.js';
|
|
3
|
+
export * from './core/CliError.js';
|
|
4
|
+
export * from './core/CommandoPool.js';
|
|
5
|
+
export * from './core/class-merger.js';
|
|
6
|
+
// Interactive and Simple commandos
|
|
7
|
+
export * from './interactive/CommandoInteractive.js';
|
|
8
|
+
export * from './simple/Commando.js';
|
|
9
|
+
// Plugins
|
|
10
|
+
export * from './plugins/basic.js';
|
|
11
|
+
export * from './plugins/nvm.js';
|
|
12
|
+
export * from './plugins/pnpm.js';
|
|
13
|
+
// Services
|
|
14
|
+
export * from './services/pnpm.js';
|
|
15
|
+
// Types
|
|
16
|
+
export * from './types.js';
|