Update dependencies and watch

This commit is contained in:
Typicode
2016-06-30 12:36:51 +02:00
parent 1eb921ecb1
commit 32c76f4a3f
8 changed files with 99 additions and 98 deletions

5
.babelrc Normal file
View File

@ -0,0 +1,5 @@
{
"presets": [
"es2015"
]
}

1
.gitignore vendored
View File

@ -3,4 +3,3 @@ node_modules
tmp
.DS_Store
.idea
generate.js

View File

@ -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",

View File

@ -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()
})
}
}
})
}

View File

@ -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)) {

View File

@ -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)
})
}
}

View File

@ -64,8 +64,9 @@
</div>
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.0.0.min.js"
integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<script>
$(function() {
$.get('db').then(function(data) {

View File

@ -1,10 +1,10 @@
var os = require('os')
var fs = require('fs')
var path = require('path')
var cp = require('child_process')
var assert = require('assert')
var supertest = require('supertest')
var rmrf = require('rimraf')
var express = require('express')
var serverReady = require('server-ready')
var pkg = require('../../package.json')
@ -95,12 +95,17 @@ describe('cli', function () {
})
describe('http://jsonplaceholder.typicode.com/db', function () {
describe('http://localhost:8080/db', function () {
beforeEach(function (done) {
child = cli(['http://jsonplaceholder.typicode.com/db'])
this.timeout(10000)
serverReady(PORT, done)
var fakeServer = express()
fakeServer.get('/db', function (req, res) {
res.jsonp({ posts: [] })
})
fakeServer.listen(8080, function () {
child = cli(['http://localhost:8080/db'])
serverReady(PORT, done)
})
})
it('should support URL file', function (done) {
@ -247,31 +252,29 @@ describe('cli', function () {
// FIXME test fails on OS X and maybe on Windows
// But manually updating db.json works...
if (os.platform() === 'linux') {
describe('--watch db.json -r routes.json', function () {
beforeEach(function (done) {
child = cli(['--watch', dbFile, '-r', routesFile])
serverReady(PORT, done)
})
it('should watch db file', function (done) {
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
setTimeout(function () {
request.get('/foo').expect(200, done)
}, 1000)
})
it('should watch routes file', function (done) {
// Can be very slow
this.timeout(10000)
fs.writeFileSync(routesFile, JSON.stringify({ '/api/': '/' }))
setTimeout(function () {
request.get('/api/posts').expect(200, done)
}, 9000)
})
describe('--watch db.json -r routes.json', function () {
beforeEach(function (done) {
child = cli(['--watch', dbFile, '-r', routesFile])
serverReady(PORT, done)
})
}
it('should watch db file', function (done) {
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
setTimeout(function () {
request.get('/foo').expect(200, done)
}, 1000)
})
it('should watch routes file', function (done) {
// Can be very slow
this.timeout(10000)
fs.writeFileSync(routesFile, JSON.stringify({ '/api/': '/' }))
setTimeout(function () {
request.get('/api/posts').expect(200, done)
}, 9000)
})
})
})