1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | function CmdAnonymizer(commandDetails, redaction) {
|
8 | this.last = null;
|
9 | this.invoked = [];
|
10 | this.redaction = redaction;
|
11 | this.options = commandDetails.options;
|
12 | this.shorthands = commandDetails.shorthands;
|
13 | }
|
14 | CmdAnonymizer.prototype.extractArgument = function (word) {
|
15 | return word.replace(/-{0,2}/, '');
|
16 | };
|
17 | CmdAnonymizer.prototype.isOptionValue = function (option, value) {
|
18 | const choice = this.options[option];
|
19 | const booleans = ['true', 'false'];
|
20 | return ((choice instanceof Array && choice.indexOf(value) !== -1) ||
|
21 | (choice === Boolean && booleans.indexOf(value.toLowerCase()) !== -1) ||
|
22 | (typeof choice === 'string' && choice === value));
|
23 | };
|
24 | CmdAnonymizer.prototype.isValueInOptions = function (options, value) {
|
25 | if (!(options instanceof Array)) {
|
26 | return this.isOptionValue(options, value);
|
27 | }
|
28 | return options.some(function (each) {
|
29 | return this.isOptionValue(this.extractArgument(each), value);
|
30 | }, this);
|
31 | };
|
32 | CmdAnonymizer.prototype.classify = function (word) {
|
33 | const arg = this.extractArgument(word);
|
34 | const whitelist = ['verbose', 'no-hooks'];
|
35 | if (whitelist.indexOf(arg) === 0) {
|
36 | this.invoked.push(word);
|
37 | this.last = arg;
|
38 | return;
|
39 | }
|
40 | if (this.shorthands && this.shorthands[arg]) {
|
41 | this.invoked.push(word);
|
42 | this.last = this.shorthands[arg];
|
43 | return;
|
44 | }
|
45 | if (this.options && this.options[arg]) {
|
46 | this.invoked.push(word);
|
47 | this.last = arg;
|
48 | return;
|
49 | }
|
50 | if (this.options && this.isValueInOptions(this.last, word)) {
|
51 | this.invoked.push(word);
|
52 | this.last = undefined;
|
53 | return;
|
54 | }
|
55 | if (this.options &&
|
56 | this.options[this.last] instanceof Array &&
|
57 | this.options[this.last].indexOf(word) !== -1) {
|
58 | this.invoked.push(word);
|
59 | this.last = undefined;
|
60 | return;
|
61 | }
|
62 | this.invoked.push(this.redaction);
|
63 | this.last = undefined;
|
64 | };
|
65 | CmdAnonymizer.prototype.resolve = function (cmd) {
|
66 |
|
67 | this.invoked.push(cmd.shift());
|
68 | cmd.forEach(this.classify, this);
|
69 | return this.invoked;
|
70 | };
|
71 | CmdAnonymizer.prototype.resolveToString = function (cmd) {
|
72 | return this.resolve(cmd).join(' ');
|
73 | };
|
74 | module.exports = CmdAnonymizer;
|
75 |
|
\ | No newline at end of file |