forked from ethereum/remix-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
178 lines (159 loc) · 4.93 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
const fs = require('fs')
const remixd = require('@remix-project/remixd/src')
const path = require('path')
const os = require('os')
const fetch = require('node-fetch')
const semver = require('semver')
const config = require('./config')
const { version } = require('./package.json')
const applicationMenu = require('./applicationMenu')
const { app, BrowserWindow, shell } = require('electron')
const { AppManager, registerPackageProtocol } = require('electron-app-manager')
const cacheDir = path.join(os.homedir(), '.cache_remix_ide')
registerPackageProtocol(cacheDir)
const remixIdeUrl = 'package://6fd22d6fe5549ad4c4d8fd3ca0b7816b.mod'
async function warnLatestVersion (current) {
const res = await fetch('https://api.github.com/repos/ethereum/remix-desktop/releases/latest')
let latest = (await res.json()).tag_name
latest = latest.indexOf('v') === 0 ? latest.replace('v', '') : latest
let ret = ''
console.log(latest, current)
if (semver.eq(latest, current)) {
console.log('\x1b[32m%s\x1b[0m', `[INFO] you are using the latest version ${latest}`)
ret = 'OK'
} else if (semver.gt(latest, current)) {
console.log('\x1b[33m%s\x1b[0m', `[WARN] latest version of remix-desktop is ${latest}, you are using ${current}`)
console.log('\x1b[33m%s\x1b[0m', `[WARN] please download the latest version:`)
console.log('\x1b[33m%s\x1b[0m', `[WARN] https://github.com/ethereum/remix-desktop/releases`)
ret = 'OUTDATED'
}
return ret
}
const updater = new AppManager({
repository: 'https://github.com/ethereum/remix-desktop',
auto: true,
electron: true
})
function createWindow () {
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false
},
icon: path.join(__dirname, 'build/icon.png')
})
win.webContents.on('new-window', function(e, url) {
e.preventDefault();
shell.openExternal(url);
})
win.loadURL('package://github.com/ethereum/remix-project')
// Modify the user agent for all requests to the following urls.
const filter = {
urls: ['https://*.dyn.plugin.remixproject.org/ipfs/*']
}
let hashes = {}
win.webContents.session.webRequest.onBeforeRequest(filter, (details, callback) => {
let { url } = details;
let reg = /dyn.plugin.remixproject.org\/ipfs\/(.*)/
let regResult = reg.exec(url)
const hash = regResult[1]
hashes[hash] = url
url = `http://localhost:5001/ipfs/${regResult[1]}`
callback({
cancel: false,
url: ( encodeURI(url ) )
})
})
win.webContents.session.webRequest.onErrorOccurred((details) => {
// console.error(details)
})
}
let sharedFolderClient = new remixd.services.sharedFolder()
let slitherClient = new remixd.services.SlitherClient()
let hardhatClient = new remixd.services.HardhatClient()
const services = {
hardhat: () => {
hardhatClient.options.customApi = {}
return hardhatClient
},
slither: () => {
slitherClient.options.customApi = {}
return slitherClient
},
folder: () => {
sharedFolderClient.options.customApi = {}
return sharedFolderClient
}
}
const setupApplicationMenu = async () => {
let status = ""
try {
status = await warnLatestVersion(version)
} catch (e) {
console.log('unable to verify latest version')
console.log(e)
}
applicationMenu(status === 'OUTDATED', cacheDir, app, (folder) => {
sharedFolderClient.sharedFolder(folder, false)
sharedFolderClient.setupNotifications(folder)
slitherClient.sharedFolder(folder)
hardhatClient.sharedFolder(folder)
})
}
// Similar object is also defined in websocket.ts
const ports = {
git: 65521,
hardhat: 65522,
slither: 65523,
folder: 65520
}
function startService (service, callback) {
try {
const socket = new remixd.Websocket(ports[service], { remixIdeUrl }, () => services[service]())
socket.start(callback)
} catch (e) {
console.error(e)
}
}
// return either current folder in client, or the one in cache or by default the os homedir
function getFolder(client) {
if(client.currentSharedFolder) return client.currentSharedFolder
const cache = config.read()
if(cache){
try {
const folder = cache.sharedFolder
if(fs.existsSync(folder)) return folder
}catch(e){
}
}
if (process.cwd()) {
return process.cwd()
} else
return os.homedir()
}
let remixdStart = () => {
console.log('start shared folder service')
try {
startService('folder', (ws, client) => {
client.setWebSocket(ws)
client.sharedFolder(getFolder(client))
client.setupNotifications(getFolder(client))
})
startService('slither', (ws, client) => {
client.setWebSocket(ws)
client.sharedFolder(getFolder(client))
})
startService('hardhat', (ws, client) => {
client.setWebSocket(ws)
client.sharedFolder(getFolder(client))
})
} catch (error) {
throw new Error(error)
}
}
app.on('ready', () => {
setupApplicationMenu()
remixdStart()
createWindow()
})