@j-o-r/sh 1.0.7 → 1.0.8

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/lib/SHDispatch.js CHANGED
@@ -18,6 +18,7 @@ import SHExec from './SHExecute.js';
18
18
  * @property {string} [prefix] - The prefix commands to ensure a safe execution environment. e.g: prefix: 'set -euo pipefail;/usr/bin/env',
19
19
  * @property {StdioOptions|StdioOption} [stdio] - The stdio configuration.
20
20
  * @property {number} [timeout] - default 20000: a timeout error is triggerd when a process execution time is exceeded, 0 is no timeout
21
+ * @property {boolean} [detached] - default false, when true it runnes as a background process
21
22
  */
22
23
  /**
23
24
  * @typedef {('pipe' | 'ignore' | 'inherit' | number)} StdioOption
@@ -86,6 +87,7 @@ const defaultOptions = {
86
87
  cwd: process.cwd(),
87
88
  env: process.env,
88
89
  shell: 'bash',
90
+ detached: false,
89
91
  prefix: '/usr/bin/env',
90
92
  stdio: ['inherit', 'pipe', 'pipe'],
91
93
  timeout: 10000 // when 0 there is no timeout
package/lib/SHExecute.js CHANGED
@@ -73,7 +73,7 @@ class SHExecute {
73
73
  if (payload && typeof payload !== 'string') {
74
74
  throw new Error('Argument is not a string');
75
75
  }
76
- let { cwd, shell, env, stdio } = this.#options;
76
+ let { cwd, shell, env, stdio, detached } = this.#options;
77
77
  // pipe need to be set on stdin when posting a payload
78
78
  if (payload) stdio[0] = 'pipe';
79
79
  const input = payload || undefined;
@@ -81,6 +81,7 @@ class SHExecute {
81
81
  cwd,
82
82
  shell,
83
83
  stdio,
84
+ detached,
84
85
  windowsHide: true,
85
86
  env,
86
87
  input
@@ -146,6 +147,7 @@ class SHExecute {
146
147
  });
147
148
  }
148
149
  /**
150
+ * Kill this process and possible child processes
149
151
  * @param {string} signal - kill signal
150
152
  * @returns {Promise<number[]>}
151
153
  */
@@ -153,7 +155,16 @@ class SHExecute {
153
155
  if (!this.#proc) throw new Error('Trying to kill a process without creating one.');
154
156
  if (!this.#proc.pid) throw new Error('The process pid is undefined.');
155
157
  this.#forcedKill = true;
156
- const res = killProcesses(this.#proc.pid, signal);
158
+ let res = [];
159
+ try {
160
+ // Try to kill pid 'childs'
161
+ res = await killProcesses(this.#proc.pid, signal);
162
+ } catch (_e) { }
163
+ if (!res.includes(this.#proc.pid)) {
164
+ // Kill self if I am not allready killed
165
+ res.push(this.#proc.pid);
166
+ this.#proc.kill(signal);
167
+ }
157
168
  this.#proc = undefined;
158
169
  return res;
159
170
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@j-o-r/sh",
3
3
  "author": "Jorrit Duin <j-o-r@duin.work>",
4
4
  "type": "module",
5
- "version": "1.0.7",
5
+ "version": "1.0.8",
6
6
  "description": "Execute shell commands on Linux-based systems from javascript",
7
7
  "main": "lib/SH.js",
8
8
  "types": "types/SH.d.ts",
@@ -53,6 +53,10 @@ export type SHOptions = {
53
53
  * - default 20000: a timeout error is triggerd when a process execution time is exceeded, 0 is no timeout
54
54
  */
55
55
  timeout?: number | undefined;
56
+ /**
57
+ * - default false, when true it runnes as a background process
58
+ */
59
+ detached?: boolean | undefined;
56
60
  };
57
61
  export type StdioOption = ("pipe" | "ignore" | "inherit" | number);
58
62
  export type StdioOptions = Array<StdioOption> | StdioOption;
@@ -16,6 +16,7 @@ declare class SHExecute {
16
16
  */
17
17
  run(payload?: string | undefined): Promise<any>;
18
18
  /**
19
+ * Kill this process and possible child processes
19
20
  * @param {string} signal - kill signal
20
21
  * @returns {Promise<number[]>}
21
22
  */