Add bufferPath to config, support bufferPath symlink (#27)

* Add bufferPath to config, support bufferPath symlink

* defaultPath before checking realpath

* Move code for retrieving buffer path into its own file

* Do the realpathSync() call for the whole final file path, to allow the buffer file to be a symlink

---------

Co-authored-by: Jonatan Heyman <jonatan@heyman.info>
This commit is contained in:
Joshua Blum 2023-12-23 05:17:13 -05:00 committed by GitHub
parent d1ea17c197
commit f532c7939b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 7 deletions

View File

@ -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",
}

View File

@ -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";

23
electron/main/buffer.js Normal file
View File

@ -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
}
}

View File

@ -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',
})