-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
187 lines (160 loc) · 5.47 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
const { app, BrowserWindow, ipcMain, Menu, shell, dialog } = require('electron');
const path = require('path');
const url = require('url');
const log = require('electron-log');
const { autoUpdater } = require('electron-updater');
const jetpack = require('fs-jetpack');
function isDev() {
//TODO: check for isDev. Latest electron update breaks old
// return require.filename.indexOf('app.asar') === -1;
return false;
};
app.allowRendererProcessReuse = false
// Logger for autoUpdater
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
//needed for forcing update check in develop
// autoUpdater.forceDevUpdateConfig = true;
log.info('App starting...');
let win = null;
app.on('ready', function () {
// Initialize the window to our specified dimensions
win = new BrowserWindow({
width: 1300,
height: 1500,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
},
});
// win.maximize();
// Specify entry point
win.loadURL(url.format({
pathname: path.join(__dirname, 'dist/verifi/index.html'),
protocol: 'file',
slashes: true
}));
if (isDev()) {
win.toggleDevTools();
}
// Remove window once app is closed
win.on('closed', function () {
win = null;
});
//signal from core.component to check for update
ipcMain.on('ready', (coreCompEvent, arg) => {
if (!isDev()) {
autoUpdater.checkForUpdates().then(() => {
log.info('done checking for updates');
if (autoUpdater.updateInfoAndProvider) {
win.webContents.send('release-info', autoUpdater.updateInfoAndProvider.info);
}
});
autoUpdater.on('update-available', (event, info) => {
log.info('update available');
win.webContents.send('available', true);
});
autoUpdater.on('error', (event, error) => {
log.info('error');
win.webContents.send('error', error);
});
autoUpdater.on('update-downloaded', (event, info) => {
autoUpdater.quitAndInstall();
win.webContents.send('update-downloaded');
});
}
})
ipcMain.once('quit-and-install', (event, arg) => {
autoUpdater.quitAndInstall(false);
})
//Check for updates and install
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
var template = [{
label: "Application",
submenu: [
{ label: "Quit", accelerator: "Command+Q", click: function () { app.quit(); } },
{ label: "Dev Tools", accelerator: "CmdOrCtrl+I", click: function () { win.toggleDevTools(); } }
]
}, {
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
]
}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
win.setMenuBarVisibility(false)
win.webContents.on('new-window', function (e, url) {
// make sure local urls stay in electron perimeter
if ('file://' === url.substr(0, 'file://'.length)) {
return;
}
e.preventDefault();
shell.openExternal(url);
});
});
// Listen for message from application (electron component) to either download updates
ipcMain.once('update', (event, arg) => {
log.info('update')
autoUpdater.downloadUpdate();
});
ipcMain.once('later', (event, arg) => {
update = null;
});
ipcMain.once('relaunch', () => {
app.relaunch();
app.exit();
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
app.on('window-all-closed', function () {
app.quit();
});
ipcMain.on("saveData", (event, arg) => {
log.info('saveData called');
delete arg.fileData.account.dataBackupFilePath;
if (jetpack.exists(arg.fileName)) {
log.info('saved existing')
jetpack.writeAsync(arg.fileName, arg.fileData);
} else if (arg.isArchive) {
log.info('createArchiveFile')
jetpack.writeAsync(arg.fileName, arg.fileData);
} else if (arg.isCreateNewFile) {
log.info('createNewFile')
jetpack.writeAsync(arg.fileName, arg.fileData);
}
});
ipcMain.on("openDialog", (event, arg) => {
log.info('openDialog');
let saveDialogOptions = {
filters: [{
name: "JSON Files",
extensions: ["json"]
}],
defaultPath: arg.fileName
}
dialog.showSaveDialog(win, saveDialogOptions).then(results => {
win.webContents.send('file-path', results.filePath);
// log.info('save new')
// jetpack.writeAsync(results.filePath, arg.fileData);
});
});
ipcMain.on("fileExists", (event, arg) => {
log.info("check for data");
let results = jetpack.exists(arg.fileName);
win.webContents.send('file-exists', results);
})
ipcMain.on("getDataFile", (event, arg) => {
let dataFile = jetpack.read(arg.fileName, 'json');
win.webContents.send('data-file', dataFile);
});