@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
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ThisShouldNotHappenException } from '@nu-art/ts-common';
|
|
2
|
+
import { SimpleShell } from './SimpleShell.js';
|
|
3
|
+
import { BaseCommando } from '../core/BaseCommando.js';
|
|
4
|
+
import { CliError } from '../core/CliError.js';
|
|
5
|
+
/**
|
|
6
|
+
* Simple shell command executor extending BaseCommando.
|
|
7
|
+
*
|
|
8
|
+
* Provides a straightforward way to build and execute shell commands
|
|
9
|
+
* using SimpleShell. Supports file execution and remote file execution.
|
|
10
|
+
*
|
|
11
|
+
* **Features**:
|
|
12
|
+
* - Command building via BaseCommando fluent API
|
|
13
|
+
* - File execution with optional interpreter
|
|
14
|
+
* - Remote file execution via curl
|
|
15
|
+
* - Error handling with CliError
|
|
16
|
+
* - UID tagging for log identification
|
|
17
|
+
*
|
|
18
|
+
* **Error Handling**:
|
|
19
|
+
* - Catches CliError and calls callback with error details
|
|
20
|
+
* - Throws ThisShouldNotHappenException for unexpected errors
|
|
21
|
+
* - Always calls callback (even on error) if provided
|
|
22
|
+
*/
|
|
23
|
+
export class Commando extends BaseCommando {
|
|
24
|
+
/** Optional unique identifier for log tagging */
|
|
25
|
+
uid;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a Commando instance with plugins.
|
|
28
|
+
*
|
|
29
|
+
* @template T - Array of plugin constructor types
|
|
30
|
+
* @param plugins - Plugin classes to merge
|
|
31
|
+
* @returns Merged Commando instance with plugins
|
|
32
|
+
*/
|
|
33
|
+
static create(...plugins) {
|
|
34
|
+
const _commando = BaseCommando._create(Commando, ...plugins);
|
|
35
|
+
return _commando;
|
|
36
|
+
}
|
|
37
|
+
constructor() {
|
|
38
|
+
super();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Sets a unique identifier for this commando instance.
|
|
42
|
+
*
|
|
43
|
+
* Used for log tagging to identify which commando instance
|
|
44
|
+
* generated log messages.
|
|
45
|
+
*
|
|
46
|
+
* @param uid - Unique identifier string
|
|
47
|
+
* @returns This instance for method chaining
|
|
48
|
+
*/
|
|
49
|
+
setUID(uid) {
|
|
50
|
+
this.uid = uid;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Executes a local file with an optional interpreter.
|
|
55
|
+
*
|
|
56
|
+
* If an interpreter is provided, prefixes the file path with it.
|
|
57
|
+
* Otherwise executes the file directly (must be executable).
|
|
58
|
+
*
|
|
59
|
+
* @param filePath - Path to file to execute
|
|
60
|
+
* @param interpreter - Optional interpreter (e.g., 'node', 'python3')
|
|
61
|
+
* @returns Promise resolving to command output
|
|
62
|
+
*/
|
|
63
|
+
executeFile(filePath, interpreter) {
|
|
64
|
+
let command = filePath;
|
|
65
|
+
// If an interpreter is provided, prefix the command with it.
|
|
66
|
+
if (interpreter) {
|
|
67
|
+
command = `${interpreter} ${filePath}`;
|
|
68
|
+
}
|
|
69
|
+
return new SimpleShell().execute(command);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Executes a remote file by downloading and piping to interpreter.
|
|
73
|
+
*
|
|
74
|
+
* Uses curl to download the file and pipes it directly to the interpreter.
|
|
75
|
+
* Does not save the file locally.
|
|
76
|
+
*
|
|
77
|
+
* **Security Note**: Executes remote code without verification.
|
|
78
|
+
*
|
|
79
|
+
* @param pathToFile - URL to remote file
|
|
80
|
+
* @param interpreter - Interpreter to execute the file (e.g., 'bash', 'node')
|
|
81
|
+
* @returns Promise resolving to command output
|
|
82
|
+
*/
|
|
83
|
+
executeRemoteFile(pathToFile, interpreter) {
|
|
84
|
+
const command = `curl -o- "${pathToFile}" | ${interpreter}`;
|
|
85
|
+
return new SimpleShell().execute(command);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Executes the accumulated commands and optionally processes output.
|
|
89
|
+
*
|
|
90
|
+
* **Behavior**:
|
|
91
|
+
* - Resets the command builder (gets accumulated command)
|
|
92
|
+
* - Creates a new SimpleShell instance with debug mode
|
|
93
|
+
* - Executes the command
|
|
94
|
+
* - On success: calls callback with stdout, stderr, exitCode=0
|
|
95
|
+
* - On error: catches CliError, calls callback with error details, returns result
|
|
96
|
+
* - On unexpected error: throws ThisShouldNotHappenException
|
|
97
|
+
*
|
|
98
|
+
* **Note**: The callback is always called if provided, even on error.
|
|
99
|
+
* This allows handling errors without try/catch.
|
|
100
|
+
*
|
|
101
|
+
* @template T - Return type of callback
|
|
102
|
+
* @param callback - Optional function to process command output
|
|
103
|
+
* @returns Promise resolving to callback result or void
|
|
104
|
+
*/
|
|
105
|
+
async execute(callback) {
|
|
106
|
+
const command = this.builder.reset();
|
|
107
|
+
const simpleShell = new SimpleShell().debug(this._debug);
|
|
108
|
+
try {
|
|
109
|
+
if (this.uid)
|
|
110
|
+
simpleShell.setUID(this.uid);
|
|
111
|
+
const { stdout, stderr } = await simpleShell.execute(command);
|
|
112
|
+
return callback?.(stdout, stderr, 0);
|
|
113
|
+
}
|
|
114
|
+
catch (_error) {
|
|
115
|
+
simpleShell.logError(_error);
|
|
116
|
+
const cliError = _error;
|
|
117
|
+
if ('isInstanceOf' in cliError && cliError.isInstanceOf(CliError))
|
|
118
|
+
return callback?.(cliError.stdout, cliError.stderr, cliError.cause.code ?? -1);
|
|
119
|
+
throw new ThisShouldNotHappenException('Unhandled error', _error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ExecOptions } from 'child_process';
|
|
2
|
+
import { Logger } from '@nu-art/ts-common';
|
|
3
|
+
/**
|
|
4
|
+
* Extended ExecOptions with encoding support.
|
|
5
|
+
*/
|
|
6
|
+
export type CliOptions = ExecOptions & {
|
|
7
|
+
encoding: BufferEncoding | string | null;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Simple shell command executor using Node.js child_process.
|
|
11
|
+
*
|
|
12
|
+
* Executes shell commands synchronously via `exec()` and handles
|
|
13
|
+
* output conversion. Supports debug logging and custom shell/options.
|
|
14
|
+
*
|
|
15
|
+
* **Behavior**:
|
|
16
|
+
* - Uses `/bin/bash` as default shell
|
|
17
|
+
* - Converts Buffer output to UTF-8 strings
|
|
18
|
+
* - Logs stdout as info, stderr as error
|
|
19
|
+
* - Throws CliError on command failure
|
|
20
|
+
* - Supports UID tagging for log identification
|
|
21
|
+
*/
|
|
22
|
+
export declare class SimpleShell extends Logger {
|
|
23
|
+
/** Debug mode flag (enables verbose command logging) */
|
|
24
|
+
private _debug;
|
|
25
|
+
/** Execution options for child_process.exec() */
|
|
26
|
+
private cliOptions;
|
|
27
|
+
/**
|
|
28
|
+
* Executes a shell command and returns stdout/stderr.
|
|
29
|
+
*
|
|
30
|
+
* **Behavior**:
|
|
31
|
+
* - Logs command in debug mode (wrapped in triple quotes)
|
|
32
|
+
* - Converts Buffer output to UTF-8 strings
|
|
33
|
+
* - Logs stdout as info, stderr as error
|
|
34
|
+
* - Throws CliError if command fails (non-zero exit code)
|
|
35
|
+
*
|
|
36
|
+
* @param command - Shell command string to execute
|
|
37
|
+
* @returns Promise resolving to stdout and stderr strings
|
|
38
|
+
* @throws CliError if command execution fails
|
|
39
|
+
*/
|
|
40
|
+
execute: (command: string) => Promise<{
|
|
41
|
+
stdout: string;
|
|
42
|
+
stderr: string;
|
|
43
|
+
}>;
|
|
44
|
+
debug(debug?: boolean): this;
|
|
45
|
+
setShell(shell: string): void;
|
|
46
|
+
setOptions(options: Partial<CliOptions>): void;
|
|
47
|
+
setUID(uid: string): this;
|
|
48
|
+
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { exec } from 'child_process';
|
|
2
2
|
import { Logger } from '@nu-art/ts-common';
|
|
3
3
|
import { CliError } from '../core/CliError.js';
|
|
4
|
+
/**
|
|
5
|
+
* Converts input to string, handling Buffer and ArrayBufferLike types.
|
|
6
|
+
*
|
|
7
|
+
* @param input - String, Buffer, or ArrayBufferLike to convert
|
|
8
|
+
* @returns UTF-8 string representation
|
|
9
|
+
*/
|
|
4
10
|
function toStringWithNewlines(input) {
|
|
5
11
|
if (typeof input === 'string')
|
|
6
12
|
return input;
|
|
@@ -8,12 +14,36 @@ function toStringWithNewlines(input) {
|
|
|
8
14
|
? input.toString('utf8')
|
|
9
15
|
: Buffer.from(input).toString('utf8');
|
|
10
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Simple shell command executor using Node.js child_process.
|
|
19
|
+
*
|
|
20
|
+
* Executes shell commands synchronously via `exec()` and handles
|
|
21
|
+
* output conversion. Supports debug logging and custom shell/options.
|
|
22
|
+
*
|
|
23
|
+
* **Behavior**:
|
|
24
|
+
* - Uses `/bin/bash` as default shell
|
|
25
|
+
* - Converts Buffer output to UTF-8 strings
|
|
26
|
+
* - Logs stdout as info, stderr as error
|
|
27
|
+
* - Throws CliError on command failure
|
|
28
|
+
* - Supports UID tagging for log identification
|
|
29
|
+
*/
|
|
11
30
|
export class SimpleShell extends Logger {
|
|
31
|
+
/** Debug mode flag (enables verbose command logging) */
|
|
12
32
|
_debug = false;
|
|
33
|
+
/** Execution options for child_process.exec() */
|
|
13
34
|
cliOptions = { shell: '/bin/bash' };
|
|
14
35
|
/**
|
|
15
|
-
* Executes
|
|
16
|
-
*
|
|
36
|
+
* Executes a shell command and returns stdout/stderr.
|
|
37
|
+
*
|
|
38
|
+
* **Behavior**:
|
|
39
|
+
* - Logs command in debug mode (wrapped in triple quotes)
|
|
40
|
+
* - Converts Buffer output to UTF-8 strings
|
|
41
|
+
* - Logs stdout as info, stderr as error
|
|
42
|
+
* - Throws CliError if command fails (non-zero exit code)
|
|
43
|
+
*
|
|
44
|
+
* @param command - Shell command string to execute
|
|
45
|
+
* @returns Promise resolving to stdout and stderr strings
|
|
46
|
+
* @throws CliError if command execution fails
|
|
17
47
|
*/
|
|
18
48
|
execute = async (command) => {
|
|
19
49
|
if (this._debug)
|
package/tools.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AbsolutePath } from '@nu-art/ts-common';
|
|
2
|
+
/**
|
|
3
|
+
* Removes ANSI escape codes from a string.
|
|
4
|
+
*
|
|
5
|
+
* Strips color codes and formatting sequences (e.g., `\x1B[31m` for red text)
|
|
6
|
+
* from shell command output. Useful for processing command output that
|
|
7
|
+
* contains terminal formatting.
|
|
8
|
+
*
|
|
9
|
+
* **Regex**: Matches ANSI escape sequences starting with `\x1B[` followed by
|
|
10
|
+
* digits, optional semicolon, more digits, and ending with `m`.
|
|
11
|
+
*
|
|
12
|
+
* @param text - Text containing ANSI escape codes
|
|
13
|
+
* @returns Text with all ANSI codes removed
|
|
14
|
+
*/
|
|
15
|
+
export declare function removeAnsiCodes(text: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Converts a relative or absolute path to a full absolute path.
|
|
18
|
+
*
|
|
19
|
+
* **Behavior**:
|
|
20
|
+
* - Removes leading slashes (normalizes paths like `/path/to/file`)
|
|
21
|
+
* - Resolves relative to `assetParentPath` (defaults to `process.cwd()`)
|
|
22
|
+
* - Returns a branded `AbsolutePath` type
|
|
23
|
+
*
|
|
24
|
+
* **Note**: The commented-out validation that checks if the resolved path
|
|
25
|
+
* is within `assetParentPath` is disabled. This could be a security concern
|
|
26
|
+
* if used with user-provided paths.
|
|
27
|
+
*
|
|
28
|
+
* @param pathToFile - Path to convert (relative or absolute)
|
|
29
|
+
* @param assetParentPath - Base path for resolution (default: `process.cwd()`)
|
|
30
|
+
* @returns Absolute path as branded type
|
|
31
|
+
* @throws Error if path is not provided or is empty
|
|
32
|
+
*/
|
|
33
|
+
export declare function convertToFullPath(pathToFile: string, assetParentPath?: string): AbsolutePath;
|
package/tools.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
/**
|
|
3
|
+
* Removes ANSI escape codes from a string.
|
|
4
|
+
*
|
|
5
|
+
* Strips color codes and formatting sequences (e.g., `\x1B[31m` for red text)
|
|
6
|
+
* from shell command output. Useful for processing command output that
|
|
7
|
+
* contains terminal formatting.
|
|
8
|
+
*
|
|
9
|
+
* **Regex**: Matches ANSI escape sequences starting with `\x1B[` followed by
|
|
10
|
+
* digits, optional semicolon, more digits, and ending with `m`.
|
|
11
|
+
*
|
|
12
|
+
* @param text - Text containing ANSI escape codes
|
|
13
|
+
* @returns Text with all ANSI codes removed
|
|
14
|
+
*/
|
|
15
|
+
export function removeAnsiCodes(text) {
|
|
16
|
+
// This regular expression matches the escape codes and removes them
|
|
17
|
+
return text.replace(/\x1B\[\d+;?\d*m/g, '');
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts a relative or absolute path to a full absolute path.
|
|
21
|
+
*
|
|
22
|
+
* **Behavior**:
|
|
23
|
+
* - Removes leading slashes (normalizes paths like `/path/to/file`)
|
|
24
|
+
* - Resolves relative to `assetParentPath` (defaults to `process.cwd()`)
|
|
25
|
+
* - Returns a branded `AbsolutePath` type
|
|
26
|
+
*
|
|
27
|
+
* **Note**: The commented-out validation that checks if the resolved path
|
|
28
|
+
* is within `assetParentPath` is disabled. This could be a security concern
|
|
29
|
+
* if used with user-provided paths.
|
|
30
|
+
*
|
|
31
|
+
* @param pathToFile - Path to convert (relative or absolute)
|
|
32
|
+
* @param assetParentPath - Base path for resolution (default: `process.cwd()`)
|
|
33
|
+
* @returns Absolute path as branded type
|
|
34
|
+
* @throws Error if path is not provided or is empty
|
|
35
|
+
*/
|
|
36
|
+
export function convertToFullPath(pathToFile, assetParentPath = process.cwd()) {
|
|
37
|
+
if (!pathToFile)
|
|
38
|
+
throw new Error('Path not provided');
|
|
39
|
+
if (pathToFile === '')
|
|
40
|
+
throw new Error('Empty path not allowed');
|
|
41
|
+
while (pathToFile.startsWith('/'))
|
|
42
|
+
pathToFile = pathToFile.substring(1);
|
|
43
|
+
const absolutePath = path.resolve(assetParentPath, pathToFile);
|
|
44
|
+
// if (!absolutePath.startsWith(assetParentPath))
|
|
45
|
+
// throw new Error(`Found path: '${absolutePath}' which is out of the scope of the assert directory: '${assetParentPath}'`);
|
|
46
|
+
return absolutePath;
|
|
47
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseCommando } from './core/BaseCommando.js';
|
|
2
|
+
/**
|
|
3
|
+
* Log output types for shell commands.
|
|
4
|
+
*
|
|
5
|
+
* - `'out'`: Standard output (stdout)
|
|
6
|
+
* - `'err'`: Standard error (stderr)
|
|
7
|
+
*/
|
|
8
|
+
export type LogTypes = 'out' | 'err';
|
|
9
|
+
/**
|
|
10
|
+
* Function type for command blocks that operate on a Commando instance.
|
|
11
|
+
*
|
|
12
|
+
* Used for composing command sequences where a function receives
|
|
13
|
+
* a Commando instance and builds commands on it.
|
|
14
|
+
*
|
|
15
|
+
* @template Commando - Commando type (must extend BaseCommando)
|
|
16
|
+
*/
|
|
17
|
+
export type CliBlock<Commando extends BaseCommando> = (cli: Commando) => void;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { BaseCliParam, CliParams } from './types.js';
|
|
2
|
-
export declare class CLIParamsResolver<T extends BaseCliParam<string, any>[], Output extends CliParams<T> = CliParams<T>> {
|
|
3
|
-
private params;
|
|
4
|
-
static create<T extends BaseCliParam<string, any>[]>(...params: T): CLIParamsResolver<T, CliParams<T>>;
|
|
5
|
-
constructor(params: BaseCliParam<string, any>[]);
|
|
6
|
-
/**
|
|
7
|
-
* Format current input params and return it structured by the app params type.
|
|
8
|
-
* @param inputParams current console input arguments
|
|
9
|
-
* @returns CliParamsObject
|
|
10
|
-
*/
|
|
11
|
-
resolveParamValue(inputParams?: string[]): Output;
|
|
12
|
-
/**
|
|
13
|
-
* Find the matching param by finding it's key in the current argument
|
|
14
|
-
* Go over the app params and find the CLIParam object representing it
|
|
15
|
-
* @param inputParam The current console argument being parsed
|
|
16
|
-
* @returns The CliParam or undefined if not found
|
|
17
|
-
* @private
|
|
18
|
-
*/
|
|
19
|
-
private findMatchingParamToResolve;
|
|
20
|
-
/**
|
|
21
|
-
* Translate BaseCLIParams passed to the constructor to CLIParams
|
|
22
|
-
* @param params User input of CLI params being passed to the constructor
|
|
23
|
-
* @private
|
|
24
|
-
* @returns CLI Params filled with all mandatory data
|
|
25
|
-
*/
|
|
26
|
-
private translate;
|
|
27
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { asArray, exists, filterDuplicates, StaticLogger } from '@nu-art/ts-common';
|
|
2
|
-
import { DefaultProcessorsMapper } from './consts.js';
|
|
3
|
-
export class CLIParamsResolver {
|
|
4
|
-
params;
|
|
5
|
-
static create(...params) {
|
|
6
|
-
return new CLIParamsResolver(params);
|
|
7
|
-
}
|
|
8
|
-
constructor(params) {
|
|
9
|
-
this.params = this.translate(params);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Format current input params and return it structured by the app params type.
|
|
13
|
-
* @param inputParams current console input arguments
|
|
14
|
-
* @returns CliParamsObject
|
|
15
|
-
*/
|
|
16
|
-
resolveParamValue(inputParams = process.argv.slice(2, process.argv.length)) {
|
|
17
|
-
const runtimeParams = inputParams.reduce((output, inputParam) => {
|
|
18
|
-
let [key, value] = inputParam.split('=');
|
|
19
|
-
const cliParamToResolve = this.findMatchingParamToResolve(key);
|
|
20
|
-
if (!cliParamToResolve)
|
|
21
|
-
return output;
|
|
22
|
-
if (value && value.startsWith('"') && value.endsWith('"')) {
|
|
23
|
-
value = value.slice(1, -1);
|
|
24
|
-
value = value.replace(/\\"/g, '"');
|
|
25
|
-
}
|
|
26
|
-
const finalValue = cliParamToResolve.process(value, cliParamToResolve.defaultValue);
|
|
27
|
-
// validate options if exits
|
|
28
|
-
if (cliParamToResolve.options && !cliParamToResolve.options.includes(finalValue))
|
|
29
|
-
throw new Error(`value not supported for param: ${cliParamToResolve.name}, supported values: ${cliParamToResolve.options.join(', ')}`);
|
|
30
|
-
const keyName = cliParamToResolve.keyName;
|
|
31
|
-
if (exists(cliParamToResolve.dependencies))
|
|
32
|
-
cliParamToResolve.dependencies?.forEach(dependency => {
|
|
33
|
-
// If dependency value is a function, call it with the current param's processed value
|
|
34
|
-
const dependencyValue = typeof dependency.value === 'function'
|
|
35
|
-
? dependency.value(finalValue)
|
|
36
|
-
: dependency.value;
|
|
37
|
-
output[dependency.param.keyName] = dependencyValue;
|
|
38
|
-
});
|
|
39
|
-
if (cliParamToResolve.isArray) {
|
|
40
|
-
let currentValues = output[keyName];
|
|
41
|
-
currentValues = filterDuplicates([...(currentValues ?? []), ...asArray(finalValue)]);
|
|
42
|
-
output[keyName] = currentValues;
|
|
43
|
-
return output;
|
|
44
|
-
}
|
|
45
|
-
//if already exists and the value ain't an array warn that the value will be overridden
|
|
46
|
-
if (output[keyName])
|
|
47
|
-
StaticLogger.logWarning(`this param does not accept multiple values, overriding prev value: ${output[keyName]}`);
|
|
48
|
-
//Apply a single value to the object
|
|
49
|
-
output[keyName] = finalValue;
|
|
50
|
-
return output;
|
|
51
|
-
}, {});
|
|
52
|
-
this.params.filter(param => exists(param.initialValue) && !exists(runtimeParams[param.keyName])).forEach(param => {
|
|
53
|
-
runtimeParams[param.keyName] = param.initialValue;
|
|
54
|
-
});
|
|
55
|
-
return runtimeParams;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Find the matching param by finding it's key in the current argument
|
|
59
|
-
* Go over the app params and find the CLIParam object representing it
|
|
60
|
-
* @param inputParam The current console argument being parsed
|
|
61
|
-
* @returns The CliParam or undefined if not found
|
|
62
|
-
* @private
|
|
63
|
-
*/
|
|
64
|
-
findMatchingParamToResolve(inputParam) {
|
|
65
|
-
let matchingParam;
|
|
66
|
-
this.params.forEach((param) => {
|
|
67
|
-
param.keys.forEach((key) => {
|
|
68
|
-
if (inputParam === key)
|
|
69
|
-
matchingParam = param;
|
|
70
|
-
if (inputParam.startsWith(`${key}=`))
|
|
71
|
-
matchingParam = param;
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
return matchingParam;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Translate BaseCLIParams passed to the constructor to CLIParams
|
|
78
|
-
* @param params User input of CLI params being passed to the constructor
|
|
79
|
-
* @private
|
|
80
|
-
* @returns CLI Params filled with all mandatory data
|
|
81
|
-
*/
|
|
82
|
-
translate(params) {
|
|
83
|
-
return params.map(param => {
|
|
84
|
-
//If name is not defined apply key name as the param name
|
|
85
|
-
if (!param.name)
|
|
86
|
-
param.name = param.keyName;
|
|
87
|
-
//If no processor is passed apply default by type
|
|
88
|
-
if (!param.process) {
|
|
89
|
-
param.process = DefaultProcessorsMapper[param.type.split('[')[0].trim()];
|
|
90
|
-
}
|
|
91
|
-
//Determine if value is array by the param type TODO: improve this chunk of code, this is not strict enough
|
|
92
|
-
if (!exists(param.isArray) && param.type.includes('[]'))
|
|
93
|
-
param.isArray = true;
|
|
94
|
-
return param;
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
package/cli-params/consts.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { TypedMap } from '@nu-art/ts-common';
|
|
2
|
-
import { CliParam } from './types.js';
|
|
3
|
-
export declare const DefaultProcessor_Boolean: CliParam<any, boolean>['process'];
|
|
4
|
-
export declare const DefaultProcessor_String: CliParam<any, string>['process'];
|
|
5
|
-
export declare const DefaultProcessor_Number: CliParam<any, number>['process'];
|
|
6
|
-
export declare const DefaultProcessorsMapper: TypedMap<CliParam<any, any>['process']>;
|
package/cli-params/consts.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { exists } from '@nu-art/ts-common';
|
|
2
|
-
export const DefaultProcessor_Boolean = (input, defaultValue) => {
|
|
3
|
-
return true;
|
|
4
|
-
};
|
|
5
|
-
export const DefaultProcessor_String = (input, defaultValue) => {
|
|
6
|
-
if (!input || !input.length) {
|
|
7
|
-
if (!exists(defaultValue))
|
|
8
|
-
throw new Error('expected string value');
|
|
9
|
-
return defaultValue;
|
|
10
|
-
}
|
|
11
|
-
return input;
|
|
12
|
-
};
|
|
13
|
-
export const DefaultProcessor_Number = (input, defaultValue) => {
|
|
14
|
-
if (!input) {
|
|
15
|
-
if (!exists(defaultValue))
|
|
16
|
-
throw new Error('expected number value');
|
|
17
|
-
return defaultValue;
|
|
18
|
-
}
|
|
19
|
-
if (isNaN(Number(input)))
|
|
20
|
-
throw new Error('expected number value');
|
|
21
|
-
return Number(input);
|
|
22
|
-
};
|
|
23
|
-
export const DefaultProcessorsMapper = {
|
|
24
|
-
string: DefaultProcessor_String,
|
|
25
|
-
boolean: DefaultProcessor_Boolean,
|
|
26
|
-
number: DefaultProcessor_Number,
|
|
27
|
-
};
|
package/cli-params/types.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { Primitive, TypeOfTypeAsString } from '@nu-art/ts-common';
|
|
2
|
-
export type CliParams<T extends BaseCliParam<string, any>[]> = {
|
|
3
|
-
[K in T[number]['keyName']]: NonNullable<Extract<T[number], {
|
|
4
|
-
keyName: K;
|
|
5
|
-
}>['defaultValue']>;
|
|
6
|
-
};
|
|
7
|
-
export type DependencyParam<T extends Primitive | Primitive[]> = {
|
|
8
|
-
param: BaseCliParam<string, T>;
|
|
9
|
-
value: T | ((currentValue: any) => T);
|
|
10
|
-
};
|
|
11
|
-
export type BaseCliParam<K extends string, V extends Primitive | Primitive[]> = {
|
|
12
|
-
keys: string[];
|
|
13
|
-
keyName: K;
|
|
14
|
-
type: TypeOfTypeAsString<V>;
|
|
15
|
-
description: string;
|
|
16
|
-
name?: string;
|
|
17
|
-
options?: string[];
|
|
18
|
-
initialValue?: V;
|
|
19
|
-
defaultValue?: V;
|
|
20
|
-
process?: (value?: string, defaultValue?: V) => V;
|
|
21
|
-
isArray?: true;
|
|
22
|
-
group?: string;
|
|
23
|
-
dependencies?: DependencyParam<any>[];
|
|
24
|
-
};
|
|
25
|
-
export type CliParam<K extends string, V extends Primitive | Primitive[]> = BaseCliParam<K, V> & {
|
|
26
|
-
name: string;
|
|
27
|
-
process: (value?: string, defaultValue?: V) => V;
|
|
28
|
-
};
|
package/shell/core/CliError.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { CustomException } from '@nu-art/ts-common';
|
|
2
|
-
import { ExecException } from 'child_process';
|
|
3
|
-
export declare class CliError extends CustomException {
|
|
4
|
-
stdout: string;
|
|
5
|
-
stderr: string;
|
|
6
|
-
cause: ExecException;
|
|
7
|
-
constructor(message: string, stdout: string, stderr: string, cause: ExecException);
|
|
8
|
-
}
|
|
9
|
-
export declare class CommandoException extends CustomException {
|
|
10
|
-
stdout: string;
|
|
11
|
-
stderr: string;
|
|
12
|
-
exitCode: number;
|
|
13
|
-
constructor(message: string, stdout: string, stderr: string, exitCode: number);
|
|
14
|
-
}
|
package/shell/core/CliError.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { CustomException } from '@nu-art/ts-common';
|
|
2
|
-
export class CliError extends CustomException {
|
|
3
|
-
stdout;
|
|
4
|
-
stderr;
|
|
5
|
-
cause;
|
|
6
|
-
constructor(message, stdout, stderr, cause) {
|
|
7
|
-
super(CliError, message, cause);
|
|
8
|
-
this.stdout = stdout;
|
|
9
|
-
this.stderr = stderr;
|
|
10
|
-
this.cause = cause;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export class CommandoException extends CustomException {
|
|
14
|
-
stdout;
|
|
15
|
-
stderr;
|
|
16
|
-
exitCode;
|
|
17
|
-
constructor(message, stdout, stderr, exitCode) {
|
|
18
|
-
super(CliError, message);
|
|
19
|
-
this.stdout = stdout;
|
|
20
|
-
this.stderr = stderr;
|
|
21
|
-
this.exitCode = exitCode;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
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
|
-
export declare const CommandoPool: {
|
|
7
|
-
allocateCommando: <T extends Constructor<any>[]>(uid: string, ...plugins: T) => MergeTypes<[...T]> & CommandoInteractive & BaseCommando & Commando_Basic;
|
|
8
|
-
killAll: () => Promise<void>;
|
|
9
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { CommandoInteractive } from '../interactive/CommandoInteractive.js';
|
|
2
|
-
import { Commando_Basic } from '../plugins/basic.js';
|
|
3
|
-
const commandoPool = [];
|
|
4
|
-
export const CommandoPool = {
|
|
5
|
-
allocateCommando: (uid, ...plugins) => {
|
|
6
|
-
const commando = CommandoInteractive.create(...plugins, Commando_Basic);
|
|
7
|
-
commando.setUID(uid);
|
|
8
|
-
commandoPool.push(commando);
|
|
9
|
-
return commando;
|
|
10
|
-
},
|
|
11
|
-
killAll: async () => {
|
|
12
|
-
await Promise.all(commandoPool.map(c => c.kill()));
|
|
13
|
-
}
|
|
14
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Constructor } from '@nu-art/ts-common';
|
|
2
|
-
/**
|
|
3
|
-
* Type utility to recursively merge the types of constructors in an array.
|
|
4
|
-
* @template T - An array of constructors.
|
|
5
|
-
*/
|
|
6
|
-
export type MergeTypes<T extends Constructor<any>[]> = T extends [a: Constructor<infer A>, ...rest: infer R] ? R extends Constructor<any>[] ? A & MergeTypes<R> : {} : {};
|
|
7
|
-
/**
|
|
8
|
-
* Function to merge multiple classes into a single class.
|
|
9
|
-
* @template T - An array of constructors.
|
|
10
|
-
* @param {...T} plugins - The constructors to merge.
|
|
11
|
-
* @returns {Constructor<MergeTypes<T>>} - A new constructor that merges all the provided constructors.
|
|
12
|
-
*/
|
|
13
|
-
export declare function MergeClass<T extends Constructor<any>[]>(...plugins: T): Constructor<MergeTypes<T>>;
|
|
14
|
-
/**
|
|
15
|
-
* Function to create an instance of a class that merges multiple classes.
|
|
16
|
-
* @template T - An array of constructors.
|
|
17
|
-
* @param {...T} plugins - The constructors to merge.
|
|
18
|
-
* @returns {MergeTypes<T>} - An instance of the merged class.
|
|
19
|
-
*/
|
|
20
|
-
export declare function CreateMergedInstance<T extends Constructor<any>[]>(...plugins: T): MergeTypes<T>;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function to merge multiple classes into a single class.
|
|
3
|
-
* @template T - An array of constructors.
|
|
4
|
-
* @param {...T} plugins - The constructors to merge.
|
|
5
|
-
* @returns {Constructor<MergeTypes<T>>} - A new constructor that merges all the provided constructors.
|
|
6
|
-
*/
|
|
7
|
-
export function MergeClass(...plugins) {
|
|
8
|
-
class SuperClass {
|
|
9
|
-
/**
|
|
10
|
-
* Constructs an instance of SuperClass, merging properties from all plugins.
|
|
11
|
-
* @param {...any[]} args - The arguments to pass to the constructors.
|
|
12
|
-
*/
|
|
13
|
-
constructor(...args) {
|
|
14
|
-
plugins.forEach(plugin => {
|
|
15
|
-
Object.getOwnPropertyNames(plugin.prototype).forEach(name => {
|
|
16
|
-
const prop = Object.getOwnPropertyDescriptor(plugin.prototype, name);
|
|
17
|
-
if (prop) {
|
|
18
|
-
Object.defineProperty(this, name, prop);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return SuperClass;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Function to create an instance of a class that merges multiple classes.
|
|
28
|
-
* @template T - An array of constructors.
|
|
29
|
-
* @param {...T} plugins - The constructors to merge.
|
|
30
|
-
* @returns {MergeTypes<T>} - An instance of the merged class.
|
|
31
|
-
*/
|
|
32
|
-
export function CreateMergedInstance(...plugins) {
|
|
33
|
-
const SuperClass = MergeClass(...plugins);
|
|
34
|
-
return new SuperClass();
|
|
35
|
-
}
|
package/shell/index.d.ts
DELETED
package/shell/index.js
DELETED