Skip to content

Commit 32c76f4

Browse files
committed
Update dependencies and watch
1 parent 1eb921e commit 32c76f4

File tree

8 files changed

+97
-96
lines changed

8 files changed

+97
-96
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"es2015"
4+
]
5+
}

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ node_modules
33
tmp
44
.DS_Store
55
.idea
6-
generate.js

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,30 @@
88
"test": "test"
99
},
1010
"dependencies": {
11-
"body-parser": "^1.8.1",
12-
"chalk": "^0.4.0",
11+
"body-parser": "^1.15.2",
12+
"chalk": "^1.1.3",
13+
"chokidar": "^1.6.0",
1314
"compression": "^1.6.0",
1415
"connect-pause": "^0.1.0",
1516
"cors": "^2.3.0",
1617
"errorhandler": "^1.2.0",
1718
"express": "^4.9.5",
18-
"got": "^3.3.0",
19+
"got": "^6.3.0",
1920
"lodash": "^4.11.2",
20-
"lowdb": "^0.13.0-beta.2",
21+
"lowdb": "^0.13.1",
2122
"method-override": "^2.1.2",
2223
"morgan": "^1.3.1",
2324
"node-uuid": "^1.4.2",
2425
"object-assign": "^4.0.1",
25-
"pluralize": "^1.1.2",
26+
"pluralize": "^3.0.0",
2627
"server-destroy": "^1.0.1",
2728
"underscore-db": "^0.10.0",
28-
"update-notifier": "^0.5.0",
29+
"update-notifier": "^1.0.2",
2930
"yargs": "^4.2.0"
3031
},
3132
"devDependencies": {
33+
"babel-cli": "^6.10.1",
34+
"babel-preset-es2015": "^6.9.0",
3235
"husky": "^0.11.4",
3336
"mocha": "^2.2.4",
3437
"rimraf": "^2.4.1",
@@ -41,7 +44,8 @@
4144
"test:cli": "NODE_ENV=test mocha -R spec test/cli/*.js",
4245
"test:server": "NODE_ENV=test mocha -R spec test/server/*.js",
4346
"start": "node bin",
44-
"prepush": "npm t"
47+
"prepush": "npm t",
48+
"build": "babel src -d lib"
4549
},
4650
"repository": {
4751
"type": "git",

src/cli/run.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
var fs = require('fs')
22
var path = require('path')
3+
var _ = require('lodash')
34
var chalk = require('chalk')
5+
var chokidar = require('chokidar')
46
var enableDestroy = require('server-destroy')
7+
var pause = require('connect-pause')
58
var is = require('./utils/is')
69
var load = require('./utils/load')
7-
var watch = require('./watch')
8-
var pause = require('connect-pause')
910
var jsonServer = require('../server')
1011

1112
function prettyPrint (argv, object, rules) {
@@ -37,9 +38,9 @@ function createApp (source, object, routes, argv) {
3738
var app = jsonServer.create()
3839

3940
var router = jsonServer.router(
40-
is.JSON(source) ?
41-
source :
42-
object
41+
is.JSON(source)
42+
? source
43+
: object
4344
)
4445

4546
var defaultsOpts = {
@@ -149,13 +150,41 @@ module.exports = function (argv) {
149150
if (argv.watch) {
150151
console.log(chalk.gray(' Watching...'))
151152
console.log()
152-
watch(argv, function (file) {
153-
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
154-
server && server.destroy()
155-
start()
156-
})
153+
var source = argv._[0]
154+
155+
// Can't watch URL
156+
if (is.URL(source)) throw new Error('Can\'t watch URL')
157+
158+
// Watch .js or .json file
159+
chokidar
160+
.watch(source)
161+
.on('change', function (file) {
162+
if (is.JSON(file)) {
163+
var obj = JSON.parse(fs.readFileSync(file))
164+
// Compare .json file content with in memory database
165+
var isDatabaseDifferent = !_.eq(obj, app.db.getState())
166+
if (isDatabaseDifferent) {
167+
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
168+
server && server.destroy()
169+
start()
170+
}
171+
return
172+
}
173+
174+
server && server.destroy()
175+
start()
176+
})
177+
178+
// Watch routes
179+
if (argv.routes) {
180+
chokidar
181+
.watch(argv.routes)
182+
.on('change', function (file) {
183+
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
184+
server && server.destroy()
185+
start()
186+
})
187+
}
157188
}
158-
159189
})
160-
161190
}

src/cli/utils/load.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ module.exports = function (source, cb) {
99

1010
if (is.URL(source)) {
1111

12-
got(source, { json: true }, function (err, data) {
13-
cb(err, data)
14-
})
12+
got(source, { json: true })
13+
.then(function (response) {
14+
cb(null, response.body)
15+
})
16+
.catch(function (err) {
17+
cb(err)
18+
})
1519

1620
} else if (is.JS(source)) {
1721

src/cli/watch.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/server/public/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ <h4>Issues</h4>
6464
</div>
6565

6666

67-
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script>
68-
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
67+
<script
68+
src="https://code.jquery.com/jquery-3.0.0.min.js"
69+
integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
6970
<script>
7071
$(function() {
7172
$.get('db').then(function(data) {

test/cli/index.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var os = require('os')
21
var fs = require('fs')
32
var path = require('path')
43
var cp = require('child_process')
54
var assert = require('assert')
65
var supertest = require('supertest')
76
var rmrf = require('rimraf')
7+
var express = require('express')
88
var serverReady = require('server-ready')
99
var pkg = require('../../package.json')
1010

@@ -95,12 +95,17 @@ describe('cli', function () {
9595

9696
})
9797

98-
describe('http://jsonplaceholder.typicode.com/db', function () {
98+
describe('http://localhost:8080/db', function () {
9999

100100
beforeEach(function (done) {
101-
child = cli(['http://jsonplaceholder.typicode.com/db'])
102-
this.timeout(10000)
103-
serverReady(PORT, done)
101+
var fakeServer = express()
102+
fakeServer.get('/db', function (req, res) {
103+
res.jsonp({ posts: [] })
104+
})
105+
fakeServer.listen(8080, function () {
106+
child = cli(['http://localhost:8080/db'])
107+
serverReady(PORT, done)
108+
})
104109
})
105110

106111
it('should support URL file', function (done) {
@@ -247,31 +252,29 @@ describe('cli', function () {
247252

248253
// FIXME test fails on OS X and maybe on Windows
249254
// But manually updating db.json works...
250-
if (os.platform() === 'linux') {
251-
describe('--watch db.json -r routes.json', function () {
255+
describe('--watch db.json -r routes.json', function () {
252256

253-
beforeEach(function (done) {
254-
child = cli(['--watch', dbFile, '-r', routesFile])
255-
serverReady(PORT, done)
256-
})
257-
258-
it('should watch db file', function (done) {
259-
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
260-
setTimeout(function () {
261-
request.get('/foo').expect(200, done)
262-
}, 1000)
263-
})
257+
beforeEach(function (done) {
258+
child = cli(['--watch', dbFile, '-r', routesFile])
259+
serverReady(PORT, done)
260+
})
264261

265-
it('should watch routes file', function (done) {
266-
// Can be very slow
267-
this.timeout(10000)
268-
fs.writeFileSync(routesFile, JSON.stringify({ '/api/': '/' }))
269-
setTimeout(function () {
270-
request.get('/api/posts').expect(200, done)
271-
}, 9000)
272-
})
262+
it('should watch db file', function (done) {
263+
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
264+
setTimeout(function () {
265+
request.get('/foo').expect(200, done)
266+
}, 1000)
267+
})
273268

269+
it('should watch routes file', function (done) {
270+
// Can be very slow
271+
this.timeout(10000)
272+
fs.writeFileSync(routesFile, JSON.stringify({ '/api/': '/' }))
273+
setTimeout(function () {
274+
request.get('/api/posts').expect(200, done)
275+
}, 9000)
274276
})
275-
}
277+
278+
})
276279

277280
})

0 commit comments

Comments
 (0)