@@ -2,55 +2,43 @@ import * as path from "path";
22import { TrackActionNames } from "../constants" ;
33
44export class OptionsTrackHelper {
5- public static SINGLE_OBJECT_SIZE_LIMIT = 400 ;
6- public static SINGLE_PROPERTY_SIZE_LIMIT = 300 ;
75 public static PASSWORD_DETECTION_STRING = "password" ;
8- public static PASSOWRD_REPLACE_VALUE = "privateData " ;
6+ public static PASSOWRD_REPLACE_VALUE = "private " ;
97 public static PATH_REPLACE_VALUE = "_localpath" ;
10- public static SIZE_EXEEDED_REPLACE_VALUE = "popertySizeExceeded" ;
11-
8+ public static SIZE_EXEEDED_REPLACE_VALUE = "sizeExceeded" ;
129
1310 constructor (
1411 private $analyticsService : IAnalyticsService ) {
1512 }
1613
1714 public async trackOptions ( options : IOptions ) {
18- const trackObjects = this . getTrackObjects ( options ) ;
19- const promises : Promise < void > [ ] = [ ] ;
20- _ . forEach ( trackObjects , trackObject => {
21- const json = JSON . stringify ( trackObject ) ;
22-
23- const trackPromise = this . $analyticsService . trackEventActionInGoogleAnalytics ( {
24- action : TrackActionNames . Options ,
25- additionalData : json
26- } ) ;
15+ const trackObject = this . getTrackObject ( options ) ;
2716
28- promises . push ( trackPromise ) ;
17+ await this . $analyticsService . trackEventActionInGoogleAnalytics ( {
18+ action : TrackActionNames . Options ,
19+ additionalData : JSON . stringify ( trackObject )
2920 } ) ;
30-
31- await Promise . all ( promises ) ;
3221 }
3322
34- private getTrackObjects ( options : IOptions ) : Object [ ] {
23+ private getTrackObject ( options : IOptions ) : IDictionary < any > {
3524 const optionsArgvCopy = _ . cloneDeep ( options . argv ) ;
36- const data = this . sanitizeTrackObject ( optionsArgvCopy , options ) ;
3725
38- return this . splitTrackObjects ( data ) ;
26+ return this . sanitizeTrackObject ( optionsArgvCopy , options ) ;
3927 }
4028
4129 private sanitizeTrackObject ( data : IDictionary < any > , options ?: IOptions ) : IDictionary < any > {
42- const shorthands = options ? options . shorthands : [ ] ;
43- const optionsDefinitions = options ? options . options : { } ;
30+ const shorthands = options ? options . shorthands : [ ] ;
31+ const optionsDefinitions = options ? options . options : { } ;
4432
4533 _ . forEach ( data , ( value , key ) => {
46- if ( this . shouldSkipProperty ( key , value , shorthands , optionsDefinitions ) ) {
34+ if ( this . shouldSkipProperty ( key , value , shorthands , optionsDefinitions ) ) {
4735 delete data [ key ] ;
4836 } else {
49- if ( key . toLowerCase ( ) . indexOf ( OptionsTrackHelper . PASSWORD_DETECTION_STRING ) >= 0 ) {
37+ if ( key . toLowerCase ( ) . indexOf ( OptionsTrackHelper . PASSWORD_DETECTION_STRING ) >= 0 ) {
5038 value = OptionsTrackHelper . PASSOWRD_REPLACE_VALUE ;
51- } else if ( typeof value === "string" && value !== path . basename ( value ) ) {
39+ } else if ( _ . isString ( value ) && value !== path . basename ( value ) ) {
5240 value = OptionsTrackHelper . PATH_REPLACE_VALUE ;
53- } else if ( _ . isObject ( value ) && ! _ . isArray ( value ) ) {
41+ } else if ( _ . isObject ( value ) && ! _ . isArray ( value ) ) {
5442 value = this . sanitizeTrackObject ( value ) ;
5543 }
5644 data [ key ] = value ;
@@ -61,68 +49,29 @@ export class OptionsTrackHelper {
6149 }
6250
6351 private shouldSkipProperty ( key : string , value : any , shorthands : string [ ] = [ ] , options : IDictionary < IDashedOption > = { } ) : Boolean {
64- if ( shorthands . indexOf ( key ) >= 0 ) {
52+ if ( shorthands . indexOf ( key ) >= 0 ) {
6553 return true ;
6654 }
6755
68- if ( key . indexOf ( "-" ) >= 0 ) {
56+ if ( key . indexOf ( "-" ) >= 0 ) {
6957 return true ;
7058 }
7159
72- if ( key === "_" ) {
60+ if ( key === "_" ) {
7361 return true ;
7462 }
75-
63+
7664 const optionDef = options [ key ] ;
77- if ( optionDef && optionDef . type === OptionType . Boolean ) {
78- if ( optionDef . default !== true && value === false || optionDef . default === true && value === true ) {
65+ if ( optionDef && optionDef . type === OptionType . Boolean ) {
66+ if ( optionDef . default !== true && value === false || optionDef . default === true && value === true ) {
7967 return true ;
8068 }
8169 }
8270
83- if ( _ . isUndefined ( value ) ) {
71+ if ( _ . isUndefined ( value ) ) {
8472 return true ;
8573 }
8674 }
87-
88- private splitTrackObjects ( trackData : Object ) : Object [ ] {
89- const json = JSON . stringify ( trackData ) ;
90- const firstObject : IDictionary < any > = { } ;
91- const secondObject : IDictionary < any > = { } ;
92- const bigFields : Object [ ] = [ ] ;
93-
94- if ( json . length > OptionsTrackHelper . SINGLE_OBJECT_SIZE_LIMIT ) {
95- const keys = _ . keys ( trackData ) ;
96-
97- if ( keys . length === 1 ) {
98- return [ trackData ] ;
99- }
100-
101- for ( let i = 0 ; i < keys . length ; i ++ ) {
102- const key = keys [ i ] ;
103- let value = trackData [ key ] ;
104- const valueLength = JSON . stringify ( value ) . length ;
105-
106- if ( valueLength > OptionsTrackHelper . SINGLE_OBJECT_SIZE_LIMIT ) {
107- value = "SIZE_EXEEDED_REPLACE_VALUE" ;
108- }
109-
110- if ( valueLength > OptionsTrackHelper . SINGLE_PROPERTY_SIZE_LIMIT ) {
111- bigFields . push ( {
112- [ key ] : value
113- } ) ;
114- } else if ( i < keys . length / 2 ) {
115- firstObject [ key ] = value ;
116- } else {
117- secondObject [ key ] = value ;
118- }
119- }
120-
121- return bigFields . concat ( this . splitTrackObjects ( firstObject ) , this . splitTrackObjects ( secondObject ) ) ;
122- } else {
123- return [ trackData ] ;
124- }
125- }
12675}
12776
12877$injector . register ( "optionsTrackHelper" , OptionsTrackHelper ) ;
0 commit comments