forked from wokim/node-perforce
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
537 lines (455 loc) · 13.3 KB
/
index.js
File metadata and controls
537 lines (455 loc) · 13.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
'use strict';
var os = require('os');
const { exec, spawn } = require('child_process');
var p4options = require('./p4options');
const { stdout } = require('process');
var ztagRegex = /^\.\.\.\s+(\w+)\s+(.+)/;
var p4 = process.platform === 'win32' ? 'p4.exe' : 'p4';
function camelize(str)
{
return str
.split(/[-_\s]+/) // Split by dash, underscore, or space
.map((word, index) =>
index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
)
.join('');
}
// 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)
{
if (NodeP4.debug_mode)
{
console.log(`[P4 DEBUG] Rejected option parameter due to wrong argument type! Option ${option}, parameter value was ${options[option]}, required type was ${p4option.type.name}.`);
}
throw new Error(`[Perforce] Rejected option parameter due to wrong argument type! Option ${option}, parameter value was ${options[option]}, required type was ${p4option.type.name}.`);
return;
}
}
if (p4option.category === 'stdin')
{
results.stdin.push(p4option.cmd + options[option]);
if (results.args.indexOf('-i') < 0 && !p4option.omit_i) 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(' ')];
if (NodeP4.debug_mode)
{
console.log('[P4 DEBUG] ' + cmd.join(' '));
}
// flatten both flags _and_ file‐spec strings, then drop blanks
const flatArgs = ob.args.reduce((a, s) => a.concat(String(s).split(/\s+/)), []);
const flatFiles = ob.files.reduce((a, s) => a.concat(String(s).split(/\s+/)), []);
const argv = [p4cmd, ...flatArgs, ...flatFiles].filter(Boolean);
// use spawn to avoid buffer size issues
var child = spawn(p4, argv, { ...childProcessOptions, shell: true });
let stdout = '', stderr = '';
child.stdout.on('data', d => { stdout += d; });
child.stderr.on('data', d => { stderr += d; });
child.on('error', (err) =>
{
// Handle spawn errors (like command not found)
callback(err);
});
child.on('close', code =>
{
// For p4 info and some other commands, we want to return stdout even if
// the exit code is non-zero, as it often contains useful information
if (code !== 0)
{
// We'll still pass the stderr as an error property
return callback(null, stdout, {error: stderr, code: code});
}
return callback(null, stdout);
});
if (ob.stdin.length > 0)
{
ob.stdin.forEach(function (line)
{
// for multi-line inputs, the first line goes in as is, and the following need to start with a tab
let multiline = line.split('\n')
child.stdin.write(multiline[0] + '\n');
if (NodeP4.debug_mode && p4cmd.toLowerCase() != 'login')
{
console.log(" > " + multiline[0]);
}
multiline.shift();
multiline.forEach(function (theLine)
{
child.stdin.write('\t' + theLine + '\n');
if (NodeP4.debug_mode && p4cmd.toLowerCase() != 'login')
{
console.log(' > ' + theLine);
}
});
});
child.stdin.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, procErr)
{
if (err) return callback(err);
const matched = stdout ? stdout.match(/([0-9]+)/g) : null;
if (matched && matched.length > 0)
{
return callback(null, parseInt(matched[0], 10));
}
const details = procErr?.error || stdout || "No stdout or stderr returned from p4 change -i.";
return callback(new Error("Failed to create changelist. " + details));
});
},
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 = camelize(line.split(':')[0].toLowerCase().trim());
if (key)
{
result[key] = line.substring(line.indexOf(':') + 1).trim();
}
});
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);
return callback(null, stdout);
});
}
};
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 = {};
stdout.split(/\r\n|\r|\n/).forEach(function (line)
{
if (!line) return;
var key = camelize(line.split(':')[0].toLowerCase());
result[key] = line.substring(line.indexOf(':') + 1).trim();
});
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;
}
// TODO: Check that no more than two file arguments are provided
execP4('-ztag diff2', 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, 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);
});
};
NodeP4.prototype.awaitCommand = function (command, options)
{
return new Promise((resolve, reject) =>
{
let commandPointer = this;
if (command.includes('.'))
{
commandPointer = this[command.substring(0, command.indexOf('.'))];
command = command.substring(command.indexOf('.') + 1);
}
commandPointer[command](options, (err, out) =>
{
// Error and output handling
if (err)
{
if (err.message && err.message.includes("file(s) up-to-date"))
{
resolve(err.message); // Not a real error, treat as success
}
else
{
reject(err); // Real error, reject the promise
}
}
else
{
resolve(out); // No errors, resolve with the actual output
}
});
});
};
NodeP4.prototype.setDebugMode = function (debug_active)
{
// change static property so that we can access it inside nested function defs like changelist.create,
// where we can't easily get an instance property using the 'this' variable since the context is nested
NodeP4.debug_mode = debug_active;
};
var commonCommands = ['add', 'delete', 'edit', 'revert', 'sync', 'diff', 'reconcile', 'reopen', 'resolved',
'shelve', 'unshelve', 'client', 'resolve', 'submit', 'describe', 'files', 'have', 'login', 'logout'];
commonCommands.forEach(function (command)
{
NodeP4.prototype[command] = function (options, callback)
{
execP4(command, options, callback);
};
});
module.exports = new NodeP4();