@@ -14,18 +14,17 @@ const production = process.env.NODE_ENV === 'production';
14
14
const development = process . env . NODE_ENV === 'development' ;
15
15
const OS_Windows = process . platform == 'win32' ;
16
16
17
- import { app , BrowserWindow , ipcMain , dialog , shell , Menu } from "electron" ;
17
+ import { app , BrowserWindow , ipcMain , dialog , shell , Menu , session } from "electron" ;
18
18
import * as path from "path" ;
19
19
import * as fs from "fs" ;
20
20
import * as webr from "webr" ;
21
21
import * as interfaces from './library/interfaces' ;
22
22
import { util } from "./library/helpers" ;
23
- import * as comm from "./communication" ;
24
23
25
24
26
25
const webR = new webr . WebR ( { interactive : false } ) ;
27
26
let mainWindow : BrowserWindow ;
28
- const root = production ? "../../" : "../" ;
27
+ // const root = production ? "../../" : "../";
29
28
30
29
async function mount ( dir : string ) {
31
30
webR . FS . unmount ( "/host" ) ;
@@ -36,45 +35,51 @@ async function mount(dir: string) {
36
35
{ root : dir } ,
37
36
"/host"
38
37
) ;
39
- return ( true )
40
38
} catch ( error ) {
41
- return false ;
39
+ console . log ( error ) ;
40
+ throw error ;
42
41
}
43
42
}
44
43
45
44
async function initWebR ( ) {
46
- await webR . init ( ) ;
47
-
48
- // mount a virtual filesystem containing contributed R packages
49
- const data = new Blob ( [
50
- fs . readFileSync (
51
- path . join ( __dirname , '../src/library/R/library.data' )
52
- )
53
- ] ) ;
54
-
55
- const metadata = JSON . parse (
56
- fs . readFileSync (
57
- path . join ( __dirname , '../src/library/R/library.js.metadata' ) ,
58
- 'utf-8'
59
- )
60
- ) ;
61
-
62
- const options = {
63
- packages : [ {
64
- blob : data ,
65
- metadata : metadata ,
66
- } ]
67
- } ;
68
-
69
- await webR . FS . mkdir ( '/my-library' ) ;
70
- await webR . FS . mount (
71
- "WORKERFS" ,
72
- options ,
73
- '/my-library'
74
- ) ;
75
-
76
- await webR . evalR ( `.libPaths(c(.libPaths(), "/my-library"))` ) ;
77
- await webR . evalR ( `library(DDIwR)` ) ;
45
+ try {
46
+ await webR . init ( ) ;
47
+
48
+ console . log ( __dirname ) ;
49
+
50
+ // mount a virtual filesystem containing contributed R packages
51
+ const data = new Blob ( [
52
+ fs . readFileSync (
53
+ path . join ( __dirname , '../src/library/R/library.data' )
54
+ )
55
+ ] ) ;
56
+
57
+ const metadata = JSON . parse (
58
+ fs . readFileSync (
59
+ path . join ( __dirname , '../src/library/R/library.js.metadata' ) ,
60
+ 'utf-8'
61
+ )
62
+ ) ;
63
+
64
+ const options = {
65
+ packages : [ {
66
+ blob : data ,
67
+ metadata : metadata ,
68
+ } ]
69
+ } ;
70
+
71
+ await webR . FS . mkdir ( '/my-library' ) ;
72
+ await webR . FS . mount (
73
+ "WORKERFS" ,
74
+ options ,
75
+ '/my-library'
76
+ ) ;
77
+
78
+ await webR . evalR ( `.libPaths(c(.libPaths(), "/my-library"))` ) ;
79
+ await webR . evalR ( `library(DDIwR)` ) ;
80
+ } catch ( error ) {
81
+ throw error ;
82
+ }
78
83
}
79
84
80
85
// Create the main browser window
@@ -110,15 +115,9 @@ function createWindow() {
110
115
111
116
}
112
117
113
- app . whenReady ( ) . then ( async ( ) => {
114
-
118
+ app . whenReady ( ) . then ( ( ) => {
115
119
createWindow ( ) ;
116
-
117
- try {
118
- await initWebR ( ) ;
119
- } catch ( error ) {
120
- console . error ( "Error during initialization:" , error ) ;
121
- }
120
+ initWebR ( ) ;
122
121
} ) ;
123
122
124
123
@@ -156,9 +155,9 @@ ipcMain.on("declared", () => {
156
155
} ) ;
157
156
158
157
159
- ipcMain . on ( "selectFileFrom" , async ( event , args ) => {
158
+ ipcMain . on ( "selectFileFrom" , ( event , args ) => {
160
159
if ( args . inputType === "Select file type" ) {
161
- comm . showError ( "Select input type" ) ;
160
+ dialog . showErrorBox ( "Error" , "Select input type" ) ;
162
161
} else {
163
162
const info = util . fileFromInfo ( args . inputType ) ;
164
163
@@ -171,7 +170,8 @@ ipcMain.on("selectFileFrom", async (event, args) => {
171
170
} ,
172
171
] ,
173
172
properties : [ "openFile" ] ,
174
- } ) . then ( ( result ) => {
173
+ } ) . then ( async ( result ) => {
174
+ // if (!result.canceled) {
175
175
if ( ! result . canceled ) {
176
176
inputOutput . fileFrom = result . filePaths [ 0 ] ;
177
177
@@ -184,20 +184,14 @@ ipcMain.on("selectFileFrom", async (event, args) => {
184
184
185
185
inputOutput . fileFromExt = ext ;
186
186
187
- mount ( inputOutput . fileFromDir ) . then ( ( result ) => {
188
- if ( result ) {
189
- // consolog(inputOutput);
190
- event . reply ( "selectFileFrom-reply" , inputOutput ) ;
191
- } else {
192
- comm . showError ( "Could not mount the directory" ) ;
193
- }
194
-
195
- } ) ;
196
-
197
187
if ( OS_Windows ) {
198
188
inputOutput . fileFrom = inputOutput . fileFrom . replace ( / \\ / g, '/' ) ;
199
189
inputOutput . fileFromDir = inputOutput . fileFromDir . replace ( / \\ / g, '/' ) ;
200
190
}
191
+
192
+ mount ( inputOutput . fileFromDir ) . then ( ( ) => {
193
+ mainWindow . webContents . send ( "selectFileFrom-reply" , inputOutput ) ;
194
+ } ) ;
201
195
}
202
196
} ) ;
203
197
@@ -211,7 +205,7 @@ ipcMain.on("outputType", (event, args) => {
211
205
212
206
ipcMain . on ( "selectFileTo" , ( event , args ) => {
213
207
if ( args . outputType === "Select file type" ) {
214
- comm . showError ( "Select output type" ) ;
208
+ dialog . showErrorBox ( "Error" , "Select output type" ) ;
215
209
} else {
216
210
const ext = util . getExtensionFromType ( args . outputType ) ;
217
211
@@ -239,11 +233,11 @@ ipcMain.on("selectFileTo", (event, args) => {
239
233
inputOutput . fileToDir = inputOutput . fileToDir . replace ( / \\ / g, '/' ) ;
240
234
}
241
235
242
- event . reply ( "selectFileTo-reply" , inputOutput ) ;
236
+ mainWindow . webContents . send ( "selectFileTo-reply" , inputOutput ) ;
243
237
}
244
238
} )
245
239
. catch ( ( err ) => {
246
- console . log ( err ) ;
240
+ consolog ( err ) ;
247
241
} ) ;
248
242
}
249
243
} ) ;
@@ -260,13 +254,20 @@ ipcMain.on("declared", () => {
260
254
261
255
// Handle the command request
262
256
ipcMain . on ( "sendCommand" , async ( event , args ) => {
263
- const command = args . command
264
- console . log ( command ) ;
257
+ const command = args . command ;
265
258
mainWindow . webContents . send ( "startLoader" ) ;
259
+ // consolog("main266: " + command);
260
+
266
261
try {
267
262
await webR . evalR ( command ) ;
268
- if ( args . variables ) {
263
+ } catch ( error ) {
264
+ console . log ( error ) ;
265
+ throw error ;
266
+ }
269
267
268
+ if ( util . isTrue ( args . updateVariables ) ) {
269
+ // consolog("main: updating variables");
270
+ try {
270
271
const result = await webR . evalR ( `as.character(jsonlite::toJSON(lapply(
271
272
collectRMetadata(dataset),
272
273
function(x) {
@@ -277,20 +278,20 @@ ipcMain.on("sendCommand", async (event, args) => {
277
278
}
278
279
)))` ) ;
279
280
280
- if ( ! webr . isRCharacter ( result ) ) {
281
- comm . showError ( 'Not a character!' ) ;
282
- }
281
+ if ( ! webr . isRCharacter ( result ) ) throw new Error ( 'Not a character!' ) ;
283
282
284
283
const response = await result . toString ( ) ;
285
- // consolog(JSON.parse(response[0] as string));
286
284
webR . destroy ( result ) ;
285
+ mainWindow . webContents . send ( "updateVariables" , JSON . parse ( response ) ) ;
287
286
288
- event . reply ( "sendCommand-reply" , JSON . parse ( response ) ) ;
287
+ } catch ( error ) {
288
+ console . log ( error ) ;
289
+ throw error ;
289
290
}
290
- } catch ( error ) {
291
- comm . showError ( String ( error ) ) ;
292
291
}
292
+
293
293
mainWindow . webContents . send ( "clearLoader" ) ;
294
+
294
295
} ) ;
295
296
296
297
const inputOutput : interfaces . InputOutput = {
@@ -307,6 +308,20 @@ const inputOutput: interfaces.InputOutput = {
307
308
fileToExt : ""
308
309
} ;
309
310
311
+
310
312
function consolog ( x : any ) {
311
313
mainWindow . webContents . send ( "consolog" , x ) ;
312
- }
314
+ }
315
+
316
+
317
+ function consoletrace ( x : any ) {
318
+ mainWindow . webContents . send ( "consoletrace" , x ) ;
319
+ }
320
+
321
+
322
+ process . on ( 'unhandledRejection' , ( error : Error , promise ) => {
323
+ consoletrace ( error ) ;
324
+ } ) ;
325
+
326
+
327
+ app . commandLine . appendSwitch ( 'log-level' , '3' ) ;
0 commit comments