This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
/
index.js
137 lines (112 loc) · 3.64 KB
/
index.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
const { app, Menu, MenuItem } = require('electron')
const { DialogUpdater, AppUpdater } = require('@philipplgh/electron-app-updater')
// interface of log, warn, error
const logger = console
const fs = require('fs')
const path = require('path')
const Module = require('module')
// hw acceleration can cause problem in VMs and in certain APIs
app.disableHardwareAcceleration()
// setup updater for shell
// setup updater for app.asar
/*
// TODO move to app code
const gethUpdater = new Updater({
// https://gethstore.blob.core.windows.net/builds?restype=container&comp=list
repo: 'https://gethstore.blob.core.windows.net',
auto: true,
interval: 60,
useDialog: true,
logger: logger
})
gethUpdater.checkForUpdates()
*/
const shellUpdater = new DialogUpdater({
repo: 'https://github.com/PhilippLgh/mist-react',
shell: true,
auto: false,
interval: 60,
logger: logger
})
const appUpdater = new DialogUpdater({
repo: 'https://github.com/ethereum/mist-ui-react',
hasMetadata: true,
auto: false,
interval: 10,
logger: logger
})
global.appUpdater = appUpdater
const is = {
dev: () => process.env.NODE_ENV && (process.env.NODE_ENV.trim() == 'development'),
prod:() => !is.dev()
}
const start = async () => {
if(is.dev()){
return
try {
let config = require('./config.json')
} catch (error) {
console.log('no config.json found')
process.exit(9)
}
// 1.) try to load .asar within build dir
// 2.) try to load from build dir
// 3.) try to load .asar from __dirname
// 4.) try to load remote
// require(path.join(config.repo, 'main.js'))
}
else if(is.prod()){
console.log('started in prod mode')
// cached updated apps are always more up-to-date than the packaged app -> try to load first
let cachedApp = await appUpdater.getCachedApp()
if(cachedApp){
let appPath = cachedApp.filePath
try {
// try to load from cache with fallback to packaged app
console.log('try starting app from cache')
loadApp(appPath)
} catch (error) {
console.log('starting cached app failed', error)
loadPackagedApp()
}
}
// if appPath does not exist or is invalid etc. fallback to packaged app
else{
loadPackagedApp()
}
}
}
start()
/*
* the "packaged" app is the one that was shipped with this shell / which was part of the installation bundle
* contrary: updated apps are downloaded and cached by the updater some time after installation and are not always available
*/
function loadPackagedApp(){
console.log('nothing cached or cache not working -> fallback to packaged app')
let files = fs.readdirSync(__dirname)
let asar = files.find(file => file.endsWith('.asar'))
if(!asar){
// fatal
throw new Error("Bad installation: application is missing or cannot be found")
}
loadApp(path.join(__dirname, asar))
}
function loadApp(fullAsarPath){
// TODO allow script to be renamed? use main from package.json?
let mainScript = path.join(fullAsarPath, 'main.js')
if(!fs.existsSync(mainScript)){
// fatal
throw new Error("Bad application: application has no entry point")
}
// start main script
Module._load(mainScript, Module, true)
}
// TODO remove and handle in app
app.on('ready', () => {
let menuNew = new Menu();
let subMenu = new Menu();
subMenu.append(new MenuItem({ label: 'Check Update App', click: appUpdater.checkForUpdates.bind(appUpdater) }))
subMenu.append(new MenuItem({ label: 'Check Update Shell', click: shellUpdater.checkForUpdates }))
menuNew.append(new MenuItem({ label: 'Updater', submenu: subMenu }));
Menu.setApplicationMenu(menuNew);
})