forked from brackets-userland/brackets-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
310 lines (286 loc) · 9 KB
/
Copy pathcli.ts
File metadata and controls
310 lines (286 loc) · 9 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* eslint-env node */
/* eslint no-console:0 */
import * as fs from "fs";
import * as ChildProcess from "child_process";
import * as ProcessUtils from "./process-utils";
const domainName = "brackets-git";
const processMap: { [id: number]: ChildProcess.ChildProcess } = {};
const resolvedPaths: { [path: string]: string } = {};
const fixEOL = (str: string) => str[str.length - 1] === "\n" ? str.slice(0, -1) : str;
const fixCommandForExec = (command: string) => {
// execute commands have to be escaped, spawn does this automatically and will fail if cmd is escaped
return command[0] !== "\"" || command[command.length - 1] !== "\"" ? "\"" + command + "\"" : command;
};
/* eslint-disable */
export interface DomainManager {
emitEvent: Function;
hasDomain: Function;
registerDomain: Function;
registerCommand: Function;
registerEvent: Function;
}
/* eslint-enable */
let domainManager: DomainManager | null;
// handler with ChildProcess.exec
// this won't handle cases where process outputs a large string
function execute(
directory: string,
command: string,
args: string[],
opts: { cliId: number },
callback: (stderr: string | null, stdout: string | null) => void
) {
// http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
const toExec = fixCommandForExec(command) + " " + args.join(" ");
const child = ChildProcess.exec(toExec, {
cwd: directory,
maxBuffer: 20 * 1024 * 1024
}, (err, stdout, stderr) => {
delete processMap[opts.cliId];
callback(err ? fixEOL(stderr) : null, err ? null : fixEOL(stdout));
});
processMap[opts.cliId] = child;
}
// handler with ChildProcess.spawn
function join(arr: Buffer[]) {
let index = 0;
const length = arr.reduce((l, b) => l + b.length, 0);
const result = new Buffer(length);
arr.forEach((b) => {
b.copy(result, index);
index += b.length;
});
return fixEOL(result.toString("utf8"));
}
function spawn(
directory: string,
command: string,
args: string[],
opts: { cliId: number, watchProgress: boolean },
callback: (stderr: string | null, stdout: string | null) => void
) {
// https://github.com/creationix/node-git
const child = ChildProcess.spawn(command, args, {
cwd: directory
});
child.on("error", (err: NodeJS.ErrnoException) => {
callback(err.stack || err.toString(), null);
});
processMap[opts.cliId] = child;
let exitCode: number;
const stdout: Buffer[] = [];
const stderr: Buffer[] = [];
child.stdout.addListener("data", (text: Buffer) => {
stdout[stdout.length] = text;
});
child.stderr.addListener("data", (text: Buffer) => {
if (opts.watchProgress && domainManager) {
domainManager.emitEvent(domainName, "progress", [
opts.cliId,
(new Date()).getTime(),
fixEOL(text.toString("utf8"))
]);
}
stderr[stderr.length] = text;
});
child.addListener("exit", (code: number) => {
exitCode = code;
});
child.addListener("close", () => {
delete processMap[opts.cliId];
callback(exitCode > 0 ? join(stderr) : null,
exitCode > 0 ? null : join(stdout));
});
child.stdin.end();
}
function doIfExists( // eslint-disable-line max-params
method: Function,
directory: string,
command: string,
args: string[],
opts: {},
callback: (stderr: string | null, stdout: string | null) => void
) {
// do not call executableExists if we already know it exists
if (resolvedPaths[command]) {
return method(directory, resolvedPaths[command], args, opts, callback);
}
ProcessUtils.executableExists(command, (err, exists, resolvedPath) => {
if (err || !exists || !resolvedPath) {
return callback("ProcessUtils can't resolve the path requested: " + command, null);
}
resolvedPaths[command] = resolvedPath;
return method(directory, resolvedPath, args, opts, callback);
});
}
function executeIfExists(
directory: string,
command: string,
args: string[],
opts: {},
callback: (stderr: string | null, stdout: string | null) => void
) {
return doIfExists(execute, directory, command, args, opts, callback);
}
function spawnIfExists(
directory: string,
command: string,
args: string[],
opts: {},
callback: (stderr: string | null, stdout: string | null) => void
) {
return doIfExists(spawn, directory, command, args, opts, callback);
}
function kill(
cliId: number,
callback: (stderr: string | null, success: boolean) => void
) {
const process = processMap[cliId];
if (!process) {
return callback("Couldn't find process to kill with ID:" + cliId, false);
}
delete processMap[cliId];
ProcessUtils.getChildrenOfPid(process.pid, (err, children) => {
if (err) {
return callback(err, false);
}
// kill also parent process
children.push(process.pid);
children.forEach((pid) => {
ProcessUtils.killSingleProcess(pid, (stderr, stdout) => {
if (stderr) {
console.warn(`killSingleProcess -> ${stderr}`);
}
});
});
callback(null, true);
});
}
function which(
directory: string,
filePath: string,
args: string[],
opts: {},
callback: (stderr: string | null, stdout: string | null) => void
) {
ProcessUtils.executableExists(filePath, (err, exists, resolvedPath) => {
if (err || !exists) {
return callback("ProcessUtils can't resolve the path requested: " + filePath, null);
}
callback(null, resolvedPath);
});
}
function pathExists(
directory: string,
path: string,
args: string[],
opts: {},
callback: (stderr: string | null, exists: boolean) => void
) {
fs.exists(path, (exists) => {
callback(null, exists);
});
}
/*
* Initializes the domain.
* @param {DomainManager} DomainManager for the server
*/
export function init(_domainManager: DomainManager) {
domainManager = _domainManager;
if (!domainManager.hasDomain(domainName)) {
domainManager.registerDomain(domainName, {
major: 0,
minor: 1
});
} else {
throw new Error(domainName +
" domain already registered. Close all Brackets instances and start again. " +
"This should only happen when updating the extension.");
}
domainManager.registerCommand(
domainName,
"execute", // command name
executeIfExists, // command handler function
true, // this command is async
"Runs a command in a shell and buffers the output.",
[
{ name: "directory", type: "string" },
{ name: "command", type: "string" },
{ name: "args", type: "array" },
{ name: "opts", type: "object" }
],
[
{ name: "stdout", type: "string" }
]
);
domainManager.registerCommand(
domainName,
"spawn", // command name
spawnIfExists, // command handler function
true, // this command is async
"Launches a new process with the given command.",
[
{ name: "directory", type: "string" },
{ name: "command", type: "string" },
{ name: "args", type: "array" },
{ name: "opts", type: "object" }
],
[
{ name: "stdout", type: "string" }
]
);
domainManager.registerCommand(
domainName,
"kill", // command name
kill, // command handler function
true, // this command is async
"Launches a new process with the given command.",
[
{ name: "commandId", type: "number" }
],
[
{ name: "stdout", type: "string" }
]
);
domainManager.registerCommand(
domainName,
"which",
which,
true,
"Looks for a given file using which.",
[
{ name: "directory", type: "string" },
{ name: "filePath", type: "string" },
{ name: "args", type: "array" },
{ name: "opts", type: "object" }
],
[
{ name: "path", type: "string" }
]
);
domainManager.registerCommand(
domainName,
"pathExists",
pathExists,
true,
"Looks if given path exists on the file system",
[
{ name: "directory", type: "string" },
{ name: "path", type: "string" },
{ name: "args", type: "array" },
{ name: "opts", type: "object" }
],
[
{ name: "exists", type: "boolean" }
]
);
domainManager.registerEvent(
domainName,
"progress",
[
{ name: "commandId", type: "number" },
{ name: "time", type: "number" },
{ name: "message", type: "string" }
]
);
}