From ea35b547902f159b5063b837f262250e5772ce7a Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sat, 10 May 2025 19:01:45 +0300 Subject: [PATCH 1/6] Pass an already imported module in helper This is useful when running in environment that transpile on the fly, such as Jest/Vitest and so on, which don't handle the dynamic import all that well. --- helper.d.ts | 2 +- helper.js | 4 ++-- start.js | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/helper.d.ts b/helper.d.ts index 55debb87..5e474e40 100644 --- a/helper.d.ts +++ b/helper.d.ts @@ -2,7 +2,7 @@ import fastify from 'fastify' declare module 'fastify-cli/helper.js' { module helper { - export function build (args: Array, additionalOptions?: Object, serverOptions?: Object): ReturnType + export function build (args: Array, additionalOptions?: Object, serverOptions?: Object, module?: Object): ReturnType } export = helper diff --git a/helper.js b/helper.js index 609dc7f8..54113f4b 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 = {}, module = undefined) { Object.defineProperty(additionalOptions, 'ready', { value: true, enumerable: false, writable: false }) - return runFastify(args, additionalOptions, serverOptions) + return runFastify(args, additionalOptions, serverOptions, module) }, listen: runFastify } diff --git a/start.js b/start.js index 23af6ddb..36ce7e4d 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, module) { 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 (module != null) { + file = module + } else { + file = await requireServerPluginFromPath(opts._[0]) + } } catch (e) { return module.exports.stop(e) } From b9e686c0fa1dbff00745398062c2fda484d3e5ac Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sat, 10 May 2025 19:21:33 +0300 Subject: [PATCH 2/6] Fix name conflict --- helper.d.ts | 2 +- helper.js | 4 ++-- start.js | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/helper.d.ts b/helper.d.ts index 5e474e40..23d3ff59 100644 --- a/helper.d.ts +++ b/helper.d.ts @@ -2,7 +2,7 @@ import fastify from 'fastify' declare module 'fastify-cli/helper.js' { module helper { - export function build (args: Array, additionalOptions?: Object, serverOptions?: Object, module?: Object): ReturnType + export function build (args: Array, additionalOptions?: Object, serverOptions?: Object, serverModule?: Object): ReturnType } export = helper diff --git a/helper.js b/helper.js index 54113f4b..46fdfa30 100644 --- a/helper.js +++ b/helper.js @@ -3,14 +3,14 @@ const { runFastify } = require('./start') module.exports = { - build (args, additionalOptions = {}, serverOptions = {}, module = undefined) { + build (args, additionalOptions = {}, serverOptions = {}, serverModule = undefined) { Object.defineProperty(additionalOptions, 'ready', { value: true, enumerable: false, writable: false }) - return runFastify(args, additionalOptions, serverOptions, module) + return runFastify(args, additionalOptions, serverOptions, serverModule) }, listen: runFastify } diff --git a/start.js b/start.js index 36ce7e4d..98e25a55 100755 --- a/start.js +++ b/start.js @@ -96,7 +96,7 @@ async function preloadESModules (opts) { }) } -async function runFastify (args, additionalOptions, serverOptions, module) { +async function runFastify (args, additionalOptions, serverOptions, serverModule) { const opts = parseArgs(args) if (opts.require) { @@ -113,8 +113,8 @@ async function runFastify (args, additionalOptions, serverOptions, module) { let file = null try { - if (module != null) { - file = module + if (serverModule != null) { + file = serverModule } else { file = await requireServerPluginFromPath(opts._[0]) } From 30d1f2d14ef004f090f1a87ba51d323f5f234c66 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sat, 10 May 2025 22:43:49 +0300 Subject: [PATCH 3/6] Docs and test --- README.md | 3 ++- helper.d.ts | 3 ++- test/helper.test.js | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) 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 23d3ff59..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 { + 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/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) +}) From bc23f4077a0c51cbfdf4f0e592e01d650c170596 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sat, 8 Nov 2025 12:18:46 +0200 Subject: [PATCH 4/6] Update start.js Co-authored-by: Matteo Pietro Dazzi Signed-off-by: Segev Finer --- start.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/start.js b/start.js index 98e25a55..7fe4bcf9 100755 --- a/start.js +++ b/start.js @@ -113,11 +113,7 @@ async function runFastify (args, additionalOptions, serverOptions, serverModule) let file = null try { - if (serverModule != null) { - file = serverModule - } else { - file = await requireServerPluginFromPath(opts._[0]) - } + file = serverModule ?? await requireServerPluginFromPath(opts._[0]) } catch (e) { return module.exports.stop(e) } From 09008dda8df7160972e840730dec68e9fcbaf22d Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sat, 8 Nov 2025 12:18:56 +0200 Subject: [PATCH 5/6] Update README.md Co-authored-by: Matteo Pietro Dazzi Signed-off-by: Segev Finer --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 02df4bff..745ac9f3 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ Both of these utilities have the `function(args, pluginOptions, serverOptions, s - `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. +- `serverModule`: is an optional parameter used to provide the already imported main server plugin module, instead of letting the helper import it. ```js // load the utility helper functions From 929e7c8627c4d2f04c9b57879d053310908465f9 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Mon, 10 Nov 2025 20:17:37 +0200 Subject: [PATCH 6/6] Update test/helper.test.js Co-authored-by: Manuel Spigolon Signed-off-by: Segev Finer --- test/helper.test.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/helper.test.js b/test/helper.test.js index ba7d8235..c68ba48d 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -164,7 +164,10 @@ test('should ensure can access all decorators', async t => { test('should return the fastify instance when using serverModule', async t => { const argv = [''] - const app = await helper.build(argv, {}, {}, require('../examples/plugin.js')) + const app = await helper.build(argv, { skipOverride: true }, {}, (app, opts, next) => { + app.decorate('foo', 'bar') + next() + }) t.teardown(() => app.close()) - t.notOk(app.server.listening) + t.equals(app.foo, 'bar') })