@@ -20,11 +20,12 @@ export interface ITelemetryData {
2020 extraData ?: { [ key : string ] : string } ;
2121}
2222
23+ const MAX_FILE_COUNT : number = 100 ;
24+
2325export default class Telemetry {
2426 private _enabled : boolean ;
2527 private _store : ITelemetryData [ ] ;
2628 private _dataFolder : string ;
27- private readonly _MAX_FILE_COUNT : number = 100 ;
2829
2930 public constructor ( private _rushConfiguration : RushConfiguration ) {
3031 this . _enabled = this . _rushConfiguration . telemetryEnabled ;
@@ -49,12 +50,12 @@ export default class Telemetry {
4950 if ( ! this . _enabled || this . _store . length === 0 ) {
5051 return ;
5152 }
52- this . _cleanUp ( ) ;
5353
5454 const fullPath : string = this . _getFilePath ( ) ;
5555 fsx . ensureDirSync ( this . _dataFolder ) ;
5656 writeFile ( fullPath , JSON . stringify ( this . _store ) ) ;
5757 this . _store = [ ] ;
58+ this . _cleanUp ( ) ;
5859 }
5960
6061 public get store ( ) : ITelemetryData [ ] {
@@ -65,22 +66,29 @@ export default class Telemetry {
6566 * When there are too many log files, delete the old ones.
6667 */
6768 private _cleanUp ( ) : void {
68- if ( fsx . exists ( this . _dataFolder ) ) {
69+ if ( fsx . existsSync ( this . _dataFolder ) ) {
6970 const files : string [ ] = fsx . readdirSync ( this . _dataFolder ) ;
70- const filesToDelete : number = files . length - this . _MAX_FILE_COUNT + 1 ;
71- if ( filesToDelete > 0 ) {
72- const sortedFiles : string [ ] = files . map ( filePath => {
71+ if ( files . length > MAX_FILE_COUNT ) {
72+ const sortedFiles : string [ ] = files . map ( fileName => {
73+ const filePath : string = path . join ( this . _dataFolder , fileName ) ;
74+ const stats : fsx . Stats = fsx . statSync ( filePath ) ;
7375 return {
7476 filePath : filePath ,
75- modifiedTime : fsx . statSync ( filePath ) . mtime . getTime ( )
77+ modifiedTime : stats . mtime . getTime ( ) ,
78+ isFile : stats . isFile ( )
7679 } ;
7780 } )
81+ . filter ( value => {
82+ // Only delete files
83+ return value . isFile ;
84+ } )
7885 . sort ( ( a , b ) => {
7986 return a . modifiedTime - b . modifiedTime ;
8087 } )
8188 . map ( s => {
8289 return s . filePath ;
8390 } ) ;
91+ const filesToDelete : number = sortedFiles . length - MAX_FILE_COUNT ;
8492 for ( let i : number = 0 ; i < filesToDelete ; i ++ ) {
8593 fsx . unlinkSync ( sortedFiles [ i ] ) ;
8694 }
0 commit comments