UNPKG

1.09 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5const path = require('path');
6const process = require('process');
7const lodash = require('lodash');
8const fsExtra = require('fs-extra');
9const DataLoader = _interopDefault(require('dataloader'));
10
11class FileCache {
12 constructor({ path: path$1 }) {
13 this.absolutePath = path.isAbsolute(path$1) ? path$1 : path.join(process.cwd(), path$1);
14 this.json = fsExtra.pathExistsSync(this.absolutePath) ? fsExtra.readJSONSync(this.absolutePath) : {};
15 this.writeDataLoader = new DataLoader(async (keys) => {
16 await fsExtra.writeJSON(this.absolutePath, this.json);
17 return keys;
18 });
19 }
20 async get(name) {
21 return lodash.get(this.json, name);
22 }
23 async set(name, value) {
24 lodash.set(this.json, name, value);
25 this.writeDataLoader.load({});
26 }
27 async delete(name) {
28 this.set(name, undefined);
29 }
30}
31
32module.exports = FileCache;
33//# sourceMappingURL=index.cjs.js.map