Skip to content

Commit

Permalink
fine tuning for all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
dusadrian committed Feb 5, 2025
1 parent a0fb2f9 commit 1ddf015
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 44 deletions.
5 changes: 5 additions & 0 deletions src/library/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ export interface ErrorTippy {
export interface CommandArgs {
command: string;
variables: boolean;
}

export interface MountArgs {
what: string;
where: string;
}
122 changes: 81 additions & 41 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,28 @@ const webR = new webr.WebR({ interactive: false });
let mainWindow: BrowserWindow;
// const root = production ? "../../" : "../";

async function mount(dir: string) {
webR.FS.unmount("/host");
await webR.FS.mkdir("/host");
async function mount(obj: interfaces.MountArgs) {

try {
await webR.FS.unmount(obj.where);
} catch (error) {
// consolog(obj.where + " directory is not mounted yet.");
try {
await webR.FS.mkdir(obj.where);
} catch (error) {
consolog("Failed to make " + obj.where);
throw error;
}
}

try {
await webR.FS.mount(
"NODEFS",
{ root: dir },
"/host"
{ root: obj.what },
obj.where
);
} catch (error) {
console.log(error);
consolog("Failed to mount " + obj.what + " to " + obj.where);
throw error;
}
}
Expand All @@ -45,8 +56,6 @@ async function initWebR() {
try {
await webR.init();

console.log(__dirname);

// mount a virtual filesystem containing contributed R packages
const data = new Blob([
fs.readFileSync(
Expand Down Expand Up @@ -142,7 +151,11 @@ ipcMain.on('showMessage', (event, args) => {
});

ipcMain.on('showError', (event, message) => {
dialog.showErrorBox("Error", message)
dialog.showMessageBox(mainWindow, {
type: "error",
title: "Error",
message: message
});
});


Expand All @@ -157,7 +170,11 @@ ipcMain.on("declared", () => {

ipcMain.on("selectFileFrom", (event, args) => {
if (args.inputType === "Select file type") {
dialog.showErrorBox("Error", "Select input type");
dialog.showMessageBox(mainWindow, {
type: "error",
title: "Error",
message: "Select input type"
});
} else {
const info = util.fileFromInfo(args.inputType);

Expand Down Expand Up @@ -189,7 +206,7 @@ ipcMain.on("selectFileFrom", (event, args) => {
inputOutput.fileFromDir = inputOutput.fileFromDir.replace(/\\/g, '/');
}

mount(inputOutput.fileFromDir).then(() => {
mount({ what: inputOutput.fileFromDir, where: "/input" }).then(() => {
mainWindow.webContents.send("selectFileFrom-reply", inputOutput);
});
}
Expand All @@ -201,11 +218,18 @@ ipcMain.on("selectFileFrom", (event, args) => {

ipcMain.on("outputType", (event, args) => {
inputOutput.fileToExt = args.extension;
if (inputOutput.fileFromDir != "" && inputOutput.fileToDir == "") {
mount({ what: inputOutput.fileFromDir, where: "/output" });
}
})

ipcMain.on("selectFileTo", (event, args) => {
if (args.outputType === "Select file type") {
dialog.showErrorBox("Error", "Select output type");
dialog.showMessageBox(mainWindow, {
type: "error",
title: "Error",
message: "Select output type"
});
} else {
const ext = util.getExtensionFromType(args.outputType);

Expand Down Expand Up @@ -233,7 +257,9 @@ ipcMain.on("selectFileTo", (event, args) => {
inputOutput.fileToDir = inputOutput.fileToDir.replace(/\\/g, '/');
}

mainWindow.webContents.send("selectFileTo-reply", inputOutput);
mount({ what: inputOutput.fileToDir, where: "/output" }).then(() => {
mainWindow.webContents.send("selectFileTo-reply", inputOutput);
});
}
})
.catch((err) => {
Expand All @@ -256,42 +282,59 @@ ipcMain.on("declared", () => {
ipcMain.on("sendCommand", async (event, args) => {
const command = args.command;
mainWindow.webContents.send("startLoader");
// consolog("main266: " + command);

try {
await webR.evalR(command);
} catch (error) {
console.log(error);
throw error;
}

if (util.isTrue(args.updateVariables)) {
// consolog("main: updating variables");
let output_dir_writable = true;
if (!util.isTrue(args.updateVariables)) {
try {
const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply(
collectRMetadata(dataset),
function(x) {
values <- names(x$labels)
names(values) <- x$labels
x$values <- as.list(values)
return(x)
}
)))`);

if (!webr.isRCharacter(result)) throw new Error('Not a character!');
await webR.evalR(`write.csv(data.frame(A = 1:2), "/output/test.csv")`);
await webR.evalR(`unlink("/output/test.csv")`);
} catch (error) {
output_dir_writable = false;
}
}

const response = await result.toString();
webR.destroy(result);
mainWindow.webContents.send("updateVariables", JSON.parse(response));
if (util.isFalse(args.updateVariables) && util.isFalse(output_dir_writable)) {
dialog.showMessageBox(mainWindow, {
type: "error",
title: "Error",
message:"The target directory has writing constraints. Try saving into a different one."
});
} else {

try {
await webR.evalR(command);
} catch (error) {
console.log(error);
throw error;
}

if (util.isTrue(args.updateVariables)) {
// consolog("main: updating variables");
try {
const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply(
collectRMetadata(dataset),
function(x) {
values <- names(x$labels)
names(values) <- x$labels
x$values <- as.list(values)
return(x)
}
)))`);

if (!webr.isRCharacter(result)) throw new Error('Not a character!');

const response = await result.toString();
webR.destroy(result);
mainWindow.webContents.send("updateVariables", JSON.parse(response));

} catch (error) {
console.log(error);
throw error;
}
}
}

mainWindow.webContents.send("clearLoader");

});

const inputOutput: interfaces.InputOutput = {
Expand Down Expand Up @@ -322,6 +365,3 @@ function consoletrace(x: any) {
process.on('unhandledRejection', (error: Error, promise) => {
consoletrace(error);
});


app.commandLine.appendSwitch('log-level', '3');
9 changes: 6 additions & 3 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ window.addEventListener('DOMContentLoaded', () => {
inputOutput.fileFromName = io.fileFromName;
inputOutput.fileFromExt = io.fileFromExt;

let command = "dataset <- convert('/host/" + io.fileFromName + io.fileFromExt + "', declared = FALSE, n_max = 10";
let command = "dataset <- convert('/input/" + io.fileFromName + io.fileFromExt + "', declared = FALSE, n_max = 10";
if (fileEncoding.value != 'utf8') {
if (fileEncoding.value == "default") {
command += ", encoding = NULL";
Expand All @@ -111,7 +111,7 @@ window.addEventListener('DOMContentLoaded', () => {
}

command += ")";
console.log("preload116: ", command);
// console.log("preload116: ", command);

ipcRenderer.send('sendCommand', {
command: command.replace(/\\/g, '/'),
Expand Down Expand Up @@ -193,7 +193,7 @@ window.addEventListener('DOMContentLoaded', () => {
if (indices.length == 0 && !all_vars_selected) {
ipcRenderer.send('showError', 'At least one variable has to be selected.');
} else {
let command = "convert('/host/" + inputOutput.fileFromName + inputOutput.fileFromExt + "', to = '/host/" + inputOutput.fileToName + inputOutput.fileToExt + "'";
let command = "convert('/input/" + inputOutput.fileFromName + inputOutput.fileFromExt + "', to = '/output/" + inputOutput.fileToName + inputOutput.fileToExt + "'";
// let command = "convert('" + inputOutput.fileFrom + "', to = '" + inputOutput.fileTo + "'";

const declaredTRUE = util.htmlElement("declaredTRUE");
Expand Down Expand Up @@ -306,6 +306,7 @@ window.addEventListener('DOMContentLoaded', () => {
}

command += ")";
// console.log("preload309: ", command);

ipcRenderer.send('sendCommand', {
command: command.replace(/\\/g, '/'),
Expand All @@ -327,6 +328,8 @@ window.addEventListener('DOMContentLoaded', () => {
}

command += ")";
// console.log("preload331: ", command);

ipcRenderer.send('sendCommand', {
command: command.replace(/\\/g, '/'),
updateVariables: true
Expand Down

0 comments on commit 1ddf015

Please sign in to comment.