-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (35 loc) · 889 Bytes
/
index.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
const fs = require('fs-promise')
const { get, set, isUndefined } = require('lodash')
const mkdirp = require('mkdirp')
const getDirName = require('path').dirname
function loadAt(filePath, jsonPath) {
return fs.readJson(filePath).then(function(json) {
if (isUndefined(jsonPath)) {
return json
}
const value = get(json, jsonPath)
if (!isUndefined(value)) {
return value
} else {
throw new Error('Invalid JSON Path')
}
})
}
function writeAt(filePath, jsonPath, value) {
mkdirp.sync(getDirName(filePath))
return fs
.readJson(filePath)
.then(function(json) {
set(json, jsonPath, value)
return fs.writeJson(filePath, json)
})
.catch(function(error) {
let json = {}
set(json, jsonPath, value)
return fs.writeJson(filePath, json)
})
}
module.exports = {
loadAt: loadAt,
writeAt: writeAt
}