Skip to content

Commit

Permalink
feat: electron write file
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 18, 2024
1 parent 5c0c623 commit a0717f3
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 46 deletions.
26 changes: 14 additions & 12 deletions libs/Universal-PicGo-Core/src/i18n/index.ts
Expand Up @@ -108,18 +108,20 @@ class I18nManager implements II18nManager {
const files = fs.readdirSync(i18nFolder, {
withFileTypes: true,
})
files.forEach((file: any) => {
if (file.isFile() && file.name.endsWith(".yml")) {
const i18nFilePath = path.join(i18nFolder, file.name)
const i18nFile = fs.readFileSync(i18nFilePath, "utf8")
try {
const i18nFileObj = yaml.load(i18nFile) as ILocales
languageList[file.name.replace(/\.yml$/, "")] = i18nFileObj
} catch (e) {
console.error(e)
}
}
})
// files.forEach((file: any) => {
// if (file.isFile() && file.name.endsWith(".yml")) {
// const i18nFilePath = path.join(i18nFolder, file.name)
// alert(i18nFilePath)
// alert(fs)
// const i18nFile = fs.readFileSync(i18nFilePath, "utf8")
// try {
// const i18nFileObj = yaml.load(i18nFile) as ILocales
// languageList[file.name.replace(/\.yml$/, "")] = i18nFileObj
// } catch (e) {
// console.error(e)
// }
// }
// })
} else {
// "i18n": [
// {
Expand Down
2 changes: 1 addition & 1 deletion libs/Universal-PicGo-Core/src/lib/PluginLoader.ts
Expand Up @@ -8,7 +8,7 @@
*/

import { IPicGo, IPicGoPlugin, IPicGoPluginInterface, IPluginLoader } from "../types"
import { hasNodeEnv, win } from "universal-picgo-store/src"
import { hasNodeEnv, win } from "universal-picgo-store"
import BrowserPluginLoaderDb from "../db/browserPluginLoderDb"

/**
Expand Down
5 changes: 4 additions & 1 deletion libs/Universal-PicGo-Core/vite.config.ts
Expand Up @@ -61,7 +61,10 @@ console.log("distDir=>", distDir)

export default defineConfig({
plugins: [
dts(),
dts({
insertTypesEntry: true,
// rollupTypes: true,
}),

nodePolyfills({
// Whether to polyfill `node:` protocol imports.
Expand Down
4 changes: 1 addition & 3 deletions libs/Universal-PicGo-Store/package.json
Expand Up @@ -27,15 +27,13 @@
"devDependencies": {
"@terwer/eslint-config-custom": "^1.3.6",
"@terwer/vite-config-custom": "^0.7.6",
"@types/lodash-es": "^4.17.12",
"@types/write-file-atomic": "^4.0.3"
"@types/lodash-es": "^4.17.12"
},
"dependencies": {
"@commonify/lowdb": "^3.0.0",
"comment-json": "^4.2.3",
"lodash-es": "^4.17.21",
"ts-localstorage": "^3.1.0",
"write-file-atomic": "^5.0.1",
"zhi-lib-base": "^0.8.0"
},
"publishConfig": {
Expand Down
6 changes: 3 additions & 3 deletions libs/Universal-PicGo-Store/src/lib/adapters/JSONAdapter.ts
@@ -1,7 +1,7 @@
import { IJSON } from "../../types"
import { TextFileSync } from "@commonify/lowdb"
import { TextFileSync } from "../base/electron/TextFile"
import json from "comment-json"
import writeFile from "write-file-atomic"
import { writeFileSync } from "../base/electron/writeFileAtomic"

export class JSONAdapter {
private readonly adapter: TextFileSync
Expand Down Expand Up @@ -36,6 +36,6 @@ export class JSONAdapter {
}

write(obj: any): void {
writeFile.sync(this.dbPath, json.stringify(obj, null, 2))
writeFileSync(this.dbPath, json.stringify(obj, null, 2))
}
}
48 changes: 48 additions & 0 deletions libs/Universal-PicGo-Store/src/lib/base/electron/TextFile.ts
@@ -0,0 +1,48 @@
/*
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2024 Terwer, Inc. <https://terwer.space/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/

import { SyncAdapter } from "@commonify/lowdb"
import { win } from "../../utils"

export class TextFileSync implements SyncAdapter<string> {
// PathLike
#tempFilename: any
// PathLike
#filename: any

// PathLike
constructor(filename: any) {
const path = win.require("path")
this.#filename = filename
const f = filename.toString()
this.#tempFilename = path.join(path.dirname(f), `.${path.basename(f)}.tmp`)
}

read(): string | null {
let data

try {
const fs = win.fs
data = fs.readFileSync(this.#filename, "utf-8")
} catch (e) {
if ((e as NodeJS.ErrnoException).code === "ENOENT") {
return null
}
throw e
}

return data
}

write(str: string): void {
const fs = win.fs
fs.writeFileSync(this.#tempFilename, str)
fs.renameSync(this.#tempFilename, this.#filename)
}
}
17 changes: 17 additions & 0 deletions libs/Universal-PicGo-Store/src/lib/base/electron/signalExitShim.ts
@@ -0,0 +1,17 @@
/*
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2024 Terwer, Inc. <https://terwer.space/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/

/**
* This is a browser shim that provides the same functional interface
* as the main node export, but it does nothing.
* @module
*/
export const onExit: (cb: any, opts?: { alwaysLast?: boolean }) => () => void = () => () => {}
export const load = () => {}
export const unload = () => {}

0 comments on commit a0717f3

Please sign in to comment.