UNPKG

2.5 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7function 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}
14CmdAnonymizer.prototype.extractArgument = function (word) {
15 return word.replace(/-{0,2}/, '');
16};
17CmdAnonymizer.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};
24CmdAnonymizer.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};
32CmdAnonymizer.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};
65CmdAnonymizer.prototype.resolve = function (cmd) {
66 // quasi-strict white list approach (best-effort)
67 this.invoked.push(cmd.shift());
68 cmd.forEach(this.classify, this);
69 return this.invoked;
70};
71CmdAnonymizer.prototype.resolveToString = function (cmd) {
72 return this.resolve(cmd).join(' ');
73};
74module.exports = CmdAnonymizer;
75//# sourceMappingURL=cmd-anonymizer.js.map
\No newline at end of file