@nu-art/commando 0.400.14 → 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.
Files changed (43) hide show
  1. package/cli-params/CLIParamsResolver.d.ts +56 -3
  2. package/cli-params/CLIParamsResolver.js +61 -4
  3. package/cli-params/consts.d.ts +37 -0
  4. package/cli-params/consts.js +38 -1
  5. package/cli-params/types.d.ts +29 -1
  6. package/package.json +23 -11
  7. package/shell/core/BaseCommando.d.ts +36 -6
  8. package/shell/core/BaseCommando.js +36 -6
  9. package/shell/core/CliError.d.ts +35 -0
  10. package/shell/core/CliError.js +36 -1
  11. package/shell/core/CommandBuilder.d.ts +27 -2
  12. package/shell/core/CommandBuilder.js +32 -3
  13. package/shell/core/CommandoPool.d.ts +33 -0
  14. package/shell/core/CommandoPool.js +34 -0
  15. package/shell/core/class-merger.d.ts +29 -10
  16. package/shell/core/class-merger.js +23 -8
  17. package/shell/interactive/CommandoInteractive.d.ts +66 -6
  18. package/shell/interactive/CommandoInteractive.js +69 -6
  19. package/shell/interactive/InteractiveShell.d.ts +38 -0
  20. package/shell/interactive/InteractiveShell.js +25 -0
  21. package/shell/plugins/basic.d.ts +90 -9
  22. package/shell/plugins/basic.js +90 -9
  23. package/shell/plugins/git.d.ts +115 -0
  24. package/shell/plugins/git.js +124 -9
  25. package/shell/plugins/nvm.d.ts +47 -0
  26. package/shell/plugins/nvm.js +47 -0
  27. package/shell/plugins/pnpm.d.ts +31 -0
  28. package/shell/plugins/pnpm.js +31 -0
  29. package/shell/plugins/programming.d.ts +23 -3
  30. package/shell/plugins/programming.js +23 -3
  31. package/shell/plugins/python.d.ts +31 -0
  32. package/shell/plugins/python.js +32 -0
  33. package/shell/services/nvm.d.ts +59 -1
  34. package/shell/services/nvm.js +67 -6
  35. package/shell/services/pnpm.d.ts +41 -0
  36. package/shell/services/pnpm.js +41 -0
  37. package/shell/simple/Commando.d.ts +75 -0
  38. package/shell/simple/Commando.js +75 -0
  39. package/shell/simple/SimpleShell.d.ts +29 -2
  40. package/shell/simple/SimpleShell.js +32 -2
  41. package/shell/tools.d.ts +30 -0
  42. package/shell/tools.js +30 -0
  43. package/shell/types.d.ts +14 -0
@@ -2,8 +2,34 @@ import { ThisShouldNotHappenException } from '@nu-art/ts-common';
2
2
  import { SimpleShell } from './SimpleShell.js';
3
3
  import { BaseCommando } from '../core/BaseCommando.js';
4
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
+ */
5
23
  export class Commando extends BaseCommando {
24
+ /** Optional unique identifier for log tagging */
6
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
+ */
7
33
  static create(...plugins) {
8
34
  const _commando = BaseCommando._create(Commando, ...plugins);
9
35
  return _commando;
@@ -11,10 +37,29 @@ export class Commando extends BaseCommando {
11
37
  constructor() {
12
38
  super();
13
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
+ */
14
49
  setUID(uid) {
15
50
  this.uid = uid;
16
51
  return this;
17
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
+ */
18
63
  executeFile(filePath, interpreter) {
19
64
  let command = filePath;
20
65
  // If an interpreter is provided, prefix the command with it.
@@ -23,10 +68,40 @@ export class Commando extends BaseCommando {
23
68
  }
24
69
  return new SimpleShell().execute(command);
25
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
+ */
26
83
  executeRemoteFile(pathToFile, interpreter) {
27
84
  const command = `curl -o- "${pathToFile}" | ${interpreter}`;
28
85
  return new SimpleShell().execute(command);
29
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
+ */
30
105
  async execute(callback) {
31
106
  const command = this.builder.reset();
32
107
  const simpleShell = new SimpleShell().debug(this._debug);
@@ -1,14 +1,41 @@
1
1
  import { ExecOptions } from 'child_process';
2
2
  import { Logger } from '@nu-art/ts-common';
3
+ /**
4
+ * Extended ExecOptions with encoding support.
5
+ */
3
6
  export type CliOptions = ExecOptions & {
4
7
  encoding: BufferEncoding | string | null;
5
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
+ */
6
22
  export declare class SimpleShell extends Logger {
23
+ /** Debug mode flag (enables verbose command logging) */
7
24
  private _debug;
25
+ /** Execution options for child_process.exec() */
8
26
  private cliOptions;
9
27
  /**
10
- * Executes the accumulated commands in the command list.
11
- * @returns {Promise<string>} A promise that resolves with the standard output of the executed command.
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
12
39
  */
13
40
  execute: (command: string) => Promise<{
14
41
  stdout: string;
@@ -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 the accumulated commands in the command list.
16
- * @returns {Promise<string>} A promise that resolves with the standard output of the executed command.
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/shell/tools.d.ts CHANGED
@@ -1,3 +1,33 @@
1
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
+ */
2
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
+ */
3
33
  export declare function convertToFullPath(pathToFile: string, assetParentPath?: string): AbsolutePath;
package/shell/tools.js CHANGED
@@ -1,8 +1,38 @@
1
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
+ */
2
15
  export function removeAnsiCodes(text) {
3
16
  // This regular expression matches the escape codes and removes them
4
17
  return text.replace(/\x1B\[\d+;?\d*m/g, '');
5
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
+ */
6
36
  export function convertToFullPath(pathToFile, assetParentPath = process.cwd()) {
7
37
  if (!pathToFile)
8
38
  throw new Error('Path not provided');
package/shell/types.d.ts CHANGED
@@ -1,3 +1,17 @@
1
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
+ */
2
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
+ */
3
17
  export type CliBlock<Commando extends BaseCommando> = (cli: Commando) => void;