forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
executable file
·157 lines (124 loc) · 3.5 KB
/
start.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
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env node
'use strict'
require('dotenv').config()
const path = require('path')
const fs = require('fs')
const assert = require('assert')
const updateNotifier = require('update-notifier')
const PinoColada = require('pino-colada')
const pump = require('pump')
const resolveFrom = require('resolve-from')
const isDocker = require('is-docker')
const listenAddressDocker = '0.0.0.0'
const watch = require('./lib/watch')
const parseArgs = require('./args')
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', 'start.txt'), 'utf8'))
return module.exports.stop()
}
function start (args, cb) {
let opts = parseArgs(args)
if (opts.help) {
return showHelp()
}
if (opts._.length !== 1) {
console.error('Missing the required file parameter\n')
return showHelp()
}
// we start crashing on unhandledRejection
require('make-promises-safe')
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
})
if (opts.watch) {
return watch(args, opts.ignoreWatch)
}
return runFastify(args, cb)
}
function stop (error) {
if (error) {
console.log(error)
process.exit(1)
}
process.exit()
}
function runFastify (args, cb) {
let opts = parseArgs(args)
opts.port = opts.port || process.env.PORT || 3000
cb = cb || assert.ifError
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.logLevel
},
pluginTimeout: opts.pluginTimeout
}
if (opts.bodyLimit) {
options.bodyLimit = opts.bodyLimit
}
if (opts.prettyLogs) {
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)
if (opts.address) {
fastify.listen(opts.port, opts.address, wrap)
} else if (opts.socket) {
fastify.listen(opts.socket, wrap)
} else if (isDocker()) {
fastify.listen(opts.port, listenAddressDocker, wrap)
} else {
fastify.listen(opts.port, wrap)
}
function wrap (err) {
cb(err, fastify)
}
return fastify
}
function cli (args) {
start(args)
}
module.exports = { start, stop, runFastify, cli }
if (require.main === module) {
cli(process.argv.slice(2))
}