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
69 changes: 38 additions & 31 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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'
},
Expand Down
4 changes: 4 additions & 0 deletions help.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Usage: fastify [opts] <file>

-p, --port
Port to listen on (default to 3000)

-a, --address
Address to listen on

-l, --log-level
Log level (default to fatal)

Expand Down
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}