diff --git a/electron/config.js b/electron/config.js index 4ba7b08..b01390f 100644 --- a/electron/config.js +++ b/electron/config.js @@ -28,6 +28,7 @@ const schema = { "allowBetaVersions": {type: "boolean", default: false}, "enableGlobalHotkey": {type: "boolean", default: false}, "globalHotkey": {type: "string", default: "CmdOrCtrl+Shift+H"}, + "bufferPath" : {type: "string", default: ""}, }, }, @@ -51,6 +52,7 @@ const defaults = { allowBetaVersions: false, enableGlobalHotkey: false, globalHotkey: "CmdOrCtrl+Shift+H", + bufferPath: "", }, theme: "system", } diff --git a/electron/detect-platform.ts b/electron/detect-platform.ts index 811b947..1f677e0 100644 --- a/electron/detect-platform.ts +++ b/electron/detect-platform.ts @@ -1,6 +1,7 @@ const os = require('os'); +export const isDev = !!process.env.VITE_DEV_SERVER_URL + export const isMac = os.platform() === "darwin"; export const isWindows = os.platform() === "win32"; export const isLinux = os.platform() === "linux"; - diff --git a/electron/main/buffer.js b/electron/main/buffer.js new file mode 100644 index 0000000..a3cbcd5 --- /dev/null +++ b/electron/main/buffer.js @@ -0,0 +1,23 @@ +import fs from "fs" +import { join } from "path" +import { app } from "electron" +import CONFIG from "../config" +import { isDev } from "../detect-platform" + + +export function getBufferFilePath() { + let defaultPath = app.getPath("userData") + let configPath = CONFIG.get("settings.bufferPath") + let bufferPath = configPath.length ? configPath : defaultPath + let bufferFilePath = join(bufferPath, isDev ? "buffer-dev.txt" : "buffer.txt") + try { + // use realpathSync to resolve a potential symlink + return fs.realpathSync(bufferFilePath) + } catch (err) { + // realpathSync will fail if the file does not exist, but that doesn't matter since the file will be created + if (err.code !== "ENOENT") { + throw err + } + return bufferFilePath + } +} diff --git a/electron/main/index.ts b/electron/main/index.ts index 9b1870e..572f575 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -8,9 +8,10 @@ import { initialContent, initialDevContent } from '../initial-content' import { WINDOW_CLOSE_EVENT, SETTINGS_CHANGE_EVENT } from '../constants'; import CONFIG from "../config" import { onBeforeInputEvent } from "../keymap" -import { isMac } from '../detect-platform'; +import { isDev } from '../detect-platform'; import { initializeAutoUpdate, checkForUpdates } from './auto-update'; import { fixElectronCors } from './cors'; +import { getBufferFilePath } from './buffer'; // The built directory structure @@ -54,7 +55,6 @@ export let win: BrowserWindow | null = null const preload = join(__dirname, '../preload/index.js') const url = process.env.VITE_DEV_SERVER_URL const indexHtml = join(process.env.DIST, 'index.html') -const isDev = !!process.env.VITE_DEV_SERVER_URL let currentKeymap = CONFIG.get("settings.keymap") let contentSaved = false @@ -210,9 +210,8 @@ ipcMain.handle('dark-mode:set', (event, mode) => { ipcMain.handle('dark-mode:get', () => nativeTheme.themeSource) -const bufferPath = isDev ? join(app.getPath("userData"), "buffer-dev.txt") : join(app.getPath("userData"), "buffer.txt") - -ipcMain.handle('buffer-content:load', async () =>  { +ipcMain.handle('buffer-content:load', async () => { + let bufferPath = getBufferFilePath() if (jetpack.exists(bufferPath) === "file") { return await jetpack.read(bufferPath, 'utf8') } else { @@ -221,7 +220,7 @@ ipcMain.handle('buffer-content:load', async () =>  { }); async function save(content) { - return await jetpack.write(bufferPath, content, { + return await jetpack.write(getBufferFilePath(), content, { atomic: true, mode: '600', })