2018-03-10 01:36:26 +03:00
|
|
|
require! <[ stream stream-snitch colors escape-string-regexp ]>
|
2018-03-09 05:56:58 +03:00
|
|
|
pty = require \pty.js
|
|
|
|
|
2018-03-28 01:02:51 +03:00
|
|
|
export ERROR = /((ford|warn): |\r\x1b\[K\/~)/
|
2018-03-09 05:56:58 +03:00
|
|
|
export class Urbit
|
|
|
|
(args)->
|
|
|
|
@stdout = process.stdout # overridable
|
|
|
|
@pty = pty.spawn \urbit args
|
|
|
|
@pty.on \data ~> @stdout.write it # TODO pipe?
|
2018-03-09 22:41:31 +03:00
|
|
|
console.log "FIXME Running Ubuntu 14.04, which causes a libtinfo version info warning. Should update to 16.04."
|
2018-03-09 05:56:58 +03:00
|
|
|
#
|
|
|
|
@last-output = Date.now()
|
|
|
|
@pty.on \data ~> @last-output = Date.now()
|
2018-03-09 22:32:17 +03:00
|
|
|
#
|
2018-03-10 01:36:26 +03:00
|
|
|
@reset-listeners!
|
2018-03-09 22:32:17 +03:00
|
|
|
process.on \exit ~> @pty.write '\04' # send EOF to gracefully checkpoint
|
2018-04-06 22:36:06 +03:00
|
|
|
@pty.on \exit (code,signal)~>
|
|
|
|
| code => process.exit code
|
|
|
|
| signal => process.exit 128 + signal # repack exit code
|
|
|
|
| _ => #TODO report if unexpected?
|
2018-03-09 05:56:58 +03:00
|
|
|
#
|
2018-03-10 01:49:49 +03:00
|
|
|
note: (...args)-> console.log "\n#{'_'*40}\nnode:".blue, ...args
|
|
|
|
warn: (...args)-> console.log "\n#{'!'*40}\nnode:".red, ...args
|
2018-03-09 05:56:58 +03:00
|
|
|
wait-silent: ~> # this feels hacky
|
|
|
|
new Promise (resolve)~>
|
|
|
|
a = set-interval ~>
|
2018-03-10 00:56:21 +03:00
|
|
|
if Date.now! > @last-output + 1000
|
2018-03-09 05:56:58 +03:00
|
|
|
clear-interval a
|
|
|
|
resolve @last-output
|
|
|
|
, 200
|
2018-03-10 01:24:00 +03:00
|
|
|
#
|
2018-03-10 01:36:26 +03:00
|
|
|
reset-listeners: ~>
|
|
|
|
@pty.socket.unpipe @listeners
|
|
|
|
@pty.socket.pipe (@listeners = new stream.PassThrough)
|
|
|
|
@
|
2018-03-10 01:25:43 +03:00
|
|
|
every: (re, cb)~>
|
2018-03-10 01:36:26 +03:00
|
|
|
@listeners.pipe (new stream-snitch re).on "match" cb
|
2018-03-09 05:56:58 +03:00
|
|
|
expect: (re)~>
|
|
|
|
new Promise (resolve)~>
|
2018-03-10 01:36:26 +03:00
|
|
|
@listeners.pipe (new stream-snitch re).once "match" resolve
|
2018-03-09 22:32:17 +03:00
|
|
|
expect-immediate: (re)->
|
|
|
|
Promise.race [
|
|
|
|
@expect re
|
|
|
|
@wait-silent!then -> throw Error "Expected #re during event"
|
|
|
|
]
|
2018-03-09 05:56:58 +03:00
|
|
|
#
|
2018-03-10 00:56:21 +03:00
|
|
|
line: (s)->
|
|
|
|
@pty.write s
|
2018-03-09 05:56:58 +03:00
|
|
|
<~ @wait-silent!then
|
|
|
|
@stdout.write "\n"
|
|
|
|
@pty.write "\r"
|
|
|
|
#
|
2018-03-09 22:32:17 +03:00
|
|
|
expect-echo: (s)-> #ALT send-and-expect
|
|
|
|
<~ @line s .then
|
2018-03-10 01:42:50 +03:00
|
|
|
@expect new RegExp escape-string-regexp s
|
2018-03-09 22:32:17 +03:00
|
|
|
#
|
2018-03-09 05:56:58 +03:00
|
|
|
exit: (code)->
|
|
|
|
@pty.on \exit -> process.exit code #REVIEW just return promise?
|
2018-03-09 22:32:17 +03:00
|
|
|
# @pty.write "\03" # ^C running event
|
2018-03-09 05:56:58 +03:00
|
|
|
@pty.write "\05\25" # ^E^U to clear prompt
|
|
|
|
@pty.write "\04" # ^D to checkpoint
|
2018-03-09 22:32:17 +03:00
|
|
|
# set-timeout # hard exit
|