-
Notifications
You must be signed in to change notification settings - Fork 882
/
Copy pathindex.js
125 lines (99 loc) · 2.79 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
const { app, ipcMain } = require('electron')
const fs = require('fs-extra')
const { join } = require('path')
const { ipfsNotRunningDialog } = require('../dialogs')
const store = require('../common/store')
const logger = require('../common/logger')
const { STATUS } = require('./consts')
const createDaemon = require('./daemon')
module.exports = async function (ctx) {
let ipfsd = null
let status = null
let wasOnline = null
const updateStatus = (stat, id = null) => {
status = stat
ipcMain.emit('ipfsd', status, id)
}
const getIpfsd = async (optional = false) => {
if (optional) {
return ipfsd
}
if (!ipfsd) {
await ipfsNotRunningDialog(ctx)
}
return ipfsd
}
const runAndStatus = (fn) => async () => {
await fn()
return status
}
const startIpfs = async () => {
if (ipfsd) {
return
}
const log = logger.start('[ipfsd] start daemon', { withAnalytics: 'DAEMON_START' })
const config = store.get('ipfsConfig')
updateStatus(STATUS.STARTING_STARTED)
const res = await createDaemon(config)
if (res.err) {
log.fail(res.err)
updateStatus(STATUS.STARTING_FAILED)
return
}
ipfsd = res.ipfsd
logger.info(`[daemon] IPFS_PATH: ${ipfsd.path}`)
logger.info(`[daemon] PeerID: ${res.id}`)
// Update the path if it was blank previously.
// This way we use the default path when it is
// not set.
if (!config.path || typeof config.path !== 'string') {
config.path = ipfsd.path
store.set('ipfsConfig', config)
}
log.end()
updateStatus(STATUS.STARTING_FINISHED, res.id)
}
const stopIpfs = async () => {
if (!ipfsd) {
return
}
const log = logger.start('[ipfsd] stop daemon', { withAnalytics: 'DAEMON_STOP' })
updateStatus(STATUS.STOPPING_STARTED)
if (!fs.pathExistsSync(join(ipfsd.path, 'config'))) {
// Is remote api... ignore
ipfsd = null
updateStatus(STATUS.STOPPING_FINISHED)
return
}
try {
await ipfsd.stop()
log.end()
updateStatus(STATUS.STOPPING_FINISHED)
} catch (err) {
logger.error(`[ipfsd] ${err.toString()}`)
updateStatus(STATUS.STOPPING_FAILED)
} finally {
ipfsd = null
}
}
const restartIpfs = async () => {
await stopIpfs()
await startIpfs()
}
ctx.startIpfs = runAndStatus(startIpfs)
ctx.stopIpfs = runAndStatus(stopIpfs)
ctx.restartIpfs = runAndStatus(restartIpfs)
ctx.getIpfsd = getIpfsd
ipcMain.on('ipfsConfigChanged', restartIpfs)
app.on('before-quit', async () => {
if (ipfsd) await stopIpfs()
})
await startIpfs()
ipcMain.on('online-status-changed', (_, isOnline) => {
if (wasOnline === false && isOnline && ipfsd) {
restartIpfs()
}
wasOnline = isOnline
})
}
module.exports.STATUS = STATUS