Fix CLI regressions and refactor

This commit is contained in:
Typicode
2015-07-23 08:04:29 +02:00
parent 756b8b4823
commit a49db2c5c3
5 changed files with 73 additions and 36 deletions

View File

@ -28,13 +28,14 @@ module.exports = function () {
alias: 'r', alias: 'r',
description: 'Load routes file' description: 'Load routes file'
}, },
id: {
description: 'Set database id property (e.g. _id)',
default: 'id'
},
delay: { delay: {
alias: 'd', alias: 'd',
description: 'Add delay to responses (ms)' description: 'Add delay to responses (ms)'
},
id: {
alias: 'i',
description: 'Set database id property (e.g. _id)',
default: 'id'
} }
}) })
.boolean('watch') .boolean('watch')

View File

@ -31,8 +31,8 @@ function prettyPrint (argv, object, rules) {
console.log() console.log()
} }
function createServer (source, object, routes, delay) { function createApp (source, object, routes, argv) {
var server = jsonServer.create() var app = jsonServer.create()
var router = jsonServer.router( var router = jsonServer.router(
is.JSON(source) ? is.JSON(source) ?
@ -40,31 +40,34 @@ function createServer (source, object, routes, delay) {
object object
) )
server.use(jsonServer.defaults) app.use(jsonServer.defaults)
if (routes) { if (routes) {
var rewriter = jsonServer.rewriter(routes) var rewriter = jsonServer.rewriter(routes)
server.use(rewriter) app.use(rewriter)
} }
if (delay) { if (argv.delay) {
server.use(pause(delay)) app.use(pause(argv.delay))
} }
server.use(router) router.db._.id = argv.id
app.db = router.db
app.use(router)
return server return app
} }
module.exports = function (argv) { module.exports = function (argv) {
var source = argv._[0] var source = argv._[0]
var app
var server var server
console.log() console.log()
console.log(chalk.cyan(' \\{^_^}/ hi!')) console.log(chalk.cyan(' \\{^_^}/ hi!'))
function start () { function start (cb) {
console.log() console.log()
console.log(chalk.gray(' Loading', source)) console.log(chalk.gray(' Loading', source))
@ -81,17 +84,34 @@ module.exports = function (argv) {
console.log(chalk.gray(' Done')) console.log(chalk.gray(' Done'))
// Create server and listen // Create app and server
server = createServer(source, data, routes, argv.delay) app = createApp(source, data, routes, argv)
.listen(argv.port, argv.host) server = app.listen(argv.port, argv.host)
// Display server informations // Display server informations
prettyPrint(argv, data, routes) prettyPrint(argv, data, routes)
cb && cb()
}) })
} }
// Start server // Start server
start() start(function () {
// Snapshot
console.log(
chalk.gray(' Type s + enter at any time to create a snapshot of the database')
)
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (chunk) {
if (chunk.trim().toLowerCase() === 's') {
var file = 'db-' + Date.now() + '.json'
app.db.saveSync(file)
console.log(' Saved snapshot to ' + file + '\n')
}
})
// Watch files // Watch files
if (argv.watch) { if (argv.watch) {
@ -99,10 +119,11 @@ module.exports = function (argv) {
console.log() console.log()
watch(argv, function (file) { watch(argv, function (file) {
console.log(chalk.gray(' ' + file + ' has changed, reloading...')) console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
// Restart server
server && server.close() server && server.close()
start() start()
}) })
} }
})
} }

View File

@ -1,18 +1,32 @@
var path = require('path') var path = require('path')
var got = require('got') var got = require('got')
var low = require('lowdb')
var is = require('./is') var is = require('./is')
module.exports = function (source, cb) { module.exports = function (source, cb) {
var data
if (is.URL(source)) { if (is.URL(source)) {
// Load URL
got(source, { json: true }, function (err, data) { got(source, { json: true }, function (err, data) {
cb(err, data) cb(err, data)
}) })
} else {
// Load JS or JSON } else if (is.JS(source)) {
var filename = path.resolve(source) var filename = path.resolve(source)
delete require.cache[filename] delete require.cache[filename]
var data = is.JSON(source) ? require(filename) : require(filename)() data = require(filename)()
cb(null, data) cb(null, data)
} else if (is.JSON(source)) {
data = low(source).object
cb(null, data)
} else {
throw new Error('Unsupported source ' + source)
} }
} }

View File

@ -122,6 +122,7 @@ module.exports = function (db, name) {
if (otherResource if (otherResource
&& otherResource.trim().length > 0 && otherResource.trim().length > 0
&& db.object[otherResource]) { && db.object[otherResource]) {
var query = {} var query = {}
var prop = pluralize.singular(name) + 'Id' var prop = pluralize.singular(name) + 'Id'
query[prop] = id query[prop] = id

View File

@ -28,7 +28,7 @@ describe('cli', function () {
beforeEach(function () { beforeEach(function () {
fs.mkdirSync(tmpDir) fs.mkdirSync(tmpDir)
fs.writeFileSync(dbFile, JSON.stringify({ posts: [] })) fs.writeFileSync(dbFile, JSON.stringify({ posts: [{ 'id': 1, '_id': 2 }] }))
fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' })) fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' }))
}) })
@ -78,15 +78,15 @@ describe('cli', function () {
}) })
describe('db.json -r routes.json', function () { describe('db.json -r routes.json -i _id', function () {
beforeEach(function (done) { beforeEach(function (done) {
child = cli([dbFile, '-r', routesFile]) child = cli([dbFile, '-r', routesFile, '-i', '_id'])
setTimeout(done, 1000) setTimeout(done, 1000)
}) })
it('should use routes.json', function (done) { it('should use routes.json and _id as the identifier', function (done) {
request.get('/blog/posts').expect(200, done) request.get('/blog/posts/2').expect(200, done)
}) })
}) })