-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcache.js
More file actions
50 lines (42 loc) · 1.27 KB
/
Copy pathcache.js
File metadata and controls
50 lines (42 loc) · 1.27 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
50
const debug = require('debug')('brandable_css:cache')
const _ = require('lodash')
const CONFIG = require('./config')
const SASS_STYLE = require('./sass_style')
const {readJsonSync, outputJsonAsync} = require('fs-extra-promise')
const caches = ['file_checksums', 'bundles_with_deps']
let cache = {
saveAll: async function () {
return Promise.all(caches.map((cacheName) => cache[cacheName].save()))
}
}
function initCache (name) {
const filename = CONFIG.paths[name] + SASS_STYLE
let self = {
isSaved: false,
data: (() => {
let json
try { json = readJsonSync(filename) } catch (e) {}
return json || {}
})(),
update (key, value) {
if (_.isFunction(key)) throw new Error('cant use function as key' + key + value)
if (self.data[key] === value) return value
debug('updating cache key', key)
self.isSaved = false
self.data[key] = value
return value
},
save () {
debug('saving', self.isSaved, filename)
if (self.isSaved) return
self.isSaved = true
return outputJsonAsync(filename, self.data, {spaces: 2})
},
clearMatching (query) {
self.data = _.omit(self.data, (v, key) => key.match(query))
}
}
cache[name] = self
}
caches.forEach(initCache)
module.exports = cache