-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
330 lines (288 loc) · 9.61 KB
/
index.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
var S = require('string');
var os = require('os');
var exec = require('child_process').exec;
var p4options = require('./p4options');
var ztagRegex = /^\.\.\.\s+(\w+)\s+(.+)/;
var p4 = process.platform === 'win32'? 'p4.exe' : 'p4';
// build a list of options/arguments for the p4 command
function optionBuilder(options) {
options = options || {};
var results = {stdin: [], args: [], files: []};
Object.keys(options).map(function (option) {
var p4option = p4options[option];
if (!p4option) return;
if (p4option.category !== 'unary') {
if ((options[option] || {}).constructor !== p4option.type) return;
}
if (p4option.category === 'stdin') {
results.stdin.push(p4option.cmd + options[option]);
if (results.args.indexOf('-i') < 0) results.args.push('-i');
} else if (p4option.cmd) {
results.args.push(p4option.cmd);
if (p4option.category === 'mixed') results.args.push(options[option]);
} else {
results.files = results.files.concat(options[option]);
}
});
return results;
}
// filter passed-in options to get a hash of child process options
// (i.e., not p4 command arguments)
function execOptionBuilder(options) {
var validKeys = {
cwd: true,
env: true,
encoding: true,
shell: true,
timeout: true,
maxBuffer: true,
killSignal: true,
uid: true,
gid: true
};
options = options || {};
return Object.keys(options).reduce(function(result, key) {
if(validKeys[key]) {
result[key] = options[key];
}
return result;
}, {});
}
function execP4(p4cmd, options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
var ob = optionBuilder(options);
var childProcessOptions = execOptionBuilder(options);
var cmd = [p4, p4cmd, ob.args.join(' '), ob.files.join(' ')];
var child = exec(cmd.join(' '), childProcessOptions, function (err, stdout, stderr) {
if (err) return callback(err);
if (stderr) return callback(new Error(stderr));
return callback(null, stdout);
});
if (ob.stdin.length > 0) {
ob.stdin.forEach(function (line) {
child.stdin.write(line + '\n');
});
child.stdin.emit('end');
}
}
// process group of lines of output from a p4 command executed with -ztag
function processZtagOutput(output) {
return output.split('\n').reduce(function(memo, line) {
var match, key, value;
match = ztagRegex.exec(line);
if(match) {
key = match[1];
value = match[2];
memo[key] = value;
}
return memo;
}, {});
}
function NodeP4() {}
NodeP4.prototype.changelist = {
create: function (options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
var newOptions = {
_change: 'new',
description: options.description || '<saved by node-perforce>'
};
execP4('change', newOptions, function (err, stdout) {
if (err) return callback(err);
var matched = stdout.match(/([0-9]+)/g);
if (matched.length > 0) return callback(null, parseInt(matched[0], 10));
else return callback(new Error('Unknown error'));
});
},
edit: function (options, callback) {
callback = callback || function(){};
if (!options || !options.changelist) return callback(new Error('Missing parameter/argument'));
if (!options.description) return callback();
var newOptions = {
_change: options.changelist.toString(),
description: options.description
};
execP4('change', newOptions, function (err) {
if (err) return callback(err);
return callback();
});
},
delete: function (options, callback) {
callback = callback || function(){};
if (!options || !options.changelist) return callback(new Error('Missing parameter/argument'));
execP4('change', {_delete: options.changelist}, function (err) {
if (err) return callback(err);
return callback();
});
},
view: function (options, callback) {
if (!options || !options.changelist) return callback(new Error('Missing parameter/argument'));
execP4('change', {_output: options.changelist}, function (err, stdout) {
if (err) return callback(err);
// preprocessing file status
stdout = stdout.replace(/(\t)+#(.)*/g, function (match) {
return '@@@' + match.substring(3);
});
var result = {};
var lines = stdout.replace(/#(.)*\n/g, '').split(os.EOL + os.EOL);
lines.forEach(function (line) {
var key = S(line.split(':')[0].toLowerCase()).trim().camelize().s;
if (key) {
result[key] = S(line).between(':').trim().s;
}
});
if (result.files) {
result.files = result.files.split('\n').map(function (file) {
var file = file.replace(/\t*/g, '').split('@@@');
return {file: file[0], action: file[1]};
});
} else {
result.files = [];
}
return callback(null, result);
});
},
submit: function (options, callback) {
if (!options || !options.changelist) return callback(new Error('Missing parameter/argument'));
execP4('submit', options, function (err, stdout) {
if (err) return callback(err);
});
}
};
NodeP4.prototype.info = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('info', options, function (err, stdout) {
if (err) return callback(err);
var result = {};
S(stdout).lines().forEach(function (line) {
if (!line) return;
var key = S((line.split(':')[0]).toLowerCase()).camelize().s;
result[key] = S(line).between(':').trim().s;
});
callback(null, result);
});
};
// return an array of file info objects for each file opened in the workspace
NodeP4.prototype.opened = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('-ztag opened', options, function (err, stdout) {
var result;
if (err) return callback(err);
// process each file
result = stdout.trim().split(/\r\n\r\n|\n\n/).reduce(function(memo, fileinfo) {
// process each line of file info, transforming into a hash
memo.push(processZtagOutput(fileinfo));
return memo;
}, []);
callback(null, result);
});
};
NodeP4.prototype.fstat = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('fstat', options, function (err, stdout) {
var result;
if (err) return callback(err);
// process each file fstat info
result = stdout.trim().split(/\r\n\r\n|\n\n/).reduce(function(memo, fstatinfo) {
// process each line of file info, transforming into a hash
memo.push(processZtagOutput(fstatinfo));
return memo;
}, []);
callback(null, result);
});
};
NodeP4.prototype.changes = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('-ztag changes', options, function (err, stdout) {
var result;
if (err) return callback(err);
// process each change
result = stdout.trim().split(/\r\n\r\n|\n\n(?=\.\.\.)/).reduce(function(memo, changeinfo) {
// process each line of change info, transforming into a hash
var item = processZtagOutput(changeinfo);
// If object representing change is not empty, push it onto array
if (Object.keys(item).length != 0)
memo.push(item);
return memo;
}, []);
callback(null, result);
});
};
NodeP4.prototype.user = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('-ztag user', options, function (err, stdout) {
var result;
if (err) return callback(err);
// process ztagged user information
result = processZtagOutput(stdout.trim());
callback(null, result);
});
};
NodeP4.prototype.users = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('-ztag users', options, function (err, stdout) {
var result;
if (err) return callback(err);
// process each change
result = stdout.trim().split(/\r\n\r\n|\n\n(?=\.\.\.)/).reduce(function(memo, userinfo) {
// process each line of user info, transforming into a hash
memo.push(processZtagOutput(userinfo));
return memo;
}, []);
callback(null, result);
});
};
NodeP4.prototype.diff2 = function (options, callback) {
if(typeof options === 'function') {
callback = options;
options = undefined;
}
execP4('-ztag diff2', options, function (err, stdout) { // TODO: Check that no more than two file arguments are provided
var result;
if (err) return callback(err);
// process each change
result = stdout.trim().split(/\r\n\r\n|\n\n(?=\.\.\.)/).reduce(function(memo, diff2info) {
// process each line of change info, transforming into a hash
var item = processZtagOutput(diff2info);
// If object representing change is not empty, push it onto array
if (Object.keys(item).length != 0)
memo.push(item);
return memo;
}, []);
callback(null, result);
});
};
var commonCommands = ['add', 'delete', 'edit', 'revert', 'sync',
'diff', 'reconcile', 'reopen', 'resolved',
'shelve', 'unshelve', 'client', 'resolve',
'submit', 'describe', 'files'];
commonCommands.forEach(function (command) {
NodeP4.prototype[command] = function (options, callback) {
execP4(command, options, callback);
};
});
module.exports = new NodeP4();