|
| 1 | +const gitNodeConnector = global.createNodeConnector("phcode-git-core", exports); |
| 2 | + |
| 3 | +const GIT_PROGRESS_EVENT = "git_progress"; |
| 4 | + |
| 5 | +let ChildProcess = require("child_process"), |
| 6 | + crossSpawn = require('cross-spawn'), |
| 7 | + ProcessUtils = require("./processUtils"), |
| 8 | + processMap = {}, |
| 9 | + resolvedPaths = {}; |
| 10 | + |
| 11 | +function fixEOL(str) { |
| 12 | + if (str[str.length - 1] === "\n") { |
| 13 | + str = str.slice(0, -1); |
| 14 | + } |
| 15 | + return str; |
| 16 | +} |
| 17 | + |
| 18 | +// handler with ChildProcess.exec |
| 19 | +// this won't handle cases where process outputs a large string |
| 20 | +function execute(directory, command, args, opts, callback) { |
| 21 | + // execute commands have to be escaped, spawn does this automatically and will fail if cmd is escaped |
| 22 | + if (command[0] !== "\"" || command[command.length - 1] !== "\"") { |
| 23 | + command = "\"" + command + "\""; |
| 24 | + } |
| 25 | + // http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback |
| 26 | + const toExec = command + " " + args.join(" "); |
| 27 | + processMap[opts.cliId] = ChildProcess.exec(toExec, { |
| 28 | + cwd: directory, |
| 29 | + maxBuffer: 20 * 1024 * 1024 |
| 30 | + }, function (err, stdout, stderr) { |
| 31 | + delete processMap[opts.cliId]; |
| 32 | + callback(err ? fixEOL(stderr) : undefined, err ? undefined : fixEOL(stdout)); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +// handler with cross-spawn |
| 37 | +function join(arr) { |
| 38 | + let result, index = 0, length; |
| 39 | + length = arr.reduce(function (l, b) { |
| 40 | + return l + b.length; |
| 41 | + }, 0); |
| 42 | + result = new Buffer(length); |
| 43 | + arr.forEach(function (b) { |
| 44 | + b.copy(result, index); |
| 45 | + index += b.length; |
| 46 | + }); |
| 47 | + return fixEOL(result.toString("utf8")); |
| 48 | +} |
| 49 | + |
| 50 | +function spawn(directory, command, args, opts, callback) { |
| 51 | + // https://github.com/creationix/node-git |
| 52 | + const child = crossSpawn(command, args, { |
| 53 | + cwd: directory |
| 54 | + }); |
| 55 | + child.on("error", function (err) { |
| 56 | + callback(err.stack, undefined); |
| 57 | + }); |
| 58 | + |
| 59 | + processMap[opts.cliId] = child; |
| 60 | + |
| 61 | + let exitCode, stdout = [], stderr = []; |
| 62 | + child.stdout.addListener("data", function (text) { |
| 63 | + stdout[stdout.length] = text; |
| 64 | + }); |
| 65 | + child.stderr.addListener("data", function (text) { |
| 66 | + // Git writes its informational messages (such as Successfully rebased |
| 67 | + // and updated) to stderr. This behavior is intentional because Git uses |
| 68 | + // stderr for all output that is not a direct result of the command (e.g., |
| 69 | + // status updates, progress, or errors). |
| 70 | + if (opts.watchProgress) { |
| 71 | + gitNodeConnector.triggerPeer(GIT_PROGRESS_EVENT, { |
| 72 | + cliId: opts.cliId, |
| 73 | + time: (new Date()).getTime(), |
| 74 | + data: fixEOL(text.toString("utf8")) |
| 75 | + }); |
| 76 | + } |
| 77 | + stderr[stderr.length] = text; |
| 78 | + }); |
| 79 | + child.addListener("exit", function (code) { |
| 80 | + exitCode = code; |
| 81 | + }); |
| 82 | + child.addListener("close", function () { |
| 83 | + delete processMap[opts.cliId]; |
| 84 | + callback(exitCode > 0 ? join(stderr) : undefined, |
| 85 | + exitCode > 0 ? undefined : join(stdout)); |
| 86 | + }); |
| 87 | + child.stdin.end(); |
| 88 | +} |
| 89 | + |
| 90 | +function doIfExists(method, directory, command, args, opts, callback) { |
| 91 | + // do not call executableExists if we already know it exists |
| 92 | + if (resolvedPaths[command]) { |
| 93 | + return method(directory, resolvedPaths[command], args, opts, callback); |
| 94 | + } |
| 95 | + |
| 96 | + ProcessUtils.executableExists(command, function (err, exists, resolvedPath) { |
| 97 | + if (exists) { |
| 98 | + resolvedPaths[command] = resolvedPath; |
| 99 | + return method(directory, resolvedPath, args, opts, callback); |
| 100 | + } else { |
| 101 | + callback("ProcessUtils can't resolve the path requested: " + command); |
| 102 | + } |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +function executeIfExists({directory, command, args, opts}) { |
| 107 | + return new Promise(function (resolve, reject) { |
| 108 | + doIfExists(execute, directory, command, args, opts, (err, stdout)=>{ |
| 109 | + if(err){ |
| 110 | + reject(err); |
| 111 | + } else { |
| 112 | + resolve(stdout); |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +function spawnIfExists({directory, command, args, opts}) { |
| 119 | + return new Promise(function (resolve, reject) { |
| 120 | + doIfExists(spawn, directory, command, args, opts, (err, stdout)=>{ |
| 121 | + if(err){ |
| 122 | + reject(err); |
| 123 | + } else { |
| 124 | + resolve(stdout); |
| 125 | + } |
| 126 | + }); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +function kill(cliId) { |
| 131 | + return new Promise(function (resolve, reject) { |
| 132 | + const process = processMap[cliId]; |
| 133 | + if (!process) { |
| 134 | + reject("Couldn't find process to kill with ID:" + cliId); |
| 135 | + } |
| 136 | + delete processMap[cliId]; |
| 137 | + resolve(""); // at this point we resolve anyways as we cant do anything after deleting the object |
| 138 | + ProcessUtils.getChildrenOfPid(process.pid, function (err, children) { |
| 139 | + // kill also parent process |
| 140 | + children.push(process.pid); |
| 141 | + children.forEach(function (pid) { |
| 142 | + ProcessUtils.killSingleProcess(pid); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +} |
| 147 | + |
| 148 | +function which({command}) { |
| 149 | + return new Promise(function (resolve, reject) { |
| 150 | + ProcessUtils.executableExists(command, function (err, exists, resolvedPath) { |
| 151 | + if (exists) { |
| 152 | + resolve(resolvedPath); |
| 153 | + } else { |
| 154 | + reject("ProcessUtils can't resolve the path requested: " + command); |
| 155 | + } |
| 156 | + }); |
| 157 | + }); |
| 158 | +} |
| 159 | + |
| 160 | +exports.execute = executeIfExists; |
| 161 | +exports.spawn = spawnIfExists; |
| 162 | +exports.kill = kill; |
| 163 | +exports.which = which; |
0 commit comments