forked from deepstreamIO/deepstream.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepstream-start.ts
99 lines (90 loc) · 3.21 KB
/
deepstream-start.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as commander from 'commander'
import { EVENT } from '@deepstream/types'
// work-around for:
// TS4023: Exported variable 'command' has or is using name 'local.Command'
// from external module "node_modules/commander/typings/index" but cannot be named.
// tslint:disable-next-line: no-empty-interface
export interface Command extends commander.Command { }
export const start = (program: Command) => {
program
.command('start')
.description('start a deepstream server')
.option('-c, --config [file]', 'configuration file, parent directory will be used as prefix for other config files')
.option('-l, --lib-dir [directory]', 'path where to lookup for plugins like connectors and logger')
.option('--host <host>', 'host for the http service')
.option<number>('--port <port>', 'port for the http service', (value: string) => parseInteger('--port', value))
.option('--disable-auth', 'Force deepstream to use "none" auth type')
.option('--disable-permissions', 'Force deepstream to use "none" permissions')
.option<string>('--log-level <level>', 'Log messages with this level and above', parseLogLevel)
.option<boolean>('--colors [true|false]', 'Enable or disable logging with colors', (value: string) => parseBoolean('--colors', value))
.option('--inspect <url>', 'Enable node inspector')
.action(action)
}
function action () {
// @ts-ignore
global.deepstreamCLI = this
// @ts-ignore
const inspectUrl = global.deepstreamCLI.inspect
if (inspectUrl) {
const inspector = require('inspector')
// @ts-ignore
const [host, port] = global.deepstreamCLI.inspect.split(':')
if (!host || !port) {
throw new Error('Invalid inspect url, please provide host:port')
}
inspector.open(port, host)
}
const { Deepstream } = require('../src/deepstream.io')
try {
const ds = new Deepstream(null)
ds.on(EVENT.FATAL_EXCEPTION, () => process.exit(1))
ds.start()
process
.removeAllListeners('SIGINT').on('SIGINT', () => {
ds.on('stopped', () => process.exit(0))
ds.stop()
})
} catch (err) {
console.error(err.toString())
process.exit(1)
}
}
/**
* Used by commander to parse the log level and fails if invalid
* value is passed in
*/
function parseLogLevel (logLevel: string) {
if (!/debug|info|warn|error|off/i.test(logLevel)) {
console.error('Log level must be one of the following (debug|info|warn|error|off)')
process.exit(1)
}
return logLevel.toUpperCase()
}
/**
* Used by commander to parse numbers and fails if invalid
* value is passed in
*/
function parseInteger (name: string, port: string) {
const portNumber = Number(port)
if (!portNumber) {
console.error(`Provided ${name} must be an integer`)
process.exit(1)
}
return portNumber
}
/**
* Used by commander to parse boolean and fails if invalid
* value is passed in
*/
function parseBoolean (name: string, enabled: string) {
let isEnabled
if (typeof enabled === 'undefined' || enabled === 'true') {
isEnabled = true
} else if (typeof enabled !== 'undefined' && enabled === 'false') {
isEnabled = false
} else {
console.error(`Invalid argument for ${name}, please provide true or false`)
process.exit(1)
}
return isEnabled
}