-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathvite.utils.ts
More file actions
82 lines (67 loc) · 2.28 KB
/
vite.utils.ts
File metadata and controls
82 lines (67 loc) · 2.28 KB
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
import { assertNonNullish, notEmptyString } from '@dfinity/utils';
import type {
JunoConfigEnv,
JunoConsoleConfig,
JunoConsoleConfigFnOrObject
} from '@junobuild/config';
import { readConsoleConfig as readConsoleConfigTools } from '@junobuild/config-loader';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
const JUNO_CONFIG_FILENAME = 'juno.config';
const readConsoleConfig = async (env: JunoConfigEnv): Promise<JunoConsoleConfig> => {
const config = (userConfig: JunoConsoleConfigFnOrObject): JunoConsoleConfig =>
typeof userConfig === 'function' ? userConfig(env) : userConfig;
return await readConsoleConfigTools({
filename: JUNO_CONFIG_FILENAME,
config
});
};
const defineJunoEnv = async ({
mode
}: JunoConfigEnv): Promise<Omit<ViteReplacements, 'VITE_APP_VERSION'>> => {
const { id, ids, authentication, api } = await readConsoleConfig({ mode });
const consoleId = id ?? ids[mode];
assertNonNullish(consoleId, 'Console ID not defined.');
const googleClientId = authentication?.google?.clientId;
const githubClientId = authentication?.github?.clientId;
const apiUrl = api?.url;
return {
VITE_CONSOLE_ID: JSON.stringify(consoleId),
VITE_GOOGLE_CLIENT_ID: notEmptyString(googleClientId)
? JSON.stringify(googleClientId)
: undefined,
VITE_GITHUB_CLIENT_ID: notEmptyString(githubClientId)
? JSON.stringify(githubClientId)
: undefined,
VITE_JUNO_API_URL: notEmptyString(apiUrl) ? JSON.stringify(apiUrl) : undefined
};
};
const defineAppVersion = (): Pick<ViteReplacements, 'VITE_APP_VERSION'> => {
const file = fileURLToPath(new URL('package.json', import.meta.url));
const json = readFileSync(file, 'utf8');
const { version } = JSON.parse(json);
return {
VITE_APP_VERSION: JSON.stringify(version)
};
};
interface ViteReplacements {
VITE_CONSOLE_ID: string;
VITE_GOOGLE_CLIENT_ID: string | undefined;
VITE_GITHUB_CLIENT_ID: string | undefined;
VITE_JUNO_API_URL: string | undefined;
VITE_APP_VERSION: string;
}
export const defineViteReplacements = async (env: JunoConfigEnv) => {
const prefix = `import.meta.env`;
const replacements = {
...defineAppVersion(),
...(await defineJunoEnv(env))
};
return Object.entries(replacements).reduce(
(acc, [key, value]) => ({
...acc,
[`${prefix}.${key}`]: value
}),
{}
);
};