forked from Metatavu/noheva-management
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
75 lines (64 loc) · 1.87 KB
/
Copy pathvite.config.ts
File metadata and controls
75 lines (64 loc) · 1.87 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
import react from '@vitejs/plugin-react'
import Vault from "node-vault";
import { type UserConfig, defineConfig, loadEnv } from "vite";
interface TokenRequest {
token: string;
endpoint: string;
path: string;
}
/**
* Fetch secrets from Hashicorp Vault
*
* @param param.token - Vault token
* @param param.endpoint - Vault endpoint
* @param param.path - Path to the secrets
* @returns map of secrets
*/
const fetchSecrets = async ({ token, endpoint, path }: TokenRequest) => {
const vault = Vault({
apiVersion: "v1",
endpoint: endpoint,
token: token,
});
const { data } = await vault.read(path);
return data.data;
};
/**
* Returns define object for Vite
*
* @param userConfig user configuration
* @returns define object
*/
const getDefine = async ({ mode }: UserConfig) => {
if (!mode) {
throw new Error("Mode is required. Please check your .env file.");
}
const env = loadEnv(mode, process.cwd(), "");
const { VAULT_TOKEN, VAULT_ADDR, VAULT_PATH } = env;
if (!VAULT_PATH) {
throw new Error("VAULT_PATH is required. Please check your .env file.");
}
if (!VAULT_ADDR || !VAULT_TOKEN) {
throw new Error(
"You must be logged in to the HCV (use withhcv -command). See https://github.com/Metatavu/development-scripts/blob/master/hcv/withhcv.sh for more information.",
);
}
const secrets = await fetchSecrets({
token: VAULT_TOKEN,
endpoint: VAULT_ADDR,
path: VAULT_PATH,
});
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
return Object.entries(secrets).reduce((acc: any, [key, value]) => {
const envKey = `import.meta.env.${key}`;
acc[envKey] = JSON.stringify(value);
return acc;
}, {});
};
// https://vitejs.dev/config/
export default defineConfig(async (userConfig: UserConfig): Promise<UserConfig> => {
return {
define: await getDefine(userConfig),
plugins: [react()],
};
});