-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
348 lines (322 loc) · 9.2 KB
/
main.js
File metadata and controls
348 lines (322 loc) · 9.2 KB
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
'use strict';
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const ipcMain = electron.ipcMain;
const fs = require('fs');
const request = require('request');
const yauzl = require('yauzl');
const mkdirp = require('mkdirp');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
// test a request to endpoint provided later
function getDocsFromServer(endPoint, locationToPlaceRetrievedFiles, cb) {
let readStream = fs.createWriteStream(locationToPlaceRetrievedFiles)
request(endPoint).pipe(readStream)
readStream.on('close', function() {
if (cb) {cb()}
})
}
const endPoints = {
node: 'http://104.131.124.20:8080/node',
express: 'http://104.131.124.20:8080/express',
mdn_javascript: 'http://104.131.124.20:8080/mdn_javascript',
mdn_html: 'http://104.131.124.20:8080/mdn_html',
mdn_css: 'http://104.131.124.20:8080/mdn_css',
}
function createWindow() {
//if the render process requests a docset, we're going to want to check if we have.
// * if we do, we'll pass back the json file - render process transforms it into sidebar listItems!
// * if we don't...
// * query the endpoint associated with that file, storing the zip in docStorage
// * unZip the zip file, creating a sibling directory next to the zip file
// * read the new directories JSON file and send it back
ipcMain.on('reqDocset', (event, arg) => {
console.log(arg);
let data = JSON.parse(fs.readFileSync(__dirname + `/app/docStorage/${arg}.docs/index.json`))
event.sender.send('reqDocset', [arg, data.result]);
// if (arg === 'node') {
// let node_data = JSON.parse(fs.readFileSync(__dirname + '/app/docStorage/node.docs/index.json'))
// event.sender.send('reqDocset', ['node', node_data.result]);
// }
// if (arg === 'express') {
// let express_data = JSON.parse(fs.readFileSync(__dirname + '/app/docStorage/express.docs/index.json'))
// event.sender.send('reqDocset', ['express', express_data.result]);
// }
});
// try {
// fs.statSync('app/docStorage/'+arg+'.docs');
// fs.readdir('app/docStorage/'+arg+'.docs', (err, data) => {
// // throw(err)
// // if (err) console.log('right under readdir in try ',err)
// // if file exists => readFile
// console.log('file exists!!');
// fs.readFile(__dirname + '/app/docStorage/'+arg+'.docs/index.json', (err, data) => {
// console.log('file exists!!', err, data);
// if (err) {event.sender.send('reqDocset', [arg, 'An error occured while trying to grab ' + arg]) }
// else { event.sender.send('reqDocset', [arg, JSON.parse(data).result]) }
// })
// }) //end of no error and data exists
//
// }
// catch(e) {
// console.log('querying docs from API');
// getDocsFromServer(endPoints[arg], 'app/docStorage/' + arg + '.docs/', () => {
// console.log('attempting unzipping');
// unZipThisFile('app/docStorage/'+arg+'.zip', 'app/docStorage/', () => {
// console.log('unzipping succesful, reading new directory');
// fs.readFile(__dirname + '/app/docStorage/'+arg+'.docs/index.json', (err, data) => {
// if (err) {event.sender.send('reqDocset', [arg, 'An error occured while trying to grab '+arg ])}
// else {
// event.sender.send('reqDocset', [arg, JSON.parse(data).result])
// }
// })
// })//end of unZipThisFile
// }) //end of getDocsFromServer
// }
// if file doesn't exist => getDocs from server, unzip them, and then read the JSON file to send back.
// else {
//
// } //end of error and no data
//}) //end of readdir
// Create the browser window.
let windowOptions = {
minWidth: 1000,
width: 1000,
minHeight: 900,
height: 900,
titleBarStyle: 'hidden-inset',
fullscreen: true
}
mainWindow = new BrowserWindow(windowOptions);
mainWindow.maximize()
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/app/index.html');
// Open the DevTools.
//mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
initMenu();
}
// Menu
function initMenu() {var template = [
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
role: 'redo'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall'
},
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.reload();
}
},
{
label: 'Toggle Full Screen',
accelerator: (function() {
if (process.platform == 'darwin')
return 'Ctrl+Command+F';
else
return 'F11';
})(),
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
},
{
label: 'Toggle Developer Tools',
accelerator: (function() {
if (process.platform == 'darwin')
return 'Alt+Command+I';
else
return 'Ctrl+Shift+I';
})(),
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.toggleDevTools();
}
},
]
},
{
label: 'Window',
role: 'window',
submenu: [
{
label: 'Minimize',
accelerator: 'CmdOrCtrl+M',
role: 'minimize'
},
{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
role: 'close'
},
]
},
{
label: 'Help',
role: 'help',
submenu: [
{
label: 'Learn More',
click: function() { require('electron').shell.openExternal('http://electron.atom.io') }
},
]
},
];
if (process.platform == 'darwin') {
var name = require('electron').app.getName();
template.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
type: 'separator'
},
{
label: 'Services',
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide ' + name,
accelerator: 'Command+H',
role: 'hide'
},
{
label: 'Hide Others',
accelerator: 'Command+Alt+H',
role: 'hideothers'
},
{
label: 'Show All',
role: 'unhide'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: function() { app.quit(); }
},
]
});
// Window menu.
template[3].submenu.push(
{
type: 'separator'
},
{
label: 'Bring All to Front',
role: 'front'
}
);
}
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function() {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
// function to unzip and place file somewhere
function unZipThisFile(fileToUnzip, locationToPlaceUnZippedFiles, cb) {
yauzl.open(fileToUnzip, {
lazyEntries: true
}, function(err, zipfile) {
if (err) throw err
zipfile.readEntry()
zipfile.on("entry", function(entry) {
if (/\/$/.test(locationToPlaceUnZippedFiles + entry.fileName)) {
// directory file names end with '/'
mkdirp(locationToPlaceUnZippedFiles + entry.fileName, function(err) {
if (err) throw err
zipfile.readEntry()
if (cb) cb()
})
} else {
// file entry
zipfile.openReadStream(entry, function(err, readStream) {
if (err) throw err
// ensure parent directory exists
mkdirp(locationToPlaceUnZippedFiles + path.dirname(entry.fileName), function(err) {
if (err) throw err
readStream.pipe(fs.createWriteStream(locationToPlaceUnZippedFiles + entry.fileName))
readStream.on("end", function() {
zipfile.readEntry()
if (cb) cb()
})
})
})
}
})
})
}