1 | const fs = require('fs-extra');
|
2 | const path = require('path');
|
3 | const jsondiff = require('jsondiffpatch');
|
4 | const getValue = require('./get-value');
|
5 |
|
6 | module.exports = config => {
|
7 | function exportData(file, value) {
|
8 | const filename = path.join(config.path, file.getValue());
|
9 | const output = getValue(value);
|
10 |
|
11 |
|
12 | const write = () => {
|
13 | fs.ensureDir(path.dirname(filename), err => {
|
14 | if (err) throw err;
|
15 | fs.writeFile(filename, JSON.stringify(output, null, ' '), writeerr => {
|
16 | if (writeerr) throw writeerr;
|
17 |
|
18 | });
|
19 | });
|
20 | };
|
21 |
|
22 |
|
23 | fs.readFile(filename, 'utf8', (readerr, existingdata) => {
|
24 |
|
25 | if (readerr && readerr.code === 'ENOENT') {
|
26 | write();
|
27 | }
|
28 |
|
29 | if (existingdata) {
|
30 |
|
31 | const existingObject = JSON.parse(existingdata);
|
32 |
|
33 | if (!jsondiff.diff(existingObject, output)) {
|
34 | return value;
|
35 | }
|
36 |
|
37 | write();
|
38 | }
|
39 | return value;
|
40 | });
|
41 |
|
42 | return value;
|
43 | }
|
44 | return exportData;
|
45 | };
|