Skip to content

Commit a49db2c

Browse files
committed
Fix CLI regressions and refactor
1 parent 756b8b4 commit a49db2c

File tree

5 files changed

+74
-37
lines changed

5 files changed

+74
-37
lines changed

src/cli/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ module.exports = function () {
2828
alias: 'r',
2929
description: 'Load routes file'
3030
},
31-
id: {
32-
description: 'Set database id property (e.g. _id)',
33-
default: 'id'
34-
},
3531
delay: {
3632
alias: 'd',
3733
description: 'Add delay to responses (ms)'
34+
},
35+
id: {
36+
alias: 'i',
37+
description: 'Set database id property (e.g. _id)',
38+
default: 'id'
3839
}
3940
})
4041
.boolean('watch')

src/cli/run.js

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,43 @@ function prettyPrint (argv, object, rules) {
3131
console.log()
3232
}
3333

34-
function createServer (source, object, routes, delay) {
35-
var server = jsonServer.create()
34+
function createApp (source, object, routes, argv) {
35+
var app = jsonServer.create()
3636

3737
var router = jsonServer.router(
3838
is.JSON(source) ?
3939
source :
4040
object
4141
)
4242

43-
server.use(jsonServer.defaults)
43+
app.use(jsonServer.defaults)
4444

4545
if (routes) {
4646
var rewriter = jsonServer.rewriter(routes)
47-
server.use(rewriter)
47+
app.use(rewriter)
4848
}
4949

50-
if (delay) {
51-
server.use(pause(delay))
50+
if (argv.delay) {
51+
app.use(pause(argv.delay))
5252
}
5353

54-
server.use(router)
54+
router.db._.id = argv.id
55+
app.db = router.db
56+
app.use(router)
5557

56-
return server
58+
return app
5759
}
5860

5961
module.exports = function (argv) {
6062

6163
var source = argv._[0]
64+
var app
6265
var server
6366

6467
console.log()
6568
console.log(chalk.cyan(' \\{^_^}/ hi!'))
6669

67-
function start () {
70+
function start (cb) {
6871
console.log()
6972
console.log(chalk.gray(' Loading', source))
7073

@@ -81,28 +84,46 @@ module.exports = function (argv) {
8184

8285
console.log(chalk.gray(' Done'))
8386

84-
// Create server and listen
85-
server = createServer(source, data, routes, argv.delay)
86-
.listen(argv.port, argv.host)
87+
// Create app and server
88+
app = createApp(source, data, routes, argv)
89+
server = app.listen(argv.port, argv.host)
8790

8891
// Display server informations
8992
prettyPrint(argv, data, routes)
93+
94+
cb && cb()
9095
})
9196
}
9297

9398
// Start server
94-
start()
95-
96-
// Watch files
97-
if (argv.watch) {
98-
console.log(chalk.gray(' Watching...'))
99-
console.log()
100-
watch(argv, function (file) {
101-
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
102-
// Restart server
103-
server && server.close()
104-
start()
99+
start(function () {
100+
101+
// Snapshot
102+
console.log(
103+
chalk.gray(' Type s + enter at any time to create a snapshot of the database')
104+
)
105+
106+
process.stdin.resume()
107+
process.stdin.setEncoding('utf8')
108+
process.stdin.on('data', function (chunk) {
109+
if (chunk.trim().toLowerCase() === 's') {
110+
var file = 'db-' + Date.now() + '.json'
111+
app.db.saveSync(file)
112+
console.log(' Saved snapshot to ' + file + '\n')
113+
}
105114
})
106-
}
115+
116+
// Watch files
117+
if (argv.watch) {
118+
console.log(chalk.gray(' Watching...'))
119+
console.log()
120+
watch(argv, function (file) {
121+
console.log(chalk.gray(' ' + file + ' has changed, reloading...'))
122+
server && server.close()
123+
start()
124+
})
125+
}
126+
127+
})
107128

108129
}

src/cli/utils/load.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
var path = require('path')
22
var got = require('got')
3+
var low = require('lowdb')
34
var is = require('./is')
45

56
module.exports = function (source, cb) {
7+
var data
8+
69
if (is.URL(source)) {
7-
// Load URL
10+
811
got(source, { json: true }, function (err, data) {
912
cb(err, data)
1013
})
11-
} else {
12-
// Load JS or JSON
14+
15+
} else if (is.JS(source)) {
16+
1317
var filename = path.resolve(source)
1418
delete require.cache[filename]
15-
var data = is.JSON(source) ? require(filename) : require(filename)()
19+
data = require(filename)()
20+
cb(null, data)
21+
22+
} else if (is.JSON(source)) {
23+
24+
data = low(source).object
1625
cb(null, data)
26+
27+
} else {
28+
29+
throw new Error('Unsupported source ' + source)
30+
1731
}
1832
}

src/server/router/plural.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ module.exports = function (db, name) {
122122
if (otherResource
123123
&& otherResource.trim().length > 0
124124
&& db.object[otherResource]) {
125+
125126
var query = {}
126127
var prop = pluralize.singular(name) + 'Id'
127128
query[prop] = id

test/cli/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('cli', function () {
2828

2929
beforeEach(function () {
3030
fs.mkdirSync(tmpDir)
31-
fs.writeFileSync(dbFile, JSON.stringify({ posts: [] }))
31+
fs.writeFileSync(dbFile, JSON.stringify({ posts: [{ 'id': 1, '_id': 2 }] }))
3232
fs.writeFileSync(routesFile, JSON.stringify({ '/blog/': '/' }))
3333
})
3434

@@ -78,15 +78,15 @@ describe('cli', function () {
7878

7979
})
8080

81-
describe('db.json -r routes.json', function () {
81+
describe('db.json -r routes.json -i _id', function () {
8282

8383
beforeEach(function (done) {
84-
child = cli([dbFile, '-r', routesFile])
84+
child = cli([dbFile, '-r', routesFile, '-i', '_id'])
8585
setTimeout(done, 1000)
8686
})
8787

88-
it('should use routes.json', function (done) {
89-
request.get('/blog/posts').expect(200, done)
88+
it('should use routes.json and _id as the identifier', function (done) {
89+
request.get('/blog/posts/2').expect(200, done)
9090
})
9191

9292
})

0 commit comments

Comments
 (0)