1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | const path = require('path');
|
6 | const process = require('process');
|
7 | const lodash = require('lodash');
|
8 | const fsExtra = require('fs-extra');
|
9 | const DataLoader = _interopDefault(require('dataloader'));
|
10 |
|
11 | class 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 |
|
32 | module.exports = FileCache;
|
33 |
|