Skip to content

Commit

Permalink
Debug mode for Fastify CLI Start (#202)
Browse files Browse the repository at this point in the history
* Forking execArgv

* Test

* Debug + debug port in watch mode

* Updated tests

* Minor

* README

* Test fix

* Node6 version check

* Std

* Allstop fix

* Stop on debug incompatible

Co-Authored-By: 小菜 <xtx1130@gmail.com>
  • Loading branch information
2 people authored and mcollina committed Aug 30, 2019
1 parent 64c11be commit 22e4c1e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -52,3 +52,4 @@ test.sock
# test artifacts
test/workdir
test/fixtures/*.js
.vscode/launch.json
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -100,6 +100,8 @@ You can pass the following options via cli arguments, every options has the corr
| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` |
| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` |
| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` |
| Start fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` |
| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` |
| 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` |
Expand Down
10 changes: 8 additions & 2 deletions args.js
Expand Up @@ -4,8 +4,8 @@ const argv = require('yargs-parser')

module.exports = function parseArgs (args) {
const parsedArgs = argv(args, {
number: ['port', 'body-limit', 'plugin-timeout'],
boolean: ['pretty-logs', 'options', 'watch'],
number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout'],
boolean: ['pretty-logs', 'options', 'watch', 'debug'],
string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch'],
envPrefix: 'FASTIFY_',
alias: {
Expand All @@ -16,6 +16,8 @@ module.exports = function parseArgs (args) {
address: ['a'],
watch: ['w'],
prefix: ['r'],
debug: ['d'],
'debug-port': ['I'],
'log-level': ['l'],
'pretty-logs': ['P'],
'plugin-timeout': ['T']
Expand All @@ -24,6 +26,8 @@ module.exports = function parseArgs (args) {
'log-level': 'fatal',
'pretty-logs': false,
watch: false,
debug: false,
debugPort: 9320,
'ignore-watch': 'node_modules build dist .git bower_components logs',
options: false,
'plugin-timeout': 10 * 1000 // everything should load in 10 seconds
Expand All @@ -38,6 +42,8 @@ module.exports = function parseArgs (args) {
prettyLogs: parsedArgs.prettyLogs,
options: parsedArgs.options,
watch: parsedArgs.watch,
debug: parsedArgs.debug,
debugPort: parsedArgs.debugPort,
ignoreWatch: parsedArgs.ignoreWatch,
logLevel: parsedArgs.logLevel,
address: parsedArgs.address,
Expand Down
1 change: 1 addition & 0 deletions lib/watch/index.js
Expand Up @@ -40,6 +40,7 @@ const watch = function (args, ignoreWatch) {
const run = (event) => {
const childEvent = { childEvent: event }
const env = Object.assign({}, process.env, childEvent)

const _child = cp.fork(forkPath, args, {
env: env,
cwd: process.cwd(),
Expand Down
8 changes: 8 additions & 0 deletions start.js
Expand Up @@ -101,6 +101,14 @@ function runFastify (args, cb) {
pump(pinoColada, process.stdout, assert.ifError)
}

if (opts.debug) {
if (process.version.match(/v[0-6]\..*/g)) {
stop('Fastify debug mode not compatible with Node.js version < 6')
} else {
require('inspector').open(opts.debugPort)
}
}

const fastify = Fastify(opts.options ? Object.assign(options, file.options) : options)

const pluginOptions = {}
Expand Down
24 changes: 20 additions & 4 deletions test/args.test.js
Expand Up @@ -17,6 +17,8 @@ test('should parse args correctly', t => {
'--prefix', 'FASTIFY_',
'--plugin-timeout', '500',
'--body-limit', '5242880',
'--debug', 'true',
'--debug-port', 1111,
'app.js'
]
const parsedArgs = parseArgs(argv)
Expand All @@ -33,7 +35,9 @@ test('should parse args correctly', t => {
logLevel: 'info',
prefix: 'FASTIFY_',
pluginTimeout: 500,
bodyLimit: 5242880
bodyLimit: 5242880,
debug: true,
debugPort: 1111
})
})

Expand All @@ -52,6 +56,8 @@ test('should parse args with = assignment correctly', t => {
'--prefix=FASTIFY_',
'--plugin-timeout=500',
'--body-limit=5242880',
'--debug=true',
'--debug-port', 1111,
'app.js'
]
const parsedArgs = parseArgs(argv)
Expand All @@ -68,7 +74,9 @@ test('should parse args with = assignment correctly', t => {
logLevel: 'info',
prefix: 'FASTIFY_',
pluginTimeout: 500,
bodyLimit: 5242880
bodyLimit: 5242880,
debug: true,
debugPort: 1111
})
})

Expand All @@ -86,6 +94,8 @@ test('should parse env vars correctly', t => {
process.env.FASTIFY_PREFIX = 'FASTIFY_'
process.env.FASTIFY_BODY_LIMIT = '5242880'
process.env.FASTIFY_PLUGIN_TIMEOUT = '500'
process.env.FASTIFY_DEBUG = 'true'
process.env.FASTIFY_DEBUG_PORT = '1111'

t.teardown(function () {
delete process.env.FASTIFY_PORT
Expand All @@ -99,6 +109,8 @@ test('should parse env vars correctly', t => {
delete process.env.FASTIFY_PREFIX
delete process.env.FASTIFY_BODY_LIMIT
delete process.env.FASTIFY_PLUGIN_TIMEOUT
delete process.env.FASTIFY_DEBUG
delete process.env.FASTIFY_DEBUG_PORT
})

const parsedArgs = parseArgs([])
Expand All @@ -115,12 +127,14 @@ test('should parse env vars correctly', t => {
port: 7777,
prefix: 'FASTIFY_',
socket: 'fastify.io.socket:9999',
pluginTimeout: 500
pluginTimeout: 500,
debug: true,
debugPort: 1111
})
})

test('should respect default values', t => {
t.plan(7)
t.plan(9)

const argv = [
'app.js'
Expand All @@ -135,4 +149,6 @@ test('should respect default values', t => {
t.is(parsedArgs.ignoreWatch, 'node_modules build dist .git bower_components logs')
t.is(parsedArgs.logLevel, 'fatal')
t.is(parsedArgs.pluginTimeout, 10000)
t.is(parsedArgs.debug, false)
t.is(parsedArgs.debugPort, 9320)
})
2 changes: 1 addition & 1 deletion test/start.test.js
Expand Up @@ -442,7 +442,7 @@ test('should start the server listening on 0.0.0.0 when runing in docker', t =>
})

test('should start the server with watch options that the child process restart when directory changed', { skip: onTravis }, (t) => {
t.plan(6)
t.plan(5)
const tmpjs = path.resolve(baseFilename + '.js')

fs.writeFile(tmpjs, 'hello world', function (err) {
Expand Down

0 comments on commit 22e4c1e

Please sign in to comment.