Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI option to specify directory or file to watch in watch mode #794

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
5 changes: 3 additions & 2 deletions args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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', '')
Expand Down Expand Up @@ -100,6 +100,7 @@ module.exports = function parseArgs (args) {
method: parsedArgs.method,
commonPrefix: parsedArgs.commonPrefix,
includeHooks: parsedArgs.includeHooks,
followWatch: followWatchArg,
trustProxy
}
}
8 changes: 4 additions & 4 deletions lib/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions test/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_',
Expand All @@ -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',
Expand Down Expand Up @@ -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_',
Expand All @@ -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',
Expand Down Expand Up @@ -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_'
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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_',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down