forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-plugins.js
79 lines (60 loc) · 1.41 KB
/
print-plugins.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
#! /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 printPlugins (args) {
const opts = parseArgs(args)
if (opts.help) {
return showHelpForCommand('print-plugins')
}
if (opts._.length !== 1) {
console.error('Missing the required file parameter\n')
return showHelpForCommand('print-plugins')
}
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.printPlugins())
return fastify
}
function stop (message) {
exit(message)
}
function cli (args) {
return printPlugins(args).then(fastify => {
if (fastify) return fastify.close()
})
}
module.exports = { cli, stop, printPlugins }
if (require.main === module) {
cli(process.argv.slice(2))
}