From 0e2be51848ac259137197aba5db2879de72f59b5 Mon Sep 17 00:00:00 2001 From: Charles Samborski Date: Sun, 12 Aug 2018 10:39:29 +0200 Subject: [PATCH] fix: Use `is-windows` package for detection This commit replaces the vendored `lib/is-windows` module by the `is-windows` package. - Closes istanbuljs/spawn-wrap#61 --- index.js | 18 +++++++++--------- lib/is-windows.js | 5 ----- package-lock.json | 5 +++++ package.json | 1 + shim.js | 10 ++-------- test/basic.js | 10 +++++----- 6 files changed, 22 insertions(+), 27 deletions(-) delete mode 100644 lib/is-windows.js diff --git a/index.js b/index.js index b922e2b..7b87340 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const cp = require('child_process') const ChildProcess = cp.ChildProcess const assert = require('assert') const crypto = require('crypto') +const IS_WINDOWS = require('is-windows')() const mkdirp = require('mkdirp') const rimraf = require('rimraf') const path = require('path') @@ -34,11 +35,9 @@ const shebang = process.platform === 'os390' ? const shim = shebang + process.execPath + '\n' + fs.readFileSync(path.join(__dirname, 'shim.js')) -const isWindows = require('./lib/is-windows')() +const pathRe = IS_WINDOWS ? /^PATH=/i : /^PATH=/ -const pathRe = isWindows ? /^PATH=/i : /^PATH=/ - -const colon = isWindows ? ';' : ':' +const colon = IS_WINDOWS ? ';' : ':' function wrap (argv, env, workingDir) { const spawnSyncBinding = process.binding('spawn_sync') @@ -108,7 +107,7 @@ function mungeSh (workingDir, options) { options.originalNode = command c = match[1] + match[2] + ' "' + workingDir + '/node" ' + match[3] options.args[cmdi + 1] = c - } else if (exe === 'npm' && !isWindows) { + } else if (exe === 'npm' && !IS_WINDOWS) { // XXX this will exhibit weird behavior when using /path/to/npm, // if some other npm is first in the path. const npmPath = whichOrUndefined('npm') @@ -123,7 +122,7 @@ function mungeSh (workingDir, options) { function isCmd (file) { const comspec = path.basename(process.env.comspec || '').replace(/\.exe$/i, '') - return isWindows && (file === comspec || /^cmd(?:\.exe)?$/i.test(file)) + return IS_WINDOWS && (file === comspec || /^cmd(?:\.exe)?$/i.test(file)) } function mungeCmd(workingDir, options) { @@ -268,7 +267,7 @@ function mungeEnv (workingDir, options) { } } if (pathEnv === undefined) { - options.envPairs.push((isWindows ? 'Path=' : 'PATH=') + workingDir) + options.envPairs.push((IS_WINDOWS ? 'Path=' : 'PATH=') + workingDir) } if (options.originalNode) { const key = path.basename(workingDir).substr('.node-spawn-wrap-'.length) @@ -285,7 +284,7 @@ function mungeEnv (workingDir, options) { function isnpm (file) { // XXX is this even possible/necessary? // wouldn't npm just be detected as a node shebang? - return file === 'npm' && !isWindows + return file === 'npm' && !IS_WINDOWS } function mungenpm (workingDir, options) { @@ -389,6 +388,7 @@ function setup (argv, env) { foregroundChild: require.resolve('foreground-child'), signalExit: require.resolve('signal-exit'), }, + isWindows: IS_WINDOWS, key: key, workingDir: workingDir, argv: argv, @@ -405,7 +405,7 @@ function setup (argv, env) { mkdirp.sync(workingDir) workingDir = fs.realpathSync(workingDir) - if (isWindows) { + if (IS_WINDOWS) { const cmdShim = '@echo off\r\n' + 'SETLOCAL\r\n' + diff --git a/lib/is-windows.js b/lib/is-windows.js deleted file mode 100644 index 278abb3..0000000 --- a/lib/is-windows.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = function () { - return process.platform === 'win32' || - process.env.OSTYPE === 'cygwin' || - process.env.OSTYPE === 'msys' -} diff --git a/package-lock.json b/package-lock.json index da1a683..281358c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1558,6 +1558,11 @@ "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", "dev": true }, + "is-windows": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", + "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==" + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", diff --git a/package.json b/package.json index f000b68..dce175a 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "foreground-child": "^1.5.6", + "is-windows": "^1.0.2", "mkdirp": "^0.5.0", "os-homedir": "^1.0.1", "rimraf": "^2.6.2", diff --git a/shim.js b/shim.js index afe5d75..7135ccf 100644 --- a/shim.js +++ b/shim.js @@ -45,6 +45,7 @@ const settings = require('./settings.json') const foregroundChild = require(settings.deps.foregroundChild) + const IS_WINDOWS = settings.isWindows const argv = settings.argv const nargs = argv.length const env = settings.env @@ -148,14 +149,7 @@ // Unwrap the PATH environment var so that we're not mucking // with the environment. It'll get re-added if they spawn anything - const isWindows = ( - // TODO: Use `lib/is-windows` - process.platform === 'win32' || - process.env.OSTYPE === 'cygwin' || - process.env.OSTYPE === 'msys' - ) - - if (isWindows) { + if (IS_WINDOWS) { for (const i in process.env) { if (/^path$/i.test(i)) { process.env[i] = process.env[i].replace(__dirname + ';', '') diff --git a/test/basic.js b/test/basic.js index 42508a7..c2e97d7 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,7 +1,7 @@ var sw = require('../') -var isWindows = require('../lib/is-windows.js')() -var winNoShebang = isWindows && 'no shebang execution on windows' -var winNoSig = isWindows && 'no signals get through cmd' +var IS_WINDOWS = require('is-windows')() +var winNoShebang = IS_WINDOWS && 'no shebang execution on windows' +var winNoSig = IS_WINDOWS && 'no signals get through cmd' var onExit = require('signal-exit') var cp = require('child_process') @@ -196,7 +196,7 @@ t.test('exec execPath', function (t) { t.plan(4) t.test('basic', function (t) { - var opt = isWindows ? null : { shell: '/bin/bash' } + var opt = IS_WINDOWS ? null : { shell: '/bin/bash' } var child = cp.exec('"' + process.execPath + '" "' + fixture + '" xyz', opt) var out = '' @@ -212,7 +212,7 @@ t.test('exec execPath', function (t) { }) t.test('execPath wrapped with quotes', function (t) { - var opt = isWindows ? null : { shell: '/bin/bash' } + var opt = IS_WINDOWS ? null : { shell: '/bin/bash' } var child = cp.exec(JSON.stringify(process.execPath) + ' ' + fixture + ' xyz', opt)