forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·146 lines (120 loc) · 3.24 KB
/
cli.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env node
'use strict'
const path = require('path')
const fs = require('fs')
const assert = require('assert')
const updateNotifier = require('update-notifier')
const minimist = require('minimist')
const PinoColada = require('pino-colada')
const pump = require('pump')
const resolveFrom = require('resolve-from')
let Fastify = null
let fastifyPackageJSON = null
function loadModules (opts) {
try {
const basedir = path.resolve(process.cwd(), opts._[0])
Fastify = require(resolveFrom.silent(basedir, 'fastify') || 'fastify')
fastifyPackageJSON = require(resolveFrom.silent(basedir, 'fastify/package.json') || 'fastify/package.json')
} catch (e) {
module.exports.stop(e)
}
}
function showHelp () {
console.log(fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf8'))
return module.exports.stop()
}
function start (opts) {
if (opts.help) {
return showHelp()
}
if (opts._.length !== 1) {
console.error('Error: Missing the required file parameter\n')
return showHelp()
}
loadModules(opts)
const notifier = updateNotifier({
pkg: {
name: 'fastify',
version: fastifyPackageJSON.version
},
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
})
notifier.notify({
isGlobal: false,
defer: false
})
return runFastify(opts)
}
function stop (error) {
if (error) {
console.log(error)
process.exit(1)
}
process.exit()
}
function runFastify (opts) {
loadModules(opts)
var file = null
try {
file = require(path.resolve(process.cwd(), opts._[0]))
} catch (e) {
return module.exports.stop(e)
}
if (file.length !== 3 && file.constructor.name === 'Function') {
return module.exports.stop(new Error('Plugin function should contain 3 arguments. Refer to ' +
'documentation for more information.'))
}
if (file.length !== 2 && file.constructor.name === 'AsyncFunction') {
return module.exports.stop(new Error('Async/Await plugin function should contain 2 arguments.' +
'Refer to documentation for more information.'))
}
const options = {
logger: {
level: opts['log-level']
}
}
if (opts['pretty-logs']) {
const pinoColada = PinoColada()
options.logger.stream = pinoColada
pump(pinoColada, process.stdout, assert.ifError)
}
const fastify = Fastify(opts.options ? Object.assign(options, file.options) : options)
const pluginOptions = {}
if (opts.prefix) {
pluginOptions.prefix = opts.prefix
}
fastify.register(file, pluginOptions, assert.ifError)
if (opts.address) {
fastify.listen(opts.port, opts.address, assert.ifError)
} else if (opts.socket) {
fastify.listen(opts.socket, assert.ifError)
} else {
fastify.listen(opts.port, assert.ifError)
}
return fastify
}
function cli () {
start(minimist(process.argv.slice(2), {
integer: ['port'],
boolean: ['pretty-logs', 'options'],
string: ['log-level', 'address'],
alias: {
port: 'p',
socket: 's',
help: 'h',
options: 'o',
address: 'a',
prefix: 'r',
'log-level': 'l',
'pretty-logs': 'P'
},
default: {
port: 3000,
'log-level': 'fatal'
}
}))
}
module.exports = { start, stop, runFastify, cli }
if (require.main === module) {
cli()
}