-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.utils.ts
45 lines (40 loc) · 1.03 KB
/
env.utils.ts
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
import { readFileSync } from 'node:fs';
/**
* Read a JSON file with canister IDs and transform it into a dictionary. Optionally add prefix to all dictionary keys.
*/
export const readCanisterIds = ({
filePath,
prefix
}: {
filePath: string;
prefix?: string;
}): Record<string, string> => {
try {
interface Details {
ic?: string;
staging?: string;
local?: string;
}
const config: Record<string, Details> = JSON.parse(readFileSync(filePath, 'utf8'));
return Object.entries(config).reduce((acc, current: [string, Details]) => {
const [canisterName, canisterDetails] = current;
const ids = Object.entries(canisterDetails).reduce(
(acc, [network, id]) => ({
...acc,
[`${prefix ?? ''}${network.toUpperCase().replaceAll('-', '_')}_${canisterName
.replaceAll('-', '_')
.replaceAll("'", '')
.toUpperCase()}_CANISTER_ID`]: id
}),
{}
);
return {
...acc,
...ids
};
}, {});
} catch (e) {
console.warn(`Could not get canister ID from ${filePath}: ${e}`);
return {};
}
};