From a49db2c5c3ec7f7fc64f16bbccefea9fa0702ecf Mon Sep 17 00:00:00 2001 From: Typicode Date: Thu, 23 Jul 2015 08:04:29 +0200 Subject: [PATCH] Fix CLI regressions and refactor --- src/cli/index.js | 9 ++--- src/cli/run.js | 67 ++++++++++++++++++++++++------------- src/cli/utils/load.js | 22 +++++++++--- src/server/router/plural.js | 1 + test/cli/index.js | 10 +++--- 5 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/cli/index.js b/src/cli/index.js index c45d3b7..6609324 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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') diff --git a/src/cli/run.js b/src/cli/run.js index a27fe4a..4a930a3 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -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() + }) + } + + }) } diff --git a/src/cli/utils/load.js b/src/cli/utils/load.js index 98ca5d3..ab83a4d 100644 --- a/src/cli/utils/load.js +++ b/src/cli/utils/load.js @@ -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) + } } diff --git a/src/server/router/plural.js b/src/server/router/plural.js index d268bae..23cd28a 100644 --- a/src/server/router/plural.js +++ b/src/server/router/plural.js @@ -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 diff --git a/test/cli/index.js b/test/cli/index.js index 4f09a0b..a76dd60 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -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) }) })