@@ -26,17 +26,28 @@ const webR = new webr.WebR({ interactive: false });
2626let 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
144153ipcMain . 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
158171ipcMain . 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
202219ipcMain . 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
206226ipcMain . 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", () => {
256282ipcMain . 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
297340const inputOutput : interfaces . InputOutput = {
@@ -322,6 +365,3 @@ function consoletrace(x: any) {
322365process . on ( 'unhandledRejection' , ( error : Error , promise ) => {
323366 consoletrace ( error ) ;
324367} ) ;
325-
326-
327- app . commandLine . appendSwitch ( 'log-level' , '3' ) ;
0 commit comments