Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,12 @@ There are two utilities provided:
- `build`: builds your application and returns the `fastify` instance without calling the `listen` method.
- `listen`: starts your application and returns the `fastify` instance listening on the configured port.

Both of these utilities have the `function(args, pluginOptions, serverOptions)` parameters:
Both of these utilities have the `function(args, pluginOptions, serverOptions, serverModule)` parameters:

- `args`: is a string or a string array within the same arguments passed to the `fastify-cli` command.
- `pluginOptions`: is an object containing the options provided to the started plugin (eg: `app.js`).
- `serverOptions`: is an object containing the additional options provided to fastify server, similar to the `--options` command line argument
- `serverModule`: is optionally the the already imported main server plugin module, instead of letting the helper import it.

```js
// load the utility helper functions
Expand Down
5 changes: 3 additions & 2 deletions helper.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fastify from 'fastify'

declare module 'fastify-cli/helper.js' {
module helper {
export function build (args: Array<string>, additionalOptions?: Object, serverOptions?: Object): ReturnType<typeof fastify>
namespace helper {
export function build (args: Array<string>, additionalOptions?: Object, serverOptions?: Object, serverModule?: Object): ReturnType<typeof fastify>
export function listen (args: Array<string>, additionalOptions?: Object, serverOptions?: Object, serverModule?: Object): ReturnType<typeof fastify>
}

export = helper
Expand Down
4 changes: 2 additions & 2 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
const { runFastify } = require('./start')

module.exports = {
build (args, additionalOptions = {}, serverOptions = {}) {
build (args, additionalOptions = {}, serverOptions = {}, serverModule = undefined) {
Object.defineProperty(additionalOptions, 'ready', {
value: true,
enumerable: false,
writable: false
})

return runFastify(args, additionalOptions, serverOptions)
return runFastify(args, additionalOptions, serverOptions, serverModule)
},
listen: runFastify
}
8 changes: 6 additions & 2 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function preloadESModules (opts) {
})
}

async function runFastify (args, additionalOptions, serverOptions) {
async function runFastify (args, additionalOptions, serverOptions, serverModule) {
const opts = parseArgs(args)

if (opts.require) {
Expand All @@ -113,7 +113,11 @@ async function runFastify (args, additionalOptions, serverOptions) {
let file = null

try {
file = await requireServerPluginFromPath(opts._[0])
if (serverModule != null) {
file = serverModule
} else {
file = await requireServerPluginFromPath(opts._[0])
}
} catch (e) {
return module.exports.stop(e)
}
Expand Down
7 changes: 7 additions & 0 deletions test/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,10 @@ test('should ensure can access all decorators', async t => {
t.teardown(() => app2.close())
t.ok(app2.test)
})

test('should return the fastify instance when using serverModule', async t => {
const argv = ['']
const app = await helper.build(argv, {}, {}, require('../examples/plugin.js'))
t.teardown(() => app.close())
t.notOk(app.server.listening)
})