diff --git a/index.js b/index.js index b922e2b..9d88a3f 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 isWindows = require('is-windows') const mkdirp = require('mkdirp') const rimraf = require('rimraf') const path = require('path') @@ -34,11 +35,11 @@ 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 IS_WINDOWS = isWindows() -const pathRe = isWindows ? /^PATH=/i : /^PATH=/ +const pathRe = IS_WINDOWS ? /^PATH=/i : /^PATH=/ -const colon = isWindows ? ';' : ':' +const colon = IS_WINDOWS ? ';' : ':' function wrap (argv, env, workingDir) { const spawnSyncBinding = process.binding('spawn_sync') @@ -108,7 +109,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 +124,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 +269,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 +286,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) { @@ -387,6 +388,7 @@ function setup (argv, env) { module: __filename, deps: { foregroundChild: require.resolve('foreground-child'), + isWindows: require.resolve('is-windows'), signalExit: require.resolve('signal-exit'), }, key: key, @@ -405,7 +407,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..fafb2ad 100644 --- a/shim.js +++ b/shim.js @@ -45,6 +45,7 @@ const settings = require('./settings.json') const foregroundChild = require(settings.deps.foregroundChild) + const isWindows = require(settings.deps.isWindows) const argv = settings.argv const nargs = argv.length const env = settings.env @@ -148,14 +149,9 @@ // 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) { + const IS_WINDOWS = 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)