Merge pull request #140 from syzer/master

FIXED #135 generate.js with watch
This commit is contained in:
typicode
2015-07-01 00:20:47 +02:00
2 changed files with 50 additions and 42 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
node_modules node_modules
db.json db.json
.DS_Store .DS_Store
.idea
generate.js

View File

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