Completed
Push — develop ( 2c5883...921cbe )
by William
41s
created

index.js ➔ filter   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ... ➔ ??? 0 3 1
1
/**
2
 * @file The main file. Controls most things
3
 * @author willyb321
4
 * @copyright MIT
5
 */
6
/**
7
 * @module Index
8
 */
9
console.time('Imports');
10
console.time('FullStart');
11
console.time('electron');
12
import electron, {Menu, dialog, ipcMain as ipc, shell} from 'electron';
13
console.timeEnd('electron');
14
console.time('path');
15
import path from 'path';
16
console.timeEnd('path');
17
console.time('os');
18
import os from 'os';
19
console.timeEnd('os');
20
console.time('autoupdater');
21
import {autoUpdater} from 'electron-updater';
22
console.timeEnd('autoupdater');
23
console.time('fs-extra');
24
import fs from 'fs-extra';
25
console.timeEnd('fs-extra');
26
console.time('isDev');
27
import isDev from 'electron-is-dev';
28
console.timeEnd('isDev');
29
console.time('bugsnag');
30
import bugsnag from 'bugsnag';
31
console.timeEnd('bugsnag');
32
console.time('about');
33
import openAboutWindow from 'about-window';
34
console.timeEnd('about');
35
console.time('winstate');
36
import windowStateKeeper from 'electron-window-state';
37
console.timeEnd('winstate');
38
console.time('readLog');
39
import {readLog} from '../lib/log-process';
40
console.timeEnd('readLog');
41
console.time('watchLog');
42
import {watchGood} from '../lib/watcher-process';
43
console.timeEnd('watchLog');
44
console.time('utils');
45
import {getMenuItem} from '../lib/utils';
46
console.timeEnd('utils');
47
console.time('debug');
48
require('electron-debug')();
49
console.timeEnd('debug');
50
console.timeEnd('Imports');
51
const app = electron.app;
52
bugsnag.register('2ec6a43af0f3ef1f61f751191d6bd847', {appVersion: app.getVersion(), sendCode: true});
53
let win;
54
export const currentData = {
55
	log: null,
56
	events: []
57
};
58
59
/** Autoupdater on update available */
60
autoUpdater.on('update-available', info => { // eslint-disable-line no-unused-vars
61
	dialog.showMessageBox({
62
		type: 'info',
63
		buttons: [],
64
		title: 'New update available.',
65
		message: 'Press OK to download the update, and the application will download the update and then tell you when its done. The version downloaded is: ' + info.version
66
	});
67
	win.loadURL(`file:///${__dirname}/../html/index.html`);
68
});
69
/** Autoupdater on downloaded */
70
autoUpdater.on('update-downloaded', () => { // eslint-disable-line no-unused-vars
71
	dialog.showMessageBox({
72
		type: 'info',
73
		buttons: [],
74
		title: 'Update ready to install.',
75
		message: 'The update is downloaded, and will be installed on quit.'
76
	});
77
});
78
/** Autoupdater if error */
79
autoUpdater.on('error', error => {
80
	dialog.showMessageBox({
81
		type: 'info',
82
		buttons: [],
83
		title: 'Update ready to install.',
84
		message: `Sorry, we've had an error. The message is ` + error
85
	});
86
	bugsnag.notify(error);
87
});
88
/**
89
 * @description Emitted on download progress.
90
 */
91
autoUpdater.on('download-progress', percent => {
92
	win.setProgressBar(percent.percent, {mode: 'normal'});
93
});
94
95
export const logPath = path.join(os.homedir(), 'Saved Games', 'Frontier Developments', 'Elite Dangerous');
96
97
// Prevent window being garbage collected
98
let mainWindow;
99
100
/**
101
 * @description Makes the main window
102
 */
103
function createMainWindow() {
104
	let mainWindowState = windowStateKeeper({ // eslint-disable-line prefer-const
105
		defaultWidth: 1280,
106
		defaultHeight: 720
107
	});
108
	win = new electron.BrowserWindow({
109
		show: false,
110
		x: mainWindowState.x,
111
		y: mainWindowState.y,
112
		width: mainWindowState.width,
113
		height: mainWindowState.height,
114
		backgroundColor: '#fff'
115
	});
116
	mainWindowState.manage(win);
117
	process.mainContents = win.webContents;
118
	win.on('closed', onClosed);
119
}
120
121
/**
122
 * Called by createMainWindow() on closing.
123
 */
124
function onClosed() {
125
	// Dereference the window
126
	// for multiple windows store them in an array
127
	mainWindow = null;
128
}
129
ipc.on('loadLog', () => {
130
	readLog();
131
});
132
ipc.on('watchLog', () => {
133
	watchGood(false);
134
	getMenuItem('Watch logs').checked = true;
135
});
136
/**
137
 * Called when app is ready, and checks for updates.
138
 */
