Skip to content
Myr

Add `cleanup` option (#53)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

As spawned processes are not cleaned up when the main process dies this option adds the abilitiy to kill those when the spawning process dies. Closes #51

authored by Friedel Ziegelmayer · committed by Sindre Sorhus · parent 03964c1 · Browse files · 6 files+69 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+69 added−1 removed

  • other:+69 added−1 removed

6 files+69 added−1 removed

added:fixtures/sub-process+5 added
1#!/usr/bin/env node2'use strict';3var m = require('../');45console.log(m('forever').pid);
added:fixtures/sub-process-false+5 added
1#!/usr/bin/env node2'use strict';3var m = require('../');45console.log(m('forever', {cleanup: false}).pid);
modified:index.js+14 added-1 removed
6 unmodified lines
7var npmRunPath = require('npm-run-path');8var isStream = require('is-stream');9var _getStream = require('get-stream');10var pathKey = require('path-key')();11var onExit = require('signal-exit');12var errname = require('./lib/errname');1314var TEN_MEBIBYTE = 1024 * 1024 * 10;15
17 unmodified lines
33 maxBuffer: TEN_MEBIBYTE,34 stripEof: true,35 preferLocal: true,36 encoding: 'utf8',36 reject: true37 reject: true,38 cleanup: true39 }, parsed.options);4041 if (opts.preferLocal) {42 opts.env = objectAssign({}, opts.env || process.env);
110 unmodified lines
153 } catch (err) {154 return Promise.reject(err);155 }156157 var removeExitHandler;158 if (parsed.opts.cleanup) {159 removeExitHandler = onExit(function () {160 spawned.kill();161 });162 }163164 var promise = Promise.all([165 processDone(spawned),166 getStream(spawned, 'stdout', encoding, maxBuffer),167 getStream(spawned, 'stderr', encoding, maxBuffer)
5 unmodified lines
173 var err = result.err;174 var code = result.code;175 var signal = result.signal;176177 if (removeExitHandler) {178 removeExitHandler();179 }180181 if (err || code !== 0 || signal !== null) {182 if (!err) {183 err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);184
80 unmodified lines
modified:package.json+3 added
modified:readme.md+7 added
9 unmodified lines
10- Supports [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries cross-platform.11- [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn#why)12- Higher max buffer. 10 MB instead of 200 KB.13- [Executes locally installed binaries by name.](#preferlocal)14- [Cleans up spawned processes when the parent process dies.](#cleanup)151617## Install18
119 unmodified lines
138Default: `true`139140Setting this to `false` resolves the promise with the error instead of rejecting it.141142#### cleanup143144Type: `boolean`<br>145Default: `true`146147Keep track of the spawned process and `kill` it when the parent process exits.148149## License150151MIT © [Sindre Sorhus](https://sindresorhus.com)
modified:test.js+35 added
1 unmodified line
2import stream from 'stream';3import childProcess from 'child_process';4import test from 'ava';5import getStream from 'get-stream';6import isRunning from 'is-running';7import delay from 'delay';8import m from './';910process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;11
272 unmodified lines
284285test(cmd, ' foo bar', 'foo', 'bar');286test(cmd, ' baz quz', 'baz', 'quz');287test(cmd, '');288289async function spawnAndKill(t, signal, cleanup) {290 const name = cleanup ? 'sub-process' : 'sub-process-false';291 const cp = m(name);292 let pid;293294 cp.stdout.setEncoding('utf8');295 cp.stdout.on('data', chunk => {296 pid = parseInt(chunk, 10);297 t.is(typeof pid, 'number');298299 setTimeout(() => {300 process.kill(cp.pid, signal);301 }, 100);302 });303304 await t.throws(cp);305306 // Give everybody some time to breath and kill things307 await delay(200);308309 t.false(isRunning(cp.pid));310 t.is(isRunning(pid), !cleanup);311}312313test('cleanup - SIGINT', spawnAndKill, 'SIGINT', true);314test('cleanup - SIGKILL', spawnAndKill, 'SIGTERM', true);315316if (process.platform !== 'win32') {317 // On Windows the subprocesses are actually always killed318 test('cleanup false - SIGINT', spawnAndKill, 'SIGTERM', false);319 test('cleanup false - SIGKILL', spawnAndKill, 'SIGKILL', false);320}