-
Notifications
You must be signed in to change notification settings - Fork 865
/
index.js
292 lines (235 loc) · 6.42 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
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
288
289
290
291
292
import {Menubar} from 'electron-menubar'
import fs from 'fs'
import DaemonFactory from 'ipfsd-ctl'
import {join} from 'path'
import {dialog, ipcMain, app, BrowserWindow} from 'electron'
import config from './config'
import registerControls from './controls/main'
import handleKnownErrors from './errors'
const {debug} = config
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit()
}
// Ensure it's a single instance.
app.makeSingleInstance(() => {
debug('Trying to start a second instance')
dialog.showErrorBox(
'Multiple instances',
'Sorry, but there can be only one instance of IPFS Desktop running at the same time.'
)
})
// Local Variables
let IPFS
let menubar
let state = 'stopped'
function send (type, ...args) {
if (menubar && menubar.window && menubar.window.webContents) {
menubar.window.webContents.send(type, ...args)
}
}
config.send = send
config.ipfs = () => IPFS
function updateState (st) {
state = st
onRequestState()
}
function onRequestState (node, event) {
send('node-status', state)
}
// Moves files from appData/file-history.json to MFS so
// v0.4.0 is backwards compatible with v0.3.0.
function moveFilesOver () {
const path = join(config.appData, 'file-history.json')
if (!fs.existsSync(path)) {
return
}
let files
try {
files = JSON.parse(fs.readFileSync(path))
} catch (e) {
debug(e)
return
}
Promise.all(files.map((file) => IPFS.files.cp([`/ipfs/${file.hash}`, `/${file.name}`])))
.then(() => {
fs.unlinkSync(path)
})
.catch((e) => {
fs.unlinkSync(path)
debug(e)
})
}
function onStartDaemon (node) {
debug('Starting daemon')
updateState('starting')
// Tries to remove the repo.lock file if it already exists.
// This fixes a bug on Windows, where the daemon seems
// not to be exiting correctly, hence the file is not
// removed.
const lockPath = join(config.settingsStore.get('ipfsPath'), 'repo.lock')
const apiPath = join(config.settingsStore.get('ipfsPath'), 'api')
if (fs.existsSync(lockPath)) {
try {
fs.unlinkSync(lockPath)
} catch (e) {
debug('Could not remove lock. Daemon might be running.')
}
}
if (fs.existsSync(apiPath)) {
try {
fs.unlinkSync(apiPath)
} catch (e) {
debug('Could not remove API file. Daemon might be running.')
}
}
const flags = []
if (config.settingsStore.get('dhtClient')) {
flags.push('--routing=dhtclient')
}
node.start(flags, (err, api) => {
if (err) {
handleKnownErrors(err)
return
}
IPFS = api
debug('Daemon started')
config.events.emit('node:started')
if (node.subprocess) {
// Stop the executation of the program if some error
// occurs on the node.
node.subprocess.on('error', (e) => {
updateState('stopped')
debug(e)
})
}
// Move files from V0.3.0
moveFilesOver()
menubar.tray.setImage(config.logo.ice)
updateState('running')
})
}
function onStopDaemon (node, done) {
debug('Stopping daemon')
updateState('stopping')
config.events.emit('node:stopped')
node.stop((err) => {
if (err) {
return debug(err.stack)
}
debug('Stopped daemon')
menubar.tray.setImage(config.logo.black)
IPFS = null
updateState('stopped')
done()
})
}
function onWillQuit (node, event) {
debug('Shutting down application')
if (IPFS == null) {
return
}
event.preventDefault()
onStopDaemon(node, () => {
app.quit()
})
}
// Initalize a new IPFS node
function initialize (path, node) {
debug('Initialzing new node')
// Initialize the welcome window.
const window = new BrowserWindow({
title: 'Welcome to IPFS',
icon: config.logo.ice,
show: false,
resizable: false,
width: 850,
height: 450
})
// Only show the window when the contents have finished loading.
window.on('ready-to-show', () => {
window.show()
window.focus()
})
// Send the default path as soon as the window is ready.
window.webContents.on('did-finish-load', () => {
window.webContents.send('setup-config-path', path)
})
// Close the application if the welcome dialog is canceled
window.once('close', () => {
if (!node.initialized) app.quit()
})
window.setMenu(null)
window.loadURL(`file://${__dirname}/views/welcome.html`)
let userPath = path
ipcMain.on('setup-browse-path', () => {
dialog.showOpenDialog(window, {
title: 'Select a directory',
defaultPath: path,
properties: [
'openDirectory',
'createDirectory'
]
}, (res) => {
if (!res) return
userPath = res[0]
if (!userPath.match(/.ipfs\/?$/)) {
userPath = join(userPath, '.ipfs')
}
window.webContents.send('setup-config-path', userPath)
})
})
// Wait for the user to hit 'Install IPFS'
ipcMain.on('initialize', (event, { keySize }) => {
debug(`Initializing new node with key size: ${keySize} in ${userPath}.`)
window.webContents.send('initializing')
node.init({
directory: userPath,
keySize: keySize
}, (err, res) => {
if (err) {
return send('initialization-error', String(err))
}
config.settingsStore.set('ipfsPath', userPath)
send('initialization-complete')
updateState('stopped')
onStartDaemon(node)
window.close()
})
})
}
// main entry point
DaemonFactory.create().spawn({
repoPath: config.settingsStore.get('ipfsPath'),
disposable: false,
init: false,
start: false,
defaultAddrs: true
}, (err, node) => {
if (err) {
// We can't start if we fail to aquire
// a ipfs node
debug(err.stack)
process.exit(1)
}
let appReady = () => {
debug('Application is ready')
menubar.tray.setHighlightMode(true)
ipcMain.on('request-state', onRequestState.bind(null, node))
ipcMain.on('start-daemon', onStartDaemon.bind(null, node))
ipcMain.on('stop-daemon', onStopDaemon.bind(null, node, () => {}))
ipcMain.on('quit-application', app.quit.bind(app))
app.once('will-quit', onWillQuit.bind(null, node))
registerControls(config)
let exists = fs.existsSync(node.repoPath)
if (!exists) {
initialize(config.settingsStore.get('ipfsPath'), node)
} else {
onStartDaemon(node)
}
}
menubar = new Menubar(config.menubar)
config.menubar = menubar
if (menubar.isReady()) appReady()
else menubar.on('ready', appReady)
})