139
app.on('ready', () => {
140
	mainWindow = createMainWindow();
141
	win.once('ready-to-show', () => {
142
		win.show();
143
	});
144
	fs.ensureDir(logPath, err => {
145
		if (err) {
146
			console.log(err);
147
		}
148
	});
149
	win.loadURL(`file:///${__dirname}/../html/index.html`);
150
	console.timeEnd('FullStart');
151
	// WatchGood();
152
	if (!isDev && process.env.NODE_ENV !== 'test') {
153
		autoUpdater.checkForUpdates();
154
	}
155
});
156
157
function filter() {
158
	ipc.on('filter', (event, args) => {
159
		console.log(args);
160
	});
161
}
162
163
/**
164
 * When all windows are closed, quit the app.
165
 */
166
app.on('window-all-closed', () => {
167
	if (process.platform !== 'darwin') {
168
		app.quit();
169
	}
170
});
171
172
/**
173
 * Save current log as HTML.
174
 */
175
function saveHTML() {
176
	if (currentData.log) {
177
		dialog.showSaveDialog({
178
			filters: [{
179
				name: 'HTML',
180
				extensions: ['html']
181
			}]
182
		}, fileName => {
183
			if (fileName === undefined) {
184
				console.log('You didn\'t save the file');
185
				return;
186
			}
187
			fs.writeFile(fileName, currentData.log, err => {
188
				if (err) {
189
					console.log(err);
190
				}
191
			});
192
		})
193
	}
194
}
195
196
/**
197
 * Open current loaded log in default program.
198
 */
199
function rawLog() {
200
	if (Array.isArray(process.loadfile) === true) {
201
		shell.openItem(process.loadfile[0]);
202
	} else if (typeof process.loadfile === 'string') {
203
		shell.openItem(process.loadfile);
204
	} else {
205
		console.log(process.loadfile);
206
	}
207
}
208
209
const template = [{
210
	label: 'File',
211
	submenu: [{
212
		label: 'Save as HTML',
213
		accelerator: 'CmdOrCtrl+S',
214
		click: saveHTML
215
	}, {
216
		label: 'Load',
217
		accelerator: 'CmdOrCtrl+O',
218
		click: readLog
219
	}, {
220
		label: 'Homepage',
221
		click: () => {
222
			currentData.log = null;
223
			win.loadURL(`file:///${__dirname}/../html/index.html`);
224
		}
225
	}, {
226
		label: 'Watch logs',
227
		accelerator: 'CmdOrCtrl+L',
228
		type: 'checkbox',
229
		id: 'checked',
230
		click(checked) {
231
			const stop = true;
232
			console.log(checked.checked);
233
			if (checked.checked === true) {
234
				watchGood(false);
235
			} else if (checked.checked === false) {
236
				watchGood(stop);
237
			}
238
		}
239
	}, {
240
		label: 'Open raw log',
241
		click: rawLog
242
	}]
243
}, {
244
	label: 'Edit',
245
	submenu: [{
246
		role: 'selectall'
247
	}]
248
}, {
249
	label: 'View',
250
	submenu: [{
251
		label: 'Reload',
252
		accelerator: 'CmdOrCtrl+R',
253
		click(focusedWindow) {
254
			if (focusedWindow) {
255
				win.reload();
256
			}
257
		}
258
	}, {
259
		role: 'togglefullscreen'
260
	}]
261
}, {
262
	role: 'window',
263
	submenu: [{
264
		role: 'minimize'
265
	}, {
266
		role: 'close'
267
	}]
268
}, {
269
	role: 'help',
270
	submenu: [{
271
		label: 'Learn More about Electron',
272
		click() {
273
			shell.openExternal('http://electron.atom.io');
274
		}
275
	}, {
276
		label: 'The Github Repo',
277
		click() {
278
			shell.openExternal('https://github.com/willyb321/elite-journal');
279
		}
280
	}, {
281
		label: 'What Version am I on?',
282
		click() {
283
			dialog.showMessageBox({
284
				type: 'info',
285
				buttons: [],
286
				title: 'Please load a file first',
287
				message: 'Current Version: ' + app.getVersion()
288
			});
289
		}
290
	}, {
291
		label: 'About',
292
		click: () => openAboutWindow({
293
			icon_path: path.join(__dirname, '..', 'icon.png'), // eslint-disable-line camelcase
294
			bug_report_url: 'https://github.com/willyb321/elite-journal/issues', // eslint-disable-line camelcase
295
			homepage: 'https://github.com/willyb321/elite-journal'
296
		})
297
	}
298
	]
299
}];
300
301
export const menu = Menu.buildFromTemplate(template);
302
Menu.setApplicationMenu(menu);
303