This commit is contained in:
Alex
2023-04-28 22:56:30 -04:00
parent 43bfb6715c
commit 904b11b7b7
30 changed files with 2790 additions and 1931 deletions

View File

@ -1,9 +1,10 @@
import type { DirEntry } from "opvault.js/src/adapter"
export interface IPC {
showDirectoryPicker(): Promise<string | undefined>
pathExists(path: string): Promise<boolean>
readdir(path: string): Promise<string[]>
readBuffer(path: string): Promise<Uint8Array>
readFile(path: string): Promise<string>
readDir(path: string): Promise<DirEntry[]>
readFile(path: string): Promise<Uint8Array>
readTextFile(path: string): Promise<string>
writeFile(path: string, data: string): Promise<void>
isDirectory(path: string): Promise<boolean>
}

View File

@ -1,5 +1,7 @@
import fs, { promises } from "fs"
import { ipcMain, dialog } from "electron"
import { adapter } from "opvault.js/src/adapter/node"
import type { DirEntry } from "opvault.js/src/adapter"
import type { IPC } from "./ipc-types"
registerService({
@ -15,11 +17,11 @@ registerService({
return fs.existsSync(path)
},
async readBuffer(_, path) {
async readFile(_, path) {
return promises.readFile(path)
},
async readFile(_, path) {
async readTextFile(_, path) {
return promises.readFile(path, "utf-8")
},
@ -27,13 +29,12 @@ registerService({
await promises.writeFile(path, content)
},
async readdir(_, path) {
return promises.readdir(path)
},
async isDirectory(_, path) {
const stats = await promises.stat(path)
return stats.isDirectory()
async readDir(_, path) {
const entries: DirEntry[] = []
for await (const dirent of adapter.fs.readDir(path)) {
entries.push(dirent)
}
return entries
},
})

View File

@ -1,5 +1,4 @@
import { Buffer } from "buffer"
import type { IAdapter } from "opvault.js/src/adapters"
import type { Adapter } from "opvault.js/src/adapter"
import type { IPC } from "../electron/ipc-types"
import { memoize } from "./memoize"
@ -9,14 +8,15 @@ export async function openDirectory() {
return ipc.showDirectoryPicker()
}
export const electronAdapter: IAdapter = {
export const electronAdapter: Adapter = {
fs: {
exists: path => ipc.pathExists(path),
readBuffer: path => ipc.readBuffer(path).then(Buffer.from),
readFile: path => ipc.readFile(path),
readdir: path => ipc.readdir(path),
writeFile: (path, data) => ipc.writeFile(path, data),
isDirectory: path => ipc.isDirectory(path),
readTextFile: path => ipc.readTextFile(path),
async *readDir(path) {
yield* await ipc.readDir(path)
},
writeTextFile: (path, data) => ipc.writeFile(path, data),
},
subtle: crypto.subtle,
}