Skip to content

Commit e90f15a

Browse files
authored
fix: node < 6.4.0 causing crash on 'rs' trigger
* fix: node < 6.4.0 can't connect to stdin * fix: child stdio when spawn is similar to fork Fixes #1218
1 parent e95ea6f commit e90f15a

File tree

9 files changed

+163
-176
lines changed

9 files changed

+163
-176
lines changed

lib/monitor/match.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ function tryBaseDir(dir) {
131131
function match(files, monitor, ext) {
132132
// sort the rules by highest specificity (based on number of slashes)
133133
// ignore rules (!) get sorted highest as they take precedent
134-
// TODO actually check separator rules work on windows
135134
const cwd = process.cwd();
136135
var rules = monitor.sort(function (a, b) {
137136
var r = b.split(path.sep).length - a.split(path.sep).length;

lib/monitor/run.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,30 @@ function run(options) {
6868
var args = runCmd ? utils.stringify(executable, cmd.args) : ':';
6969
var spawnArgs = [sh, [shFlag, args]];
7070

71-
if (utils.version.major === 0 && utils.version.minor < 8) {
72-
// use the old spawn args :-\
73-
} else {
74-
spawnArgs.push({
75-
env: utils.merge(options.execOptions.env, process.env),
76-
stdio: stdio,
77-
});
78-
}
71+
spawnArgs.push({
72+
env: utils.merge(options.execOptions.env, process.env),
73+
stdio: stdio,
74+
});
7975

8076
const firstArg = cmd.args[0] || '';
8177

78+
// hasStdio allows us to correctly handle stdin piping
79+
// see: https://git.io/vNtX3
80+
const hasStdio = utils.satisfies('>= 6.4.0 || < 5');
81+
8282
if (
83-
// this is a hack to avoid forking if there's a node argument being passed
84-
// it's a short term fix, and I'm not 100% sure that `fork` is the right way
85-
firstArg.indexOf('-') === -1 &&
86-
firstArg !== 'inspect' &&
87-
executable === 'node' &&
88-
utils.version.major > 4
83+
firstArg.indexOf('-') === -1 && // don't fork if there's a node arg
84+
firstArg !== 'inspect' && // don't fork it's `inspect` debugger
85+
executable === 'node' && // only fork if node
86+
utils.version.major > 4 // only fork if node version > 4
8987
) {
9088
var forkArgs = cmd.args.slice(1);
9189
var env = utils.merge(options.execOptions.env, process.env);
9290
stdio.push('ipc');
9391
child = fork(options.execOptions.script, forkArgs, {
9492
env: env,
9593
stdio: stdio,
94+
silent: !hasStdio,
9695
});
9796
utils.log.detail('forking');
9897
debug(forkArgs);
@@ -216,9 +215,7 @@ function run(options) {
216215
// if the stdin piping is on, we need to unpipe, but also close stdin on
217216
// the child, otherwise linux can throw EPIPE or ECONNRESET errors.
218217
if (options.stdin) {
219-
if (process.stdin.unpipe) { // node > 0.8
220-
process.stdin.unpipe(child.stdin);
221-
}
218+
process.stdin.unpipe(child.stdin);
222219
}
223220

224221
if (utils.isWindows) {
@@ -234,12 +231,8 @@ function run(options) {
234231
// this seems to fix the 0.11.x issue with the "rs" restart command,
235232
// though I'm unsure why. it seems like more data is streamed in to
236233
// stdin after we close.
237-
if (child && options.stdin && oldPid === child.pid) {
238-
// this is stupid and horrible, but node 0.12 on windows blows up
239-
// with this line, so we'll skip it entirely.
240-
if (!utils.isWindows) {
241-
child.stdin.end();
242-
}
234+
if (child && options.stdin && child.stdin && oldPid === child.pid) {
235+
child.stdin.end();
243236
}
244237
callback();
245238
});
@@ -263,8 +256,12 @@ function run(options) {
263256

264257
// swallow the stdin error if it happens
265258
// ref: https://github.com/remy/nodemon/issues/1195
266-
child.stdin.on('error', () => { });
267-
process.stdin.pipe(child.stdin);
259+
if (hasStdio) {
260+
child.stdin.on('error', () => { });
261+
process.stdin.pipe(child.stdin);
262+
} else {
263+
child.stdout.pipe(process.stdout);
264+
}
268265

269266
bus.once('exit', function () {
270267
if (child && process.stdin.unpipe) { // node > 0.8

lib/spawn.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
var utils = require('./utils');
2-
var merge = utils.merge;
3-
var bus = utils.bus;
4-
var childProcess = require('child_process');
5-
var _spawn = childProcess.spawn;
1+
const utils = require('./utils');
2+
const merge = utils.merge;
3+
const bus = utils.bus;
4+
const spawn = require('child_process').spawn;
65

7-
module.exports = function spawn(command, config, eventArgs) {
6+
module.exports = function spawnCommand(command, config, eventArgs) {
87
var stdio = ['pipe', 'pipe', 'pipe'];
9-
var child = null;
108

119
if (config.options.stdout) {
1210
stdio = ['pipe', process.stdout, process.stderr];
@@ -20,23 +18,18 @@ module.exports = function spawn(command, config, eventArgs) {
2018
shFlag = '/c';
2119
}
2220

23-
var args = '';
2421

2522
if (!Array.isArray(command)) {
2623
command = [command];
2724
}
2825

29-
args = command.join(' ');
26+
const args = command.join(' ');
3027

31-
if (utils.version.major >= 1 || utils.version.minor >= 8) {
32-
var env = merge(process.env, { FILENAME: eventArgs[0] });
33-
child = _spawn(sh, [shFlag, args], {
34-
env: merge(config.options.execOptions.env, env),
35-
stdio: stdio,
36-
});
37-
} else {
38-
child = spawn(sh, args);
39-
}
28+
const env = merge(process.env, { FILENAME: eventArgs[0] });
29+
const child = spawn(sh, [shFlag, args], {
30+
env: merge(config.options.execOptions.env, env),
31+
stdio: stdio,
32+
});
4033

4134
if (config.required) {
4235
var emit = {

lib/utils/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
var noop = function () { };
22
var path = require('path');
3+
const semver = require('semver');
34
var version = process.versions.node.split('.') || [null, null, null];
45

56
var utils = (module.exports = {
7+
semver: semver,
8+
satisfies: test => semver.satisfies(process.versions.node, test),
69
version: {
710
major: parseInt(version[0] || 0, 10),
811
minor: parseInt(version[1] || 0, 10),

0 commit comments

Comments
 (0)