-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathcache.js
More file actions
44 lines (39 loc) · 846 Bytes
/
cache.js
File metadata and controls
44 lines (39 loc) · 846 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import fs from 'fs';
import _ from 'lodash';
export {
getCacheValueSync,
readCacheSync,
setCacheValueSync,
};
/**
* Reads the entire cache
*/
function readCacheSync (cachePath) {
return JSON.parse(fs.readFileSync(cachePath, 'utf8'));
}
/**
* Sets a cache value and writes the file to disk
*/
function setCacheValueSync (cachePath, key, value) {
var originalCache;
try {
originalCache = readCacheSync(cachePath);
} catch (e) {
originalCache = {};
}
var newCache = _.assign(originalCache, {
[key]: value
});
fs.writeFileSync(cachePath, JSON.stringify(newCache, null, ' '));
return newCache;
}
/**
* Gets a single value from the cache given a key
*/
function getCacheValueSync (cachePath, repoPath) {
try {
let cache = readCacheSync(cachePath);
return cache[repoPath];
} catch (e) {
}
}