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 exportDataSync(file, value) {
|
8 | const filename = path.join(config.path, file.getValue());
|
9 | const output = getValue(value);
|
10 |
|
11 |
|
12 | const write = () => {
|
13 | fs.ensureDirSync(path.dirname(filename));
|
14 | fs.writeFileSync(filename, JSON.stringify(output, null, ' '));
|
15 |
|
16 | };
|
17 |
|
18 |
|
19 | let existingdata;
|
20 |
|
21 | try {
|
22 | existingdata = fs.readFileSync(filename, 'utf8');
|
23 | } catch (readerr) {
|
24 |
|
25 | if (readerr && readerr.code === 'ENOENT') {
|
26 | write();
|
27 | }
|
28 | }
|
29 |
|
30 | if (!existingdata) {
|
31 | write();
|
32 | } else {
|
33 |
|
34 | const existingObject = JSON.parse(existingdata);
|
35 |
|
36 | if (!jsondiff.diff(existingObject, output)) {
|
37 | return value;
|
38 | }
|
39 |
|
40 | write();
|
41 | }
|
42 |
|
43 | return value;
|
44 | }
|
45 |
|
46 | return exportDataSync;
|
47 | };
|