@@ -26,17 +26,28 @@ const webR = new webr.WebR({ interactive: false });
26
26
let mainWindow : BrowserWindow ;
27
27
// const root = production ? "../../" : "../";
28
28
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
+
32
43
try {
33
44
await webR . FS . mount (
34
45
"NODEFS" ,
35
- { root : dir } ,
36
- "/host"
46
+ { root : obj . what } ,
47
+ obj . where
37
48
) ;
38
49
} catch ( error ) {
39
- console . log ( error ) ;
50
+ consolog ( "Failed to mount " + obj . what + " to " + obj . where ) ;
40
51
throw error ;
41
52
}
42
53
}
@@ -45,8 +56,6 @@ async function initWebR() {
45
56
try {
46
57
await webR . init ( ) ;
47
58
48
- console . log ( __dirname ) ;
49
-
50
59
// mount a virtual filesystem containing contributed R packages
51
60
const data = new Blob ( [
52
61
fs . readFileSync (
@@ -142,7 +151,11 @@ ipcMain.on('showMessage', (event, args) => {
142
151
} ) ;
143
152
144
153
ipcMain . on ( 'showError' , ( event , message ) => {
145
- dialog . showErrorBox ( "Error" , message )
154
+ dialog . showMessageBox ( mainWindow , {
155
+ type : "error" ,
156
+ title : "Error" ,
157
+ message : message
158
+ } ) ;
146
159
} ) ;
147
160
148
161
@@ -157,7 +170,11 @@ ipcMain.on("declared", () => {
157
170
158
171
ipcMain . on ( "selectFileFrom" , ( event , args ) => {
159
172
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
+ } ) ;
161
178
} else {
162
179
const info = util . fileFromInfo ( args . inputType ) ;
163
180
@@ -189,7 +206,7 @@ ipcMain.on("selectFileFrom", (event, args) => {
189
206
inputOutput . fileFromDir = inputOutput . fileFromDir . replace ( / \\ / g, '/' ) ;
190
207
}
191
208
192
- mount ( inputOutput . fileFromDir ) . then ( ( ) => {
209
+ mount ( { what : inputOutput . fileFromDir , where : "/input" } ) . then ( ( ) => {
193
210
mainWindow . webContents . send ( "selectFileFrom-reply" , inputOutput ) ;
194
211
} ) ;
195
212
}
@@ -201,11 +218,18 @@ ipcMain.on("selectFileFrom", (event, args) => {
201
218
202
219
ipcMain . on ( "outputType" , ( event , args ) => {
203
220
inputOutput . fileToExt = args . extension ;
221
+ if ( inputOutput . fileFromDir != "" && inputOutput . fileToDir == "" ) {
222
+ mount ( { what : inputOutput . fileFromDir , where : "/output" } ) ;
223
+ }
204
224
} )
205
225
206
226
ipcMain . on ( "selectFileTo" , ( event , args ) => {
207
227
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
+ } ) ;
209
233
} else {
210
234
const ext = util . getExtensionFromType ( args . outputType ) ;
211
235
@@ -233,7 +257,9 @@ ipcMain.on("selectFileTo", (event, args) => {
233
257
inputOutput . fileToDir = inputOutput . fileToDir . replace ( / \\ / g, '/' ) ;
234
258
}
235
259
236
- mainWindow . webContents . send ( "selectFileTo-reply" , inputOutput ) ;
260
+ mount ( { what : inputOutput . fileToDir , where : "/output" } ) . then ( ( ) => {
261
+ mainWindow . webContents . send ( "selectFileTo-reply" , inputOutput ) ;
262
+ } ) ;
237
263
}
238
264
} )
239
265
. catch ( ( err ) => {
@@ -256,42 +282,59 @@ ipcMain.on("declared", () => {
256
282
ipcMain . on ( "sendCommand" , async ( event , args ) => {
257
283
const command = args . command ;
258
284
mainWindow . webContents . send ( "startLoader" ) ;
259
- // consolog("main266: " + command);
260
285
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 ) ) {
270
288
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
+ }
282
295
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 {
286
303
304
+ try {
305
+ await webR . evalR ( command ) ;
287
306
} catch ( error ) {
288
307
console . log ( error ) ;
289
308
throw error ;
290
309
}
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
+ }
291
335
}
292
336
293
337
mainWindow . webContents . send ( "clearLoader" ) ;
294
-
295
338
} ) ;
296
339
297
340
const inputOutput : interfaces . InputOutput = {
@@ -322,6 +365,3 @@ function consoletrace(x: any) {
322
365
process . on ( 'unhandledRejection' , ( error : Error , promise ) => {
323
366
consoletrace ( error ) ;
324
367
} ) ;
325
-
326
-
327
- app . commandLine . appendSwitch ( 'log-level' , '3' ) ;
0 commit comments