Files: 7e467f6efa8f6320f4c92bbfbe6e905689e0b8eb / command.js
1376 bytesRaw
1 | const Url = require('url') |
2 | const muxrpcli = require('muxrpcli') |
3 | const defined = require('defined') |
4 | |
5 | const listen = require('./listen') |
6 | const connect = require('./connect') |
7 | |
8 | module.exports = command |
9 | |
10 | function command (services, config, options, argv) { |
11 | options = defined(options, {}) |
12 | argv = defined(argv, process.argv) |
13 | |
14 | const args = argv.slice(2) |
15 | if (args[0] === 'server') { |
16 | // special server command: |
17 | // start the server |
18 | options.onListen = onListen |
19 | return listen(services, config, options) |
20 | } else { |
21 | // normal command: |
22 | // create a client connection to the server |
23 | options.onConnect = onConnect |
24 | var client = connect(services, config, options) |
25 | return client |
26 | } |
27 | |
28 | function onListen (err) { |
29 | if (err) throw err |
30 | const url = (typeof options.url === 'string' |
31 | ? options.url |
32 | : Url.format(options.url) |
33 | ) || `http://localhost:${options.port}` |
34 | |
35 | console.log(`server listening at ${url}`) |
36 | } |
37 | |
38 | function onConnect (err, ws) { |
39 | if (err) { |
40 | if (err.code === 'ECONNREFUSED') { |
41 | console.log(`Error: Could not connect to the server at ${err.target.url}.`) |
42 | console.log('Use the "server" command to start it.') |
43 | if (options.verbose) throw err |
44 | process.exit(1) |
45 | } |
46 | throw err |
47 | } |
48 | |
49 | // run commandline flow |
50 | muxrpcli(args, client.manifest, client, options.verbose) |
51 | } |
52 | } |
53 |
Built with git-ssb-web