-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload.ls
More file actions
28 lines (25 loc) · 959 Bytes
/
Copy pathload.ls
File metadata and controls
28 lines (25 loc) · 959 Bytes
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
require! {
\fs : { write-file-sync, read-file-sync, exists-sync }
\require-ls
}
read-from-cache = (filename, cb)->
data = JSON.parse read-file-sync(filename, \utf8)
cb null, data
get-filename = (name, params)->
return "#{__dirname}/db/#{name}.json" if (params ? []).length is 0
"#{__dirname}/db/#{name}-#{params.join('-')}.json"
parse-params = (input)->
| typeof! input is \Array => input
| typeof! input is \String => input.split(' ')
| typeof! input is \Object => [input.name] ++ (input.params ? [])
| _ => []
module.exports = (input, cb)->
[name, ...params] = parse-params input
return cb "Name is required" if not name?
filename = get-filename name, params
return read-from-cache filename, cb if exists-sync filename
loader = require "./loaders/#{name}.ls"
err, data <- loader params
return cb err if err?
write-file-sync filename , JSON.stringify(data, null, 4)
cb null, data