-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from expatfile/development
chore: 🔖 release stable version
- Loading branch information
Showing
4 changed files
with
79 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
import { getPublicEnv } from './helpers/get-public-env'; | ||
import { writeBrowserEnv } from './helpers/write-browser-env'; | ||
|
||
export type ConfigureRuntimeEnvOptions = { | ||
/** | ||
* The subdirectory of `/public` where the `__ENV.js` file should be written | ||
* eg. `subdirectory/`to. | ||
*/ | ||
subdirectory?: string; | ||
}; | ||
|
||
/** | ||
* Reads all environment variables that start with `NEXT_PUBLIC_` and writes | ||
* them to the public `__ENV.js` file. This makes them accessible under the | ||
* `window.__ENV` object. | ||
* | ||
* Options: | ||
* ```ts | ||
* type ConfigureRuntimeEnvOptions = { | ||
* // The subdirectory of `/public` where the `__ENV.js` file should be written to. eg. `subdirectory/` | ||
* subdirectory?: string; | ||
* }; | ||
* ``` | ||
*/ | ||
export function configureRuntimeEnv() { | ||
export function configureRuntimeEnv(options?: ConfigureRuntimeEnvOptions) { | ||
const publicEnv = getPublicEnv(); | ||
|
||
writeBrowserEnv(publicEnv); | ||
writeBrowserEnv(publicEnv, options?.subdirectory); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import * as log from '../utils/log'; | ||
|
||
/** | ||
* Writes the environment variables to the public __ENV.js file and make them | ||
* accessible under `window.__ENV`. | ||
*/ | ||
export function writeBrowserEnv(env: NodeJS.ProcessEnv) { | ||
export function writeBrowserEnv(env: NodeJS.ProcessEnv, subdirectory = '') { | ||
const base = fs.realpathSync(process.cwd()); | ||
const path = `${base}/public/__ENV.js`; | ||
const file = `${base}/public/${subdirectory}__ENV.js`; | ||
|
||
const content = `window.__ENV = ${JSON.stringify(env)};`; | ||
|
||
fs.writeFileSync(path, content); | ||
const dirname = path.dirname(file); | ||
|
||
log.info(`Wrote browser runtime environment variables to '${path}'.`); | ||
if (!fs.existsSync(dirname)) { | ||
fs.mkdirSync(dirname, { recursive: true }); | ||
} | ||
|
||
fs.writeFileSync(file, content); | ||
|
||
log.info(`Wrote browser runtime environment variables to '${file}'.`); | ||
} |