diff --git a/.babelrc b/.babelrc
new file mode 100644
index 0000000..3c078e9
--- /dev/null
+++ b/.babelrc
@@ -0,0 +1,5 @@
+{
+ "presets": [
+ "es2015"
+ ]
+}
diff --git a/.gitignore b/.gitignore
index 4beb073..835d515 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,3 @@ node_modules
tmp
.DS_Store
.idea
-generate.js
diff --git a/package.json b/package.json
index 9bea5a6..434c51c 100644
--- a/package.json
+++ b/package.json
@@ -8,27 +8,30 @@
"test": "test"
},
"dependencies": {
- "body-parser": "^1.8.1",
- "chalk": "^0.4.0",
+ "body-parser": "^1.15.2",
+ "chalk": "^1.1.3",
+ "chokidar": "^1.6.0",
"compression": "^1.6.0",
"connect-pause": "^0.1.0",
"cors": "^2.3.0",
"errorhandler": "^1.2.0",
"express": "^4.9.5",
- "got": "^3.3.0",
+ "got": "^6.3.0",
"lodash": "^4.11.2",
- "lowdb": "^0.13.0-beta.2",
+ "lowdb": "^0.13.1",
"method-override": "^2.1.2",
"morgan": "^1.3.1",
"node-uuid": "^1.4.2",
"object-assign": "^4.0.1",
- "pluralize": "^1.1.2",
+ "pluralize": "^3.0.0",
"server-destroy": "^1.0.1",
"underscore-db": "^0.10.0",
- "update-notifier": "^0.5.0",
+ "update-notifier": "^1.0.2",
"yargs": "^4.2.0"
},
"devDependencies": {
+ "babel-cli": "^6.10.1",
+ "babel-preset-es2015": "^6.9.0",
"husky": "^0.11.4",
"mocha": "^2.2.4",
"rimraf": "^2.4.1",
@@ -41,7 +44,8 @@
"test:cli": "NODE_ENV=test mocha -R spec test/cli/*.js",
"test:server": "NODE_ENV=test mocha -R spec test/server/*.js",
"start": "node bin",
- "prepush": "npm t"
+ "prepush": "npm t",
+ "build": "babel src -d lib"
},
"repository": {
"type": "git",
diff --git a/src/cli/run.js b/src/cli/run.js
index d35e1de..70c6e04 100644
--- a/src/cli/run.js
+++ b/src/cli/run.js
@@ -1,11 +1,12 @@
var fs = require('fs')
var path = require('path')
+var _ = require('lodash')
var chalk = require('chalk')
+var chokidar = require('chokidar')
var enableDestroy = require('server-destroy')
+var pause = require('connect-pause')
var is = require('./utils/is')
var load = require('./utils/load')
-var watch = require('./watch')
-var pause = require('connect-pause')
var jsonServer = require('../server')
function prettyPrint (argv, object, rules) {
@@ -37,9 +38,9 @@ function createApp (source, object, routes, argv) {
var app = jsonServer.create()
var router = jsonServer.router(
- is.JSON(source) ?
- source :
- object
+ is.JSON(source)
+ ? source
+ : object
)
var defaultsOpts = {
@@ -149,13 +150,41 @@ module.exports = function (argv) {
if (argv.watch) {
console.log(chalk.gray(' Watching...'))
console.log()
- watch(argv, function (file) {
- console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
- server && server.destroy()
- start()
- })
+ var source = argv._[0]
+
+ // Can't watch URL
+ if (is.URL(source)) throw new Error('Can\'t watch URL')
+
+ // Watch .js or .json file
+ chokidar
+ .watch(source)
+ .on('change', function (file) {
+ if (is.JSON(file)) {
+ var obj = JSON.parse(fs.readFileSync(file))
+ // Compare .json file content with in memory database
+ var isDatabaseDifferent = !_.eq(obj, app.db.getState())
+ if (isDatabaseDifferent) {
+ console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
+ server && server.destroy()
+ start()
+ }
+ return
+ }
+
+ server && server.destroy()
+ start()
+ })
+
+ // Watch routes
+ if (argv.routes) {
+ chokidar
+ .watch(argv.routes)
+ .on('change', function (file) {
+ console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
+ server && server.destroy()
+ start()
+ })
+ }
}
-
})
-
}
diff --git a/src/cli/utils/load.js b/src/cli/utils/load.js
index df2fec8..c37e5c6 100644
--- a/src/cli/utils/load.js
+++ b/src/cli/utils/load.js
@@ -9,9 +9,13 @@ module.exports = function (source, cb) {
if (is.URL(source)) {
- got(source, { json: true }, function (err, data) {
- cb(err, data)
- })
+ got(source, { json: true })
+ .then(function (response) {
+ cb(null, response.body)
+ })
+ .catch(function (err) {
+ cb(err)
+ })
} else if (is.JS(source)) {
diff --git a/src/cli/watch.js b/src/cli/watch.js
deleted file mode 100644
index 5b452cb..0000000
--- a/src/cli/watch.js
+++ /dev/null
@@ -1,44 +0,0 @@
-var fs = require('fs')
-var path = require('path')
-var is = require('./utils/is')
-
-module.exports = watch
-
-// Because JSON file can be modified by the server, we need to be able to
-// distinguish between user modification vs server modification.
-// When the server modifies the JSON file, it generates a rename event.
-// When the user modifies the JSON file, it generate a change event.
-function watchDB (file, cb) {
- var watchedDir = path.dirname(file)
- var watchedFile = path.basename(file)
-
- fs.watch(watchedDir, function (event, changedFile) {
- if (event === 'change' && changedFile === watchedFile) cb()
- })
-}
-
-function watchJS (file, cb) {
- fs.watchFile(file, cb)
-}
-
-function watchSource (source, cb) {
- if (is.JSON(source)) {
- return watchDB(source, cb)
- }
- if (is.JS(source)) return watchJS(source, cb)
- if (is.URL(source)) throw new Error('Can\'t watch URL')
-}
-
-function watch (argv, cb) {
- var source = argv._[0]
-
- watchSource(source, function () {
- cb(source)
- })
-
- if (argv.routes) {
- fs.watchFile(argv.routes, function () {
- cb(argv.routes)
- })
- }
-}
diff --git a/src/server/public/index.html b/src/server/public/index.html
index 84db060..ecd16b4 100644
--- a/src/server/public/index.html
+++ b/src/server/public/index.html
@@ -64,8 +64,9 @@
-
-
+