@j-o-r/sh 1.0.6 → 1.0.7

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
@@ -140,11 +140,11 @@ class SHDispatch {
140
140
  // @ts-ignore
141
141
  return new SHExec(this.#cmd, this.#options).runSync(payload);
142
142
  }
143
- async kill() {
144
- try {
145
- await this.#proc.kill();
146
- } catch (_e) { }
143
+ async kill(signal = 'SIGTERM') {
144
+ let res = [];
145
+ res = await this.#proc.kill(signal);
147
146
  this.#proc = undefined;
147
+ return res;
148
148
  }
149
149
  }
150
150
 
package/lib/SHExecute.js CHANGED
@@ -20,7 +20,7 @@ const killProcesses = (processPid, signal) => {
20
20
  reject(new Error(stderr));
21
21
  return;
22
22
  }
23
- const pids = stdout.split(/\r?\n/).filter(pid => pid);
23
+ const pids = stdout.split(/\r?\n/).filter(pid => pid) || [];
24
24
  // Kill each child process
25
25
  try {
26
26
  for (const pid of pids) {
@@ -48,6 +48,7 @@ class SHExecute {
48
48
  /**
49
49
  * @type {import('child_process').ChildProcess}
50
50
  */
51
+ #forcedKill = false;
51
52
  #proc;
52
53
  #command = '';
53
54
  #options = {};
@@ -127,6 +128,11 @@ class SHExecute {
127
128
  }
128
129
  this.#proc.on('close', (code) => {
129
130
  if (timeout) clearTimeout(timeout);
131
+ if (this.#forcedKill) {
132
+ // Resolve without content
133
+ resolve();
134
+ return;
135
+ }
130
136
  if (code === 0) {
131
137
  resolve(this.#stdout.trim());
132
138
  } else {
@@ -140,13 +146,16 @@ class SHExecute {
140
146
  });
141
147
  }
142
148
  /**
149
+ * @param {string} signal - kill signal
143
150
  * @returns {Promise<number[]>}
144
151
  */
145
152
  async kill(signal = 'SIGTERM') {
146
153
  if (!this.#proc) throw new Error('Trying to kill a process without creating one.');
147
154
  if (!this.#proc.pid) throw new Error('The process pid is undefined.');
148
-
149
- return killProcesses(this.#proc.pid, signal);
155
+ this.#forcedKill = true;
156
+ const res = killProcesses(this.#proc.pid, signal);
157
+ this.#proc = undefined;
158
+ return res;
150
159
  }
151
160
  }
152
161
 
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.6",
5
+ "version": "1.0.7",
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",
@@ -77,6 +77,6 @@ declare class SHDispatch {
77
77
  * @returns {SpawnSyncResponse}
78
78
  */
79
79
  runSync(payload?: string | undefined): SpawnSyncResponse;
80
- kill(): Promise<void>;
80
+ kill(signal?: string): Promise<number[]>;
81
81
  #private;
82
82
  }
@@ -16,6 +16,7 @@ declare class SHExecute {
16
16
  */
17
17
  run(payload?: string | undefined): Promise<any>;
18
18
  /**
19
+ * @param {string} signal - kill signal
19
20
  * @returns {Promise<number[]>}
20
21
  */
21
22
  kill(signal?: string): Promise<number[]>;