-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommand.js
52 lines (44 loc) · 1.34 KB
/
command.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
const Url = require('url')
const muxrpcli = require('muxrpcli')
const defined = require('defined')
const listen = require('./listen')
const connect = require('./connect')
module.exports = command
function command (services, config, options, argv) {
options = defined(options, {})
argv = defined(argv, process.argv)
const args = argv.slice(2)
if (args[0] === 'server') {
// special server command:
// start the server
options.onListen = onListen
return listen(services, config, options)
} else {
// normal command:
// create a client connection to the server
options.onConnect = onConnect
var client = connect(services, config, options)
return client
}
function onListen (err) {
if (err) throw err
const url = (typeof options.url === 'string'
? options.url
: Url.format(options.url)
) || `ws://localhost:${options.port}`
console.log(`server listening at ${url}`)
}
function onConnect (err, ws) {
if (err) {
if (err.code === 'ECONNREFUSED') {
console.log(`Error: Could not connect to the server at ${err.target.url}.`)
console.log('Use the "server" command to start it.')
if (options.verbose) throw err
process.exit(1)
}
throw err
}
// run commandline flow
muxrpcli(args, client.manifest, client, options.verbose)
}
}