This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
135 lines (110 loc) · 3.49 KB
/
index.ts
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
import dotenv from 'dotenv'
// @ts-ignore
const dotenvExpand = require('dotenv-expand')
const env = dotenv.config()
dotenvExpand(env)
import type HoprCoreConnector from '@hoprnet/hopr-core-connector-interface'
import type { HoprOptions } from '@hoprnet/hopr-core'
import Hopr from '@hoprnet/hopr-core'
import { clearString } from '@hoprnet/hopr-utils'
import chalk from 'chalk'
import readline from 'readline'
import PeerInfo from 'peer-info'
import clear from 'clear'
import { parseOptions, yesOrNoQuestion } from './utils'
import { Commands } from './commands'
import { renderHoprLogo } from './logo'
import pkg from './package.json'
export * as commands from './commands'
// Name our process 'hopr'
process.title = 'hopr'
let node: Hopr<HoprCoreConnector>
async function runAsRegularNode() {
let commands: Commands
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
// See readline for explanation of this signature.
completer: async (line: string, cb: (err: Error | undefined, hits: [string[], string]) => void) => {
let results = await commands.autocomplete(line)
cb(undefined, results)
}
})
commands = new Commands(node, rl)
rl.on('SIGINT', async () => {
const question = 'Are you sure you want to exit?'
const shouldTerminate = await yesOrNoQuestion(rl, question)
if (shouldTerminate) {
clearString(question, rl)
await commands.execute('quit')
return
}
rl.prompt()
})
rl.once('close', async () => {
await commands.execute('quit')
return
})
console.log(`Connecting to bootstrap node${node.bootstrapServers.length == 1 ? '' : 's'}...`)
rl.on('line', async (input: string) => {
if (input == null || input == '') {
rl.prompt()
return
}
let result = await commands.execute(input)
if (result) {
console.log(result)
}
rl.prompt()
})
rl.prompt()
}
function runAsBootstrapNode() {
console.log(`... running as bootstrap node!.`)
node.on('peer:connect', (peer: PeerInfo) => {
console.log(`Incoming connection from ${chalk.blue(peer.id.toB58String())}.`)
})
process.once('exit', async () => {
await node.down()
return
})
}
async function main() {
clear()
renderHoprLogo()
console.log(`Welcome to ${chalk.bold('HOPR')}!\n`)
let options: HoprOptions
try {
options = await parseOptions()
} catch (err) {
console.log(err.message + '\n')
return
}
console.log(`Chat Version: ${chalk.bold(pkg.version)}`)
console.log(`Core Version: ${chalk.bold(pkg.dependencies['@hoprnet/hopr-core'])}`)
console.log(`Utils Version: ${chalk.bold(pkg.dependencies['@hoprnet/hopr-utils'])}`)
console.log(`Connector Version: ${chalk.bold(pkg.dependencies['@hoprnet/hopr-core-connector-interface'])}\n`)
console.log(
`Bootstrap Servers: ${chalk.bold((options.bootstrapServers || []).map((x: PeerInfo) => x.id.toB58String()))}\n`
)
try {
node = await Hopr.create(options)
} catch (err) {
console.log(chalk.red(err.message))
process.exit(1)
}
console.log('Successfully started HOPR Chat.\n')
console.log(
`Your HOPR Chat node is available at the following addresses:\n ${node.peerInfo.multiaddrs.toArray().join('\n ')}\n`
)
console.log('Use the “help” command to see which commands are available.\n')
if (options.bootstrapNode) {
runAsBootstrapNode()
} else {
runAsRegularNode()
}
}
// If module is run as main (ie. from command line)
if (typeof module !== 'undefined' && !module.parent) {
main()
}