-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp4.js
60 lines (47 loc) · 1.25 KB
/
p4.js
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
/*jshint node:true*/
"use strict";
var exec = require("child_process").spawn;
function runCommand(command, args, done) {
if(typeof args === "function") {
done = args;
args = "";
}
if(!Array.isArray(args)) {
args = [args];
}
args.unshift(command);
var child = spawn("p4", args);
var stdOutBuf = "";
var stdErrBuf = "";
child.stdout.on("data", (data) => stdOutBuf += data);
child.stderr.on("data", (data) => stdErrBuf += data)
child.on("exit", (code) => {
if (code !== 0) {
return done(new Error(`p4 subcommand exited with return code ${}`));
}
if (stdErrBuf.length > 0) {
return done(new Error(stdErrBuf));
}
done(null, stdOutBuf);
});
}
function edit(path, done) {
runCommand("edit", path, done);
}
function add(path, done) {
runCommand("add", path, done);
}
function smartEdit(path, done) {
edit(path, function(err, stdout) {
if(!err) return done(err, stdout);
add(path, done);
});
}
function revertUnchanged(path, done) {
runCommand("revert", "-a", done);
}
exports.edit = edit;
exports.add = add;
exports.smartEdit = smartEdit;
exports.run = runCommand;
exports.revertUnchanged = revertUnchanged;