mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
FIXED #135, --watch with generate.js
This commit is contained in:
88
bin/index.js
88
bin/index.js
@ -8,45 +8,46 @@ var chalk = require('chalk')
|
|||||||
var got = require('got')
|
var got = require('got')
|
||||||
var pkg = require('../package.json')
|
var pkg = require('../package.json')
|
||||||
var jsonServer = require('../src')
|
var jsonServer = require('../src')
|
||||||
|
var jsMockGenerator = null
|
||||||
|
|
||||||
updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify()
|
updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify()
|
||||||
|
|
||||||
// Parse arguments
|
// Parse arguments
|
||||||
var argv = yargs
|
var argv = yargs
|
||||||
.usage('$0 [options] <source>')
|
.usage('$0 [options] <source>')
|
||||||
.options({
|
.options({
|
||||||
port: {
|
port: {
|
||||||
alias: 'p',
|
alias: 'p',
|
||||||
description: 'Set port',
|
description: 'Set port',
|
||||||
default: 3000
|
default: 3000
|
||||||
},
|
},
|
||||||
host: {
|
host: {
|
||||||
alias: 'H',
|
alias: 'H',
|
||||||
description: 'Set host',
|
description: 'Set host',
|
||||||
default: '0.0.0.0'
|
default: '0.0.0.0'
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
alias: 'w',
|
alias: 'w',
|
||||||
description: 'Reload database on JSON file change'
|
description: 'Reload database on JSON file change'
|
||||||
},
|
},
|
||||||
routes: {
|
routes: {
|
||||||
alias: 'r',
|
alias: 'r',
|
||||||
description: 'Load routes file'
|
description: 'Load routes file'
|
||||||
},
|
},
|
||||||
id: {
|
id: {
|
||||||
description: 'Set database id property (e.g. _id)',
|
description: 'Set database id property (e.g. _id)',
|
||||||
default: 'id'
|
default: 'id'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.boolean('watch')
|
.boolean('watch')
|
||||||
.help('help').alias('help', 'h')
|
.help('help').alias('help', 'h')
|
||||||
.version(pkg.version).alias('version', 'v')
|
.version(pkg.version).alias('version', 'v')
|
||||||
.example('$0 db.json', '')
|
.example('$0 db.json', '')
|
||||||
.example('$0 file.js', '')
|
.example('$0 file.js', '')
|
||||||
.example('$0 http://example.com/db.json', '')
|
.example('$0 http://example.com/db.json', '')
|
||||||
.epilog('https://github.com/typicode/json-server')
|
.epilog('https://github.com/typicode/json-server')
|
||||||
.require(1, 'Missing <source> argument')
|
.require(1, 'Missing <source> argument')
|
||||||
.argv
|
.argv
|
||||||
|
|
||||||
function showResources (hostname, port, object) {
|
function showResources (hostname, port, object) {
|
||||||
for (var prop in object) {
|
for (var prop in object) {
|
||||||
@ -62,11 +63,11 @@ function start (object, filename) {
|
|||||||
showResources(hostname, port, object)
|
showResources(hostname, port, object)
|
||||||
console.log()
|
console.log()
|
||||||
console.log(
|
console.log(
|
||||||
'You can now go to ' + chalk.gray('http://' + hostname + ':' + port)
|
'You can now go to ' + chalk.gray('http://' + hostname + ':' + port)
|
||||||
)
|
)
|
||||||
console.log()
|
console.log()
|
||||||
console.log(
|
console.log(
|
||||||
'Enter ' + chalk.cyan('s') + ' at any time to create a snapshot of the db'
|
'Enter ' + chalk.cyan('s') + ' at any time to create a snapshot of the db'
|
||||||
)
|
)
|
||||||
|
|
||||||
// Snapshot
|
// Snapshot
|
||||||
@ -84,7 +85,7 @@ function start (object, filename) {
|
|||||||
var router = jsonServer.router(filename ? filename : object)
|
var router = jsonServer.router(filename ? filename : object)
|
||||||
|
|
||||||
// Watcher
|
// Watcher
|
||||||
if (filename && argv.watch) {
|
if (argv.watch) {
|
||||||
console.log('Watching', chalk.cyan(source))
|
console.log('Watching', chalk.cyan(source))
|
||||||
|
|
||||||
var db = router.db
|
var db = router.db
|
||||||
@ -94,12 +95,16 @@ function start (object, filename) {
|
|||||||
fs.watch(watchedDir, function (event, changedFile) {
|
fs.watch(watchedDir, function (event, changedFile) {
|
||||||
// lowdb generates 'rename' event on watchedFile
|
// lowdb generates 'rename' event on watchedFile
|
||||||
// using it to know if file has been modified by the user
|
// using it to know if file has been modified by the user
|
||||||
if (event === 'change' && changedFile === watchedFile) {
|
if ((event === 'change' || event === 'rename') && (changedFile === watchedFile || changedFile === source)) {
|
||||||
console.log(chalk.cyan(source), 'has changed, reloading database')
|
console.log(chalk.cyan(source), 'has changed, reloading database')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var watchedFileObject = JSON.parse(fs.readFileSync(filename))
|
if (filename) {
|
||||||
db.object = watchedFileObject
|
db.object = JSON.parse(fs.readFileSync(filename))
|
||||||
|
} else {
|
||||||
|
require.cache[jsMockGenerator] = null
|
||||||
|
db.object = require(jsMockGenerator)()
|
||||||
|
}
|
||||||
showResources(hostname, port, db.object)
|
showResources(hostname, port, db.object)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Can\'t parse', chalk.cyan(source))
|
console.log('Can\'t parse', chalk.cyan(source))
|
||||||
@ -154,6 +159,7 @@ if (/^(http|https):/.test(source)) {
|
|||||||
start(object, filename)
|
start(object, filename)
|
||||||
// JS file
|
// JS file
|
||||||
} else if (/\.js$/.test(source)) {
|
} else if (/\.js$/.test(source)) {
|
||||||
var object = require(process.cwd() + '/' + source)()
|
jsMockGenerator = process.cwd() + '/' + source
|
||||||
|
var object = require(jsMockGenerator)()
|
||||||
start(object)
|
start(object)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user