@gasket/utils
Advanced tools
Comparing version 6.14.0 to 6.15.0
# `@gasket/utils` | ||
### 6.15.0 | ||
- Support for AbortController with `runShellCommand` ([#331]) | ||
### 6.10.0 | ||
@@ -53,1 +57,2 @@ | ||
[#311]: https://github.com/godaddy/gasket/pull/311 | ||
[#331]: https://github.com/godaddy/gasket/pull/331 |
@@ -152,4 +152,8 @@ | ||
Options can be passed to the underlying spawn. An additional `signal` option | ||
can be passed to use AbortController, allowing processes to be killed when | ||
no longer needed. | ||
**Kind**: global function | ||
**Returns**: `Promise` - A promise represents if npm succeeds or fails. | ||
**Returns**: `Promise` - A promise represents if command succeeds or fails. | ||
**Access**: public | ||
@@ -160,4 +164,5 @@ | ||
| cmd | `string` | binary that is run | | ||
| argv | `array` | args passed to npm binary through spawn. | | ||
| options | `object` | options passed to npm binary through spawn | | ||
| argv | `array` | args passed to npm binary through spawn. | | ||
| options | `object` | options passed to npm binary through spawn | | ||
| \[options.signal\] | `object` | AbortControl signal allowing process to be canceled | | ||
| \[debug\] | `boolean` | When present pipes std{out,err} to process.* | | ||
@@ -173,3 +178,18 @@ | ||
``` | ||
**Example** | ||
```js | ||
// With timeout using AbortController | ||
const { runShellCommand } = require('@gasket/utils'); | ||
const AbortController = require('abort-controller'); | ||
async function helloWorld() { | ||
const controller = new AbortController(); | ||
// abort the process after 60 seconds | ||
const id = setTimeout(() => controller.abort(), 60000); | ||
await runShellCommand('long-process', ['something'], { signal: controller.signal }); | ||
clearTimeout(id); | ||
} | ||
``` | ||
## tryRequire(path) | ||
@@ -176,0 +196,0 @@ |
@@ -8,2 +8,6 @@ const concat = require('concat-stream'); | ||
* | ||
* Options can be passed to the underlying spawn. An additional `signal` option | ||
* can be passed to use AbortController, allowing processes to be killed when | ||
* no longer needed. | ||
* | ||
* @example | ||
@@ -16,12 +20,33 @@ * const { runShellCommand } = require('@gasket/utils'); | ||
* | ||
* @example | ||
* // With timeout using AbortController | ||
* | ||
* const { runShellCommand } = require('@gasket/utils'); | ||
* const AbortController = require('abort-controller'); | ||
* | ||
* async function helloWorld() { | ||
* const controller = new AbortController(); | ||
* // abort the process after 60 seconds | ||
* const id = setTimeout(() => controller.abort(), 60000); | ||
* await runShellCommand('long-process', ['something'], { signal: controller.signal }); | ||
* clearTimeout(id); | ||
* } | ||
* | ||
* @param {string} cmd binary that is run | ||
* @param {array} argv args passed to npm binary through spawn. | ||
* @param {object} options options passed to npm binary through spawn | ||
* @param {array} argv args passed to npm binary through spawn. | ||
* @param {object} options options passed to npm binary through spawn | ||
* @param {object} [options.signal] AbortControl signal allowing process to be canceled | ||
* @param {boolean} [debug] When present pipes std{out,err} to process.* | ||
* @returns {Promise} A promise represents if npm succeeds or fails. | ||
* @returns {Promise} A promise represents if command succeeds or fails. | ||
* @public | ||
*/ | ||
function runShellCommand(cmd, argv, options, debug) { | ||
function runShellCommand(cmd, argv, options = {}, debug = false) { | ||
const { signal, ...opts } = options; | ||
if (signal && signal.aborted) { | ||
return Promise.reject(Object.assign(new Error(`${ cmd } was aborted before spawn`), { argv, aborted: true })); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const child = spawn(cmd, argv, options); | ||
const child = spawn(cmd, argv, opts); | ||
@@ -44,8 +69,18 @@ let stderr; | ||
let aborted = false; | ||
if (signal) { | ||
signal.addEventListener('abort', () => { | ||
aborted = true; | ||
child.kill(); | ||
}); | ||
} | ||
child.on('close', code => { | ||
if (code !== 0) { | ||
return reject(Object.assign(new Error(`${cmd} exited with non-zero code`), { | ||
return reject(Object.assign(new Error(`${ cmd } exited with non-zero code`), { | ||
argv, | ||
stdout, | ||
stderr | ||
stderr, | ||
aborted, | ||
code | ||
})); | ||
@@ -52,0 +87,0 @@ } |
{ | ||
"name": "@gasket/utils", | ||
"version": "6.14.0", | ||
"version": "6.15.0", | ||
"description": "Reusable utilities for Gasket internals", | ||
@@ -44,2 +44,3 @@ "main": "lib", | ||
"@godaddy/dmd": "^1.0.0", | ||
"abort-controller": "^3.0.0", | ||
"assume": "^2.3.0", | ||
@@ -73,3 +74,3 @@ "assume-sinon": "^1.1.0", | ||
], | ||
"gitHead": "b13c1b3ab1c25dff4f7efc67e9685e790c88e38b" | ||
"gitHead": "a5519d55df47e3b1d3b9008c1a1c1add562192d9" | ||
} |
22716
336
14