-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvite.utils.ts
130 lines (112 loc) · 3.63 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { execSync } from 'node:child_process';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { UserConfig } from 'vite';
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;
beta?: 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;
VITE_GIT_COMMIT_HASH: string;
VITE_GIT_BRANCH_NAME: 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';
const isTestFe = network.startsWith('test_fe_');
const commitHash = isTestFe ? execSync('git rev-parse --short HEAD').toString().trim() : '';
const branchName = isTestFe ? execSync('git rev-parse --abbrev-ref HEAD').toString().trim() : '';
return {
VITE_APP_VERSION: JSON.stringify(version),
VITE_DFX_NETWORK: JSON.stringify(network),
VITE_GIT_COMMIT_HASH: JSON.stringify(commitHash),
VITE_GIT_BRANCH_NAME: JSON.stringify(branchName)
};
};
export const CSS_CONFIG_OPTIONS: Pick<UserConfig, 'css'> = {
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler'
}
}
}
};