-
Notifications
You must be signed in to change notification settings - Fork 8
/
compute-aggregated.js
49 lines (43 loc) · 1.74 KB
/
compute-aggregated.js
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
require("dotenv").config();
const fs = require("fs");
const { Elm } = require("./compute-aggregated-app");
const elmApp = Elm.ComputeAggregated.init({
flags: {
definitionsString: fs.readFileSync("public/data/impacts.json", "utf-8"),
textileProcessesString: fs.readFileSync("public/data/textile/processes_impacts.json", "utf-8"),
foodProcessesString: fs.readFileSync("public/data/food/processes_impacts.json", "utf-8"),
},
});
const exportJson = async (filepath, json) => {
// Using dynamic import to avoid jest runtime error
// eg. “A dynamic import callback was invoked without --experimental-vm-modules”
const prettier = require("prettier");
const jsonString = JSON.stringify(json, null, 2);
const formattedJson = await prettier.format(jsonString, { filepath });
fs.writeFileSync(filepath, formattedJson);
};
elmApp.ports.export.subscribe(
({
textileProcesses,
foodProcesses,
textileProcessesOnlyAggregated,
foodProcessesOnlyAggregated,
textileProcessesFakeDetails,
foodProcessesFakeDetails,
}) => {
try {
exportJson("public/data/textile/processes_impacts.json", textileProcesses);
exportJson("public/data/food/processes_impacts.json", foodProcesses);
exportJson("public/data/textile/processes.json", textileProcessesOnlyAggregated);
exportJson("public/data/food/processes.json", foodProcessesOnlyAggregated);
exportJson("public/data/textile/processes_impacts_fake.json", textileProcessesFakeDetails);
exportJson("public/data/food/processes_impacts_fake.json", foodProcessesFakeDetails);
console.log("EXPORTED!");
} catch (err) {
console.error(err);
}
},
);
elmApp.ports.logError.subscribe((errorMessage) => {
console.error("Error:", errorMessage);
});