Last active
June 25, 2025 17:52
-
-
Save Ephraim-Bryski/c218cdff6bbe3ca34a0aa67c1f87a715 to your computer and use it in GitHub Desktop.
reading and writing files in electron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FILE_A = 'a.json' | |
const FILE_B = 'b.json' | |
const files = [FILE_A,FILE_B] | |
const contents = {} | |
window.api.receive("sendReadContent", (file_name,data) => { | |
if (!files.includes(file_name)){ | |
throw "read file not in list" | |
} | |
contents[file_name] = data | |
const read_files = Object.keys(contents) | |
const all_is_read = files.every(a => {return read_files.includes(a)}) | |
if (all_is_read){ | |
do_stuff_after_all_read() | |
} | |
}) | |
function do_stuff_after_all_read(){ | |
console.log(contents) | |
} | |
window.api.send("askToRead", FILE_A) | |
window.api.send("askToRead", FILE_B) | |
const data_to_write = {important: "stuff to write"} | |
window.api.send("askToWrite", "to_write.json", JSON.stringify(data_to_write)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="client.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {app,BrowserWindow,ipcMain} = require("electron"); | |
const path = require("path"); | |
const fs = require("fs"); | |
let win; | |
async function createWindow() { | |
win = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
preload: path.join(__dirname, "preload.js") | |
} | |
}) | |
win.loadFile(path.join(__dirname, "public/index.html")); | |
} | |
app.on("ready", createWindow); | |
ipcMain.on("askToRead", (event, file_name) => { | |
fs.readFile(path.join(__dirname, file_name),'utf8', (error, data) => { | |
win.webContents.send("sendReadContent", file_name, data); | |
}) | |
}) | |
ipcMain.on("askToWrite", (event, file_name, contents) => { | |
fs.writeFile(path.join(__dirname, file_name), contents, () => {}); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {contextBridge,ipcRenderer} = require("electron"); | |
contextBridge.exposeInMainWorld( | |
"api", { | |
send: (channel, ...args) => { | |
// whitelist channels | |
let validChannels = ["askToRead", "askToWrite"]; | |
if (validChannels.includes(channel)) { | |
ipcRenderer.send(channel, ...args); | |
}else { | |
throw "invalid channel" | |
} | |
}, | |
receive: (channel, func) => { | |
let validChannels = ["sendReadContent"]; | |
if (validChannels.includes(channel)) { | |
// Deliberately strip event as it includes `sender` | |
ipcRenderer.on(channel, (event, ...args) => func(...args)); | |
}else { | |
throw "invalid channel" | |
} | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment