mirror of
https://github.com/typicode/json-server.git
synced 2025-08-02 11:32:47 +08:00
Refactor and update to LowDB 0.4
This commit is contained in:
82
bin/index.js
82
bin/index.js
@ -1,20 +1,74 @@
|
||||
#!/usr/bin/env node
|
||||
var minimist = require('minimist')
|
||||
var updateNotifier = require('update-notifier')
|
||||
var cli = require('../src/cli')
|
||||
var _db = require('underscore-db')
|
||||
var yargs = require('yargs')
|
||||
var chalk = require('chalk')
|
||||
var got = require('got')
|
||||
var pkg = require('../package.json')
|
||||
var server = require('../src')
|
||||
|
||||
var notifier = updateNotifier({packagePath: '../package'})
|
||||
if (notifier.update) notifier.notify()
|
||||
updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify()
|
||||
|
||||
var argv = minimist(process.argv.slice(2), {
|
||||
boolean: ["silent"],
|
||||
alias: {
|
||||
'p': 'port',
|
||||
's': 'silent'
|
||||
},
|
||||
default: {
|
||||
silent: false
|
||||
var argv = yargs
|
||||
.usage('$0 <source>')
|
||||
.help('help').alias('help', 'h')
|
||||
.version(pkg.version, 'version').alias('version', 'v')
|
||||
.options({
|
||||
port: {
|
||||
alias: 'p',
|
||||
description: 'Set port',
|
||||
default: 3000
|
||||
}
|
||||
})
|
||||
})
|
||||
.example('$0 db.json', '')
|
||||
.example('$0 file.js', '')
|
||||
.example('$0 http://example.com/db.json', '')
|
||||
.require(1, 'Missing <source> argument')
|
||||
.argv
|
||||
|
||||
cli.run(argv)
|
||||
function start(object, filename) {
|
||||
for (var prop in object) {
|
||||
console.log('http://localhost:' + port + '/' + chalk.green(prop))
|
||||
}
|
||||
|
||||
console.log(
|
||||
'\nEnter ' + chalk.green('`s`') + ' at any time to create a snapshot of the db\n'
|
||||
)
|
||||
|
||||
process.stdin.resume()
|
||||
process.stdin.setEncoding('utf8')
|
||||
process.stdin.on('data', function (chunk) {
|
||||
if (chunk.trim().toLowerCase() === 's') {
|
||||
var file = 'db-' + Date.now() + '.json'
|
||||
_db.save(object, filename)
|
||||
console.log('\nSaved snapshot to ' + chalk.green(file) + '\n')
|
||||
}
|
||||
})
|
||||
|
||||
server(object, filename).listen(port)
|
||||
}
|
||||
|
||||
var source = argv._[0]
|
||||
var port = process.env.PORT || argv.port
|
||||
|
||||
console.log(chalk.green('\n{^ ^} Yo!\n'))
|
||||
console.log('Loading database from ' + source + '\n')
|
||||
|
||||
if (/\.json$/.test(source)) {
|
||||
var filename = process.cwd() + '/' + source
|
||||
var object = require(filename)
|
||||
start(object, filename)
|
||||
}
|
||||
|
||||
if (/\.js$/.test(source)) {
|
||||
var object = require(process.cwd() + '/' + source)()
|
||||
start(object)
|
||||
}
|
||||
|
||||
if (/^http/.test(source)) {
|
||||
got(source, function(err, data) {
|
||||
if (err) throw err
|
||||
var object = JSON.parse(data)
|
||||
start(object)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user