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` | 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..9b3c1997 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', @@ -133,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_' @@ -155,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 @@ -176,6 +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: 'plugin/', verboseWatch: true, address: 'fastify.dev:9999', bodyLimit: 5242880, @@ -238,6 +245,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 +274,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 +328,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 +373,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 +420,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 +466,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,