import { BrowserWindow , dialog , shell , ipcMain , IpcMainInvokeEvent } from 'electron' ; import Path from 'path' ; import { IPCKey } from '../common/Constants' ; import { FileItem , SelectFolderResult } from '../common/Types' ; import { enumFiles } from './FileManager' ; param ev const onSelectFolder = async ( ev ) : Promise < SelectFolderResult | undefined > => { const win = BrowserWindow . fromWebContents ( ev . sender ) ; if ( ! win ) { throw new Error ( 'Message sender window does not exist' ) ; } const result = await dialog . showOpenDialog ( win , { title : 'Select root folder' , properties : [ 'openDirectory' ] } ) ; if ( ! result || ! result . filePaths || result . filePaths . length === 0 ) { return ; } const folderPath = result . filePaths [ 0 ] ; const items = await enumFiles ( folderPath ) ; return { name : Path . basename ( folderPath ) , folderPath , items } ; } ; param ev param folderPath const onEnumItems = async ( ev , folderPath ) : Promise < FileItem [ ] > => { return await enumFiles ( folderPath ) ; } ; param ev param itemPath returns const onOpenItem = async ( ev , itemPath ) : Promise < string > => { return shell . openPath ( itemPath ) ; } ; /**
 * A value indicating that an IPC events has been initialized.
 */ let initialized = false ; /**
 * Initialize IPC events.
 */ export const initializeIpcEvents = ( ) => { if ( initialized ) { return ; } initialized = true ; ipcMain . handle ( IPCKey . SelectFolder , onSelectFolder ) ; ipcMain . handle ( IPCKey . EnumItems , onEnumItems ) ; ipcMain . handle ( IPCKey . OpenItem , onOpenItem ) ; } ; /**
 * Release IPC events.
 */ export const releaseIpcEvents = ( ) => { if ( initialized ) { ipcMain . removeAllListeners ( IPCKey . SelectFolder ) ; ipcMain . removeAllListeners ( IPCKey . EnumItems ) ; ipcMain . removeAllListeners ( IPCKey . OpenItem ) ; } initialized = false ; } ;