Skip to content

Commit af74cea

Browse files
committed
Merge pull request typicode#194 from andreruffert/master
feature: add optional `--quiet` argument
2 parents f69211b + 4e27a0f commit af74cea

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"method-override": "^2.1.2",
2121
"morgan": "^1.3.1",
2222
"node-uuid": "^1.4.2",
23+
"object-assign": "^4.0.1",
2324
"pluralize": "^1.1.2",
2425
"underscore-db": "^0.9.1",
2526
"update-notifier": "^0.5.0",

src/cli/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ module.exports = function () {
4545
alias: 'i',
4646
description: 'Set database id property (e.g. _id)',
4747
default: 'id'
48+
},
49+
quiet: {
50+
alias: 'q',
51+
description: 'Suppress log messages from output'
4852
}
4953
})
5054
.boolean('watch')
55+
.boolean('quiet')
5156
.help('help').alias('help', 'h')
5257
.version(pkg.version).alias('version', 'v')
5358
.example('$0 db.json', '')

src/cli/run.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ function createApp (source, object, routes, argv) {
4444
var defaults
4545
if (argv.static) {
4646
defaults = jsonServer.defaults({
47+
logger: !argv.quiet,
4748
static: path.join(process.cwd(), argv.static)
4849
})
4950
} else {
50-
defaults = jsonServer.defaults()
51+
defaults = jsonServer.defaults({
52+
logger: !argv.quiet
53+
})
5154
}
5255

5356
app.use(defaults)
@@ -79,6 +82,11 @@ module.exports = function (argv) {
7982
process.exit(1)
8083
}
8184

85+
// noop log fn
86+
if (argv.quiet) {
87+
console.log = function () {}
88+
}
89+
8290
console.log()
8391
console.log(chalk.cyan(' \\{^_^}/ hi!'))
8492

src/server/defaults.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var express = require('express')
44
var logger = require('morgan')
55
var cors = require('cors')
66
var errorhandler = require('errorhandler')
7+
var objectAssign = require('object-assign')
78

89
module.exports = function (opts) {
910
var userDir = path.join(process.cwd(), 'public')
@@ -12,17 +13,19 @@ module.exports = function (opts) {
1213
userDir :
1314
defaultDir
1415

15-
opts = opts || { static: staticDir }
16+
opts = objectAssign({ logger: true, static: staticDir }, opts)
1617

1718
var arr = []
1819

1920
// Logger
20-
arr.push(logger('dev', {
21-
skip: function (req, res) {
22-
return process.env.NODE_ENV === 'test' ||
23-
req.path === '/favicon.ico'
24-
}
25-
}))
21+
if (opts.logger) {
22+
arr.push(logger('dev', {
23+
skip: function (req, res) {
24+
return process.env.NODE_ENV === 'test' ||
25+
req.path === '/favicon.ico'
26+
}
27+
}))
28+
}
2629

2730
// Enable CORS for all the requests, including static files
2831
arr.push(cors({ origin: true, credentials: true }))

0 commit comments

Comments
 (0)