-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathwrite-browser-env.ts
More file actions
31 lines (24 loc) · 917 Bytes
/
write-browser-env.ts
File metadata and controls
31 lines (24 loc) · 917 Bytes
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
import fs from 'fs';
import path from 'path';
import { type ProcessEnv } from '../typings/process-env';
import * as log from '../utils/log';
import { getBrowserEnvScript } from './get-browser-env-script';
import { ConfigureRuntimeEnvOptions } from '../configure';
/**
* Writes the environment variables to the public __ENV.js file and make them
* accessible under `window.__ENV`.
*/
export function writeBrowserEnv(
env: ProcessEnv,
options?: ConfigureRuntimeEnvOptions,
) {
const base = fs.realpathSync(process.cwd());
const file = `${base}/${options?.rootdirectory ?? ''}public/${options?.subdirectory ?? ''}__ENV.js`;
const content = getBrowserEnvScript(env);
const dirname = path.dirname(file);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}
fs.writeFileSync(file, content);
log.ready(`wrote browser runtime environment variables to '${file}'.`);
}