@nu-art/commando 0.401.0 → 0.401.1
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/cli-params/CLIParamsResolver.d.ts +56 -3
- package/cli-params/CLIParamsResolver.js +56 -3
- package/cli-params/consts.d.ts +37 -0
- package/cli-params/consts.js +38 -1
- package/cli-params/types.d.ts +28 -0
- package/package.json +23 -11
- package/shell/core/BaseCommando.d.ts +36 -6
- package/shell/core/BaseCommando.js +36 -6
- package/shell/core/CliError.d.ts +35 -0
- package/shell/core/CliError.js +36 -1
- package/shell/core/CommandBuilder.d.ts +27 -2
- package/shell/core/CommandBuilder.js +32 -3
- package/shell/core/CommandoPool.d.ts +33 -0
- package/shell/core/CommandoPool.js +34 -0
- package/shell/core/class-merger.d.ts +29 -10
- package/shell/core/class-merger.js +23 -8
- package/shell/interactive/CommandoInteractive.d.ts +66 -6
- package/shell/interactive/CommandoInteractive.js +69 -6
- package/shell/interactive/InteractiveShell.d.ts +38 -0
- package/shell/interactive/InteractiveShell.js +25 -0
- package/shell/plugins/basic.d.ts +90 -9
- package/shell/plugins/basic.js +90 -9
- package/shell/plugins/git.d.ts +115 -0
- package/shell/plugins/git.js +124 -9
- package/shell/plugins/nvm.d.ts +47 -0
- package/shell/plugins/nvm.js +47 -0
- package/shell/plugins/pnpm.d.ts +31 -0
- package/shell/plugins/pnpm.js +31 -0
- package/shell/plugins/programming.d.ts +23 -3
- package/shell/plugins/programming.js +23 -3
- package/shell/plugins/python.d.ts +31 -0
- package/shell/plugins/python.js +32 -0
- package/shell/services/nvm.d.ts +59 -1
- package/shell/services/nvm.js +67 -6
- package/shell/services/pnpm.d.ts +41 -0
- package/shell/services/pnpm.js +41 -0
- package/shell/simple/Commando.d.ts +75 -0
- package/shell/simple/Commando.js +75 -0
- package/shell/simple/SimpleShell.d.ts +29 -2
- package/shell/simple/SimpleShell.js +32 -2
- package/shell/tools.d.ts +30 -0
- package/shell/tools.js +30 -0
- package/shell/types.d.ts +14 -0
|
@@ -1,12 +1,65 @@
|
|
|
1
1
|
import { BaseCliParam, CliParams } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type-safe CLI parameter resolver and parser.
|
|
4
|
+
*
|
|
5
|
+
* Parses command-line arguments (`process.argv`) into a typed object based on
|
|
6
|
+
* parameter definitions. Supports:
|
|
7
|
+
* - Multiple keys/aliases per parameter
|
|
8
|
+
* - Type validation and conversion
|
|
9
|
+
* - Default values and initial values
|
|
10
|
+
* - Option validation (restrict to specific values)
|
|
11
|
+
* - Array parameters (collect multiple values)
|
|
12
|
+
* - Dependencies (set other params based on current param)
|
|
13
|
+
* - Quoted string handling
|
|
14
|
+
*
|
|
15
|
+
* **Usage**:
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const resolver = CLIParamsResolver.create(
|
|
18
|
+
* { keys: ['--name', '-n'], keyName: 'name', type: 'string', description: 'Name' },
|
|
19
|
+
* { keys: ['--count'], keyName: 'count', type: 'number', defaultValue: 1 }
|
|
20
|
+
* );
|
|
21
|
+
* const params = resolver.resolveParamValue();
|
|
22
|
+
* // params.name: string, params.count: number
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @template T - Array of BaseCliParam definitions
|
|
26
|
+
* @template Output - Resolved parameters object type
|
|
27
|
+
*/
|
|
2
28
|
export declare class CLIParamsResolver<T extends BaseCliParam<string, any>[], Output extends CliParams<T> = CliParams<T>> {
|
|
29
|
+
/** Processed parameters with all required fields filled */
|
|
3
30
|
private params;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a CLIParamsResolver instance.
|
|
33
|
+
*
|
|
34
|
+
* @template T - Array of BaseCliParam definitions
|
|
35
|
+
* @param params - Parameter definitions
|
|
36
|
+
* @returns New CLIParamsResolver instance
|
|
37
|
+
*/
|
|
4
38
|
static create<T extends BaseCliParam<string, any>[]>(...params: T): CLIParamsResolver<T, CliParams<T>>;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a CLIParamsResolver and processes parameters.
|
|
41
|
+
*
|
|
42
|
+
* @param params - Parameter definitions (may be incomplete)
|
|
43
|
+
*/
|
|
5
44
|
constructor(params: BaseCliParam<string, any>[]);
|
|
6
45
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
46
|
+
* Parses command-line arguments into a typed parameters object.
|
|
47
|
+
*
|
|
48
|
+
* **Parsing Behavior**:
|
|
49
|
+
* - Splits arguments by `=` (e.g., `--key=value`)
|
|
50
|
+
* - Strips quotes from values and unescapes quotes
|
|
51
|
+
* - Processes values using parameter processors
|
|
52
|
+
* - Validates against options if provided
|
|
53
|
+
* - Handles dependencies (sets dependent params)
|
|
54
|
+
* - Accumulates array values (removes duplicates)
|
|
55
|
+
* - Applies initial values for missing params
|
|
56
|
+
* - Warns when overriding non-array values
|
|
57
|
+
*
|
|
58
|
+
* **Input Format**: `--key=value` or `--key value` (space-separated not supported)
|
|
59
|
+
*
|
|
60
|
+
* @param inputParams - Command-line arguments (default: `process.argv.slice(2)`)
|
|
61
|
+
* @returns Typed object with resolved parameter values
|
|
62
|
+
* @throws Error if value not in options, or if required value missing
|
|
10
63
|
*/
|
|
11
64
|
resolveParamValue(inputParams?: string[]): Output;
|
|
12
65
|
/**
|
|
@@ -1,17 +1,70 @@
|
|
|
1
1
|
import { asArray, exists, filterDuplicates, StaticLogger } from '@nu-art/ts-common';
|
|
2
2
|
import { DefaultProcessorsMapper } from './consts.js';
|
|
3
|
+
/**
|
|
4
|
+
* Type-safe CLI parameter resolver and parser.
|
|
5
|
+
*
|
|
6
|
+
* Parses command-line arguments (`process.argv`) into a typed object based on
|
|
7
|
+
* parameter definitions. Supports:
|
|
8
|
+
* - Multiple keys/aliases per parameter
|
|
9
|
+
* - Type validation and conversion
|
|
10
|
+
* - Default values and initial values
|
|
11
|
+
* - Option validation (restrict to specific values)
|
|
12
|
+
* - Array parameters (collect multiple values)
|
|
13
|
+
* - Dependencies (set other params based on current param)
|
|
14
|
+
* - Quoted string handling
|
|
15
|
+
*
|
|
16
|
+
* **Usage**:
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const resolver = CLIParamsResolver.create(
|
|
19
|
+
* { keys: ['--name', '-n'], keyName: 'name', type: 'string', description: 'Name' },
|
|
20
|
+
* { keys: ['--count'], keyName: 'count', type: 'number', defaultValue: 1 }
|
|
21
|
+
* );
|
|
22
|
+
* const params = resolver.resolveParamValue();
|
|
23
|
+
* // params.name: string, params.count: number
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @template T - Array of BaseCliParam definitions
|
|
27
|
+
* @template Output - Resolved parameters object type
|
|
28
|
+
*/
|
|
3
29
|
export class CLIParamsResolver {
|
|
30
|
+
/** Processed parameters with all required fields filled */
|
|
4
31
|
params;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a CLIParamsResolver instance.
|
|
34
|
+
*
|
|
35
|
+
* @template T - Array of BaseCliParam definitions
|
|
36
|
+
* @param params - Parameter definitions
|
|
37
|
+
* @returns New CLIParamsResolver instance
|
|
38
|
+
*/
|
|
5
39
|
static create(...params) {
|
|
6
40
|
return new CLIParamsResolver(params);
|
|
7
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a CLIParamsResolver and processes parameters.
|
|
44
|
+
*
|
|
45
|
+
* @param params - Parameter definitions (may be incomplete)
|
|
46
|
+
*/
|
|
8
47
|
constructor(params) {
|
|
9
48
|
this.params = this.translate(params);
|
|
10
49
|
}
|
|
11
50
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
51
|
+
* Parses command-line arguments into a typed parameters object.
|
|
52
|
+
*
|
|
53
|
+
* **Parsing Behavior**:
|
|
54
|
+
* - Splits arguments by `=` (e.g., `--key=value`)
|
|
55
|
+
* - Strips quotes from values and unescapes quotes
|
|
56
|
+
* - Processes values using parameter processors
|
|
57
|
+
* - Validates against options if provided
|
|
58
|
+
* - Handles dependencies (sets dependent params)
|
|
59
|
+
* - Accumulates array values (removes duplicates)
|
|
60
|
+
* - Applies initial values for missing params
|
|
61
|
+
* - Warns when overriding non-array values
|
|
62
|
+
*
|
|
63
|
+
* **Input Format**: `--key=value` or `--key value` (space-separated not supported)
|
|
64
|
+
*
|
|
65
|
+
* @param inputParams - Command-line arguments (default: `process.argv.slice(2)`)
|
|
66
|
+
* @returns Typed object with resolved parameter values
|
|
67
|
+
* @throws Error if value not in options, or if required value missing
|
|
15
68
|
*/
|
|
16
69
|
resolveParamValue(inputParams = process.argv.slice(2, process.argv.length)) {
|
|
17
70
|
const runtimeParams = inputParams.reduce((output, inputParam) => {
|
package/cli-params/consts.d.ts
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
import { TypedMap } from '@nu-art/ts-common';
|
|
2
2
|
import { CliParam } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Default processor for boolean CLI parameters.
|
|
5
|
+
*
|
|
6
|
+
* Returns the defaultValue or `true` if the flag is present
|
|
7
|
+
*
|
|
8
|
+
* @param input - Optional input string (flag presence indicator)
|
|
9
|
+
* @param defaultValue - Optional default value if flag is absent
|
|
10
|
+
* @returns `true` if flag is present, otherwise `defaultValue ?? false`
|
|
11
|
+
*/
|
|
3
12
|
export declare const DefaultProcessor_Boolean: CliParam<any, boolean>['process'];
|
|
13
|
+
/**
|
|
14
|
+
* Default processor for string CLI parameters.
|
|
15
|
+
*
|
|
16
|
+
* Returns the input string, or defaultValue if input is empty/missing.
|
|
17
|
+
* Throws if input is empty and no default is provided.
|
|
18
|
+
*
|
|
19
|
+
* @param input - Optional input string
|
|
20
|
+
* @param defaultValue - Optional default value
|
|
21
|
+
* @returns Processed string value
|
|
22
|
+
* @throws Error if input is empty and no default provided
|
|
23
|
+
*/
|
|
4
24
|
export declare const DefaultProcessor_String: CliParam<any, string>['process'];
|
|
25
|
+
/**
|
|
26
|
+
* Default processor for number CLI parameters.
|
|
27
|
+
*
|
|
28
|
+
* Parses the input as a number, or returns defaultValue if input is missing.
|
|
29
|
+
* Throws if input is not a valid number or if input is empty and no default provided.
|
|
30
|
+
*
|
|
31
|
+
* @param input - Optional input string
|
|
32
|
+
* @param defaultValue - Optional default value
|
|
33
|
+
* @returns Parsed number value
|
|
34
|
+
* @throws Error if input is not a number or missing with no default
|
|
35
|
+
*/
|
|
5
36
|
export declare const DefaultProcessor_Number: CliParam<any, number>['process'];
|
|
37
|
+
/**
|
|
38
|
+
* Map of default processors by type name.
|
|
39
|
+
*
|
|
40
|
+
* Used by CLIParamsResolver to assign processors when not provided.
|
|
41
|
+
* Keys match the type strings from `TypeOfTypeAsString`.
|
|
42
|
+
*/
|
|
6
43
|
export declare const DefaultProcessorsMapper: TypedMap<CliParam<any, any>['process']>;
|
package/cli-params/consts.js
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
import { exists } from '@nu-art/ts-common';
|
|
2
|
+
/**
|
|
3
|
+
* Default processor for boolean CLI parameters.
|
|
4
|
+
*
|
|
5
|
+
* Returns the defaultValue or `true` if the flag is present
|
|
6
|
+
*
|
|
7
|
+
* @param input - Optional input string (flag presence indicator)
|
|
8
|
+
* @param defaultValue - Optional default value if flag is absent
|
|
9
|
+
* @returns `true` if flag is present, otherwise `defaultValue ?? false`
|
|
10
|
+
*/
|
|
2
11
|
export const DefaultProcessor_Boolean = (input, defaultValue) => {
|
|
3
|
-
return true;
|
|
12
|
+
return defaultValue ?? true;
|
|
4
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Default processor for string CLI parameters.
|
|
16
|
+
*
|
|
17
|
+
* Returns the input string, or defaultValue if input is empty/missing.
|
|
18
|
+
* Throws if input is empty and no default is provided.
|
|
19
|
+
*
|
|
20
|
+
* @param input - Optional input string
|
|
21
|
+
* @param defaultValue - Optional default value
|
|
22
|
+
* @returns Processed string value
|
|
23
|
+
* @throws Error if input is empty and no default provided
|
|
24
|
+
*/
|
|
5
25
|
export const DefaultProcessor_String = (input, defaultValue) => {
|
|
6
26
|
if (!input || !input.length) {
|
|
7
27
|
if (!exists(defaultValue))
|
|
@@ -10,6 +30,17 @@ export const DefaultProcessor_String = (input, defaultValue) => {
|
|
|
10
30
|
}
|
|
11
31
|
return input;
|
|
12
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* Default processor for number CLI parameters.
|
|
35
|
+
*
|
|
36
|
+
* Parses the input as a number, or returns defaultValue if input is missing.
|
|
37
|
+
* Throws if input is not a valid number or if input is empty and no default provided.
|
|
38
|
+
*
|
|
39
|
+
* @param input - Optional input string
|
|
40
|
+
* @param defaultValue - Optional default value
|
|
41
|
+
* @returns Parsed number value
|
|
42
|
+
* @throws Error if input is not a number or missing with no default
|
|
43
|
+
*/
|
|
13
44
|
export const DefaultProcessor_Number = (input, defaultValue) => {
|
|
14
45
|
if (!input) {
|
|
15
46
|
if (!exists(defaultValue))
|
|
@@ -20,6 +51,12 @@ export const DefaultProcessor_Number = (input, defaultValue) => {
|
|
|
20
51
|
throw new Error('expected number value');
|
|
21
52
|
return Number(input);
|
|
22
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Map of default processors by type name.
|
|
56
|
+
*
|
|
57
|
+
* Used by CLIParamsResolver to assign processors when not provided.
|
|
58
|
+
* Keys match the type strings from `TypeOfTypeAsString`.
|
|
59
|
+
*/
|
|
23
60
|
export const DefaultProcessorsMapper = {
|
|
24
61
|
string: DefaultProcessor_String,
|
|
25
62
|
boolean: DefaultProcessor_Boolean,
|
package/cli-params/types.d.ts
CHANGED
|
@@ -8,18 +8,46 @@ export type DependencyParam<T extends Primitive | Primitive[]> = {
|
|
|
8
8
|
param: BaseCliParam<string, T>;
|
|
9
9
|
value: T | ((currentValue: any) => T);
|
|
10
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Base CLI parameter definition (before processing).
|
|
13
|
+
*
|
|
14
|
+
* Defines a command-line parameter with keys, type, and optional features.
|
|
15
|
+
* Can be incomplete (missing name/process) and will be filled by CLIParamsResolver.
|
|
16
|
+
*
|
|
17
|
+
* **Features**:
|
|
18
|
+
* - Multiple keys (aliases): `['--help', '-h']`
|
|
19
|
+
* - Type validation: `'string'`, `'number'`, `'boolean'`, `'string[]'`, etc.
|
|
20
|
+
* - Options validation: Restrict values to specific options
|
|
21
|
+
* - Dependencies: Set other params based on this param's value
|
|
22
|
+
* - Array support: Collect multiple values
|
|
23
|
+
*
|
|
24
|
+
* @template K - Key name type (string literal)
|
|
25
|
+
* @template V - Value type (primitive or array)
|
|
26
|
+
*/
|
|
11
27
|
export type BaseCliParam<K extends string, V extends Primitive | Primitive[]> = {
|
|
28
|
+
/** Array of CLI keys/aliases (e.g., `['--help', '-h']`) */
|
|
12
29
|
keys: string[];
|
|
30
|
+
/** Unique key name for the resolved object */
|
|
13
31
|
keyName: K;
|
|
32
|
+
/** Type string representation (e.g., `'string'`, `'number[]'`) */
|
|
14
33
|
type: TypeOfTypeAsString<V>;
|
|
34
|
+
/** Human-readable description */
|
|
15
35
|
description: string;
|
|
36
|
+
/** Optional parameter name (defaults to keyName) */
|
|
16
37
|
name?: string;
|
|
38
|
+
/** Optional array of allowed values (validates input) */
|
|
17
39
|
options?: string[];
|
|
40
|
+
/** Initial value if param not provided */
|
|
18
41
|
initialValue?: V;
|
|
42
|
+
/** Default value if param provided but empty */
|
|
19
43
|
defaultValue?: V;
|
|
44
|
+
/** Optional processor function (defaults by type) */
|
|
20
45
|
process?: (value?: string, defaultValue?: V) => V;
|
|
46
|
+
/** Whether this param accepts multiple values (array) */
|
|
21
47
|
isArray?: true;
|
|
48
|
+
/** Optional grouping for help/validation */
|
|
22
49
|
group?: string;
|
|
50
|
+
/** Parameters that depend on this one */
|
|
23
51
|
dependencies?: DependencyParam<any>[];
|
|
24
52
|
};
|
|
25
53
|
export type CliParam<K extends string, V extends Primitive | Primitive[]> = BaseCliParam<K, V> & {
|
package/package.json
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nu-art/commando",
|
|
3
|
-
"version": "0.401.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.401.1",
|
|
4
|
+
"description": "Shell command execution framework with interactive sessions, CLI parameter resolution, and plugin system for building and executing shell scripts programmatically",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"shell",
|
|
8
|
+
"command-execution",
|
|
9
|
+
"cli",
|
|
10
|
+
"bash",
|
|
11
|
+
"interactive-shell",
|
|
12
|
+
"shell-scripts",
|
|
13
|
+
"cli-params",
|
|
14
|
+
"command-line",
|
|
9
15
|
"nu-art",
|
|
10
|
-
"
|
|
16
|
+
"thunderstorm",
|
|
17
|
+
"commando",
|
|
18
|
+
"typescript"
|
|
11
19
|
],
|
|
12
20
|
"homepage": "https://github.com/nu-art-js/thunderstorm",
|
|
13
21
|
"bugs": {
|
|
14
|
-
"url": "https://github.com/nu-art-js/thunderstorm/issues"
|
|
15
|
-
},
|
|
16
|
-
"publishConfig": {
|
|
17
|
-
"directory": "dist"
|
|
22
|
+
"url": "https://github.com/nu-art-js/thunderstorm/issues?q=is%3Aissue+label%3Acommando"
|
|
18
23
|
},
|
|
19
24
|
"repository": {
|
|
20
25
|
"type": "git",
|
|
21
|
-
"url": "git+ssh://git@github.com:nu-art-js/thunderstorm.git"
|
|
26
|
+
"url": "git+ssh://git@github.com:nu-art-js/thunderstorm.git",
|
|
27
|
+
"directory": "_thunderstorm/commando"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"directory": "dist"
|
|
22
31
|
},
|
|
23
32
|
"license": "Apache-2.0",
|
|
24
33
|
"author": "TacB0sS",
|
|
@@ -29,7 +38,10 @@
|
|
|
29
38
|
"build": "tsc"
|
|
30
39
|
},
|
|
31
40
|
"dependencies": {
|
|
32
|
-
"@nu-art/ts-common": "0.401.
|
|
41
|
+
"@nu-art/ts-common": "0.401.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@nu-art/testalot": "0.401.1"
|
|
33
45
|
},
|
|
34
46
|
"unitConfig": {
|
|
35
47
|
"type": "typescript-lib"
|
|
@@ -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
|
/**
|
|
@@ -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;
|
package/shell/core/CliError.d.ts
CHANGED
|
@@ -1,14 +1,49 @@
|
|
|
1
1
|
import { CustomException } from '@nu-art/ts-common';
|
|
2
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
|
+
*/
|
|
3
9
|
export declare class CliError extends CustomException {
|
|
10
|
+
/** Standard output from the failed command */
|
|
4
11
|
stdout: string;
|
|
12
|
+
/** Standard error from the failed command */
|
|
5
13
|
stderr: string;
|
|
14
|
+
/** Underlying Node.js ExecException */
|
|
6
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
|
+
*/
|
|
7
24
|
constructor(message: string, stdout: string, stderr: string, cause: ExecException);
|
|
8
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
|
+
*/
|
|
9
33
|
export declare class CommandoException extends CustomException {
|
|
34
|
+
/** Standard output from the command */
|
|
10
35
|
stdout: string;
|
|
36
|
+
/** Standard error from the command */
|
|
11
37
|
stderr: string;
|
|
38
|
+
/** Exit code from the command */
|
|
12
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
|
+
*/
|
|
13
48
|
constructor(message: string, stdout: string, stderr: string, exitCode: number);
|
|
14
49
|
}
|
package/shell/core/CliError.js
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
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
|
+
*/
|
|
2
8
|
export class CliError extends CustomException {
|
|
9
|
+
/** Standard output from the failed command */
|
|
3
10
|
stdout;
|
|
11
|
+
/** Standard error from the failed command */
|
|
4
12
|
stderr;
|
|
13
|
+
/** Underlying Node.js ExecException */
|
|
5
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
|
+
*/
|
|
6
23
|
constructor(message, stdout, stderr, cause) {
|
|
7
24
|
super(CliError, message, cause);
|
|
8
25
|
this.stdout = stdout;
|
|
@@ -10,12 +27,30 @@ export class CliError extends CustomException {
|
|
|
10
27
|
this.cause = cause;
|
|
11
28
|
}
|
|
12
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
|
+
*/
|
|
13
37
|
export class CommandoException extends CustomException {
|
|
38
|
+
/** Standard output from the command */
|
|
14
39
|
stdout;
|
|
40
|
+
/** Standard error from the command */
|
|
15
41
|
stderr;
|
|
42
|
+
/** Exit code from the command */
|
|
16
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
|
+
*/
|
|
17
52
|
constructor(message, stdout, stderr, exitCode) {
|
|
18
|
-
super(
|
|
53
|
+
super(CommandoException, message);
|
|
19
54
|
this.stdout = stdout;
|
|
20
55
|
this.stderr = stderr;
|
|
21
56
|
this.exitCode = exitCode;
|
|
@@ -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
|
/**
|