files.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import type { OpenDialogOptions, Event } from 'electron'
  2. import { dialog, ipcMain, shell } from 'electron'
  3. import * as fs from 'fs-extra'
  4. /**
  5. * 选择文件夹
  6. */
  7. export const choeseDirectory = async () => {
  8. const { canceled, filePaths } = await dialog.showOpenDialog({
  9. properties: ['openDirectory']
  10. })
  11. if (!canceled) {
  12. return filePaths[0]
  13. }
  14. return
  15. }
  16. /**
  17. * 选择文件
  18. */
  19. export const choeseFile = async (_e: Event, options: OpenDialogOptions = {}) => {
  20. const { canceled, filePaths } = await dialog.showOpenDialog({
  21. title: '选择文件',
  22. buttonLabel: '选择',
  23. ...options
  24. })
  25. if (!canceled) {
  26. return filePaths
  27. }
  28. return
  29. }
  30. /**
  31. * 保存文件
  32. */
  33. export const writeFile = async (_e: Event, filePath: string, content: string) => {
  34. return fs.writeFileSync(filePath, content)
  35. }
  36. /**
  37. * 读取文件
  38. */
  39. export const readFile = async (_e: Event, filePath: string, encoding: BufferEncoding) => {
  40. return fs.readFileSync(filePath, { encoding })
  41. }
  42. /**
  43. * 检查文件是否存在
  44. */
  45. export const checkFileExists = async (_e: Event, filePath: string) => {
  46. return fs.existsSync(filePath)
  47. }
  48. /**
  49. * 创建目录
  50. */
  51. export const createDirectory = async (_e: Event, directoryPath: string) => {
  52. return fs.mkdirSync(directoryPath, { recursive: true })
  53. }
  54. /**
  55. * 复制文件
  56. */
  57. export const copyFile = async (_e: Event, sourcePath: string, destinationPath: string) => {
  58. return fs.copyFileSync(sourcePath, destinationPath)
  59. }
  60. /**
  61. * 打开文件夹
  62. */
  63. export const openDir = async (
  64. _e: Event,
  65. directoryPath: string,
  66. cb: (err: NodeJS.ErrnoException | null, dir: fs.Dir) => void
  67. ) => {
  68. return fs.opendir(directoryPath, cb)
  69. }
  70. /**
  71. * 复制文件夹
  72. */
  73. export const cp = (_e: Event, sourcePath: string, destinationPath: string) => {
  74. return fs.cpSync(sourcePath, destinationPath)
  75. }
  76. /**
  77. * 删除文件
  78. */
  79. export const removeFile = (_e: Event, sourcePath: string) => {
  80. return fs.rmSync(sourcePath)
  81. }
  82. /**
  83. * 打开并锁定文件
  84. */
  85. export const openFile = (_e: Event, sourcePath: string, flag: string) => {
  86. return fs.openSync(sourcePath, flag || 'w')
  87. }
  88. /**
  89. * 关闭并解锁文件
  90. */
  91. export const closeFile = (_e: Event, fd: number) => {
  92. return fs.closeSync(fd)
  93. }
  94. /**
  95. * 修改文件名
  96. */
  97. export const renameFile = (_e: Event, sourcePath: string, targetPath: string) => {
  98. return fs.renameSync(sourcePath, targetPath)
  99. }
  100. /**
  101. * 资源管理器中打开
  102. * @param path 文件路径
  103. */
  104. export const openFileInExplorer = (_e: Event, path: string) => {
  105. return shell.showItemInFolder(path)
  106. }
  107. export function handleFile() {
  108. // 获取文件夹
  109. ipcMain.handle('get-directory', choeseDirectory)
  110. // 获取文件
  111. ipcMain.handle('get-file', choeseFile)
  112. // 创建文件夹
  113. ipcMain.handle('create-directory', createDirectory)
  114. // 写入文件
  115. ipcMain.handle('write-file', writeFile)
  116. // 读取文件
  117. ipcMain.handle('read-file', readFile)
  118. // 检查路径是否存在
  119. ipcMain.handle('check-file-path', checkFileExists)
  120. // 复制文件
  121. ipcMain.handle('copy-file', copyFile)
  122. // 删除文件
  123. ipcMain.handle('delete-file', removeFile)
  124. // 独占打开
  125. ipcMain.handle('exclusive-open', openFile)
  126. // 独占关闭
  127. ipcMain.handle('exclusive-close', closeFile)
  128. // 修改文件名
  129. ipcMain.handle('modify-file-name', renameFile)
  130. // 资源管理器中打开
  131. ipcMain.handle('open-file-in-explorer', openFileInExplorer)
  132. }