Skip to content

Commit a7f7473

Browse files
committed
Add watch option
1 parent 4830fd7 commit a7f7473

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

bin/index.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env node
2+
var fs = require('fs')
3+
var path = require('path')
24
var updateNotifier = require('update-notifier')
35
var _db = require('underscore-db')
46
var yargs = require('yargs')
@@ -11,7 +13,7 @@ updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify()
1113

1214
// Parse arguments
1315
var argv = yargs
14-
.usage('$0 <source>')
16+
.usage('$0 [options] <source>')
1517
.help('help').alias('help', 'h')
1618
.version(pkg.version, 'version').alias('version', 'v')
1719
.options({
@@ -24,38 +26,48 @@ var argv = yargs
2426
alias: 'H',
2527
description: 'Set host',
2628
default: '0.0.0.0'
29+
},
30+
watch: {
31+
alias: 'w',
32+
description: 'Reload database on JSON file change'
2733
}
2834
})
35+
.boolean('w')
2936
.example('$0 db.json', '')
3037
.example('$0 file.js', '')
3138
.example('$0 http://example.com/db.json', '')
3239
.require(1, 'Missing <source> argument')
3340
.argv
3441

35-
// Start server function
36-
function start (object, filename) {
37-
var port = process.env.PORT || argv.port
38-
var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host
39-
42+
function showResources (hostname, port, object) {
4043
for (var prop in object) {
4144
console.log(chalk.gray(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop))
4245
}
46+
}
47+
48+
function start (object, filename) {
49+
var port = process.env.PORT || argv.port
50+
var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host
4351

52+
console.log()
53+
showResources(hostname, port, object)
54+
console.log()
4455
console.log(
45-
'\nYou can now go to ' + chalk.gray('http://' + hostname + ':' + port + '/\n')
56+
'You can now go to ' + chalk.gray('http://' + hostname + ':' + port)
4657
)
47-
58+
console.log()
4859
console.log(
49-
'Enter ' + chalk.cyan('`s`') + ' at any time to create a snapshot of the db\n'
60+
'Enter ' + chalk.cyan('s') + ' at any time to create a snapshot of the db'
5061
)
5162

63+
5264
process.stdin.resume()
5365
process.stdin.setEncoding('utf8')
5466
process.stdin.on('data', function (chunk) {
5567
if (chunk.trim().toLowerCase() === 's') {
5668
var file = 'db-' + Date.now() + '.json'
5769
_db.save(object, file)
58-
console.log('\nSaved snapshot to ' + chalk.cyan(file) + '\n')
70+
console.log('Saved snapshot to ' + chalk.cyan(file) + '\n')
5971
}
6072
})
6173

@@ -66,6 +78,35 @@ function start (object, filename) {
6678
router = jsonServer.router(object)
6779
}
6880

81+
if (filename && argv.watch) {
82+
console.log('Watching', chalk.cyan(source))
83+
84+
var db = router.db
85+
var watchedDir = path.dirname(filename)
86+
var watchedFile = path.basename(filename)
87+
88+
fs.watch(watchedDir, function (event, changedFile) {
89+
// lowdb generates 'rename' event on watchedFile
90+
// using it to know if file has been modified by the user
91+
if (event === 'change' && changedFile === watchedFile) {
92+
console.log(chalk.cyan(source), 'has changed, reloading database')
93+
94+
try {
95+
var watchedFileObject = JSON.parse(fs.readFileSync(filename))
96+
db.object = watchedFileObject
97+
showResources(hostname, port, db.object)
98+
} catch (e) {
99+
console.log('Can\'t parse', chalk.cyan(source))
100+
console.log(e.message)
101+
}
102+
103+
console.log()
104+
}
105+
})
106+
}
107+
console.log()
108+
109+
69110
var server = jsonServer.create()
70111
server.use(jsonServer.defaults)
71112
server.use(router)
@@ -76,24 +117,26 @@ function start (object, filename) {
76117
var source = argv._[0]
77118

78119
// Say hi, load file and start server
79-
console.log(chalk.cyan('{^_^} Hi!\n'))
120+
console.log(chalk.cyan(' {^_^} Hi!\n'))
80121
console.log('Loading database from ' + chalk.cyan(source))
81122

82-
if (/\.json$/.test(source)) {
123+
// Remote source
124+
if (/^(http|https):/.test(source)) {
125+
got(source, function (err, data) {
126+
if (err) {
127+
console.log('Error', err)
128+
process.exit(1)
129+
}
130+
var object = JSON.parse(data)
131+
start(object)
132+
})
133+
// JSON file
134+
} else if (/\.json$/.test(source)) {
83135
var filename = process.cwd() + '/' + source
84136
var object = require(filename)
85137
start(object, filename)
86-
}
87-
88-
if (/\.js$/.test(source)) {
138+
// JS file
139+
} else if (/\.js$/.test(source)) {
89140
var object = require(process.cwd() + '/' + source)()
90141
start(object)
91142
}
92-
93-
if (/^http/.test(source)) {
94-
got(source, function (err, data) {
95-
if (err) throw err
96-
var object = JSON.parse(data)
97-
start(object)
98-
})
99-
}

0 commit comments

Comments
 (0)