mirror of
https://github.com/typicode/json-server.git
synced 2025-07-29 21:23:41 +08:00
Update dependencies and watch
This commit is contained in:
@ -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()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -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)) {
|
||||
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user