diff --git a/cli.js b/cli.js index 03a32a3b..27833582 100755 --- a/cli.js +++ b/cli.js @@ -2,23 +2,36 @@ 'use strict' -const minimist = require('minimist') -const Fastify = require('fastify') const path = require('path') -const pino = require('pino') -const pump = require('pump') const fs = require('fs') +const assert = require('assert') + +const updateNotifier = require('update-notifier') +const minimist = require('minimist') +const PinoColada = require('pino-colada') +const pump = require('pump') +const Fastify = require('fastify') function start (opts) { + const notifier = updateNotifier({ + pkg: { + name: 'fastify', + version: require('fastify/package.json').version + }, + updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week + }) + + notifier.notify({ + isGlobal: false, + defer: false + }) + if (opts.help) { console.log(fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf8')) stop(0) } - if (opts._.length !== 1) { - console.log('Missing the file parameter') - stop(1) - } + assert(opts._.length === 1, 'Missing the file parameter') runFastify(opts) } @@ -28,11 +41,11 @@ function stop (code) { } function runFastify (opts) { - let file = null + var file = null try { file = require(path.resolve(process.cwd(), opts._[0])) } catch (e) { - console.log('Cannot find the specified file') + console.log(`Cannot find the specified file: '${opts._[0]}'`) stop(1) } @@ -43,43 +56,37 @@ function runFastify (opts) { } if (opts['pretty-logs']) { - const pretty = pino.pretty() - pump(pretty, process.stdout, err => { - if (err) { - console.log(err) - stop(1) - } - }) - options.logger.stream = pretty + const pinoColada = PinoColada() + options.logger.stream = pinoColada + pump(pinoColada, process.stdout, assert.ifError) } const fastify = Fastify(opts.options ? Object.assign(options, file.options) : options) - fastify.register(file, function (err) { - if (err) { - console.log(err) - stop(1) - } - }) + fastify.register(file, assert.ifError) - fastify.listen(opts.port, function (err) { - if (err) { - console.log(err) - stop(1) - } + if (opts.address) { + fastify.listen(opts.port, opts.address, listen) + } else { + fastify.listen(opts.port, listen) + } + + function listen (err) { + assert.ifError(err) console.log(`Server listening on http://localhost:${fastify.server.address().port}`) - }) + } } if (require.main === module) { start(minimist(process.argv.slice(2), { integer: ['port'], boolean: ['pretty-logs', 'options'], - string: ['log-level'], + string: ['log-level', 'address'], alias: { port: 'p', help: 'h', options: 'o', + address: 'a', 'log-level': 'l', 'pretty-logs': 'P' }, diff --git a/help.txt b/help.txt index a6354ad7..76833e81 100644 --- a/help.txt +++ b/help.txt @@ -1,7 +1,11 @@ Usage: fastify [opts] + -p, --port Port to listen on (default to 3000) + -a, --address + Address to listen on + -l, --log-level Log level (default to fatal) diff --git a/package.json b/package.json index 18115ac5..c4c0f8a3 100644 --- a/package.json +++ b/package.json @@ -18,24 +18,25 @@ "license": "MIT", "repository": { "type": "git", - "url": "git+https://github.com/delvedor/fastify-cli.git" + "url": "git+https://github.com/fastify/fastify-cli.git" }, "bugs": { - "url": "https://github.com/delvedor/fastify-cli/issues" + "url": "https://github.com/fastify/fastify-cli/issues" }, - "homepage": "https://github.com/delvedor/fastify-cli#readme", + "homepage": "https://github.com/fastify/fastify-cli#readme", "engines": { "node": ">= 4.0" }, "dependencies": { - "fastify": "^0.17.0", "minimist": "^1.2.0", - "pino": "^4.2.3", - "pump": "^1.0.2" + "pino-colada": "^1.4.0", + "pump": "^1.0.2", + "update-notifier": "^2.1.0" }, "devDependencies": { - "request": "^2.79.0", - "standard": "^10.0.1", - "tap": "^10.3.0" + "fastify": "^0.17.0", + "request": "^2.81.0", + "standard": "^10.0.2", + "tap": "^10.3.2" } }