-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathrun.js
251 lines (215 loc) · 6.8 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const fs = require('fs')
const path = require('path')
const jph = require('json-parse-helpfulerror')
const _ = require('lodash')
const chalk = require('chalk')
const enableDestroy = require('server-destroy')
const pause = require('connect-pause')
const is = require('./utils/is')
const load = require('./utils/load')
const jsonServer = require('../server')
function prettyPrint(argv, object, rules) {
const root = `http://${argv.host}:${argv.port}`
console.log()
console.log(chalk.bold(' Resources'))
for (const prop in object) {
// skip printing $schema nodes
if (prop === '$schema') continue
console.log(` ${root}/${prop}`)
}
if (rules) {
console.log()
console.log(chalk.bold(' Other routes'))
for (const rule in rules) {
console.log(` ${rule} -> ${rules[rule]}`)
}
}
console.log()
console.log(chalk.bold(' Home'))
console.log(` ${root}`)
console.log()
}
function createApp(db, routes, middlewares, argv) {
const app = jsonServer.create()
const router = jsonServer.router(
db,
argv,
)
const defaultsOpts = {
_noRemoveDependents: argv._noRemoveDependents,
_noDataNext: argv._noDataNext,
_noDbRoute: argv._noDbRoute,
logger: !argv.quiet,
readOnly: argv.readOnly,
noCors: argv.noCors,
noGzip: argv.noGzip,
bodyParser: true,
}
if (argv.static) {
defaultsOpts.static = path.join(process.cwd(), argv.static)
}
const defaults = jsonServer.defaults(defaultsOpts)
app.use(defaults)
if (routes) {
const rewriter = jsonServer.rewriter(routes)
app.use(rewriter)
}
if (middlewares) {
app.use(middlewares)
}
if (argv.delay) {
app.use(pause(argv.delay))
}
router.db._.id = argv.id
app.db = router.db
app.use(router)
return app
}
module.exports = function (argv) {
const source = argv._[0]
let app
let server
if (!fs.existsSync(argv.snapshots)) {
console.log(`Error: snapshots directory ${argv.snapshots} doesn't exist`)
process.exit(1)
}
// noop log fn
if (argv.quiet) {
console.log = () => {}
}
console.log()
console.log(chalk.cyan(' \\{^_^}/ hi!'))
function start(cb) {
console.log()
console.log(chalk.gray(' Loading', source))
server = undefined
// create db and load object, JSON file, JS or HTTP database
return load(source).then((db) => {
// Load additional routes
let routes
if (argv.routes) {
console.log(chalk.gray(' Loading', argv.routes))
routes = JSON.parse(fs.readFileSync(argv.routes))
}
// Load middlewares
let middlewares
if (argv.middlewares) {
middlewares = argv.middlewares.map(function (m) {
console.log(chalk.gray(' Loading', m))
return require(path.resolve(m))
})
}
// Done
console.log(chalk.gray(' Done'))
// Create app and server
app = createApp(db, routes, middlewares, argv)
server = app.listen(argv.port, argv.host)
// Enhance with a destroy function
enableDestroy(server)
// Display server informations
prettyPrint(argv, db.getState(), routes)
// Catch and handle any error occurring in the server process
process.on('uncaughtException', (error) => {
if (error.errno === 'EADDRINUSE')
console.log(
chalk.red(
`Cannot bind to the port ${error.port}. Please specify another port number either through --port argument or through the json-server.json configuration file`,
),
)
else console.log('Some error occurred', error)
process.exit(1)
})
})
}
// Start server
start()
.then(() => {
// Snapshot
console.log(
chalk.gray(
' Type s + enter at any time to create a snapshot of the database',
),
)
// Support nohup
// https://github.com/typicode/json-server/issues/221
process.stdin.on('error', () => {
console.log(` Error, can't read from stdin`)
console.log(` Creating a snapshot from the CLI won't be possible`)
})
process.stdin.setEncoding('utf8')
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`,
)
}
})
// Watch files
if (argv.watch) {
console.log(chalk.gray(' Watching...'))
console.log()
const source = argv._[0]
// 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
const watchedDir = path.dirname(source)
let readError = false
fs.watch(watchedDir, (event, file) => {
// https://github.com/typicode/json-server/issues/420
// file can be null
if (file) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(source)) {
if (is.FILE(watchedFile)) {
let obj
try {
obj = jph.parse(fs.readFileSync(watchedFile))
if (readError) {
console.log(chalk.green(` Read error has been fixed :)`))
readError = false
}
} catch (e) {
readError = true
console.log(chalk.red(` Error reading ${watchedFile}`))
console.error(e.message)
return
}
// Compare .json file content with in memory database
const isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
if (isDatabaseDifferent) {
console.log(
chalk.gray(` ${source} has changed, reloading...`),
)
server && server.destroy(() => start())
}
}
}
}
})
// Watch routes
if (argv.routes) {
const watchedDir = path.dirname(argv.routes)
fs.watch(watchedDir, (event, file) => {
if (file) {
const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(argv.routes)) {
console.log(
chalk.gray(` ${argv.routes} has changed, reloading...`),
)
server && server.destroy(() => start())
}
}
})
}
}
})
.catch((err) => {
console.log(err)
process.exit(1)
})
}