|
| 1 | +var fs = require('graceful-fs') |
| 2 | +var child_process = require('child_process') |
| 3 | +var exec = child_process.exec |
| 4 | + |
| 5 | +if (!String.prototype.startsWith) { |
| 6 | + String.prototype.startsWith = function(search, pos) { |
| 7 | + return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; |
| 8 | + }; |
| 9 | +} |
| 10 | + |
| 11 | +function processExecSync(file, args, options) { |
| 12 | + var child, error, timeout, tmpdir, command, quote; |
| 13 | + command = makeCommand(file, args); |
| 14 | + |
| 15 | + /* |
| 16 | + this function emulates child_process.execSync for legacy node <= 0.10.x |
| 17 | + derived from https://github.com/gvarsanyi/sync-exec/blob/master/js/sync-exec.js |
| 18 | + */ |
| 19 | + |
| 20 | + options = options || {}; |
| 21 | + // init timeout |
| 22 | + timeout = Date.now() + options.timeout; |
| 23 | + // init tmpdir |
| 24 | + var os_temp_base = "/tmp"; |
| 25 | + var os = determine_os(); |
| 26 | + os_temp_base = "/tmp" |
| 27 | + |
| 28 | + if(process.env.TMP){ |
| 29 | + os_temp_base = process.env.TMP; |
| 30 | + } |
| 31 | + |
| 32 | + if(os_temp_base[os_temp_base.length - 1] !== "/"){ |
| 33 | + os_temp_base += "/"; |
| 34 | + } |
| 35 | + |
| 36 | + tmpdir = os_temp_base+'processExecSync.' + Date.now() + Math.random(); |
| 37 | + fs.mkdirSync(tmpdir); |
| 38 | + |
| 39 | + // init command |
| 40 | + if(os === "linux"){ |
| 41 | + command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir + |
| 42 | + '/stderr); echo $? > ' + tmpdir + '/status'; |
| 43 | + }else{ |
| 44 | + command = '(' + command + ' > ' + tmpdir + '/stdout 2> ' + tmpdir + |
| 45 | + '/stderr) | echo %errorlevel% > ' + tmpdir + '/status | exit'; |
| 46 | + } |
| 47 | + |
| 48 | + // init child |
| 49 | + child = exec(command, options, function () { |
| 50 | + return; |
| 51 | + }); |
| 52 | + |
| 53 | + var maxTry = 100000; // increases the test time by 6 seconds on win-2016-node-0.10 |
| 54 | + var tryCount = 0; |
| 55 | + while (tryCount < maxTry) { |
| 56 | + try { |
| 57 | + var x = fs.readFileSync(tmpdir + '/status'); |
| 58 | + if(x.toString() === "0"){ |
| 59 | + break; |
| 60 | + } |
| 61 | + } catch (ignore) { |
| 62 | + } |
| 63 | + tryCount++; |
| 64 | + if (Date.now() > timeout) { |
| 65 | + error = child; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + ['stdout', 'stderr', 'status'].forEach(function (file) { |
| 71 | + child[file] = fs.readFileSync(tmpdir + '/' + file, options.encoding); |
| 72 | + setTimeout(unlinkFile, 500, tmpdir + '/' + file); |
| 73 | + }); |
| 74 | + |
| 75 | + child.status = Number(child.status); |
| 76 | + if (child.status !== 0) { |
| 77 | + error = child; |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + fs.rmdirSync(tmpdir); |
| 82 | + } catch (ignore) { |
| 83 | + } |
| 84 | + if (error) { |
| 85 | + throw error; |
| 86 | + } |
| 87 | + return child.stdout; |
| 88 | +} |
| 89 | + |
| 90 | +module.exports = processExecSync; |
| 91 | + |
| 92 | +function makeCommand(file, args){ |
| 93 | + var command, quote; |
| 94 | + command = file |
| 95 | + if(args.length > 0){ |
| 96 | + for(var i in args){ |
| 97 | + command = command + " "; |
| 98 | + if(args[i][0] === "-"){ |
| 99 | + command = command + args[i]; |
| 100 | + }else{ |
| 101 | + if(!quote){ |
| 102 | + command = command + "\""; |
| 103 | + quote = true; |
| 104 | + } |
| 105 | + command = command + args[i]; |
| 106 | + if(quote){ |
| 107 | + if(args.length === (parseInt(i) + 1)){ |
| 108 | + command = command + "\""; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return command; |
| 115 | +} |
| 116 | + |
| 117 | +function determine_os(){ |
| 118 | + var os = ""; |
| 119 | + var tmpVar = ""; |
| 120 | + if(process.env.OSTYPE){ |
| 121 | + tmpVar = process.env.OSTYPE; |
| 122 | + }else if(process.env.OS){ |
| 123 | + tmpVar = process.env.OS; |
| 124 | + }else{ |
| 125 | + //default is linux |
| 126 | + tmpVar = "linux"; |
| 127 | + } |
| 128 | + |
| 129 | + if(tmpVar.startsWith("linux")){ |
| 130 | + os = "linux" |
| 131 | + } |
| 132 | + if(tmpVar.startsWith("win")){ |
| 133 | + os = "win" |
| 134 | + } |
| 135 | + |
| 136 | + return os; |
| 137 | +} |
| 138 | + |
| 139 | +function unlinkFile(file){ |
| 140 | + fs.unlinkSync(file); |
| 141 | +} |
0 commit comments