forked from typicode/json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
161 lines (128 loc) · 3.51 KB
/
Copy pathrun.js
File metadata and controls
161 lines (128 loc) · 3.51 KB
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
var fs = require('fs')
var path = require('path')
var chalk = require('chalk')
var enableDestroy = require('server-destroy')
var is = require('./utils/is')
var load = require('./utils/load')
var watch = require('./watch')
var pause = require('connect-pause')
var jsonServer = require('../server')
function prettyPrint (argv, object, rules) {
var host = argv.host === '0.0.0.0' ? 'localhost' : argv.host
var port = argv.port
var root = 'http://' + host + ':' + port
console.log()
console.log(chalk.bold(' Resources'))
for (var prop in object) {
console.log(' ' + root + '/' + prop)
}
if (rules) {
console.log()
console.log(chalk.bold(' Other routes'))
for (var rule in rules) {
console.log(' ' + rule + ' -> ' + rules[rule])
}
}
console.log()
console.log(chalk.bold(' Home'))
console.log(' ' + root)
console.log()
}
function createApp (source, object, routes, argv) {
var app = jsonServer.create()
var router = jsonServer.router(
is.JSON(source) ?
source :
object
)
var defaultsOpts = {
logger: !argv.quiet,
readOnly: argv.readOnly,
noCors: argv.noCors,
noGzip: argv.noGzip
}
if (argv.static) {
defaultsOpts.static = path.join(process.cwd(), argv.static)
}
var defaults = jsonServer.defaults(defaultsOpts)
app.use(defaults)
if (routes) {
var rewriter = jsonServer.rewriter(routes)
app.use(rewriter)
}
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) {
var source = argv._[0]
var app
var 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 = function () {}
}
console.log()
console.log(chalk.cyan(' \\{^_^}/ hi!'))
function start (cb) {
console.log()
console.log(chalk.gray(' Loading', source))
// Load JSON, JS or HTTP database
load(source, function (err, data) {
if (err) throw err
// Load additional routes
if (argv.routes) {
console.log(chalk.gray(' Loading', argv.routes))
var routes = JSON.parse(fs.readFileSync(argv.routes))
}
console.log(chalk.gray(' Done'))
// Create app and server
app = createApp(source, data, routes, argv)
server = app.listen(argv.port, argv.host)
// Enhance with a destroy function
enableDestroy(server)
// Display server informations
prettyPrint(argv, data, routes)
cb && cb()
})
}
// Start server
start(function () {
// Snapshot
console.log(
chalk.gray(' Type s + enter at any time to create a snapshot of the database')
)
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (chunk) {
if (chunk.trim().toLowerCase() === 's') {
var filename = 'db-' + Date.now() + '.json'
var file = path.join(argv.snapshots, filename)
app
.db
.write(file)
.then(function () {
console.log(' Saved snapshot to ' + path.relative(process.cwd(), file) + '\n')
})
}
})
// Watch files
if (argv.watch) {
console.log(chalk.gray(' Watching...'))
console.log()
watch(argv, function (file) {
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
server && server.destroy()
start()
})
}
})
}