From 392a31e58eb9db7520a8b3e4ab5cbeaca5744170 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Mon, 26 Feb 2018 23:18:23 +0000 Subject: [PATCH] fix: put windows drive letter tweak in right place Ref #1263 --- lib/monitor/run.js | 47 +++++++++------------------------ lib/monitor/spawn-args.js | 46 ++++++++++++++++++++++++++++++++ lib/monitor/watch.js | 15 ++++++----- package-lock.json | 39 +++++++++++++++++++++++++++ package.json | 3 ++- test/monitor/spawn-args.test.js | 31 ++++++++++++++++++++++ 6 files changed, 139 insertions(+), 42 deletions(-) create mode 100644 lib/monitor/spawn-args.js create mode 100644 test/monitor/spawn-args.test.js diff --git a/lib/monitor/run.js b/lib/monitor/run.js index 930b3099..ad5dc0d1 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -14,51 +14,30 @@ var restart = null; var psTree = require('pstree.remy'); var path = require('path'); var signals = require('./signals'); +const getSpawnArgs = require('./spawn-args'); function run(options) { - var cmd = config.command.raw; - - var runCmd = !options.runOnChangeOnly || config.lastStarted !== 0; - if (runCmd) { - utils.log.status('starting `' + config.command.string + '`'); - } - /*jshint validthis:true*/ restart = run.bind(this, options); run.restart = restart; config.lastStarted = Date.now(); - var stdio = ['pipe', 'pipe', 'pipe']; - - if (config.options.stdout) { - stdio = ['pipe', process.stdout, process.stderr]; - } - - if (config.options.stdin === false) { - stdio = [process.stdin, process.stdout, process.stderr]; - } + const { + args, + cmd, + executable, + runCmd, + sh, + shFlag, + spawnArgs, + stdio, + } = getSpawnArgs({ options, config }); - var sh = 'sh'; - var shFlag = '-c'; - - - const spawnOptions = { - env: utils.merge(options.execOptions.env, process.env), - stdio: stdio, - } - - if (utils.isWindows) { - // taken from npm's cli: https://git.io/vNFD4 - sh = process.env.comspec || 'cmd'; - shFlag = '/d /s /c'; - spawnOptions.windowsVerbatimArguments = true; + if (runCmd) { + utils.log.status('starting `' + config.command.string + '`'); } - var executable = cmd.executable; - var args = runCmd ? utils.stringify(executable, cmd.args) : ':'; - var spawnArgs = [sh, [shFlag, args], spawnOptions]; - const firstArg = cmd.args[0] || ''; // hasStdio allows us to correctly handle stdin piping diff --git a/lib/monitor/spawn-args.js b/lib/monitor/spawn-args.js new file mode 100644 index 00000000..7c0a484d --- /dev/null +++ b/lib/monitor/spawn-args.js @@ -0,0 +1,46 @@ +const utils = require('../utils'); + +module.exports = ({ options, config }) => { + + var stdio = ['pipe', 'pipe', 'pipe']; + + if (config.options.stdout) { + stdio = ['pipe', process.stdout, process.stderr]; + } + + if (config.options.stdin === false) { + stdio = [process.stdin, process.stdout, process.stderr]; + } + + var sh = 'sh'; + var shFlag = '-c'; + + const spawnOptions = { + env: utils.merge(options.execOptions.env, process.env), + stdio: stdio, + } + + if (utils.isWindows) { + // taken from npm's cli: https://git.io/vNFD4 + sh = process.env.comspec || 'cmd'; + shFlag = '/d /s /c'; + spawnOptions.windowsVerbatimArguments = true; + } + + var runCmd = !options.runOnChangeOnly || config.lastStarted !== 0; + var cmd = config.command.raw; + var executable = cmd.executable; + var args = runCmd ? utils.stringify(executable, cmd.args) : ':'; + var spawnArgs = [sh, [shFlag, args], spawnOptions]; + + return { + args, + cmd, + executable, + runCmd, + sh, + shFlag, + spawnArgs, + stdio, + }; +} diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js index 19b8d508..60e4c712 100644 --- a/lib/monitor/watch.js +++ b/lib/monitor/watch.js @@ -129,13 +129,6 @@ function filterAndRestart(files) { files = [files]; } if (files.length) { - if (utils.isWindows) { - // ensure the drive letter is in uppercase (c:\foo -> C:\foo) - files = files.map(function (f) { - return f[0].toUpperCase() + f.slice(1); - }); - } - var cwd = this.options ? this.options.cwd : process.cwd(); utils.log.detail( 'files triggering change check: ' + @@ -150,6 +143,14 @@ function filterAndRestart(files) { return path.relative(process.cwd(), path.join(cwd, file)); }); + if (utils.isWindows) { + // ensure the drive letter is in uppercase (c:\foo -> C:\foo) + files = files.map(function (f) { + return f[0].toUpperCase() + f.slice(1); + }); + } + + debug('filterAndRestart on', files); var matched = match( diff --git a/package-lock.json b/package-lock.json index fba233a4..4636c6c9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1421,6 +1421,16 @@ "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, + "fill-keys": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/fill-keys/-/fill-keys-1.0.2.tgz", + "integrity": "sha1-mo+jb06K1jTjv2tPPIiCVRRS6yA=", + "dev": true, + "requires": { + "is-object": "1.0.1", + "merge-descriptors": "1.0.1" + } + }, "fill-range": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz", @@ -3154,6 +3164,12 @@ "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz", "integrity": "sha1-PkcprB9f3gJc19g6iW2rn09n2w8=" }, + "is-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.1.tgz", + "integrity": "sha1-iVJojF7C/9awPsyF52ngKQMINHA=", + "dev": true + }, "is-odd": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-odd/-/is-odd-1.0.0.tgz", @@ -3852,6 +3868,12 @@ "trim-newlines": "1.0.0" } }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=", + "dev": true + }, "micromatch": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-3.1.5.tgz", @@ -4044,6 +4066,12 @@ "integrity": "sha1-4rbN65zhn5kxelNyLz2/XfXqqrI=", "dev": true }, + "module-not-found-error": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/module-not-found-error/-/module-not-found-error-1.0.1.tgz", + "integrity": "sha1-z4tP9PKWQGdNbN0CsOO8UjwrvcA=", + "dev": true + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -4679,6 +4707,17 @@ "integrity": "sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=", "dev": true }, + "proxyquire": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/proxyquire/-/proxyquire-1.8.0.tgz", + "integrity": "sha1-AtUUpb7ZhvBMuyCTrxZ0FTX3ntw=", + "dev": true, + "requires": { + "fill-keys": "1.0.2", + "module-not-found-error": "1.0.1", + "resolve": "1.1.7" + } + }, "ps-tree": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/ps-tree/-/ps-tree-1.1.0.tgz", diff --git a/package.json b/package.json index 9e8f9b0b..3671fbbf 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "istanbul": "^0.4.5", "jscs": "^3.0.7", "mocha": "^2.3.3", + "proxyquire": "^1.8.0", "semantic-release": "^8.2.0", "should": "~4.0.0" }, @@ -64,5 +65,5 @@ "undefsafe": "^2.0.1", "update-notifier": "^2.3.0" }, - "version": "0.0.0-development" + "version": "1.15.2-alpha.1" } diff --git a/test/monitor/spawn-args.test.js b/test/monitor/spawn-args.test.js new file mode 100644 index 00000000..2ebf4df0 --- /dev/null +++ b/test/monitor/spawn-args.test.js @@ -0,0 +1,31 @@ +/*global describe:true, it: true, after: true, beforeEach */ +const assert = require('assert'); +const merge = require('../../lib/utils/merge'); +const proxyquire = require('proxyquire').noPreserveCache(); + +describe('spawn args', () => { + it('handles windows urls correctly', () => { + const getSpawnArgs = proxyquire('../../lib/monitor/spawn-args', { + merge, + isWindows: true, + }); + + const res = getSpawnArgs({ + options: { + execOptions: { env: [] }, + }, + config: { + options: {}, + command: { + raw: 'touch' + } + }, + }); + + console.log(res); + + assert('ok'); + + + }); +});