Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
use sh as SHELL for Cygwin/Git-Bash
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed May 16, 2018
1 parent d030653 commit 74c5bfa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {

if (customShell) {
sh = customShell
} else if (process.platform === 'win32') {
} else if (opts.isWindowsShell == null ? process.platform === 'win32' : opts.isWindowsShell) {
sh = process.env.comspec || 'cmd'
shFlag = '/d /s /c'
conf.windowsVerbatimArguments = true
Expand Down
73 changes: 73 additions & 0 deletions lib/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

module.exports = spawn

const os = require('os')
const fs = require('fs')
const path = require('path')
const _spawn = require('child_process').spawn
const EventEmitter = require('events').EventEmitter
const _spawnSync = require('child_process').spawnSync

let progressEnabled
let running = 0
Expand All @@ -29,7 +33,76 @@ function willCmdOutput (stdio) {
return false
}

function getGitDirByRegstry (arch) {
const args = [
'QUERY',
'HKLM\\SOFTWARE\\GitForWindows',
'/v',
'InstallPath'
]

if (arch) {
args.push('/reg:' + arch)
}

const stdout = _spawnSync('reg.exe', args).stdout

if (stdout && /^\s*InstallPath\s+REG(?:_[A-Z]+)+\s+(.+?)$/im.test(stdout.toString())) {
return RegExp.$1
} else if (arch === 64) {
return getGitDirByRegstry(32)
}
}

function getGitPath (cmd) {
let gitInstRoot
if ('GIT_INSTALL_ROOT' in process.env) {
gitInstRoot = process.env.GIT_INSTALL_ROOT
} else {
const osArch = /64$/.test(process.env.PROCESSOR_ARCHITEW6432 || process.arch) ? 64 : 32
gitInstRoot = getGitDirByRegstry(osArch)
process.env.GIT_INSTALL_ROOT = gitInstRoot
if (gitInstRoot && !process.env.MSYSTEM) {
let binDir = [
'mingw64/bin',
'usr/local/bin',
'usr/bin',
'bin',
'usr/bin/vendor_perl',
'usr/bin/core_perl'
].map(function (dir) {
return path.join(gitInstRoot, dir)
})

if (!fs.existsSync(binDir[0])) {
binDir[0] = path.join(gitInstRoot, 'mingw32/bin')
}

binDir.unshift(path.join(os.homedir(), 'bin'))

const rawPath = process.env.PATH.split(path.delimiter)

binDir = binDir.filter(function (dir) {
return rawPath.indexOf(dir) < 0
})

binDir.push(process.env.PATH)

process.env.PATH = binDir.join(path.delimiter)
}
}

if (gitInstRoot) {
cmd = path.join(gitInstRoot, cmd)
}
return cmd
}

function spawn (cmd, args, options, log) {
if (process.platform === 'win32' && cmd[0] === '/') {
cmd = getGitPath(cmd)
}

const cmdWillOutput = willCmdOutput(options && options.stdio)

if (cmdWillOutput) startRunning(log)
Expand Down

0 comments on commit 74c5bfa

Please sign in to comment.