-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-merge.js
More file actions
49 lines (38 loc) · 1.08 KB
/
split-merge.js
File metadata and controls
49 lines (38 loc) · 1.08 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
const path = require('path')
const fs = require('fs')
const proc = require('child_process')
const DEFAULT_MODELS_DIR = './models'
module.exports = { split, merge }
function split (models, dir) {
dir = path.resolve(dir || DEFAULT_MODELS_DIR)
models.forEach(function (m) {
const fname = toFilePath(dir, m.id)
fs.writeFile(path.resolve(fname), JSON.stringify(m, null, 2), rethrow)
})
}
function merge (modelsDir, outFilePath) {
if (typeof outFilePath === 'undefined') {
outFilePath = modelsDir
modelsDir = null
}
modelsDir = path.resolve(modelsDir || DEFAULT_MODELS_DIR)
proc.exec(`git ls-files "${modelsDir}/*.json"`, function (err, result) {
if (err) throw err
const models = result.trim().split('\n')
.map(p => `\nrequire('./${p}')`)
.join(',')
const contents =
`const models = module.exports = [${models}]
models.forEach(function (m) {
models[m.id] = m
})
`
fs.writeFile(path.resolve(outFilePath), contents)
})
}
function toFilePath (dir, id) {
return path.join(dir, id + '.json')
}
function rethrow (err) {
if (err) throw err
}