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