This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
178 lines (152 loc) · 5.3 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
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
const fs = require('fs')
const path = require('path')
const url = require('url')
const net = require('net')
const createRenderer = require('./electron-shell')
const { setupRpc } = require('./Rpc')
const { getMenuTemplate } = require('./Menu')
const { registerGlobalPluginHost } = require('./ethereum_clients/PluginHost')
const { registerGlobalAppManager } = require('./grid_apps/AppManager')
const { registerGlobalUserConfig } = require('./Config')
const is = require('./utils/main/util')
registerGlobalUserConfig()
const log = {
dev: require('debug')('dev'),
appManager: {
log: require('debug')('AppManager')
}
}
const { app, dialog, Menu } = require('electron')
const {
AppManager,
registerPackageProtocol
} = require('@philipplgh/electron-app-manager')
registerPackageProtocol()
const CONFIG_NAME = '.shell.config.js'
// hw acceleration can cause problem in VMs and in certain APIs
app.disableHardwareAcceleration()
const shellManager = new AppManager({
repository: 'https://github.com/ethereum/grid',
auto: true,
electron: true
})
AppManager.on('menu-available', updaterTemplate => {
const template = getMenuTemplate()
// replace old updater menu with new one
const idx = template.findIndex(mItem => mItem.label === 'Updater')
template[idx] = updaterTemplate
updaterTemplate.submenu.push({
id: 'check',
label: 'Check Update',
click: function() {
shellManager.checkForUpdatesAndNotify(true)
}
})
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
})
// TODO util
app.on('web-contents-created', (event, contents) => {
// https://electronjs.org/docs/tutorial/security#11-verify-webview-options-before-creation
contents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away preload scripts if unused or verify their location is legitimate
delete webPreferences.preload
delete webPreferences.preloadURL
console.log('will attach webview')
webPreferences.preload = path.join(__dirname, 'preload-webview')
// Disable Node.js integration
webPreferences.nodeIntegration = false
})
})
// Step 1 - configure and start user interface
const startWithDevConfig = async appUrl => {
const {
packagePath,
packageServer,
packageUrl,
packageVersion
} = require(`./${CONFIG_NAME}`)
// test if local path configured
if (packagePath && fs.existsSync(packagePath)) {
log.dev('load user provided package from fs:', packagePath)
appUrl = await appManager.load(packagePath)
}
// test if user provided server is defined and reachable
else if (packageServer) {
const { hostname, port } = url.parse(packageServer)
const isServerRunning = await checkConnection(hostname, port)
if (isServerRunning) {
log.dev('load user provided package url:', packageServer)
appUrl = packageServer
} else {
log.dev('user provided server unreachable:', packageServer)
}
}
// fallback to user defined package url
else if (packageUrl) {
throw Error('not implemented')
} else if (packageVersion) {
log.dev('load user provided version:', packageVersion)
const releases = await appManager.getReleases()
// console.log(releases.map(r => r.version).join(', '))
const release = releases.find(r => r.version === packageVersion)
if (release) {
appUrl = await appManager.hotLoad(release)
} else {
log.dev('user provided version not found')
}
}
// else: display error: module not found
mainWindow = createRenderer(appUrl)
}
const startUI = async () => {
let errorUrl = url.format({
slashes: true,
protocol: 'file:',
pathname: path.join(__dirname, 'public', 'error.html')
})
if (is.dev()) {
const template = getMenuTemplate()
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
// load user-provided package if possible
if (fs.existsSync(path.join(__dirname, CONFIG_NAME))) {
const { useDevSettings } = require(`./${CONFIG_NAME}`)
if (useDevSettings) {
return startWithDevConfig(errorUrl)
}
}
// else: no dev config found or deactivated -> use default = dev server
const PORT = '3080'
const appUrl = `http://localhost:${PORT}/index.html`
const isServerRunning = await checkConnection('localhost', PORT)
if (!isServerRunning) {
log.dev('dev server unreachable at:', appUrl)
dialog.showMessageBox({
title: 'Error',
message: 'Dev Server not running or unreachable at: ' + appUrl
})
return
}
// else: server running -> display app
mainWindow = createRenderer(appUrl)
return
}
// else is production:
const appUrl = 'package://github.com/ethereum/grid-ui'
mainWindow = createRenderer(appUrl)
return
/*
// else: no valid app url -> display error
mainWindow = createRenderer(errorUrl)
*/
}
// ########## MAIN APP ENTRY POINT #########
const onReady = async () => {
const pluginHost = registerGlobalPluginHost()
pluginHost.on('plugins-loaded', async () => {
// FIXME don't defer start
// 1. start UI for quick user-feedback without long init procedures
await startUI()
})
const appManager = registerGlobalAppManager()
}
app.once('ready', onReady)