Louis 99d083d84a
refactor: file prefix replace utils & add unit test (#1676)
* refactor: file prefix replace utils

* chore: add unit tests for core module
2024-01-22 10:05:47 +07:00

27 lines
743 B
TypeScript

import { ipcMain } from 'electron'
import { FileSystemRoute } from '@janhq/core'
import { userSpacePath } from '../utils/path'
import { join } from 'path'
import { normalizeFilePath } from '@janhq/core/node'
/**
* Handles file system operations.
*/
export function handleFsIPCs() {
const moduleName = 'fs'
Object.values(FileSystemRoute).forEach((route) => {
ipcMain.handle(route, async (event, ...args) => {
return import(moduleName).then((mdl) =>
mdl[route](
...args.map((arg) =>
typeof arg === 'string' &&
(arg.includes(`file:/`) || arg.includes(`file:\\`))
? join(userSpacePath, normalizeFilePath(arg))
: arg
)
)
)
})
})
}