-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
317 lines (300 loc) · 9.39 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
class AppController {
constructor() {
this._fs = require('fs');
this._uuid = require('uuid');
this._axios = require('axios');
this.config = this._readConfig();
this.stores = this.config.stores;
}
async registerStore({name, key, password, url}) {
let shopData = await this._axios(`https://${key}:${password}@${url}/admin/api/2020-10/shop.json`);
let plus = shopData.data.shop.plan_name === "shopify_plus";
let data = arguments[0];
data.registered_at = new Date().getTime();
data.uuid = this._uuid.v4();
data.plus = plus;
this.stores.push(data);
this._writeConfig();
return data;
}
deregisterStore(uuid) {
this.config.stores = this.config.stores.filter(s => s.uuid !== uuid);
this.stores = this.config.stores;
this._writeConfig();
}
_writeConfig() {
this._fs.writeFileSync(path.join(app.getPath('userData'), `./app-config.json`), JSON.stringify(this.config));
}
_readConfig() {
if (this._fs.existsSync(path.join(app.getPath('userData'), `./app-config.json`))) return JSON.parse(this._fs.readFileSync(path.join(app.getPath('userData'), `./app-config.json`), {encoding: 'utf8'}));
else return {
"general": {
"no_data_threshold": 6,
"no_data_default_sleep": 5000,
"minimum_duration": 2000,
"gc_datapile_size": 500,
"sync_frequency": 8.64e8
},
"logging": {
"report_logs_to_console": true,
"report_network_logs": false
},
"queries": {
"cache_ttl": 3e5,
"use_caching": true,
"automatic_result_view": false
},
"stores": []
}
}
}
const path = require('path');
const {Menu, Tray, app, BrowserWindow, ipcMain, shell, dialog} = require('electron');
const electron_is_dev = require('electron-is-dev');
const controller = new AppController();
const singleInstanceLock = app.requestSingleInstanceLock();
if (!singleInstanceLock) return app.quit();
/**
* Setup and open windows
*/
let view, worker, tray, trueQuit = false;
app.whenReady().then(() => {
view = new BrowserWindow({
width: 800,
height: 600,
frame: false,
icon: path.join(__dirname, './view/assets/icons/icon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
})
if (electron_is_dev) view.webContents.openDevTools();
view.webContents.on('new-window', (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
view.loadFile(path.join(__dirname, './view/view.html'));
view.on('minimize', function (event) {
event.preventDefault();
view.hide();
return false;
});
view.on('close', function (event) {
if (trueQuit === true) return true;
event.preventDefault();
view.hide();
return false;
});
tray = new Tray(path.join(__dirname, process.platform === "darwin" ? './view/assets/icons/[email protected]' : './view/assets/icons/icon.png'));
let contextMenu = Menu.buildFromTemplate([
{
label: 'Close application', click: () => {
trueQuit = true;
worker.close();
view.close();
tray.destroy();
worker = null;
view = null;
tray = null;
}
}
]);
tray.on('click', () => {
view.show();
});
tray.setToolTip('Shop Databaser');
tray.setContextMenu(contextMenu);
worker = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
});
if (electron_is_dev) worker.webContents.openDevTools();
worker.loadFile(path.join(__dirname, 'worker/worker.html'));
});
app.on('window-all-closed', () => {
if (trueQuit === true) {
app.quit()
}
});
app.on('second-instance', () => {
view.show();
view.focus();
});
app.on('activate', (ev, hasWindow) => {
if (!hasWindow) view.show();
});
/**
* IPCs
*/
/**
* View is requesting a new thread to display query results in
*/
ipcMain.handle('new-terminal', async () => {
ipcMain.removeHandler('terminal-data-chunk');
let consoleParent = new BrowserWindow({
show: false,
frame: false,
webPreferences: {nodeIntegration: true}
});
await consoleParent.loadFile(path.join(__dirname, `./view/modals/result-viewer.html`));
await new Promise(r => {
consoleParent.once('ready-to-show', () => {
consoleParent.webContents.openDevTools({mode: 'detach'});
consoleParent.webContents.on('devtools-closed', () => {
ipcMain.removeHandler('terminal-data-chunk');
consoleParent.close();
});
r();
});
});
ipcMain.handle('terminal-data-chunk', (event, data) => {
consoleParent.webContents.send('console-load-data', data);
});
return;
});
/**
* View is requesting that the user select a location to save a json file
*/
ipcMain.handle('save-json', () => {
let result = dialog.showSaveDialogSync(view, {
title: 'Save query results to JSON file'
});
if (result) {
ModalProvider.serveModal({
width: 400,
height: 150,
parent: view,
content: path.join(__dirname, './view/modals/save-json.html'),
data: {
path: result
}
}).then(aborted => {
if (aborted) {
view.webContents.send('abort-save');
}
});
}
return result;
});
/**
* View is requesting that we connect a new store. We do so via 'controller', then send reload requests to view+worker
*/
ipcMain.handle('add-store', () => {
return ModalProvider.serveModal({
width: 400,
height: 300,
parent: view,
content: path.join(__dirname, './view/modals/add-store.html'),
}).then(store => {
if (store) {
controller.registerStore(store).then(r => {
worker.webContents.send('reload');
view.webContents.send('reload');
});
}
});
});
/**
* View is requesting that we disconnect a store. We show a modal for confirmation.
*/
ipcMain.handle('confirm-disconnect-store', (event, store) => {
return ModalProvider.serveModal({
width: 400,
height: 160,
parent: view,
content: path.join(__dirname, './view/modals/confirm-disconnect.html'),
data: {
name: store.name,
disk_usage: store.disk_usage
}
}).then(confirmed => {
if (confirmed) {
worker.webContents.send('deregister-store', store.uuid);
controller.deregisterStore(store.uuid);
}
return confirmed;
});
});
/**
* View is requesting that we re-sync a store. We show a modal for confirmation
*/
ipcMain.handle('confirm-resync-store', (event, store) => {
return ModalProvider.serveModal({
width: 400,
height: 160,
parent: view,
content: path.join(__dirname, './view/modals/confirm-resync.html'),
data: {
name: store.name
}
}).then(confirmed => {
if (confirmed) worker.webContents.send('force-resync', store.uuid);
return confirmed;
});
});
/**
* View/worker is requesting a list of currently-connected stores
*/
ipcMain.handle('get-stores', (event) => {
return controller.stores || [];
});
/**
* View is explicitly requesting a status update
*/
ipcMain.handle('get-status', () => {
worker.webContents.send('get-status');
});
/**
* View is sending a 'settings' update
*/
ipcMain.handle('update-config', (event, {type, key, value}) => {
controller.config[type][key] = value;
controller._writeConfig();
view.webContents.send('config-update', controller.config);
worker.webContents.send('config-update', controller.config);
});
/**
* View/worker is requesting appconfig
*/
ipcMain.handle('get-config', (event) => {
return controller.config;
});
/**
* data-task-manager will periodically send information about each task
* This handler forwards that information to the view for display to the user
*/
ipcMain.handle('status', (event, status) => {
view.webContents.send('status', status);
});
class ModalProvider {
static async serveModal({width, height, parent, content, data}) {
let modal = new BrowserWindow({
width: width,
height: height,
parent: parent,
modal: true,
frame: false,
webPreferences: {nodeIntegration: true}
});
modal.loadFile(path.join(__dirname, './view/modal-template.html'));
modal.once('ready-to-show', () => {
modal.webContents.send('modal-load-data', data);
modal.webContents.send('modal-load-contents', content);
modal.show();
});
modal.webContents.on('new-window', (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
return new Promise(resolve => {
ipcMain.handleOnce('modal-close-window', (event, returnValue) => {
modal.close();
resolve(returnValue);
});
});
}
}