import { ipcMain , IpcMainEvent } from 'electron' ; import path from 'path' ; import fsWathcer , { WatchrChangeEventListener , WatchrStalker , WatchrStartCallback } from 'watchr' ; import recorder from './recorder' ; import replayer from './replayer' ; import { Environment } from 'last-hit-replayer' ; import { Environment as EnvironmentOptions } from 'last-hit-types' ; enum WatchrEvent { CHANGE = 'change' , CLOSE = 'close' } enum WatchrChangeType { CREATE = 'create' , UPDATE = 'update' , DELETE = 'delete' } enum WatchrConfigPreferredMethod { WATCH = 'watch' , WATCH_FILE = 'watchFile' } type State = { workspaceFile ? : string ; stalker ? : any ; } ; let state = { } ; const generateKeyByString = ( storyName , flowName ) : string => { return `[${ flowName }@${ storyName }]` ; } ; const getWorkspaceRootFolder = ( workspaceFile ) : string => { const parsedPath = path . parse ( workspaceFile ) ; const rootFolder = path . resolve ( parsedPath . root , parsedPath . dir ) ; console . log ( `workspace root folder is ${ rootFolder }` ) ; return rootFolder ; } ; /**
 * start to watch file system for given workspace
 */ const startFileSystemWatch = ( workspaceFile ) : WatchrStalker => { const rootFolder = getWorkspaceRootFolder ( workspaceFile ) ; const listener = ( changeType , fullPath , currentStat , previousStat ) => { switch ( changeType ) { case WatchrChangeType . UPDATE : console . log ( 'the file' , fullPath , 'was updated' ) ; break ; case WatchrChangeType . CREATE : console . log ( 'the file' , fullPath , 'was created' ) ; break ; case WatchrChangeType . DELETE : console . log ( 'the file' , fullPath , 'was deleted' ) ; break ; } } ; const stalker = fsWathcer . create ( rootFolder ) ; stalker . on ( WatchrEvent . CHANGE , listener ) ; stalker . once ( WatchrEvent . CLOSE , ( reason ) : void => { console . log ( 'closed because' , reason ) ; stalker . removeAllListeners ( ) ; } ) ; stalker . setConfig ( { stat : null , interval : 5007 , persistent : true , catchupDelay : 2000 , preferredMethods : [ WatchrConfigPreferredMethod . WATCH , WatchrConfigPreferredMethod . WATCH_FILE ] , followLinks : true , ignorePaths : false , ignoreHiddenFiles : false , ignoreCommonPatterns : true , ignoreCustomPatterns : null } ) ; const next = err => { if ( err ) { console . log ( 'watch failed on' , rootFolder , 'with error' , err ) ; } else { console . log ( 'watch successful on' , rootFolder ) ; } } ; stalker . watch ( next ) ; return stalker ; } ; export const initialize = ( ) : void => { ipcMain . on ( 'workspace-closed' , ( ) => { const { workspaceFile } = state ; recorder . destory ( ) ; replayer . destory ( ) ; state . stalker && state . stalker . close ( ) ; state = { } ; console . log ( `Workspace[${ workspaceFile }] closed.` ) ; } ) ; ipcMain . on ( 'workspace-opened' , ( event , arg ) : void => { state = arg ; const { workspaceFile } = state ; const env = new Environment ( { name : 'IDE' , workspace : getWorkspaceRootFolder ( workspaceFile ) } ) ; replayer . swtichEnv ( env ) ; console . log ( `Workspace[${ workspaceFile }] opened.` ) ; } ) ; ipcMain . on ( 'switch-replayer-env' , ( event , arg ) : void => { const { env , workspaceFile } = arg ; if ( env != null ) { const newEnv = new Environment ( { ... env , name : env . name || 'IDE' , urlReplaceRegexp : Array . isArray ( env . urlReplaceRegexp ) ? env . urlReplaceRegexp . join ( '&&' ) : env . urlReplaceRegexp , urlReplaceTo : Array . isArray ( env . urlReplaceTo ) ? env . urlReplaceTo . join ( '&&' ) : env . urlReplaceTo , workspace : getWorkspaceRootFolder ( workspaceFile ) } ) ; replayer . swtichEnv ( newEnv ) ; } else { const env = new Environment ( { name : 'IDE' , workspace : getWorkspaceRootFolder ( workspaceFile ) } ) ; replayer . swtichEnv ( env ) ; } event . reply ( 'replayer-env-switched' ) ; } ) ; } ;