import * as fs from 'fs-extra' ; import { tmpdir } from 'os' ; import * as path from 'path' ; const debug = require ( 'debug' ) ( 'windows-build-tools' ) ; const tmp = tmpdir ( ) ; returns { string | null } export function findVCCLogFile ( ) { return new Promise ( ( resolve ) => { fs . readdir ( tmp ) . then ( ( contents ) => { const matchingFiles = contents . filter ( ( f ) => f . startsWith ( 'dd_installer_' ) ) ; let matchingFile = null ; if ( matchingFiles && matchingFiles . length === 1 ) { matchingFile = path . join ( tmp , matchingFiles [ 0 ] ) ; debug ( `Find LogFile: Just one file found, resolving with ${ matchingFile }` ) ; } else if ( ! matchingFiles || matchingFiles . length === 0 ) { debug ( `Find LogFile: No files found, resolving with null` ) ; matchingFile = null ; } else { debug ( `Find LogFile: Multiple files found, determining last modified one` ) ; const lastModified = matchingFiles . reduce ( ( previous , current ) => { const file = path . join ( tmp , current ) ; const stats = fs . statSync ( file ) ; let modifiedTime ; if ( stats && ( stats as any ) . mtimeMs ) { modifiedTime = ( stats as any ) . mtimeMs ; } else if ( stats && stats . mtime ) { modifiedTime = new Date ( stats . mtime ) . getTime ( ) ; } debug ( `Find LogFile: Comparing ${ modifiedTime } to ${ previous . timestamp }` ) ; if ( modifiedTime && modifiedTime > previous . timestamp ) { return { file : current , timestamp : modifiedTime } ; } else { return previous ; } } , { file : null , timestamp : 0 } ) ; debug ( `Find LogFile: Returning ${ lastModified . file }` ) ; matchingFile = path . join ( tmp , lastModified . file ) ; } resolve ( matchingFile ) ; } ) . catch ( ( error ) => { debug ( `Did not find VCC logfile: ${ error }` ) ; return null ; } ) ; } ) ; }