| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import type { OpenDialogOptions, Event } from 'electron'
- import { dialog, ipcMain, shell } from 'electron'
- import * as fs from 'fs-extra'
- /**
- * 选择文件夹
- */
- export const choeseDirectory = async () => {
- const { canceled, filePaths } = await dialog.showOpenDialog({
- properties: ['openDirectory']
- })
- if (!canceled) {
- return filePaths[0]
- }
- return
- }
- /**
- * 选择文件
- */
- export const choeseFile = async (_e: Event, options: OpenDialogOptions = {}) => {
- const { canceled, filePaths } = await dialog.showOpenDialog({
- title: '选择文件',
- buttonLabel: '选择',
- ...options
- })
- if (!canceled) {
- return filePaths
- }
- return
- }
- /**
- * 保存文件
- */
- export const writeFile = async (_e: Event, filePath: string, content: string) => {
- return fs.writeFileSync(filePath, content)
- }
- /**
- * 读取文件
- */
- export const readFile = async (_e: Event, filePath: string, encoding: BufferEncoding) => {
- return fs.readFileSync(filePath, { encoding })
- }
- /**
- * 检查文件是否存在
- */
- export const checkFileExists = async (_e: Event, filePath: string) => {
- return fs.existsSync(filePath)
- }
- /**
- * 创建目录
- */
- export const createDirectory = async (_e: Event, directoryPath: string) => {
- return fs.mkdirSync(directoryPath, { recursive: true })
- }
- /**
- * 复制文件
- */
- export const copyFile = async (_e: Event, sourcePath: string, destinationPath: string) => {
- return fs.copyFileSync(sourcePath, destinationPath)
- }
- /**
- * 打开文件夹
- */
- export const openDir = async (
- _e: Event,
- directoryPath: string,
- cb: (err: NodeJS.ErrnoException | null, dir: fs.Dir) => void
- ) => {
- return fs.opendir(directoryPath, cb)
- }
- /**
- * 复制文件夹
- */
- export const cp = (_e: Event, sourcePath: string, destinationPath: string) => {
- return fs.cpSync(sourcePath, destinationPath)
- }
- /**
- * 删除文件
- */
- export const removeFile = (_e: Event, sourcePath: string) => {
- return fs.rmSync(sourcePath)
- }
- /**
- * 打开并锁定文件
- */
- export const openFile = (_e: Event, sourcePath: string, flag: string) => {
- return fs.openSync(sourcePath, flag || 'w')
- }
- /**
- * 关闭并解锁文件
- */
- export const closeFile = (_e: Event, fd: number) => {
- return fs.closeSync(fd)
- }
- /**
- * 修改文件名
- */
- export const renameFile = (_e: Event, sourcePath: string, targetPath: string) => {
- return fs.renameSync(sourcePath, targetPath)
- }
- /**
- * 资源管理器中打开
- * @param path 文件路径
- */
- export const openFileInExplorer = (_e: Event, path: string) => {
- return shell.showItemInFolder(path)
- }
- export function handleFile() {
- // 获取文件夹
- ipcMain.handle('get-directory', choeseDirectory)
- // 获取文件
- ipcMain.handle('get-file', choeseFile)
- // 创建文件夹
- ipcMain.handle('create-directory', createDirectory)
- // 写入文件
- ipcMain.handle('write-file', writeFile)
- // 读取文件
- ipcMain.handle('read-file', readFile)
- // 检查路径是否存在
- ipcMain.handle('check-file-path', checkFileExists)
- // 复制文件
- ipcMain.handle('copy-file', copyFile)
- // 删除文件
- ipcMain.handle('delete-file', removeFile)
- // 独占打开
- ipcMain.handle('exclusive-open', openFile)
- // 独占关闭
- ipcMain.handle('exclusive-close', closeFile)
- // 修改文件名
- ipcMain.handle('modify-file-name', renameFile)
- // 资源管理器中打开
- ipcMain.handle('open-file-in-explorer', openFileInExplorer)
- }
|