mirror of
https://github.com/typicode/json-server.git
synced 2025-07-29 21:23:41 +08:00
Add prettier
This commit is contained in:
@ -3,7 +3,7 @@ const yargs = require('yargs')
|
||||
const run = require('./run')
|
||||
const pkg = require('../../package.json')
|
||||
|
||||
module.exports = function () {
|
||||
module.exports = function() {
|
||||
updateNotifier({ pkg }).notify()
|
||||
|
||||
const argv = yargs
|
||||
@ -83,14 +83,15 @@ module.exports = function () {
|
||||
.boolean('quiet')
|
||||
.boolean('no-cors')
|
||||
.boolean('no-gzip')
|
||||
.help('help').alias('help', 'h')
|
||||
.version(pkg.version).alias('version', 'v')
|
||||
.help('help')
|
||||
.alias('help', 'h')
|
||||
.version(pkg.version)
|
||||
.alias('version', 'v')
|
||||
.example('$0 db.json', '')
|
||||
.example('$0 file.js', '')
|
||||
.example('$0 http://example.com/db.json', '')
|
||||
.epilog('https://github.com/typicode/json-server')
|
||||
.require(1, 'Missing <source> argument')
|
||||
.argv
|
||||
.require(1, 'Missing <source> argument').argv
|
||||
|
||||
run(argv)
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ const load = require('./utils/load')
|
||||
const example = require('./example.json')
|
||||
const jsonServer = require('../server')
|
||||
|
||||
function prettyPrint (argv, object, rules) {
|
||||
function prettyPrint(argv, object, rules) {
|
||||
const host = argv.host === '0.0.0.0' ? 'localhost' : argv.host
|
||||
const port = argv.port
|
||||
const root = `http://${host}:${port}`
|
||||
@ -35,7 +35,7 @@ function prettyPrint (argv, object, rules) {
|
||||
console.log()
|
||||
}
|
||||
|
||||
function createApp (source, object, routes, middlewares, argv) {
|
||||
function createApp(source, object, routes, middlewares, argv) {
|
||||
const app = jsonServer.create()
|
||||
|
||||
let router
|
||||
@ -86,7 +86,7 @@ function createApp (source, object, routes, middlewares, argv) {
|
||||
return app
|
||||
}
|
||||
|
||||
module.exports = function (argv) {
|
||||
module.exports = function(argv) {
|
||||
const source = argv._[0]
|
||||
let app
|
||||
let server
|
||||
@ -104,7 +104,7 @@ module.exports = function (argv) {
|
||||
console.log()
|
||||
console.log(chalk.cyan(' \\{^_^}/ hi!'))
|
||||
|
||||
function start (cb) {
|
||||
function start(cb) {
|
||||
console.log()
|
||||
|
||||
// Be nice and create a default db.json if it doesn't exist
|
||||
@ -131,7 +131,7 @@ module.exports = function (argv) {
|
||||
// Load middlewares
|
||||
let middlewares
|
||||
if (argv.middlewares) {
|
||||
middlewares = argv.middlewares.map(function (m) {
|
||||
middlewares = argv.middlewares.map(function(m) {
|
||||
console.log(chalk.gray(' Loading', m))
|
||||
return require(path.resolve(m))
|
||||
})
|
||||
@ -158,7 +158,9 @@ module.exports = function (argv) {
|
||||
start(() => {
|
||||
// Snapshot
|
||||
console.log(
|
||||
chalk.gray(' Type s + enter at any time to create a snapshot of the database')
|
||||
chalk.gray(
|
||||
' Type s + enter at any time to create a snapshot of the database'
|
||||
)
|
||||
)
|
||||
|
||||
// Support nohup
|
||||
@ -168,13 +170,15 @@ module.exports = function (argv) {
|
||||
console.log(` Creating a snapshot from the CLI won't be possible`)
|
||||
})
|
||||
process.stdin.setEncoding('utf8')
|
||||
process.stdin.on('data', (chunk) => {
|
||||
process.stdin.on('data', chunk => {
|
||||
if (chunk.trim().toLowerCase() === 's') {
|
||||
const filename = 'db-' + Date.now() + '.json'
|
||||
const file = path.join(argv.snapshots, filename)
|
||||
const state = app.db.getState()
|
||||
fs.writeFileSync(file, JSON.stringify(state, null, 2), 'utf-8')
|
||||
console.log(` Saved snapshot to ${path.relative(process.cwd(), file)}\n`)
|
||||
console.log(
|
||||
` Saved snapshot to ${path.relative(process.cwd(), file)}\n`
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@ -185,7 +189,7 @@ module.exports = function (argv) {
|
||||
const source = argv._[0]
|
||||
|
||||
// Can't watch URL
|
||||
if (is.URL(source)) throw new Error('Can\'t watch URL')
|
||||
if (is.URL(source)) throw new Error("Can't watch URL")
|
||||
|
||||
// Watch .js or .json file
|
||||
// Since lowdb uses atomic writing, directory is watched instead of file
|
||||
@ -231,7 +235,9 @@ module.exports = function (argv) {
|
||||
if (file) {
|
||||
const watchedFile = path.resolve(watchedDir, file)
|
||||
if (watchedFile === path.resolve(argv.routes)) {
|
||||
console.log(chalk.gray(` ${argv.routes} has changed, reloading...`))
|
||||
console.log(
|
||||
chalk.gray(` ${argv.routes} has changed, reloading...`)
|
||||
)
|
||||
server && server.destroy()
|
||||
start()
|
||||
}
|
||||
|
@ -4,14 +4,14 @@ module.exports = {
|
||||
URL
|
||||
}
|
||||
|
||||
function JSON (s) {
|
||||
function JSON(s) {
|
||||
return !URL(s) && /\.json$/.test(s)
|
||||
}
|
||||
|
||||
function JS (s) {
|
||||
function JS(s) {
|
||||
return !URL(s) && /\.js$/.test(s)
|
||||
}
|
||||
|
||||
function URL (s) {
|
||||
function URL(s) {
|
||||
return /^(http|https):/.test(s)
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ const low = require('lowdb')
|
||||
const fileAsync = require('lowdb/lib/storages/file-async')
|
||||
const is = require('./is')
|
||||
|
||||
module.exports = function (source, cb) {
|
||||
module.exports = function(source, cb) {
|
||||
if (is.URL(source)) {
|
||||
// Load remote data
|
||||
const opts = {
|
||||
@ -23,7 +23,9 @@ module.exports = function (source, cb) {
|
||||
const dataFn = require(filename)
|
||||
|
||||
if (typeof dataFn !== 'function') {
|
||||
throw new Error('The database is a JavaScript file but the export is not a function.')
|
||||
throw new Error(
|
||||
'The database is a JavaScript file but the export is not a function.'
|
||||
)
|
||||
}
|
||||
|
||||
// Run dataFn to generate data
|
||||
|
Reference in New Issue
Block a user