-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathoptions-track-helper.ts
80 lines (64 loc) · 2.32 KB
/
options-track-helper.ts
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
import * as path from "path";
import { TrackActionNames } from "../constants";
export class OptionsTracker {
public static PASSWORD_DETECTION_STRING = "password";
public static PRIVATE_REPLACE_VALUE = "private";
public static PATH_REPLACE_VALUE = "_localpath";
public static SIZE_EXEEDED_REPLACE_VALUE = "sizeExceeded";
constructor(
private $analyticsService: IAnalyticsService) {
}
public async trackOptions(options: IOptions) {
const trackObject = this.getTrackObject(options);
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.Options,
additionalData: JSON.stringify(trackObject)
});
}
private getTrackObject(options: IOptions): IDictionary<any> {
const optionsArgvCopy = _.cloneDeep(options.argv);
return this.sanitizeTrackObject(optionsArgvCopy, options);
}
private sanitizeTrackObject(data: IDictionary<any>, options?: IOptions): IDictionary<any> {
const shorthands = options ? options.shorthands : [];
const optionsDefinitions = options ? options.options : {};
_.forEach(data, (value, key) => {
if (this.shouldSkipProperty(key, value, shorthands, optionsDefinitions)) {
delete data[key];
} else {
if (options && optionsDefinitions[key] && optionsDefinitions[key].private) {
value = OptionsTracker.PRIVATE_REPLACE_VALUE;
} else if (key.toLowerCase().indexOf(OptionsTracker.PASSWORD_DETECTION_STRING) >= 0) {
value = OptionsTracker.PRIVATE_REPLACE_VALUE;
} else if (_.isString(value) && value !== path.basename(value)) {
value = OptionsTracker.PATH_REPLACE_VALUE;
} else if (_.isObject(value) && !_.isArray(value)) {
value = this.sanitizeTrackObject(value);
}
data[key] = value;
}
});
return data;
}
private shouldSkipProperty(key: string, value: any, shorthands: string[] = [], options: IDictionary<IDashedOption> = {}): Boolean {
if (shorthands.indexOf(key) >= 0) {
return true;
}
if (key.indexOf("-") >= 0) {
return true;
}
if (key === "_") {
return true;
}
const optionDef = options[key];
if (optionDef && optionDef.type === OptionType.Boolean) {
if (optionDef.default !== true && value === false || optionDef.default === true && value === true) {
return true;
}
}
if (_.isUndefined(value)) {
return true;
}
}
}
$injector.register("optionsTracker", OptionsTracker);