2020-07-30 19:43:55 +00:00
|
|
|
// preload.js for Electron app
|
|
|
|
const { ipcRenderer } = require('electron');
|
2020-08-02 04:06:18 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const modpath = require('path');
|
|
|
|
|
2020-07-30 19:43:55 +00:00
|
|
|
process.once('loaded', () => {
|
2020-08-03 01:22:20 +00:00
|
|
|
// workspace root path
|
|
|
|
// reload() clears this, so we have to set it every time
|
|
|
|
var wsroot;
|
|
|
|
// from browser: read workspace file synchronously
|
|
|
|
window.getWorkspaceFile = function(path, filetype) {
|
|
|
|
if (wsroot == null) throw Error("no workspace root set");
|
|
|
|
try {
|
|
|
|
// TODO: detect text or binary, or decide based on incbin vs. source file
|
|
|
|
var fullpath = modpath.join(wsroot, modpath.normalize(path));
|
|
|
|
var encoding = filetype == 'text' ? 'utf8' : null;
|
|
|
|
var data = fs.readFileSync(fullpath, {encoding:encoding});
|
|
|
|
if (encoding == null) data = new Uint8Array(data);
|
|
|
|
console.log("getWorkspaceFile", path, filetype, data.length);
|
|
|
|
// TODO: add to watched files
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 04:06:18 +00:00
|
|
|
// from electron.js: set workspace root directory
|
|
|
|
ipcRenderer.on('setWorkspaceRoot', (event, data) => {
|
|
|
|
wsroot = data.root;
|
|
|
|
console.log('setWorkspaceRoot', wsroot);
|
2020-07-30 19:43:55 +00:00
|
|
|
});
|
2020-08-02 04:06:18 +00:00
|
|
|
// from electron.js: file changed
|
|
|
|
ipcRenderer.on('fileChanged', (event, data) => {
|
|
|
|
var path = data.path;
|
|
|
|
console.log('fileChanged', path);
|
|
|
|
window.reloadWorkspaceFile(path);
|
2020-07-30 19:43:55 +00:00
|
|
|
});
|
|
|
|
});
|