@@ -18,6 +18,9 @@ function createWindow() {
18
18
height : 600 ,
19
19
useContentSize : true ,
20
20
webPreferences : {
21
+ // JAIME: i tried adding these two below to make window.require('fs') work
22
+ // nodeIntegration: true,
23
+ // contextIsolation: false,
21
24
contextIsolation : true ,
22
25
// More info: https://v2.quasar.dev/quasar-cli-webpack/developing-electron-apps/electron-preload-script
23
26
preload : path . resolve ( __dirname , process . env . QUASAR_ELECTRON_PRELOAD ) ,
@@ -63,40 +66,95 @@ ipcMain.handle('showSaveDialog', async (event, options) => {
63
66
return { filePath } ;
64
67
} ) ;
65
68
66
- ipcMain . handle ( 'writeFile ' , async ( event , { filePath, data } ) => {
69
+ ipcMain . handle ( 'writeJSON ' , async ( event , { filePath, data } ) => {
67
70
fs . writeFileSync ( filePath , data ) ;
68
71
} ) ;
69
72
70
- // //handler for Import
73
+ //handler for Import
74
+
71
75
ipcMain . handle ( 'openProject' , async ( event , options ) => {
72
76
if ( mainWindow ) {
73
77
//{ filePaths } destructured comes fromdialog.showOpenDialog() function.
74
- const { filePaths } = await dialog . showOpenDialog ( mainWindow , options ) ;
75
- return { filePaths } ;
78
+ const { filePaths } = await dialog . showOpenDialog ( mainWindow , options ) ;
79
+
80
+ console . log ( "filePaths from main is" , filePaths ) ;
81
+ if ( ! filePaths ) return ;
82
+ const readData = await fs . readFileSync ( filePaths [ 0 ] , "utf8" ) ;
83
+ console . log ( "readData is" , readData ) ;
84
+ const jsonFile = JSON . parse ( readData ) ;
85
+ console . log ( "jsonFile is" , jsonFile ) ;
86
+ return { jsonFile } ;
76
87
}
88
+
89
+ //Kevin: I think calling readFileSync here in electron main is actually what we're supposed to do
90
+ //and then we send the received data back to Import
91
+ //because in here, readFileSync should be a function that has access to the res data
92
+
77
93
} ) ;
78
94
79
- //START: OLD import code block below
80
- // try {
81
- // if (mainWindow) {
82
- // const { filePaths } = await dialog.showOpenDialog(mainWindow, options);
83
- // console.log('Import dialog result:', filePaths); // Log the filePaths
84
- // if (filePaths.length > 0) {
85
- // const data = fs.readFileSync(filePaths[0], 'utf8');
86
- // return JSON.parse(data); // Return the imported data
87
- // }
88
- // } else {
89
- // console.error('Main window is undefined');
90
- // }
91
- // } catch (error) {
92
- // console.error('Failed to import project:', error);
93
- // }
94
- //END
95
-
96
-
97
- // //handler for Export
95
+
96
+
97
+
98
+ //handlers for Export
98
99
ipcMain . handle ( "exportProject" , async ( event , options ) => {
99
100
const { dialog } = require ( "electron" ) ;
100
101
const { filePath } = await dialog . showSaveDialog ( options ) ;
102
+ if ( filePath === '' ) {
103
+ throw new Error ( 'No file path selected' ) ;
104
+ }
101
105
return { filePath } ;
102
106
} ) ;
107
+
108
+ ipcMain . handle ( 'writeFile' , async ( event , filePath , content ) => { //possibly merge this with 'writeJSON' handle
109
+ // console.log('writeFile filePath:', filePath, '\n content:', content);
110
+ console . log ( "writeFile filePath:" , filePath ) ;
111
+ await fs . writeFile ( filePath , content , ( err ) => {
112
+ if ( err ) {
113
+ console . log ( `${ err } in fs.writeFile` ) ;
114
+ } else {
115
+ console . log ( "File written successfully" ) ;
116
+ }
117
+ } ) ;
118
+ console . log ( 'finished fs.writeSync' )
119
+ return { status : "success" } ;
120
+ // return new Promise(async (resolve, reject) => {
121
+ // await fs.writeFile(filePath, content, (err) => {
122
+ // if (err) {
123
+ // reject(err);
124
+ // } else {
125
+ // resolve("File written successfully");
126
+ // }
127
+ // });
128
+ // });
129
+ } ) ;
130
+
131
+ // ipcMain.handle('existSync',(event, { path }) => {
132
+ // return fs.existsSync(path)
133
+ // });
134
+
135
+ ipcMain . handle ( 'check-file-exists' , async ( event , path ) => {
136
+ const fileExists = await fs . existsSync ( path ) ;
137
+ console . log ( "fileExists" , fileExists ) ;
138
+
139
+ if ( fileExists ) return { status : true } ;
140
+ return { status : false } ;
141
+ } ) ;
142
+
143
+ ipcMain . handle ( 'mkdirSync' , async ( event , args : string [ ] ) => {
144
+ console . log ( 'about to make new directory' )
145
+ console . log ( "args" , args ) ;
146
+ const mkdirPath = await path . join ( ...args ) ;
147
+ console . log ( "mkdirPath" , mkdirPath ) ;
148
+ await fs . mkdirSync ( mkdirPath ) ;
149
+ console . log ( "finished making new directory" ) ;
150
+ return { status : 'success' } ;
151
+ } ) ;
152
+
153
+ ipcMain . handle ( 'pathJoin' , ( event , ...args : any [ ] ) => { //should expect args and output to be string
154
+ // for(const arg:any of [...args])
155
+ console . log ( 'pathJoin args:' , ...args ) ;
156
+ const newPath :string = path . join ( ...args ) ;
157
+ console . log ( 'newPath' , newPath )
158
+ return newPath ; //return string directly instead of object
159
+ } ) ;
160
+
0 commit comments