@sap-ux/nodejs-utils 0.2.1 → 0.2.3

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.
@@ -1,4 +1,5 @@
1
1
  import { type SpawnOptionsWithoutStdio } from 'child_process';
2
+ import type { Logger } from '@sap-ux/logger';
2
3
  /**
3
4
  *
4
5
  */
@@ -9,9 +10,10 @@ export declare class CommandRunner {
9
10
  * @param {string} cmd to execute
10
11
  * @param {string[]} args to pass to the command
11
12
  * @param {SpawnOptionsWithoutStdio} [opts] options to pass to the command
13
+ * @param {Logger} [logger] optional logger to capture command output
12
14
  * @returns {*} {(Promise<any | void>)}
13
15
  * @memberof CommandRunner
14
16
  */
15
- run(cmd: string, args?: string[], opts?: SpawnOptionsWithoutStdio): Promise<string | void>;
17
+ run(cmd: string, args?: string[], opts?: SpawnOptionsWithoutStdio, logger?: Logger): Promise<string | void>;
16
18
  }
17
19
  //# sourceMappingURL=commandRunner.d.ts.map
@@ -12,10 +12,14 @@ class CommandRunner {
12
12
  * @param {string} cmd to execute
13
13
  * @param {string[]} args to pass to the command
14
14
  * @param {SpawnOptionsWithoutStdio} [opts] options to pass to the command
15
+ * @param {Logger} [logger] optional logger to capture command output
15
16
  * @returns {*} {(Promise<any | void>)}
16
17
  * @memberof CommandRunner
17
18
  */
18
- run(cmd, args = [], opts = {}) {
19
+ run(cmd, args = [], opts = {}, logger) {
20
+ if (logger) {
21
+ logger.debug(`Running command: ${cmd} ${args.join(' ')}`);
22
+ }
19
23
  return new Promise((resolve, reject) => {
20
24
  const stack = [];
21
25
  const spawnOpts = process.platform === 'win32' ? { ...opts, shell: true } : opts;
@@ -23,17 +27,23 @@ class CommandRunner {
23
27
  spawnedCmd.stdout.setEncoding('utf8');
24
28
  let response;
25
29
  spawnedCmd.stdout.on('data', (data) => {
30
+ logger?.info(data.toString().replace(/[\r\n]$/, '')); // remove trailing newline as another is added by the logger
26
31
  response = data.toString();
27
32
  });
28
33
  spawnedCmd.stderr.on('data', (data) => {
34
+ logger?.info(data.toString().replace(/[\r\n]$/, '')); // remove trailing newline as another is added by the logger
29
35
  stack.push(data.toString());
30
36
  });
31
37
  spawnedCmd.on('error', (error) => {
32
- reject(`Command failed with error: ${error.message}`);
38
+ const cmdFailedMsg = `Command failed with error: ${error.message}`;
39
+ logger?.error(cmdFailedMsg);
40
+ reject(error);
33
41
  });
34
42
  spawnedCmd.on('close', (errorCode) => {
35
43
  if (errorCode !== 0) {
36
- reject(`Command failed, \`${cmd} ${args.join(' ')}\`, ${stack.join(', ')}`);
44
+ const cmdFailedMsg = `Command failed, \`${cmd} ${args.join(' ')}\`, ${stack.join(', ')}`;
45
+ logger?.debug(cmdFailedMsg);
46
+ reject(cmdFailedMsg);
37
47
  }
38
48
  resolve(response);
39
49
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sap-ux/nodejs-utils",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Nodejs utility wrappers",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,12 +17,13 @@
17
17
  "fast-glob": "3.3.1",
18
18
  "read-pkg-up": "7.0.1",
19
19
  "semver": "7.5.4",
20
- "@sap-ux/btp-utils": "1.1.0"
20
+ "@sap-ux/btp-utils": "1.1.1"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/semver": "7.5.2",
24
24
  "@types/vscode": "1.73.1",
25
- "mock-spawn": "0.2.6"
25
+ "mock-spawn": "0.2.6",
26
+ "@sap-ux/logger": "0.7.0"
26
27
  },
27
28
  "files": [
28
29
  "dist",