-
Notifications
You must be signed in to change notification settings - Fork 21
/
vite.utils.ts
108 lines (93 loc) · 2.98 KB
/
vite.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readCanisterIds as readIds } from './env.utils';
/**
* Read all the locally deployed canister IDs. For example Oisy backend, ckBTC|ETH, ICP etc.
* @param prefix
*/
const readLocalCanisterIds = ({ prefix }: { prefix?: string }): Record<string, string> => {
const dfxCanisterIdsJsonFile = join(process.cwd(), '.dfx', 'local', 'canister_ids.json');
const e2eCanisterIdsJsonFile = join(process.cwd(), 'canister_e2e_ids.json');
return readIds({
filePath: existsSync(dfxCanisterIdsJsonFile) ? dfxCanisterIdsJsonFile : e2eCanisterIdsJsonFile,
prefix
});
};
/**
* Read Oisy staging and production canister IDs
* @param prefix
*/
const readOisyCanisterIds = ({ prefix }: { prefix?: string }): Record<string, string> => {
const canisterIdsJsonFile = join(process.cwd(), 'canister_ids.json');
return readIds({ filePath: canisterIdsJsonFile, prefix });
};
/**
* Read IC staging and production canister IDs. For example ckBTC staging and production but, also ICP ledger production
* @param prefix
*/
const readRemoteCanisterIds = ({ prefix }: { prefix?: string }): Record<string, string> => {
const dfxJsonFile = join(process.cwd(), 'dfx.json');
try {
interface DetailsId {
ic: string;
staging?: string;
}
interface Details {
remote?: {
id: DetailsId;
};
}
interface DfxJson {
canisters: Record<string, Details>;
}
const { canisters }: DfxJson = JSON.parse(readFileSync(dfxJsonFile, 'utf8'));
return Object.entries(canisters).reduce((acc, current: [string, Details]) => {
const [canisterName, canisterDetails] = current;
if (canisterDetails.remote !== undefined) {
const ids = Object.entries(canisterDetails.remote.id).reduce(
(acc, [network, id]) => ({
...acc,
[`${prefix ?? ''}${network.toUpperCase().replaceAll('-', '_')}_${canisterName
.replaceAll('-', '_')
.replaceAll("'", '')
.toUpperCase()}_CANISTER_ID`]: id
}),
{}
);
return {
...acc,
...ids
};
}
return acc;
}, {});
} catch (e) {
console.warn(`Could not get canisters ID from ${dfxJsonFile}: ${e}`);
return {};
}
};
export const readCanisterIds = (params: { prefix?: string }): Record<string, string> => ({
...readLocalCanisterIds(params),
...readOisyCanisterIds(params),
...readRemoteCanisterIds(params)
});
export const defineViteReplacements = (): {
VITE_APP_VERSION: string;
VITE_DFX_NETWORK: string;
} => {
const file = fileURLToPath(new URL('package.json', import.meta.url));
const json = readFileSync(file, 'utf8');
const { version } = JSON.parse(json);
// npm run dev = local
// npm run build = local
// npm run test = local
// dfx deploy = local
// dfx deploy --network ic = ic
// dfx deploy --network staging = staging
const network = process.env.DFX_NETWORK ?? 'local';
return {
VITE_APP_VERSION: JSON.stringify(version),
VITE_DFX_NETWORK: JSON.stringify(network)
};
};