-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
50 lines (41 loc) · 1.51 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
const Chalk = require('chalk')
const FriendDaemon = require('./lib/friends')
const repl = require('./repl')
module.exports = async function (opts) {
opts = opts || {}
console.log(Chalk.bold('...get by with a little help from your 👫👭👬'))
const ctx = await getInitialCtx()
console.log('Type "help" then <Enter> for a list of commands.')
return repl(ctx, opts)
}
async function getInitialCtx () {
const commands = require('./commands')
const createFriendDaemon = async ipfs => {
const friendDaemon = new FriendDaemon(ipfs)
friendDaemon
.on('message:share', ({ peerName, peerId, cid, shareName }) => {
console.log(`🎁 ${peerName} shared ${shareName} ${cid}`)
})
.on('message:shared', ({ peerName, peerId, shareName }) => {
console.log(`${peerName} got ${shareName} 🙌`)
})
.on('message:online', ({ peerName }) => {
console.log(`${peerName} is here ✨🎷🐩`)
})
.on('message:offline', ({ peerName }) => {
console.log(`${peerName} went away 👋`)
})
await friendDaemon.start()
return friendDaemon
}
const onConfigUpdate = async (key, value) => {
if (key === 'apiAddr') {
const ipfs = require('ipfs-api')(value)
const friendDaemon = await createFriendDaemon(ipfs)
return { ipfs, friendDaemon }
}
}
const apiAddr = (await commands.config({}, 'get', 'apiAddr')).out
const ctx = await onConfigUpdate('apiAddr', apiAddr)
return Object.assign(ctx, { commands, onConfigUpdate })
}