-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommandline.js
executable file
·69 lines (54 loc) · 1.71 KB
/
commandline.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
#!/usr/bin/env node
const printHelp = () => {
console.error(`
Assert, retract, or select facts from a living room server
Note the backslashes are important for escaping bash quoting
Assert a new fact
room assert "Gorog the barbarian is at (0.5, 0.7)"
Select a fact
room select "\$who the \$what is at (\$x, \$y)"
Retract a fact
room retract "Gorog the barbarian is at (0.5, 0.7)"
Subscribe to changes (press enter to exit)
room subscribe "\$who the \$what is at (\$x, \$y)"
`)
}
if (process.argv.length < 2) process.exit(printHelp())
import Room from '../src/room.js'
const room = new Room() // Defaults to http://localhost:3000
const facts = process.argv.slice(3)[0]
const verbose = ['--verbose', '-v'].some(
arg => process.argv.indexOf(arg) !== -1
)
async function main () {
switch (process.argv[2]) {
case 'assert':
return room.assert(facts).send().then(console.log)
case 'retract':
return room.retract(facts).send().then(console.log)
case 'select':
return room.select(facts).send().then(({assertions}) => console.dir(assertions))
case 'subscribe':
return new Promise(resolve => {
let delay = 100
const logWithDelayOnce = a => setTimeout(() => console.log(a), (delay = 0))
room.subscribe(facts, logWithDelayOnce)
.then(() => {
console.error(`subscribed to "${facts}"`)
console.error(`press any key to quit`)
})
process.stdin.on('data', resolve)
})
default:
return printHelp()
}
}
main()
.catch(err => {
let code = err.code || 'Error'
console.error(`${code}: ${err.message}`)
if (verbose) {
console.error(err.stack)
}
})
.then(process.exit)