import * as FS from '../tools/fs' ; import * as path from 'path' ; import * as tar from 'tar' ; import Logger from '../tools/env.logger' ; import ServiceElectron from './service.electron' ; import ServicePaths from './service.paths' ; import { IService } from '../interfaces/interface.service' ; import { IPCMessages , Subscription } from './service.electron' ; import { dialog , SaveDialogReturnValue } from 'electron' ; const CLogsFiles = [ 'chipmunk.log' , 'chipmunk.indexer.log' , 'chipmunk.launcher.log' , 'chipmunk.updater.log' , ] ; class description class ServiceLogsExtractor implements IService { private _logger : Logger = new Logger ( 'ServiceLogsExtractor' ) ; private _subscriptions : { [ key ] : Subscription ; } = { } ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { ServiceElectron . IPC . subscribe ( IPCMessages . ChipmunkLogsRequest , this . _ipc_onChipmunkLogsRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . ChipmunkLogsRequest = subscription ; resolve ( ) ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "ChipmunkLogsRequest" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; resolve ( ) ; } ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceLogsExtractor' ; } private _ipc_onChipmunkLogsRequest ( req , response ) { const request = req as IPCMessages . ChipmunkLogsRequest ; this . _getFileName ( ) . then ( ( filename ) => { if ( filename === undefined ) { return response ( new IPCMessages . ChipmunkLogsResponse ( { } ) ) ; } const targets = [ ] ; Promise . all ( CLogsFiles . map ( ( file ) => { const fullfilename = path . resolve ( ServicePaths . getHome ( ) , file ) ; return FS . exist ( fullfilename ) . then ( ( exist ) => { if ( exist ) { targets . push ( file ) ; } } ) . catch ( ( err ) => { this . _logger . warn ( `Fail check file ${ fullfilename } due error: ${ err . message }` ) ; } ) ; } ) ) . then ( ( ) => { if ( targets . length === 0 ) { return response ( new IPCMessages . ChipmunkLogsResponse ( { error : `No any chipmunk's logs are found` , } ) ) ; } tar . c ( { gzip : true , file : filename , cwd : ServicePaths . getHome ( ) , } , targets ) . then ( ( ) => { response ( new IPCMessages . ChipmunkLogsResponse ( { } ) ) ; } ) . catch ( ( compressErr ) => { this . _logger . warn ( `Fail to compress logs files due error: ${ compressErr . message }` ) ; return response ( new IPCMessages . ChipmunkLogsResponse ( { error : `Fail to compress logs files due error: ${ compressErr . message }` , } ) ) ; } ) ; } ) . catch ( ( checkErr ) => { this . _logger . error ( `Fail to check log files due error: ${ checkErr . message }` ) ; response ( new IPCMessages . ChipmunkLogsResponse ( { error : checkErr . message , } ) ) ; } ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail get target file name due error: ${ error . message }` ) ; response ( new IPCMessages . ChipmunkLogsResponse ( { error : error . message , } ) ) ; } ) ; } private _getFileName ( ) { return new Promise ( ( resolve , reject ) => { const win = ServiceElectron . getBrowserWindow ( ) ; if ( win === undefined ) { return ; } dialog . showSaveDialog ( win , { title : 'Saving chipmunk\'s logs' , filters : [ { name : 'Tar file' , extensions : [ 'tgz' ] , } ] , } ) . then ( ( returnValue ) => { resolve ( returnValue . filePath ) ; } ) . catch ( ( error ) => { this . _logger . error ( `Fail get filename for saving due error: ${ error . message }` ) ; reject ( error ) ; } ) ; } ) ; } } export default ( new ServiceLogsExtractor ( ) ) ;