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',
description: 'Load routes file'
},
id: {
description: 'Set database id property (e.g. _id)',
default: 'id'
},
delay: {
alias: 'd',
description: 'Add delay to responses (ms)'
},
id: {
alias: 'i',
description: 'Set database id property (e.g. _id)',
default: 'id'
}
})
.boolean('watch')

View File

@ -31,8 +31,8 @@ function prettyPrint (argv, object, rules) {
console.log()
}
function createServer (source, object, routes, delay) {
var server = jsonServer.create()
function createApp (source, object, routes, argv) {
var app = jsonServer.create()
var router = jsonServer.router(
is.JSON(source) ?
@ -40,31 +40,34 @@ function createServer (source, object, routes, delay) {
object
)
server.use(jsonServer.defaults)
app.use(jsonServer.defaults)
if (routes) {
var rewriter = jsonServer.rewriter(routes)
server.use(rewriter)
app.use(rewriter)
}
if (delay) {
server.use(pause(delay))
if (argv.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) {
var source = argv._[0]
var app
var server
console.log()
console.log(chalk.cyan(' \\{^_^}/ hi!'))
function start () {
function start (cb) {
console.log()
console.log(chalk.gray(' Loading', source))
@ -81,28 +84,46 @@ module.exports = function (argv) {
console.log(chalk.gray(' Done'))
// Create server and listen
server = createServer(source, data, routes, argv.delay)
.listen(argv.port, argv.host)
// Create app and server
app = createApp(source, data, routes, argv)
server = app.listen(argv.port, argv.host)
// Display server informations
prettyPrint(argv, data, routes)
cb && cb()
})
}
// Start server
start()
start(function () {
// Watch files
if (argv.watch) {
console.log(chalk.gray(' Watching...'))
console.log()
watch(argv, function (file) {
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
// Restart server
server && server.close()
start()
// 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
if (argv.watch) {
console.log(chalk.gray(' Watching...'))
console.log()
watch(argv, function (file) {
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
server && server.close()
start()
})
}
})
}

View File

@ -1,18 +1,32 @@
var path = require('path')
var got = require('got')
var low = require('lowdb')
var is = require('./is')
module.exports = function (source, cb) {
var data
if (is.URL(source)) {
// Load URL
got(source, { json: true }, function (err, data) {
cb(err, data)
})
} else {
// Load JS or JSON
} else if (is.JS(source)) {
var filename = path.resolve(source)
delete require.cache[filename]
var data = is.JSON(source) ? require(filename) : require(filename)()
data = require(filename)()
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
&& otherResource.trim().length > 0
&& db.object[otherResource]) {
var query = {}
var prop = pluralize.singular(name) + 'Id'
query[prop] = id

View File

@ -28,7 +28,7 @@ describe('cli', function () {
beforeEach(function () {
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/': '/' }))
})
@ -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) {
child = cli([dbFile, '-r', routesFile])
child = cli([dbFile, '-r', routesFile, '-i', '_id'])
setTimeout(done, 1000)
})
it('should use routes.json', function (done) {
request.get('/blog/posts').expect(200, done)
it('should use routes.json and _id as the identifier', function (done) {
request.get('/blog/posts/2').expect(200, done)
})
})