This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun.js
141 lines (111 loc) · 2.96 KB
/
run.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
'use strict'
const assert = require('assert')
const fp = require('fastify-plugin')
const fs = require('fs')
const isDocker = require('is-docker')
const resolveFrom = require('resolve-from')
const parseArgs = require('./args/run')
const path = require('path')
const PinoColada = require('pino-colada')
const pump = require('pump')
const watch = require('./lib/watch')
let Fastify = null
function showHelp () {
fs.readFile(path.join(__dirname, 'help', 'start.txt'), 'utf8', (err, data) => {
if (err) {
module.exports.stop(err)
}
console.log(data)
module.exports.stop()
})
}
function stop (error) {
if (error) {
console.error(error)
process.exit(1)
}
process.exit()
}
function loadModules (opts) {
try {
const basedir = path.resolve(process.cwd(), opts.file)
Fastify = require(resolveFrom.silent(basedir, 'fastify') || 'fastify')
} catch (e) {
module.exports.stop(e)
}
}
function start (args, cb) {
const opts = parseArgs(args)
if (!fs.existsSync(opts.file)) {
console.error('Missing the required file index.js\n')
return showHelp()
}
if (opts.help) {
return showHelp()
}
loadModules(opts)
if (opts.watch) {
return watch(args)
}
return run(args, cb)
}
function run (args, cb) {
require('dotenv').config()
const 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.file))
} catch (e) {
return module.exports.stop(e)
}
if (file.length !== 3 && file.constructor.name === 'Function') {
return module.exports.stop(`Plugin function should contain 3 arguments. Refer to\n
docs for more information about it`)
}
if (file.length !== 2 && file.constructor.name === 'AsyncFunction') {
return module.exports.stop(`Aysnc/Await plugin function should contain 2 arguments. Refer to\n
docs for more information about it`)
}
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.option ? Object.assign(options, file.options) : options)
const pluginOptions = {}
if (opts.prefix) {
pluginOptions.prefix = opts.prefix
pluginOptions._routePrefix = opts.prefix || ''
}
fastify.register(fp(file), pluginOptions)
if (opts.address) {
fastify.listen(opts.port, opts.address, wrap)
} else if (isDocker()) {
fastify.listen(opts.port, '0.0.0.0', wrap)
} else {
fastify.listen(opts.port, wrap)
}
function wrap (err) {
cb(err, fastify)
}
return fastify
}
function cli (args) {
start(args)
}
module.exports = { start, cli, stop, run }
if (require.main === module) {
cli(process.argv.slice(2))
}