This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.js
269 lines (214 loc) · 7.63 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
/* eslint-env node */
const environment = process.env.ELECTRON_ENV || process.env.EMBER_ENV || process.env.NODE_ENV || 'production';
process.env.NODE_ENV = environment;
process.env.EMBER_ENV = environment;
process.env.ELECTRON_ENV = environment;
if (typeof process.env.ELECTRON_IS_DEV === 'undefined') {
if (environment === 'development') {
process.env.ELECTRON_IS_DEV = 1;
}
}
const Promise = require('bluebird');
global.Promise = Promise;
const log = require('electron-log');
const unhandled = require('electron-unhandled');
const { is, appLaunchTimestamp } = require('electron-util');
log.transports.file.level = 'info';
log.transports.rendererConsole.level = 'info';
// Handle an unhandled error in the main thread
//
// Note that 'uncaughtException' is a crude mechanism for exception handling intended to
// be used only as a last resort. The event should not be used as an equivalent to
// "On Error Resume Next". Unhandled exceptions inherently mean that an application is in
// an undefined state. Attempting to resume application code without properly recovering
// from the exception can cause additional unforeseen and unpredictable issues.
//
// Attempting to resume normally after an uncaught exception can be similar to pulling out
// of the power cord when upgrading a computer -- nine out of ten times nothing happens -
// but the 10th time, the system becomes corrupted.
//
// The correct use of 'uncaughtException' is to perform synchronous cleanup of allocated
// resources (e.g. file descriptors, handles, etc) before shutting down the process. It is
// not safe to resume normal operation after 'uncaughtException'.
unhandled({
logger(...args) {
return log.error(...args);
},
});
// https://github.com/electron-archive/grunt-electron-installer#handling-squirrel-events
if (process.platform === 'win32') {
// eslint-disable-next-line global-require
if (require('electron-squirrel-startup')) {
return;
}
}
const path = require('path');
const fs = require('graceful-fs');
const del = require('del');
const semver = require('semver');
const locale2 = require('locale2');
const makeDir = require('make-dir');
const pathExists = require('path-exists');
const prettyMs = require('pretty-ms');
const electron = require('electron');
const debug = require('electron-debug');
const Store = require('electron-store');
const contextMenu = require('electron-context-menu');
const protocolServe = require('electron-protocol-serve');
const { default: installExtension, EMBER_INSPECTOR } = require('electron-devtools-installer');
const updateElectronApp = require('update-electron-app');
const { createWindow } = require('./window');
const { downloadStart, nodeStart } = require('./ipc');
const { version, productName } = require('../package');
const {
app,
ipcMain,
protocol,
autoUpdater,
} = electron;
let mainWindow = null;
const shouldQuit = !app.requestSingleInstanceLock();
if (shouldQuit) {
app.quit();
return;
}
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
const basePath = is.development ? __dirname : path.join(process.resourcesPath, 'app.asar.unpacked', 'ember-electron');
global.environment = environment;
global.dataPath = path.normalize(app.getPath('userData'));
global.resourcesPath = path.normalize(path.join(basePath, 'resources'));
global.locale = null;
global.isDataDownloaded = false;
global.isNodeStarted = false;
global.isQuitting = false;
global.isUpdating = false;
global.authorizationToken = null;
app.on('before-quit', () => {
global.isQuitting = true;
});
app.on('window-all-closed', () => {
if (!is.macos) {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow) {
mainWindow.show();
}
});
ipcMain.on('download-start', downloadStart);
ipcMain.on('node-start', nodeStart);
// Registering a protocol & schema to serve our Ember application
const protocolServeName = protocolServe({
app,
protocol,
cwd: path.join(__dirname || path.resolve(path.dirname('')), '..', 'ember'),
});
protocol.registerStandardSchemes([protocolServeName], { secure: true });
// Uncomment the lines below to enable Electron's crash reporter
// For more information, see http://electron.atom.io/docs/api/crash-reporter/
// electron.crashReporter.start({
// productName: 'YourName',
// companyName: 'YourCompany',
// submitURL: 'https://your-domain.com/url-to-submit',
// autoSubmit: true
// });
const run = async () => {
log.info(`Starting application: ${productName} ${version} (${environment})`);
await app.whenReady();
if (!is.development) {
autoUpdater.on('checking-for-update', () => {
log.info('Checking for update:', productName, '>', version);
global.isUpdating = false;
});
autoUpdater.on('update-available', () => {
log.info('Update available:', productName, '>', version);
});
autoUpdater.on('update-not-available', () => {
log.info('Update not available:', productName, '>', version);
});
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
log.info('Update downloaded:', productName, releaseName);
global.isUpdating = true;
});
updateElectronApp({
logger: log,
updateInterval: '30 minutes',
});
}
const store = new Store({ name: 'settings' });
if (!store.has('dataPath')) {
store.set('dataPath', global.dataPath);
}
let dataPath = path.normalize(store.get('dataPath'));
if (!path.isAbsolute(dataPath)) {
dataPath = path.resolve(path.relative(app.getPath('userData'), dataPath));
}
await makeDir(dataPath, { fs });
const storeVersion = store.get('version');
if (!storeVersion || semver.gt(version, storeVersion)) {
const outdatedAssets = ['config.json', 'log', 'tls'];
log.info('Deleting outdated assets:', outdatedAssets.join(', '));
await del(outdatedAssets, { force: true, cwd: dataPath });
}
store.set('version', version);
Object.defineProperty(global, 'dataPath', { value: dataPath });
Object.defineProperty(global, 'locale', {
get() {
return app.getLocale() || locale2 || null;
},
});
Object.defineProperty(global, 'locale', {
get() {
return app.getLocale() || locale2 || null;
},
});
const databasePath = path.join(dataPath, 'data.ldb');
Object.defineProperty(global, 'isDataDownloaded', {
get() {
return pathExists.sync(databasePath);
},
});
if (is.development) {
await installExtension(EMBER_INSPECTOR);
}
mainWindow = await createWindow();
mainWindow.on('unresponsive', () => {
log.warn('Application window has become unresponsive:', mainWindow.getTitle());
});
mainWindow.on('responsive', () => {
log.info('Application window has become responsive again:', mainWindow.getTitle());
});
mainWindow.once('ready-to-show', () => {
const elapsed = Date.now() - appLaunchTimestamp;
log.info(`Application window ready to show (took ${prettyMs(elapsed)}):`, mainWindow.getTitle());
mainWindow.show();
});
const emberAppLocation = 'serve://dist';
// Load the ember application using our custom protocol/scheme
mainWindow.loadURL(emberAppLocation);
// If a loading operation goes wrong, we'll send Electron back to
// Ember App entry point
mainWindow.webContents.on('did-fail-load', () => {
if (!mainWindow.isDestroyed()) {
mainWindow.loadURL(emberAppLocation);
}
});
mainWindow.webContents.on('crashed', () => {
log.error('Application in window has crashed:', mainWindow.getTitle());
});
mainWindow.on('closed', () => {
mainWindow = null;
});
return mainWindow;
};
debug({ showDevTools: true });
contextMenu();
module.exports = run();