2026-08-02 23:40:29 -05:00
|
|
|
import { contextBridge, ipcRenderer, type IpcRendererEvent } from 'electron'
|
2026-08-02 23:16:26 -05:00
|
|
|
import { electronAPI } from '@electron-toolkit/preload'
|
2026-08-02 23:40:29 -05:00
|
|
|
import type {
|
|
|
|
|
DdevProjectDetail,
|
|
|
|
|
DdevProjectSummary,
|
|
|
|
|
TerminalDataEvent,
|
|
|
|
|
TerminalExitEvent
|
|
|
|
|
} from '../shared/types'
|
2026-08-02 23:16:26 -05:00
|
|
|
|
|
|
|
|
// Custom APIs for renderer
|
2026-08-02 23:31:45 -05:00
|
|
|
const api = {
|
|
|
|
|
projects: {
|
|
|
|
|
list: (): Promise<DdevProjectSummary[]> => ipcRenderer.invoke('projects:list'),
|
|
|
|
|
describe: (name: string): Promise<DdevProjectDetail> =>
|
|
|
|
|
ipcRenderer.invoke('projects:describe', name),
|
2026-08-02 23:40:29 -05:00
|
|
|
start: (operationId: string, name: string): Promise<void> =>
|
|
|
|
|
ipcRenderer.invoke('projects:start', operationId, name),
|
|
|
|
|
stop: (operationId: string, name: string): Promise<void> =>
|
|
|
|
|
ipcRenderer.invoke('projects:stop', operationId, name),
|
|
|
|
|
restart: (operationId: string, name: string): Promise<void> =>
|
|
|
|
|
ipcRenderer.invoke('projects:restart', operationId, name)
|
|
|
|
|
},
|
|
|
|
|
terminal: {
|
|
|
|
|
cancel: (operationId: string): Promise<boolean> =>
|
|
|
|
|
ipcRenderer.invoke('terminal:cancel', operationId),
|
|
|
|
|
onData: (callback: (event: TerminalDataEvent) => void): (() => void) => {
|
|
|
|
|
const listener = (_event: IpcRendererEvent, data: TerminalDataEvent): void => callback(data)
|
|
|
|
|
ipcRenderer.on('terminal:data', listener)
|
|
|
|
|
return () => ipcRenderer.removeListener('terminal:data', listener)
|
|
|
|
|
},
|
|
|
|
|
onExit: (callback: (event: TerminalExitEvent) => void): (() => void) => {
|
|
|
|
|
const listener = (_event: IpcRendererEvent, data: TerminalExitEvent): void => callback(data)
|
|
|
|
|
ipcRenderer.on('terminal:exit', listener)
|
|
|
|
|
return () => ipcRenderer.removeListener('terminal:exit', listener)
|
|
|
|
|
}
|
2026-08-02 23:31:45 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Api = typeof api
|
2026-08-02 23:16:26 -05:00
|
|
|
|
|
|
|
|
// Use `contextBridge` APIs to expose Electron APIs to
|
|
|
|
|
// renderer only if context isolation is enabled, otherwise
|
|
|
|
|
// just add to the DOM global.
|
|
|
|
|
if (process.contextIsolated) {
|
|
|
|
|
try {
|
|
|
|
|
contextBridge.exposeInMainWorld('electron', electronAPI)
|
|
|
|
|
contextBridge.exposeInMainWorld('api', api)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// @ts-ignore (define in dts)
|
|
|
|
|
window.electron = electronAPI
|
|
|
|
|
// @ts-ignore (define in dts)
|
|
|
|
|
window.api = api
|
|
|
|
|
}
|