Skip to content

Commit 1ddf015

Browse files
committed
fine tuning for all platforms
1 parent a0fb2f9 commit 1ddf015

File tree

3 files changed

+92
-44
lines changed

3 files changed

+92
-44
lines changed

src/library/interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ export interface ErrorTippy {
5454
export interface CommandArgs {
5555
command: string;
5656
variables: boolean;
57+
}
58+
59+
export interface MountArgs {
60+
what: string;
61+
where: string;
5762
}

src/main.ts

Lines changed: 81 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,28 @@ const webR = new webr.WebR({ interactive: false });
2626
let mainWindow: BrowserWindow;
2727
// const root = production ? "../../" : "../";
2828

29-
async function mount(dir: string) {
30-
webR.FS.unmount("/host");
31-
await webR.FS.mkdir("/host");
29+
async function mount(obj: interfaces.MountArgs) {
30+
31+
try {
32+
await webR.FS.unmount(obj.where);
33+
} catch (error) {
34+
// consolog(obj.where + " directory is not mounted yet.");
35+
try {
36+
await webR.FS.mkdir(obj.where);
37+
} catch (error) {
38+
consolog("Failed to make " + obj.where);
39+
throw error;
40+
}
41+
}
42+
3243
try {
3344
await webR.FS.mount(
3445
"NODEFS",
35-
{ root: dir },
36-
"/host"
46+
{ root: obj.what },
47+
obj.where
3748
);
3849
} catch (error) {
39-
console.log(error);
50+
consolog("Failed to mount " + obj.what + " to " + obj.where);
4051
throw error;
4152
}
4253
}
@@ -45,8 +56,6 @@ async function initWebR() {
4556
try {
4657
await webR.init();
4758

48-
console.log(__dirname);
49-
5059
// mount a virtual filesystem containing contributed R packages
5160
const data = new Blob([
5261
fs.readFileSync(
@@ -142,7 +151,11 @@ ipcMain.on('showMessage', (event, args) => {
142151
});
143152

144153
ipcMain.on('showError', (event, message) => {
145-
dialog.showErrorBox("Error", message)
154+
dialog.showMessageBox(mainWindow, {
155+
type: "error",
156+
title: "Error",
157+
message: message
158+
});
146159
});
147160

148161

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

158171
ipcMain.on("selectFileFrom", (event, args) => {
159172
if (args.inputType === "Select file type") {
160-
dialog.showErrorBox("Error", "Select input type");
173+
dialog.showMessageBox(mainWindow, {
174+
type: "error",
175+
title: "Error",
176+
message: "Select input type"
177+
});
161178
} else {
162179
const info = util.fileFromInfo(args.inputType);
163180

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

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

202219
ipcMain.on("outputType", (event, args) => {
203220
inputOutput.fileToExt = args.extension;
221+
if (inputOutput.fileFromDir != "" && inputOutput.fileToDir == "") {
222+
mount({ what: inputOutput.fileFromDir, where: "/output" });
223+
}
204224
})
205225

206226
ipcMain.on("selectFileTo", (event, args) => {
207227
if (args.outputType === "Select file type") {
208-
dialog.showErrorBox("Error", "Select output type");
228+
dialog.showMessageBox(mainWindow, {
229+
type: "error",
230+
title: "Error",
231+
message: "Select output type"
232+
});
209233
} else {
210234
const ext = util.getExtensionFromType(args.outputType);
211235

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

236-
mainWindow.webContents.send("selectFileTo-reply", inputOutput);
260+
mount({ what: inputOutput.fileToDir, where: "/output" }).then(() => {
261+
mainWindow.webContents.send("selectFileTo-reply", inputOutput);
262+
});
237263
}
238264
})
239265
.catch((err) => {
@@ -256,42 +282,59 @@ ipcMain.on("declared", () => {
256282
ipcMain.on("sendCommand", async (event, args) => {
257283
const command = args.command;
258284
mainWindow.webContents.send("startLoader");
259-
// consolog("main266: " + command);
260285

261-
try {
262-
await webR.evalR(command);
263-
} catch (error) {
264-
console.log(error);
265-
throw error;
266-
}
267-
268-
if (util.isTrue(args.updateVariables)) {
269-
// consolog("main: updating variables");
286+
let output_dir_writable = true;
287+
if (!util.isTrue(args.updateVariables)) {
270288
try {
271-
const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply(
272-
collectRMetadata(dataset),
273-
function(x) {
274-
values <- names(x$labels)
275-
names(values) <- x$labels
276-
x$values <- as.list(values)
277-
return(x)
278-
}
279-
)))`);
280-
281-
if (!webr.isRCharacter(result)) throw new Error('Not a character!');
289+
await webR.evalR(`write.csv(data.frame(A = 1:2), "/output/test.csv")`);
290+
await webR.evalR(`unlink("/output/test.csv")`);
291+
} catch (error) {
292+
output_dir_writable = false;
293+
}
294+
}
282295

283-
const response = await result.toString();
284-
webR.destroy(result);
285-
mainWindow.webContents.send("updateVariables", JSON.parse(response));
296+
if (util.isFalse(args.updateVariables) && util.isFalse(output_dir_writable)) {
297+
dialog.showMessageBox(mainWindow, {
298+
type: "error",
299+
title: "Error",
300+
message:"The target directory has writing constraints. Try saving into a different one."
301+
});
302+
} else {
286303

304+
try {
305+
await webR.evalR(command);
287306
} catch (error) {
288307
console.log(error);
289308
throw error;
290309
}
310+
311+
if (util.isTrue(args.updateVariables)) {
312+
// consolog("main: updating variables");
313+
try {
314+
const result = await webR.evalR(`as.character(jsonlite::toJSON(lapply(
315+
collectRMetadata(dataset),
316+
function(x) {
317+
values <- names(x$labels)
318+
names(values) <- x$labels
319+
x$values <- as.list(values)
320+
return(x)
321+
}
322+
)))`);
323+
324+
if (!webr.isRCharacter(result)) throw new Error('Not a character!');
325+
326+
const response = await result.toString();
327+
webR.destroy(result);
328+
mainWindow.webContents.send("updateVariables", JSON.parse(response));
329+
330+
} catch (error) {
331+
console.log(error);
332+
throw error;
333+
}
334+
}
291335
}
292336

293337
mainWindow.webContents.send("clearLoader");
294-
295338
});
296339

297340
const inputOutput: interfaces.InputOutput = {
@@ -322,6 +365,3 @@ function consoletrace(x: any) {
322365
process.on('unhandledRejection', (error: Error, promise) => {
323366
consoletrace(error);
324367
});
325-
326-
327-
app.commandLine.appendSwitch('log-level', '3');

src/preload.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ window.addEventListener('DOMContentLoaded', () => {
9595
inputOutput.fileFromName = io.fileFromName;
9696
inputOutput.fileFromExt = io.fileFromExt;
9797

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

113113
command += ")";
114-
console.log("preload116: ", command);
114+
// console.log("preload116: ", command);
115115

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

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

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

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

329330
command += ")";
331+
// console.log("preload331: ", command);
332+
330333
ipcRenderer.send('sendCommand', {
331334
command: command.replace(/\\/g, '/'),
332335
updateVariables: true

0 commit comments

Comments
 (0)