forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-routes.js
86 lines (66 loc) · 1.58 KB
/
print-routes.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
'use strict'
const parseArgs = require('./args')
const log = require('./log')
const {
exit,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand
} = require('./util')
let Fastify = null
function loadModules (opts) {
try {
Fastify = requireFastifyForModule(opts._[0]).module
} catch (e) {
module.exports.stop(e)
}
}
function printRoutes (args) {
const opts = parseArgs(args)
if (opts.help) {
return showHelpForCommand('print-routes')
}
if (opts._.length !== 1) {
console.error('Missing the required file parameter\n')
return showHelpForCommand('print-routes')
}
// we start crashing on unhandledRejection
require('make-promises-safe')
loadModules(opts)
return runFastify(opts)
}
async function runFastify (opts) {
require('dotenv').config()
let file = null
try {
file = await requireServerPluginFromPath(opts._[0])
} catch (e) {
return module.exports.stop(e)
}
const fastify = Fastify(opts.options)
const pluginOptions = {}
if (opts.prefix) {
pluginOptions.prefix = opts.prefix
}
await fastify.register(file, pluginOptions)
await fastify.ready()
log('debug', fastify.printRoutes({
method: opts.method,
commonPrefix: opts.commonPrefix,
includeHooks: opts.includeHooks
}))
return fastify
}
function stop (message) {
exit(message)
}
function cli (args) {
return printRoutes(args).then(fastify => {
if (fastify) return fastify.close()
})
}
module.exports = { cli, stop, printRoutes }
if (require.main === module) {
cli(process.argv.slice(2))
}