forked from rchain-community/repl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (39 loc) · 923 Bytes
/
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
const fs = require('fs')
const REPL = require('./lib/REPL')
// Library mode:
if (require.main !== module) {
module.exports = REPL
return REPL
}
const help = () => {
console.error('Usage: rnode-repl <program.rho> [host] [port]')
process.exit(1)
}
// CLI mode:
// Parse command-line arguments
if (process.argv.length < 2) {
help()
}
if (process.argv[0].indexOf('rnode-repl') < 0) {
process.argv.shift()
}
const file = process.argv[1] || help()
const host = process.argv[2] || 'localhost'
const port = process.argv[3] && parseInt(process.argv[3]) || 40404
// Get file contents
const program = fs.readFileSync(file, 'utf8')
if (!program.length) {
console.error('File is empty or unreadable')
return
}
// Run the program
const repl = new REPL(host, port)
repl.eval(program)
.then(output => {
console.log(output)
process.exit(0)
})
.catch(err => {
console.log(err)
process.exit(1)
})