From 09350c3f930b9a89c21068f3fa9fa403c8dfc9bc Mon Sep 17 00:00:00 2001 From: sonic Date: Fri, 15 May 2015 02:34:00 +0200 Subject: [PATCH 1/6] Enhance query parameters parsing --- src/utils.js | 2 +- test/utils.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 807eb426c..ec86aeebc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -9,7 +9,7 @@ _.mixin(_inflections) // '1' -> 1 function toNative (value) { if (typeof value === 'string') { - if (value === '' || value.trim() !== value) { + if (value === '' || value.trim() !== value || value[0] === '0') { return value } else if (value === 'true' || value === 'false') { return value === 'true' diff --git a/test/utils.js b/test/utils.js index 111dc93ab..ccfe1ec04 100644 --- a/test/utils.js +++ b/test/utils.js @@ -41,6 +41,7 @@ describe('utils', function () { assert.strictEqual(utils.toNative(''), '') assert.strictEqual(utils.toNative('\t\n'), '\t\n') assert.strictEqual(utils.toNative('1 '), '1 ') + assert.strictEqual(utils.toNative('01'), '01') assert.strictEqual(utils.toNative(' 1'), ' 1') assert.strictEqual(utils.toNative('string'), 'string') assert.strictEqual(utils.toNative(1), 1) From 939eb8d71f873c9eb0425cf51343c328e778e8e2 Mon Sep 17 00:00:00 2001 From: sonic Date: Fri, 15 May 2015 02:34:17 +0200 Subject: [PATCH 2/6] Add watch option --- bin/index.js | 89 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 23 deletions(-) diff --git a/bin/index.js b/bin/index.js index 1c49755c0..5386188df 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,4 +1,6 @@ #!/usr/bin/env node +var fs = require('fs') +var path = require('path') var updateNotifier = require('update-notifier') var _db = require('underscore-db') var yargs = require('yargs') @@ -11,7 +13,7 @@ updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify() // Parse arguments var argv = yargs - .usage('$0 ') + .usage('$0 [options] ') .help('help').alias('help', 'h') .version(pkg.version, 'version').alias('version', 'v') .options({ @@ -24,38 +26,48 @@ var argv = yargs alias: 'H', description: 'Set host', default: '0.0.0.0' + }, + watch: { + alias: 'w', + description: 'Reload database on JSON file change' } }) + .boolean('w') .example('$0 db.json', '') .example('$0 file.js', '') .example('$0 http://example.com/db.json', '') .require(1, 'Missing argument') .argv -// Start server function -function start (object, filename) { - var port = process.env.PORT || argv.port - var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host - +function showResources (hostname, port, object) { for (var prop in object) { console.log(chalk.gray(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop)) } +} + +function start (object, filename) { + var port = process.env.PORT || argv.port + var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host + console.log() + showResources(hostname, port, object) + console.log() console.log( - '\nYou can now go to ' + chalk.gray('http://' + hostname + ':' + port + '/\n') + 'You can now go to ' + chalk.gray('http://' + hostname + ':' + port) ) - + console.log() console.log( - 'Enter ' + chalk.cyan('`s`') + ' at any time to create a snapshot of the db\n' + 'Enter ' + chalk.cyan('s') + ' at any time to create a snapshot of the db' ) + process.stdin.resume() process.stdin.setEncoding('utf8') process.stdin.on('data', function (chunk) { if (chunk.trim().toLowerCase() === 's') { var file = 'db-' + Date.now() + '.json' _db.save(object, file) - console.log('\nSaved snapshot to ' + chalk.cyan(file) + '\n') + console.log('Saved snapshot to ' + chalk.cyan(file) + '\n') } }) @@ -66,6 +78,35 @@ function start (object, filename) { router = jsonServer.router(object) } + if (filename && argv.watch) { + console.log('Watching', chalk.cyan(source)) + + var db = router.db + var watchedDir = path.dirname(filename) + var watchedFile = path.basename(filename) + + fs.watch(watchedDir, function (event, changedFile) { + // lowdb generates 'rename' event on watchedFile + // using it to know if file has been modified by the user + if (event === 'change' && changedFile === watchedFile) { + console.log(chalk.cyan(source), 'has changed, reloading database') + + try { + var watchedFileObject = JSON.parse(fs.readFileSync(filename)) + db.object = watchedFileObject + showResources(hostname, port, db.object) + } catch (e) { + console.log('Can\'t parse', chalk.cyan(source)) + console.log(e.message) + } + + console.log() + } + }) + } + console.log() + + var server = jsonServer.create() server.use(jsonServer.defaults) server.use(router) @@ -76,24 +117,26 @@ function start (object, filename) { var source = argv._[0] // Say hi, load file and start server -console.log(chalk.cyan('{^_^} Hi!\n')) +console.log(chalk.cyan(' {^_^} Hi!\n')) console.log('Loading database from ' + chalk.cyan(source)) -if (/\.json$/.test(source)) { +// Remote source +if (/^(http|https):/.test(source)) { + got(source, function (err, data) { + if (err) { + console.log('Error', err) + process.exit(1) + } + var object = JSON.parse(data) + start(object) + }) +// JSON file +} else if (/\.json$/.test(source)) { var filename = process.cwd() + '/' + source var object = require(filename) start(object, filename) -} - -if (/\.js$/.test(source)) { +// JS file +} else if (/\.js$/.test(source)) { var object = require(process.cwd() + '/' + source)() start(object) } - -if (/^http/.test(source)) { - got(source, function (err, data) { - if (err) throw err - var object = JSON.parse(data) - start(object) - }) -} From 58c49f1c9de64067f10f99b77bfd6eb4a4dd7988 Mon Sep 17 00:00:00 2001 From: sonic Date: Fri, 15 May 2015 02:35:06 +0200 Subject: [PATCH 3/6] Fix syntax --- bin/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/bin/index.js b/bin/index.js index 5386188df..b06e941f1 100755 --- a/bin/index.js +++ b/bin/index.js @@ -60,7 +60,6 @@ function start (object, filename) { 'Enter ' + chalk.cyan('s') + ' at any time to create a snapshot of the db' ) - process.stdin.resume() process.stdin.setEncoding('utf8') process.stdin.on('data', function (chunk) { @@ -106,7 +105,6 @@ function start (object, filename) { } console.log() - var server = jsonServer.create() server.use(jsonServer.defaults) server.use(router) From a88f5f8ae7e70af24d87c407b4bef37ca453c1ec Mon Sep 17 00:00:00 2001 From: sonic Date: Fri, 15 May 2015 02:36:31 +0200 Subject: [PATCH 4/6] 0.7.7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 780275168..1ea3d7bb6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-server", - "version": "0.7.6", + "version": "0.7.7", "description": "Serves JSON files through REST routes.", "main": "./src/index.js", "bin": "./bin/index.js", From 2cccdb93b4715ddd198e7fdce52eb30f77dfb559 Mon Sep 17 00:00:00 2001 From: sonic Date: Fri, 15 May 2015 02:47:57 +0200 Subject: [PATCH 5/6] Fix query parameters conversion --- src/utils.js | 4 +++- test/utils.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index ec86aeebc..a954acfdf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -9,7 +9,9 @@ _.mixin(_inflections) // '1' -> 1 function toNative (value) { if (typeof value === 'string') { - if (value === '' || value.trim() !== value || value[0] === '0') { + if (value === '' + || value.trim() !== value + || (value.length > 1 && value[0] === '0')) { return value } else if (value === 'true' || value === 'false') { return value === 'true' diff --git a/test/utils.js b/test/utils.js index ccfe1ec04..0e7bfdd3b 100644 --- a/test/utils.js +++ b/test/utils.js @@ -36,6 +36,7 @@ describe('utils', function () { it('should convert string to native type', function () { // should convert assert.strictEqual(utils.toNative('1'), 1) + assert.strictEqual(utils.toNative('0'), 0) assert.strictEqual(utils.toNative('true'), true) // should not convert assert.strictEqual(utils.toNative(''), '') From df7da5ddf04315362caff8f9aaa19a49586fcd94 Mon Sep 17 00:00:00 2001 From: sonic Date: Fri, 15 May 2015 02:48:27 +0200 Subject: [PATCH 6/6] 0.7.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1ea3d7bb6..28dccd16d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-server", - "version": "0.7.7", + "version": "0.7.8", "description": "Serves JSON files through REST routes.", "main": "./src/index.js", "bin": "./bin/index.js",