|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { spawn } = require('child_process') |
| 4 | +const os = require('os') |
| 5 | +const which = require('which') |
| 6 | + |
| 7 | +const escape = require('./escape.js') |
| 8 | + |
| 9 | +// 'extra' object is for decorating the error a bit more |
| 10 | +const promiseSpawn = (cmd, args, opts = {}, extra = {}) => { |
| 11 | + if (opts.shell) { |
| 12 | + return spawnWithShell(cmd, args, opts, extra) |
| 13 | + } |
| 14 | + |
| 15 | + let resolve, reject |
| 16 | + const promise = new Promise((_resolve, _reject) => { |
| 17 | + resolve = _resolve |
| 18 | + reject = _reject |
| 19 | + }) |
| 20 | + |
| 21 | + // Create error here so we have a more useful stack trace when rejecting |
| 22 | + const closeError = new Error('command failed') |
| 23 | + |
| 24 | + const stdout = [] |
| 25 | + const stderr = [] |
| 26 | + |
| 27 | + const getResult = (result) => ({ |
| 28 | + cmd, |
| 29 | + args, |
| 30 | + ...result, |
| 31 | + ...stdioResult(stdout, stderr, opts), |
| 32 | + ...extra, |
| 33 | + }) |
| 34 | + const rejectWithOpts = (er, erOpts) => { |
| 35 | + const resultError = getResult(erOpts) |
| 36 | + reject(Object.assign(er, resultError)) |
| 37 | + } |
| 38 | + |
| 39 | + const proc = spawn(cmd, args, opts) |
| 40 | + promise.stdin = proc.stdin |
| 41 | + promise.process = proc |
| 42 | + |
| 43 | + proc.on('error', rejectWithOpts) |
| 44 | + |
| 45 | + if (proc.stdout) { |
| 46 | + proc.stdout.on('data', c => stdout.push(c)) |
| 47 | + proc.stdout.on('error', rejectWithOpts) |
| 48 | + } |
| 49 | + |
| 50 | + if (proc.stderr) { |
| 51 | + proc.stderr.on('data', c => stderr.push(c)) |
| 52 | + proc.stderr.on('error', rejectWithOpts) |
| 53 | + } |
| 54 | + |
| 55 | + proc.on('close', (code, signal) => { |
| 56 | + if (code || signal) { |
| 57 | + rejectWithOpts(closeError, { code, signal }) |
| 58 | + } else { |
| 59 | + resolve(getResult({ code, signal })) |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + return promise |
| 64 | +} |
| 65 | + |
| 66 | +const spawnWithShell = (cmd, args, opts, extra) => { |
| 67 | + let command = opts.shell |
| 68 | + // if shell is set to true, we use a platform default. we can't let the core |
| 69 | + // spawn method decide this for us because we need to know what shell is in use |
| 70 | + // ahead of time so that we can escape arguments properly. we don't need coverage here. |
| 71 | + if (command === true) { |
| 72 | + // istanbul ignore next |
| 73 | + command = process.platform === 'win32' ? (process.env.ComSpec || 'cmd.exe') : 'sh' |
| 74 | + } |
| 75 | + |
| 76 | + const options = { ...opts, shell: false } |
| 77 | + const realArgs = [] |
| 78 | + let script = cmd |
| 79 | + |
| 80 | + // first, determine if we're in windows because if we are we need to know if we're |
| 81 | + // running an .exe or a .cmd/.bat since the latter requires extra escaping |
| 82 | + const isCmd = /(?:^|\\)cmd(?:\.exe)?$/i.test(command) |
| 83 | + if (isCmd) { |
| 84 | + let doubleEscape = false |
| 85 | + |
| 86 | + // find the actual command we're running |
| 87 | + let initialCmd = '' |
| 88 | + let insideQuotes = false |
| 89 | + for (let i = 0; i < cmd.length; ++i) { |
| 90 | + const char = cmd.charAt(i) |
| 91 | + if (char === ' ' && !insideQuotes) { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + initialCmd += char |
| 96 | + if (char === '"' || char === "'") { |
| 97 | + insideQuotes = !insideQuotes |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + let pathToInitial |
| 102 | + try { |
| 103 | + pathToInitial = which.sync(initialCmd, { |
| 104 | + path: (options.env && findInObject(options.env, 'PATH')) || process.env.PATH, |
| 105 | + pathext: (options.env && findInObject(options.env, 'PATHEXT')) || process.env.PATHEXT, |
| 106 | + }).toLowerCase() |
| 107 | + } catch (err) { |
| 108 | + pathToInitial = initialCmd.toLowerCase() |
| 109 | + } |
| 110 | + |
| 111 | + doubleEscape = pathToInitial.endsWith('.cmd') || pathToInitial.endsWith('.bat') |
| 112 | + for (const arg of args) { |
| 113 | + script += ` ${escape.cmd(arg, doubleEscape)}` |
| 114 | + } |
| 115 | + realArgs.push('/d', '/s', '/c', script) |
| 116 | + options.windowsVerbatimArguments = true |
| 117 | + } else { |
| 118 | + for (const arg of args) { |
| 119 | + script += ` ${escape.sh(arg)}` |
| 120 | + } |
| 121 | + realArgs.push('-c', script) |
| 122 | + } |
| 123 | + |
| 124 | + return promiseSpawn(command, realArgs, options, extra) |
| 125 | +} |
| 126 | + |
| 127 | +// open a file with the default application as defined by the user's OS |
| 128 | +const open = (_args, opts = {}, extra = {}) => { |
| 129 | + const options = { ...opts, shell: true } |
| 130 | + const args = [].concat(_args) |
| 131 | + |
| 132 | + let platform = process.platform |
| 133 | + // process.platform === 'linux' may actually indicate WSL, if that's the case |
| 134 | + // open the argument with sensible-browser which is pre-installed |
| 135 | + // In WSL, set the default browser using, for example, |
| 136 | + // export BROWSER="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" |
| 137 | + // or |
| 138 | + // export BROWSER="/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" |
| 139 | + // To permanently set the default browser, add the appropriate entry to your shell's |
| 140 | + // RC file, e.g. .bashrc or .zshrc. |
| 141 | + if (platform === 'linux' && os.release().toLowerCase().includes('microsoft')) { |
| 142 | + platform = 'wsl' |
| 143 | + if (!process.env.BROWSER) { |
| 144 | + return Promise.reject( |
| 145 | + new Error('Set the BROWSER environment variable to your desired browser.')) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + let command = options.command |
| 150 | + if (!command) { |
| 151 | + if (platform === 'win32') { |
| 152 | + // spawnWithShell does not do the additional os.release() check, so we |
| 153 | + // have to force the shell here to make sure we treat WSL as windows. |
| 154 | + options.shell = process.env.ComSpec |
| 155 | + // also, the start command accepts a title so to make sure that we don't |
| 156 | + // accidentally interpret the first arg as the title, we stick an empty |
| 157 | + // string immediately after the start command |
| 158 | + command = 'start ""' |
| 159 | + } else if (platform === 'wsl') { |
| 160 | + command = 'sensible-browser' |
| 161 | + } else if (platform === 'darwin') { |
| 162 | + command = 'open' |
| 163 | + } else { |
| 164 | + command = 'xdg-open' |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return spawnWithShell(command, args, options, extra) |
| 169 | +} |
| 170 | +promiseSpawn.open = open |
| 171 | + |
| 172 | +const isPipe = (stdio = 'pipe', fd) => { |
| 173 | + if (stdio === 'pipe' || stdio === null) { |
| 174 | + return true |
| 175 | + } |
| 176 | + |
| 177 | + if (Array.isArray(stdio)) { |
| 178 | + return isPipe(stdio[fd], fd) |
| 179 | + } |
| 180 | + |
| 181 | + return false |
| 182 | +} |
| 183 | + |
| 184 | +const stdioResult = (stdout, stderr, { stdioString = true, stdio }) => { |
| 185 | + const result = { |
| 186 | + stdout: null, |
| 187 | + stderr: null, |
| 188 | + } |
| 189 | + |
| 190 | + // stdio is [stdin, stdout, stderr] |
| 191 | + if (isPipe(stdio, 1)) { |
| 192 | + result.stdout = Buffer.concat(stdout) |
| 193 | + if (stdioString) { |
| 194 | + result.stdout = result.stdout.toString().trim() |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (isPipe(stdio, 2)) { |
| 199 | + result.stderr = Buffer.concat(stderr) |
| 200 | + if (stdioString) { |
| 201 | + result.stderr = result.stderr.toString().trim() |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return result |
| 206 | +} |
| 207 | + |
| 208 | +// case insensitive lookup in an object |
| 209 | +const findInObject = (obj, key) => { |
| 210 | + key = key.toLowerCase() |
| 211 | + for (const objKey of Object.keys(obj).sort()) { |
| 212 | + if (objKey.toLowerCase() === key) { |
| 213 | + return obj[objKey] |
| 214 | + } |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +module.exports = promiseSpawn |
0 commit comments