-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
interpolate-env-variables-to-index.ts
43 lines (38 loc) · 1.39 KB
/
interpolate-env-variables-to-index.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
export function interpolateEnvironmentVariablesToIndex(
contents: string,
deployUrl?: string
): string {
const environmentVariables = getClientEnvironment(deployUrl || '');
return interpolateEnvironmentVariables(contents, environmentVariables as any);
}
const NX_PREFIX = /^NX_/i;
// Prevent sensitive keys from being bundled when source code uses entire `process.env` object rather than individual keys (e.g. `process.env.NX_FOO`).
// TODO(v19): Only env vars prefixed with NX_PUBLIC should be bundled. This is a breaking change so we won't do it in v18.
const excludedKeys = ['NX_CLOUD_ACCESS_TOKEN', 'NX_CLOUD_ENCRYPTION_KEY'];
function isNxEnvironmentKey(x: string): boolean {
return !excludedKeys.includes(x) && NX_PREFIX.test(x);
}
function getClientEnvironment(deployUrl: string) {
return Object.keys(process.env)
.filter(isNxEnvironmentKey)
.reduce(
(env, key) => {
env[key] = process.env[key];
return env;
},
{
NODE_ENV: process.env.NODE_ENV || 'development',
DEPLOY_URL: deployUrl || process.env.DEPLOY_URL || '',
}
);
}
function interpolateEnvironmentVariables(
documentContents: string,
environmentVariables: Record<string, string>
): string {
let temp = documentContents;
for (const [key, value] of Object.entries(environmentVariables)) {
temp = temp.replace(new RegExp(`%${key}%`, 'g'), value);
}
return temp;
}