From 63c06e1f37852ed94ac886338e4114c00db4eb52 Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Tue, 28 Jan 2025 23:25:19 +0100 Subject: [PATCH 1/3] feat: add followWatch parameters to watch mode (#787) --- args.js | 5 +++-- lib/watch/index.js | 8 ++++---- start.js | 2 +- test/args.test.js | 11 +++++++++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/args.js b/args.js index 3fa05366..95163696 100644 --- a/args.js +++ b/args.js @@ -28,7 +28,7 @@ module.exports = function parseArgs (args) { 'populate--': true }, number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay', 'trust-proxy-hop'], - string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'import', 'config', 'method', 'trust-proxy-ips'], + string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'import', 'config', 'method', 'trust-proxy-ips', 'follow-watch'], boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks', 'trust-proxy-enabled'], envPrefix: 'FASTIFY_', alias: { @@ -58,7 +58,7 @@ module.exports = function parseArgs (args) { const additionalArgs = commandLineArguments['--'] || [] const { _, ...pluginOptions } = argv(additionalArgs) const ignoreWatchArg = commandLineArguments.ignoreWatch || configFileOptions?.ignoreWatch || '' - + const followWatchArg = commandLineArguments.followWatch || configFileOptions?.followWatch || '' let ignoreWatch = `${DEFAULT_IGNORE} ${ignoreWatchArg}`.trim() if (ignoreWatchArg.includes('.ts$')) { ignoreWatch = ignoreWatch.replace('dist', '') @@ -100,6 +100,7 @@ module.exports = function parseArgs (args) { method: parsedArgs.method, commonPrefix: parsedArgs.commonPrefix, includeHooks: parsedArgs.includeHooks, + followWatch: followWatchArg, trustProxy } } diff --git a/lib/watch/index.js b/lib/watch/index.js index 7b9589b6..ffe4a115 100644 --- a/lib/watch/index.js +++ b/lib/watch/index.js @@ -10,11 +10,10 @@ const EventEmitter = require('node:events') const chokidar = require('chokidar') const forkPath = path.join(__dirname, './fork.js') -const watch = function (args, ignoreWatch, verboseWatch) { +const watch = function (args, ignoreWatch, verboseWatch, followWatch) { const emitter = new EventEmitter() let allStop = false let childs = [] - const stop = (watcher = null, err = null) => { childs.forEach(function (child) { child.kill() @@ -76,9 +75,10 @@ const watch = function (args, ignoreWatch, verboseWatch) { childs.push(run('start')) const ignoredArr = ignoreWatch.split(' ').map((item) => item.trim()).filter((item) => item.length) - const ignoredPattern = arrayToRegExp(ignoredArr) - const watcher = chokidar.watch(process.cwd(), { ignored: ignoredPattern }) + const ignoredPattern = arrayToRegExp(ignoredArr) + const watchDir = followWatch || process.cwd() + const watcher = chokidar.watch(watchDir, { ignored: ignoredPattern }) watcher.on('ready', function () { watcher.on('all', function (event, filepath) { if (verboseWatch) { diff --git a/start.js b/start.js index 23af6ddb..2506d61b 100755 --- a/start.js +++ b/start.js @@ -50,7 +50,7 @@ async function start (args) { loadModules(opts) if (opts.watch) { - return watch(args, opts.ignoreWatch, opts.verboseWatch) + return watch(args, opts.ignoreWatch, opts.verboseWatch, opts.followWatch) } return runFastify(args) diff --git a/test/args.test.js b/test/args.test.js index 616291b5..812959af 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -16,6 +16,7 @@ test('should parse args correctly', t => { '--pretty-logs', 'true', '--watch', 'true', '--ignore-watch', 'ignoreme.js', + '--follow-watch', 'watchme.js', '--verbose-watch', 'true', '--options', 'true', '--prefix', 'FASTIFY_', @@ -38,6 +39,7 @@ test('should parse args correctly', t => { options: true, watch: true, ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output ignoreme.js', + followWatch: 'watchme.js', verboseWatch: true, port: 7777, address: 'fastify.dev:9999', @@ -75,6 +77,7 @@ test('should parse args with = assignment correctly', t => { '--pretty-logs=true', '--watch=true', '--ignore-watch=ignoreme.js', + '--follow-watch=watchme.js', '--verbose-watch=true', '--options=true', '--prefix=FASTIFY_', @@ -97,6 +100,7 @@ test('should parse args with = assignment correctly', t => { options: true, watch: true, ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output ignoreme.js', + followWatch: 'watchme.js', verboseWatch: true, port: 7777, address: 'fastify.dev:9999', @@ -176,6 +180,7 @@ test('should parse env vars correctly', t => { options: true, watch: true, ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output ignoreme.js', + followWatch: '', verboseWatch: true, address: 'fastify.dev:9999', bodyLimit: 5242880, @@ -238,6 +243,7 @@ test('should parse custom plugin options', t => { '--pretty-logs', 'true', '--watch', 'true', '--ignore-watch', 'ignoreme.js', + '--follow-watch', 'watchme.js', '--verbose-watch', 'true', '--options', 'true', '--prefix', 'FASTIFY_', @@ -266,6 +272,7 @@ test('should parse custom plugin options', t => { options: true, watch: true, ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output ignoreme.js', + followWatch: 'watchme.js', verboseWatch: true, port: 7777, address: 'fastify.dev:9999', @@ -319,6 +326,7 @@ test('should parse config file correctly and prefer config values over default o debugPort: 4000, debugHost: '1.1.1.1', ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output', + followWatch: '', verboseWatch: false, logLevel: 'fatal', address: 'fastify.dev:9999', @@ -363,6 +371,7 @@ test('should prefer command line args over config file options', t => { debugPort: 9320, debugHost: '1.1.1.1', ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output', + followWatch: '', verboseWatch: false, logLevel: 'fatal', address: 'fastify.dev:9999', @@ -409,6 +418,7 @@ test('should favor trust proxy enabled over trust proxy ips and trust proxy hop' debugPort: 1111, debugHost: '1.1.1.1', ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output', + followWatch: '', verboseWatch: false, logLevel: 'fatal', address: undefined, @@ -454,6 +464,7 @@ test('should favor trust proxy ips over trust proxy hop', t => { debugPort: 1111, debugHost: '1.1.1.1', ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output', + followWatch: '', verboseWatch: false, logLevel: 'fatal', address: undefined, From 7d708d6c9906ec3614f3c52201c38e1640e65c5c Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Tue, 28 Jan 2025 23:56:39 +0100 Subject: [PATCH 2/3] docs: add follow-watch to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ad677172..6db7f5a9 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ You can pass the following options via CLI arguments. You can also use `--config | Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` | | Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` | | Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` | +| Watch changes only into the specified files or directories when watch is enabled. (e.g. `--follow-watch='plugins/'` ) | | `--follow-watch` | `FASTIFY_FOLLOW_WATCH` | | Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` | | Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` | | Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` | From 16f4ad73899c391be2f6d335b0362b469b4a05fe Mon Sep 17 00:00:00 2001 From: Gabriele Fontana Date: Wed, 29 Jan 2025 00:04:16 +0100 Subject: [PATCH 3/3] test: add FASTIFY_FOLLOW_WATCH env var --- test/args.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/args.test.js b/test/args.test.js index 812959af..9b3c1997 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -137,6 +137,7 @@ test('should parse env vars correctly', t => { process.env.FASTIFY_PRETTY_LOGS = 'true' process.env.FASTIFY_WATCH = 'true' process.env.FASTIFY_IGNORE_WATCH = 'ignoreme.js' + process.env.FASTIFY_FOLLOW_WATCH = 'plugin/' process.env.FASTIFY_VERBOSE_WATCH = 'true' process.env.FASTIFY_OPTIONS = 'true' process.env.FASTIFY_PREFIX = 'FASTIFY_' @@ -159,6 +160,7 @@ test('should parse env vars correctly', t => { delete process.env.FASTIFY_PRETTY_LOGS delete process.env.FASTIFY_WATCH delete process.env.FASTIFY_IGNORE_WATCH + delete process.env.FASTIFY_FOLLOW_WATCH delete process.env.FASTIFY_VERBOSE_WATCH delete process.env.FASTIFY_OPTIONS delete process.env.FASTIFY_PREFIX @@ -180,7 +182,7 @@ test('should parse env vars correctly', t => { options: true, watch: true, ignoreWatch: 'node_modules build dist .git bower_components logs .swp .nyc_output ignoreme.js', - followWatch: '', + followWatch: 'plugin/', verboseWatch: true, address: 'fastify.dev:9999', bodyLimit: 5242880,