-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
287 lines (253 loc) · 9.85 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
// Modules to control application life and create native browser window
import { app, BrowserWindow } from 'electron'
import { parseAddonDetails } from './src/parsePage'
import { checkWhichHost } from './src/checkHost'
import { installAddon } from './src/installAddon'
import { initConfig, readAddonList, saveToAddonList, saveToConfig } from './src/config'
import { checkUpdate } from './src/updateAddon'
import { integrityCheck } from './src/integrityCheck'
import { uninstallAddon } from './src/uninstallAddon'
import { checkWoAVersion } from './src/checkWoAVersion'
import os from 'os'
import path from 'path'
const chokidar = require('chokidar')
// 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.
export let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({ width: 1000, height: 700, icon: 'assets/500x500.png', 'web-preferences': { 'direct-write': false, 'subpixel-font-scaling': false } })
// and load the index.html of the app.
mainWindow.loadFile('dist/src-react/index.html')
// Open the DevTools.
if (process.env.ENV === 'dev') {
mainWindow.webContents.openDevTools()
} else {
mainWindow.setMenu(null)
}
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// 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
})
}
function initSubDirWatcher (configObj, installedAddonsDict) {
const subDirWatcher = chokidar.watch(configObj.addonDir, { // Watches wow Addon folder for new addons or deletions
ignored: /(^|[/\\])\../,
persistent: true,
depth: 0
})
subDirWatcher
.on('addDir', function (path) {
console.log('Addon subdir: ', path)
integrityCheck(installedAddonsDict, configObj) // Verifies that addon was installed
})
.on('unlinkDir', function (path) {
console.log('Addon deleted: ', path)
integrityCheck(installedAddonsDict, configObj) // Verifies that addon was uninstalled
})
.on('error', function (error) {
console.log('ERROR: ', error)
})
return subDirWatcher
}
function initInstallAddonsJsonWatcher (configObj, installedAddonsDict) {
const installedAddonsJsonWatcher = chokidar.watch(configObj.addonRecordFile, { persistent: true }) // Watches for changes in addons.json,
installedAddonsJsonWatcher.on('all', function () { // if there are changes then update the installAddonsDict variable.
readAddonList(configObj).then(value => {
console.log('Save/change detected in addons.json, updating installedAddonsDict')
installedAddonsDict = value
})
})
return installedAddonsJsonWatcher
}
// This function handles recording changes in an addon's install status and
// sending this information to be displayed
function recordAddonStatus (key, checkedStatus) {
if (installedAddonsDict[key].status !== checkedStatus) {
installedAddonsDict[key].status = checkedStatus
saveToAddonList(configObj, installedAddonsDict)
}
if (checkedStatus === 'INSTALLED') {
mainWindow.webContents.send('addonNoUpdate', {
'name': installedAddonsDict[key].name,
'status': 'NO_UPDATE'
})
}
if (checkedStatus === 'NEW_UPDATE') {
mainWindow.webContents.send('modAddonObj', installedAddonsDict[key])
}
}
// This function checks all addons for updates
function checkAllUpdates (installedAddonsDict) {
Object.keys(installedAddonsDict).forEach(function (key) {
checkUpdate(installedAddonsDict[key]).then(checkedStatus => {
recordAddonStatus(key, checkedStatus)
})
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// 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', () => {
// 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()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
// --- Initialization Start---
let configObj // JSON object that holds application config such as location of addon directory and installed addons file
let installedAddonsDict // Dictionary of all installed addons. Reference addons using "name" as key
let installedAddonsJsonWatcher
let subDirWatcher
initConfig()
.then(value => {
configObj = value // Sets config settings
return configObj
})
.then(configObj => {
console.log(configObj)
readAddonList(configObj).then(val => {
installedAddonsDict = val
return val
})
installedAddonsJsonWatcher = initInstallAddonsJsonWatcher(configObj, installedAddonsDict)
})
.then(val => {
subDirWatcher = initSubDirWatcher(configObj, installedAddonsDict)
})
// --- Initialization End---
const { ipcMain } = require('electron')
// newURL listener
ipcMain.on('newURL', (e, newURL) => {
console.log('Received new url ' + newURL)
console.log('\tSending url to be matched with host and parse addon page')
const URLObj = checkWhichHost(newURL)
parseAddonDetails(URLObj).then(addonObj => {
if (!installedAddonsDict.hasOwnProperty(addonObj.name)) {
mainWindow.webContents.send('modAddonObj', addonObj)
}
}).catch((err) => {
mainWindow.webContents.send('error', err)
})
})
// installAddon() listener
ipcMain.on('installAddon', (e, addonObj) => {
console.log('Received request to install addon ' + addonObj.name)
installAddon(addonObj, configObj.addonDir)
.then((newAddon) => {
installedAddonsDict[newAddon.name] = newAddon
integrityCheck(installedAddonsDict, configObj)
})
})
// update addon listener, fetches new version number and downloads the addon
ipcMain.on('installUpdate', (e, addonObj) => {
console.log('Received request to update addon ' + addonObj.name)
const URLObj = checkWhichHost(addonObj.url)
parseAddonDetails(URLObj).then(addonObj => {
installAddon(addonObj, configObj.addonDir)
.then((newAddon) => {
installedAddonsDict[newAddon.name] = newAddon
integrityCheck(installedAddonsDict, configObj)
})
})
})
// update all addons that need updating listener
ipcMain.on('updateAll', (e, notUpdated) => {
notUpdated.forEach(function (addonObj) {
const URLObj = checkWhichHost(addonObj.url)
parseAddonDetails(URLObj).then(addonObj => {
installAddon(addonObj, configObj.addonDir).then((newAddon) => {
installedAddonsDict[newAddon.name] = newAddon
integrityCheck(installedAddonsDict, configObj)
})
})
})
})
// checkAddonUpdate() listener
ipcMain.on('checkAddonUpdate', (e, addonObj) => {
console.log(`Received request to check ${addonObj.name} for updates`)
checkUpdate(addonObj).then(checkedStatus => {
recordAddonStatus(addonObj.name, checkedStatus)
})
})
// Check all addons for update listener
ipcMain.on('checkAll', (e, notChecked) => {
notChecked.forEach(function (addonObj) {
checkUpdate(addonObj).then(checkedStatus => {
recordAddonStatus(addonObj.name, checkedStatus)
})
})
})
// error listener
ipcMain.on('error', (e, errorObj) => {
console.log('\tSending error message ' + errorObj.error)
mainWindow.webContents.send('error', errorObj)
})
// uninstall addon listener
ipcMain.on('uninstallAddon', (e, addonObj) => {
console.log(`Received request to delete ${addonObj.name}`)
if (addonObj.status === '' || addonObj.status === 'NOT_INSTALLED') {
mainWindow.webContents.send('delAddonObj', addonObj)
} else {
mainWindow.webContents.send('modAddonObj', {
'displayName': addonObj.displayName,
'name': addonObj.name,
'version': addonObj.version,
'host': addonObj.host,
'url': addonObj.url,
'authors': addonObj.authors,
'status': 'NOT_INSTALLED'
})
installedAddonsDict = uninstallAddon(addonObj, configObj, installedAddonsDict)
saveToAddonList(configObj, installedAddonsDict)
}
})
ipcMain.on('getSettings', (e) => {
mainWindow.webContents.send('modSettings', configObj)
checkWoAVersion().then(value => {
mainWindow.webContents.send('latestVersion', value)
})
})
ipcMain.on('newSettings', (e, newConfig) => {
const homedir = os.homedir() // Fetchs user's homedir
const worldOfAddonsDir = path.join(homedir, 'WorldOfAddons') // World of Addons stores information in user's home dir
const WoAConfig = path.join(worldOfAddonsDir, 'config.json') // Saves all config information in config.json
if (configObj.addonRecordFile !== newConfig.addonRecordFile) {
installedAddonsJsonWatcher.close()
readAddonList(newConfig).then(val => {
installedAddonsDict = val
mainWindow.webContents.send('addonList', installedAddonsDict)
installedAddonsJsonWatcher = initInstallAddonsJsonWatcher(newConfig, installedAddonsDict)
})
}
if (configObj.addonDir !== newConfig.addonDir) {
subDirWatcher.close()
subDirWatcher = initSubDirWatcher(newConfig, installedAddonsDict)
}
configObj = newConfig
mainWindow.webContents.send('modSettings', configObj)
saveToConfig(WoAConfig, configObj)
})
// need to wait for react to finishing building Dom
ipcMain.on('windowDoneLoading', () => {
mainWindow.webContents.send('addonList', installedAddonsDict) // Note: Soft race-condition, Window can be done loading before addons.json is read
mainWindow.webContents.send('modSettings', configObj)
if (configObj.checkUpdateOnStart === true) {
checkAllUpdates(installedAddonsDict)
}
})