Skip to content
Merged
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 @@ -137,6 +137,7 @@ You can pass the following options via cli arguments, every options has the corr
| 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` |
| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` |
| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` |
| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` |
| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` |
Expand Down
4 changes: 3 additions & 1 deletion args.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = function parseArgs (args) {
'populate--': true
},
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'],
boolean: ['pretty-logs', 'options', 'watch', 'debug'],
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require'],
boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug'],
envPrefix: 'FASTIFY_',
alias: {
port: ['p'],
Expand All @@ -31,6 +31,7 @@ module.exports = function parseArgs (args) {
'log-level': 'fatal',
'pretty-logs': false,
watch: false,
verboseWatch: false,
debug: false,
debugPort: 9320,
'ignore-watch': 'node_modules build dist .git bower_components logs .swp .nyc_output',
Expand All @@ -57,6 +58,7 @@ module.exports = function parseArgs (args) {
debugPort: parsedArgs.debugPort,
debugHost: parsedArgs.debugHost,
ignoreWatch: parsedArgs.ignoreWatch,
verboseWatch: parsedArgs.verboseWatch,
logLevel: parsedArgs.logLevel,
address: parsedArgs.address,
socket: parsedArgs.socket,
Expand Down
9 changes: 6 additions & 3 deletions lib/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
const path = require('path')
const cp = require('child_process')
const chalk = require('chalk')
const { arrayToRegExp } = require('./utils')
const { arrayToRegExp, logWatchVerbose } = require('./utils')
const { GRACEFUL_SHUT } = require('./constants.js')

const EventEmitter = require('events')
const chokidar = require('chokidar')
const forkPath = path.join(__dirname, './fork.js')

const watch = function (args, ignoreWatch) {
const watch = function (args, ignoreWatch, verboseWatch) {
const emitter = new EventEmitter()
let allStop = false
let childs = []
Expand Down Expand Up @@ -84,7 +84,10 @@ const watch = function (args, ignoreWatch) {

const watcher = chokidar.watch(process.cwd(), { ignored: ignoredPattern })
watcher.on('ready', function () {
watcher.on('all', function () {
watcher.on('all', function (event, filepath) {
if (verboseWatch) {
logWatchVerbose(event, filepath)
}
try {
const child = childs.shift()
child.send(GRACEFUL_SHUT)
Expand Down
15 changes: 14 additions & 1 deletion lib/watch/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

const chalk = require('chalk')
const path = require('path')

const arrayToRegExp = (arr) => {
const reg = arr.map((file) => {
if (/^\./.test(file)) { return `\\${file}` }
Expand All @@ -8,6 +11,16 @@ const arrayToRegExp = (arr) => {
return new RegExp(`(${reg})`)
}

const logWatchVerbose = (event, filepath) => {
const relativeFilepath = path.relative(process.cwd(), filepath)
console.log(
chalk.gray(
`[fastify-cli] watch - '${event}' occurred on '${relativeFilepath}'`
)
)
}

module.exports = {
arrayToRegExp
arrayToRegExp,
logWatchVerbose
}
2 changes: 1 addition & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function start (args) {
loadModules(opts)

if (opts.watch) {
return watch(args, opts.ignoreWatch)
return watch(args, opts.ignoreWatch, opts.verboseWatch)
}

return runFastify(args)
Expand Down
12 changes: 11 additions & 1 deletion test/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('should parse args correctly', t => {
'--pretty-logs', 'true',
'--watch', 'true',
'--ignore-watch', 'ignoreme.js',
'--verbose-watch', 'true',
'--options', 'true',
'--prefix', 'FASTIFY_',
'--plugin-timeout', '500',
Expand All @@ -33,6 +34,7 @@ test('should parse args correctly', t => {
options: true,
watch: true,
ignoreWatch: 'ignoreme.js',
verboseWatch: true,
port: 7777,
address: 'fastify.io:9999',
socket: 'fastify.io.socket:9999',
Expand Down Expand Up @@ -62,6 +64,7 @@ test('should parse args with = assignment correctly', t => {
'--pretty-logs=true',
'--watch=true',
'--ignore-watch=ignoreme.js',
'--verbose-watch=true',
'--options=true',
'--prefix=FASTIFY_',
'--plugin-timeout=500',
Expand All @@ -81,6 +84,7 @@ test('should parse args with = assignment correctly', t => {
options: true,
watch: true,
ignoreWatch: 'ignoreme.js',
verboseWatch: true,
port: 7777,
address: 'fastify.io:9999',
socket: 'fastify.io.socket:9999',
Expand Down Expand Up @@ -109,6 +113,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_VERBOSE_WATCH = 'true'
process.env.FASTIFY_OPTIONS = 'true'
process.env.FASTIFY_PREFIX = 'FASTIFY_'
process.env.FASTIFY_BODY_LIMIT = '5242880'
Expand All @@ -127,6 +132,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_VERBOSE_WATCH
delete process.env.FASTIFY_OPTIONS
delete process.env.FASTIFY_PREFIX
delete process.env.FASTIFY_BODY_LIMIT
Expand All @@ -145,6 +151,7 @@ test('should parse env vars correctly', t => {
options: true,
watch: true,
ignoreWatch: 'ignoreme.js',
verboseWatch: true,
address: 'fastify.io:9999',
bodyLimit: 5242880,
logLevel: 'info',
Expand All @@ -163,7 +170,7 @@ test('should parse env vars correctly', t => {
})

test('should respect default values', t => {
t.plan(11)
t.plan(12)

const argv = [
'app.js'
Expand All @@ -176,6 +183,7 @@ test('should respect default values', t => {
t.is(parsedArgs.prettyLogs, false)
t.is(parsedArgs.watch, false)
t.is(parsedArgs.ignoreWatch, 'node_modules build dist .git bower_components logs .swp .nyc_output')
t.is(parsedArgs.verboseWatch, false)
t.is(parsedArgs.logLevel, 'fatal')
t.is(parsedArgs.pluginTimeout, 10000)
t.is(parsedArgs.debug, false)
Expand All @@ -196,6 +204,7 @@ test('should parse custom plugin options', t => {
'--pretty-logs', 'true',
'--watch', 'true',
'--ignore-watch', 'ignoreme.js',
'--verbose-watch', 'true',
'--options', 'true',
'--prefix', 'FASTIFY_',
'--plugin-timeout', '500',
Expand All @@ -222,6 +231,7 @@ test('should parse custom plugin options', t => {
options: true,
watch: true,
ignoreWatch: 'ignoreme.js',
verboseWatch: true,
port: 7777,
address: 'fastify.io:9999',
socket: 'fastify.io.socket:9999',
Expand Down
43 changes: 43 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,49 @@ test('should start the server with watch options that the child process restart
t.pass('should receive restart event')
})

test('should start the server with watch and verbose-watch options that the child process restart when directory changed with console message about changes ', { skip: onGithubAction }, async (t) => {
t.plan(5)

const spy = sinon.spy()
const watch = proxyquire('../lib/watch', {
'./utils': {
logWatchVerbose: spy
}
})

const start = proxyquire('../start', {
'./lib/watch': watch
})

const tmpjs = path.resolve(baseFilename + '.js')

await writeFile(tmpjs, 'hello world')
const argv = ['-p', '4042', '-w', '--verbose-watch', './examples/plugin.js']
const fastifyEmitter = await start.start(argv)

t.tearDown(() => {
if (fs.existsSync(tmpjs)) {
fs.unlinkSync(tmpjs)
}
fastifyEmitter.emit('close')
})

await once(fastifyEmitter, 'start')
t.pass('should receive start event')

await once(fastifyEmitter, 'ready')
t.pass('should receive ready event')

await writeFile(tmpjs, 'hello fastify', { flag: 'a+' }) // chokidar watch can't catch change event in CI, but local test is all ok. you can remove annotation in local environment.
t.pass('change tmpjs')

// this might happen more than once but does not matter in this context
await once(fastifyEmitter, 'restart')
t.pass('should receive restart event')

t.ok(spy.calledOnce, 'should print a console message on file update')
})

test('should reload the env on restart when watching', async (t) => {
const testdir = t.testdir({
'.env': 'GREETING=world',
Expand Down