forked from aliscie/odoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupEnv.js
64 lines (59 loc) · 1.88 KB
/
setupEnv.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const fs = require("fs");
const path = require("path");
function initCanisterEnv() {
let localCanisters, prodCanisters;
try {
localCanisters = require(path.resolve(
".dfx",
"local",
"canister_ids.json"
));
} catch (error) {
console.log("No local canister_ids.json found");
}
try {
prodCanisters = require(path.resolve("canister_ids.json"));
} catch (error) {
console.log("No production canister_ids.json found");
}
const network =
process.env.DFX_NETWORK ||
(process.env.NODE_ENV === "production" ? "ic" : "local");
const canisterConfig = network === "local" ? localCanisters : prodCanisters;
const localMap = localCanisters
? Object.entries(localCanisters).reduce((prev, current) => {
const [canisterName, canisterDetails] = current;
prev[canisterName.toUpperCase() + "_CANISTER_ID"] =
canisterDetails[network];
return prev;
}, {})
: undefined;
const prodMap = prodCanisters
? Object.entries(prodCanisters).reduce((prev, current) => {
const [canisterName, canisterDetails] = current;
prev[canisterName.toUpperCase() + "_CANISTER_ID"] =
canisterDetails[network];
return prev;
}, {})
: undefined;
return [localMap, prodMap];
}
const [localCanisters, prodCanisters] = initCanisterEnv();
if (localCanisters) {
const localTemplate = Object.entries(localCanisters).reduce((start, next) => {
const [key, val] = next;
if (!start) return `${key}=${val}`;
return `${start ?? ""}
${key}=${val}`;
}, ``);
fs.writeFileSync(".env.development", localTemplate);
}
if (prodCanisters) {
const prodTemplate = Object.entries(prodCanisters).reduce((start, next) => {
const [key, val] = next;
if (!start) return `${key}=${val}`;
return `${start ?? ""}
${key}=${val}`;
}, ``);
fs.writeFileSync(".env", localTemplate);
}