-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.js
executable file
·86 lines (73 loc) · 2.08 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
const async = require('async')
const bindAll = require('lodash/fp/bindAll')
const delay = require('lodash/fp/delay')
const isNil = require('lodash/fp/isNil')
const pick = require('lodash/fp/pick')
const OctoDash = require('octodash')
const WebSocket = require('ws')
const debug = require('debug')('skype-websocket:command')
const packageJSON = require('./package.json')
const Server = require('./src/server')
const CLI_OPTIONS = [
{
names: ['hostname'],
type: 'string',
required: true,
env: 'SKYPE_WEBSOCKET_HOSTNAME',
help: 'The host where to bind the server (Use 0.0.0.0 to listen to all incoming connections)',
helpArg: 'HOST',
default: 'localhost',
},
{
names: ['port'],
type: 'integer',
required: true,
env: 'SKYPE_WEBSOCKET_PORT',
help: 'Port to listen to incoming websockets on',
helpArg: 'PORT',
default: 52052,
},
]
class Command {
constructor({ argv, cliOptions = CLI_OPTIONS } = {}) {
bindAll(Object.getOwnPropertyNames(Command.prototype), this)
process.env.MESHBLU_CONNECTOR_CWD = process.cwd()
const octoDash = new OctoDash({ argv, cliOptions, name: packageJSON.name, version: packageJSON.version })
this.serverOptions = pick(['hostname', 'port'], octoDash.parseOptions())
}
restart() {
debug('restart')
async.series([this.stop, delay(5000), this.start, delay(5000), this.startSkype], (error) => {
debug('restarted', error)
if (error) return this.restart()
})
}
run() {
debug('run')
this.start((error) => {
debug('run -> start', error)
if (error) return this.restart()
})
}
start(callback) {
debug('start')
this.server = new Server(this.serverOptions)
this.server.once('error', this.restart)
this.server.start(callback)
}
startSkype(callback) {
this.server.autoLaunchSkype()
callback()
}
stop(callback) {
debug('stop')
if (isNil(this.server)) return callback()
this.server.stop(callback)
}
_onError(error) {
throw error
}
}
const command = new Command({ argv: process.argv })
command.run()