diff --git a/README.md b/README.md index 67b64fe4..02df4bff 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/helper.d.ts b/helper.d.ts index 55debb87..985de405 100644 --- a/helper.d.ts +++ b/helper.d.ts @@ -1,8 +1,9 @@ import fastify from 'fastify' declare module 'fastify-cli/helper.js' { - module helper { - export function build (args: Array, additionalOptions?: Object, serverOptions?: Object): ReturnType + namespace helper { + export function build (args: Array, additionalOptions?: Object, serverOptions?: Object, serverModule?: Object): ReturnType + export function listen (args: Array, additionalOptions?: Object, serverOptions?: Object, serverModule?: Object): ReturnType } export = helper diff --git a/helper.js b/helper.js index 609dc7f8..46fdfa30 100644 --- a/helper.js +++ b/helper.js @@ -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 } diff --git a/start.js b/start.js index 23af6ddb..98e25a55 100755 --- a/start.js +++ b/start.js @@ -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) { @@ -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) } diff --git a/test/helper.test.js b/test/helper.test.js index 3d165b83..ba7d8235 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -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) +})