-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.js
367 lines (317 loc) · 11 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const { app, BrowserWindow, dialog, Menu, shell } = require('electron');
const path = require('path');
const ipcMain = require('electron').ipcMain;
const archiver = require('archiver');
const fs = require('fs');
const buildExportTestSuite = require('./src/utils/buildExportTestSuite.js');
const pgQuery = require('./src/pg-import/pgQuery.js')
// Global reference of the window object to avoid JS garbage collection
// when window is created
let win;
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600,
minHeight: 600,
webPreferences: {
nodeIntegration: true
},
// this is only for Windows and Linux
icon: path.join(__dirname, 'public/assets/pictures/ProtoGraphQLLogo64.png')
});
win.setMinimumSize(265, 630);
//Maximize browser window
win.maximize();
// Serve our index.html file
// mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`);
win.loadFile('index.html');
// Add event listener to set our global window variable to null
// This is needed so that window is able to reopen when user relaunches the app
win.on('closed', () => {
//added to overwrite .env with null every time window is closed so new import can work
fs.writeFileSync(path.join(__dirname, '/.env'), "", 'utf8');
win = null;
});
}
// Creates our window when electron has initialized for the first time
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', () => {
if (win === null) createWindow();
});
//---------------------APOLLO SERVER EXPORT ------------------------//
// Overwrite default Apollo Server code files
const createApolloFile = (filePath, data) => {
try {
fs.writeFileSync(path.join(__dirname, `/apollo-server/${filePath}`), data, 'utf8');
} catch (err) {
return console.error(err);
}
}
//function to run when user clicks export
function showExportDialog(event, gqlSchema, gqlResolvers, sqlScripts, env, queries) {
dialog.showOpenDialog(
{
title: 'Choose location to save folder in',
defaultPath: app.getPath('desktop'),
message: 'Choose location to save folder in',
properties: ['openDirectory']
},
result => {
//if user closes dialog window without selecting a folder
if (!result) return;
// creates the files in the apollo-sever folder
createApolloFile('graphql/schema.js', gqlSchema);
createApolloFile('graphql/resolvers.js', gqlResolvers);
createApolloFile('db/createTables.sql', sqlScripts);
createApolloFile('.env', env);
//generate tests
createApolloFile('tests/tests.js', buildExportTestSuite.createTest(queries[0], queries[1]));
const output = fs.createWriteStream(result + '/apollo-server.zip',
// { autoClose: false }
);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function (err) {
if (err.code === 'ENOENT') console.error(err)
else throw err;
});
archive.on('error', function (err) {
throw err;
});
// append files from apollo-server directory and naming it `apollo-server` within the archive
archive.directory(__dirname + '/apollo-server/', 'apollo-server');
// pipe the archive details to our zip file -> pushes files into the zip
archive.pipe(output);
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close' will be fired afterwards
archive.finalize();
// listen for all archive data to be written and output associated details
output.on('close', function () {
console.log('Zip file size is ', archive.pointer() + ' total bytes');
console.log('Archived zip file is complete.');
//reverts templates to empty files for future use
createApolloFile('graphql/schema.js', '');
createApolloFile('graphql/resolvers.js', '');
createApolloFile('db/createTables.sql', '');
createApolloFile('.env', '');
createApolloFile('tests/tests.js','')
dialog.showMessageBox(win,
{
type: "info",
buttons: ["Ok"],
message: "Export Successful!",
detail: 'File saved to ' + result + '/apollo-server.zip'
}
)
});
}
);
}
//The function to create the test file
const createTestFile = (filePath, data) => {
try {
//write to a file and replace if it already exists
fs.writeFileSync(path.join(__dirname, `/tests/${filePath}`), data, 'utf8');
} catch (err) {
return console.error(err);
}
}
//---------------------TEST EXPORT -------------------//
//function to run when user clicks "Export Tests"
function showTestExportDialog(event, queries) {
dialog.showOpenDialog(
{
title: 'Choose location to save file',
defaultPath: app.getPath('desktop'),
message: 'Choose location to save file',
properties: ['openDirectory']
},
result => {
//if user closes dialog window without selecting a folder
if (!result) return;
// creates the files in the tests folder
// createTestFile('.env', env);
//generate tests
console.log(queries);
createTestFile('tests.js', buildExportTestSuite.createTest(queries[0], queries[1]));
//make a new output function that is an fs WRITE with the same data.
fs.writeFile(result + '/tests.js', buildExportTestSuite.createTest(queries[0], queries[1]),function (err) {
if (err) throw err;});
createTestFile('tests.js','')
dialog.showMessageBox(win,
{
type: "info",
buttons: ["Ok"],
message: "Export Successful!",
detail: 'File saved to ' + result + '/tests.js'
}
)
});
}
//listener for Apollo Server export button being clicked
ipcMain.on('show-export-dialog', (event, gqlSchema, gqlResolvers, sqlScripts, env, queries) => {
console.log('show-export-dialog => ', queries);
showExportDialog(event, gqlSchema, gqlResolvers, sqlScripts, env, queries);
});
//listener for test export button being clicked
ipcMain.on('show-test-export-dialog', (event, queries) => {
console.log('show-test-export-dialog => ', queries);
showTestExportDialog(event, queries);
});
//--------------------- IMPORT POSTGRES TABLES -----------------//
// function importTables(event, gqlSchema, gqlResolvers, sqlScripts, env){
// pgQuery();
// }
ipcMain.on('import-tables', (event, env) => {
let tables;
pgQuery(tables)
.then((tables) => {
// console.log("tables (main):", tables)
event.reply('tables-imported', tables)
})
.catch(err => console.error("Error importing tables from postgres"))
})
//--------------------- CREATE ENV FILE -------------------//
async function createEnvFile(env) {
try {
fs.writeFileSync(path.join(__dirname, '/.env'), env, 'utf8')
} catch (err) {
return console.error(err);
}
}
ipcMain.on('create-env-file', (event, env) => {
console.log('create env file URI: ', env);
createEnvFile(env)
.then(res => {
event.reply('env-file-created');
console.log('env-file-created')
})
.catch(err => {
console.log('error occurred in createEnvFile promise')
})
});
//--------------------- MENU CUSTOMIZATION -------------------//
// customizes the about option in the menu bar
const originalTeam = 'Alena Budzko, Bryan Fong, Rodolfo Guzman, Jarred Jack Harewood, Geoffrey Lin';
const contributors = 'Haris Hambasic, Michelle Moody, Jessica Vaughan, Vance Wallace';
app.setAboutPanelOptions({applicationName: 'ProtoGraphQL', applicationVersion: '2.0', copyright: 'MIT License', credits: `Original Team\n${originalTeam}\n\nAdditional Contributors\n${contributors}`, website: 'https://github.com/oslabs-beta/protographql', iconPath: './public/assets/pictures/icon/icon.png'});
// set options for custom menu feature
const protographqlHelp = {
buttons: ['OK'],
message: 'Add Table\nCreate tables that mimic PSQL tables\n\nSchema\nView, edit or delete tables\n\nCode\nView generated GraphQL and SQL code before export\n\nVisualize\nView the GraphQL schema intuitively as a simple tree\n\nExport\nExport project to interact with database',
title: 'ProtoGraphQL Help',
type: 'info',
icon: './public/assets/pictures/icon/icon.png',
normalizeAccessKeys: true,
}
// the template and functions required to customize the menu bar - use the following format to add functionality { label: 'Swim', click () { 'clicked swim'}},
const template = [
// { role: 'appMenu' }
...(process.platform === 'darwin' ? [{
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}] : []),
// { role: 'fileMenu' }
{
label: 'File',
submenu: [
process.platform === 'darwin' ? { role: 'close' } : { role: 'quit' },
]
},
// { role: 'editMenu' }
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
...(process.platform === 'darwin' ? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startspeaking' },
{ role: 'stopspeaking' }
]
}
] : [
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
])
]
},
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
// { role: 'windowMenu' }
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
...(process.platform === 'darwin' ? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }
])
]
},
{
role: 'help',
submenu: [
{ label: 'ProtoGraphQL Help', click () { dialog.showMessageBox(protographqlHelp) }},
{
label: 'ProtoGraphQL on GitHub',
click: async () => {
await shell.openExternal('https://github.com/oslabs-beta/protographql')
}
},
{ type: 'separator' },
{
label: 'Learn More About Electron',
click: async () => {
await shell.openExternal('https://electronjs.org')
}
}
]
}
]
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);