opusdev/vector-similarity-api
1
1const path = require('path');2const utils = require('./utils');3const merge = utils.merge;4const bus = utils.bus;5const spawn = require('child_process').spawn;6 7module.exports = function spawnCommand(command, config, eventArgs) {8 var stdio = ['pipe', 'pipe', 'pipe'];9 10 if (config.options.stdout) {11 stdio = ['pipe', process.stdout, process.stderr];12 }13 14 const env = merge(process.env, { FILENAME: eventArgs[0] });15 16 var sh = 'sh';17 var shFlag = '-c';18 var spawnOptions = {19 env: merge(config.options.execOptions.env, env),20 stdio: stdio,21 };22 23 if (!Array.isArray(command)) {24 command = [command];25 }26 27 if (utils.isWindows) {28 // if the exec includes a forward slash, reverse it for windows compat29 // but *only* apply to the first command, and none of the arguments.30 // ref #1251 and #123631 command = command.map(executable => {32 if (executable.indexOf('/') === -1) {33 return executable;34 }35 36 return executable.split(' ').map((e, i) => {37 if (i === 0) {38 return path.normalize(e);39 }40 return e;41 }).join(' ');42 });43 // taken from npm's cli: https://git.io/vNFD444 sh = process.env.comspec || 'cmd';45 shFlag = '/d /s /c';46 spawnOptions.windowsVerbatimArguments = true;47 spawnOptions.windowsHide = true;48 }49 50 const args = command.join(' ');51 const child = spawn(sh, [shFlag, args], spawnOptions);52 53 if (config.required) {54 var emit = {55 stdout: function (data) {56 bus.emit('stdout', data);57 },58 stderr: function (data) {59 bus.emit('stderr', data);60 },61 };62 63 // now work out what to bind to...64 if (config.options.stdout) {65 child.on('stdout', emit.stdout).on('stderr', emit.stderr);66 } else {67 child.stdout.on('data', emit.stdout);68 child.stderr.on('data', emit.stderr);69 70 bus.stdout = child.stdout;71 bus.stderr = child.stderr;72 }73 }74};75 