Skip to content

Commit

Permalink
Merge pull request #13 from expatfile/development
Browse files Browse the repository at this point in the history
chore: 🔖 release stable version
  • Loading branch information
HofmannZ authored May 4, 2023
2 parents b307621 + aeb9c1d commit 822e377
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
28 changes: 26 additions & 2 deletions src/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,39 @@ const mockWriteBrowserEnv = writeBrowserEnv as jest.MockedFunction<
typeof writeBrowserEnv
>;

afterEach(() => {
mockGetPublicEnv.mockClear();
mockWriteBrowserEnv.mockClear();
});

describe('configureRuntimeEnv()', () => {
it('should call the helper methods', () => {
configureRuntimeEnv();

expect(mockGetPublicEnv).toHaveBeenCalledTimes(1);

expect(mockWriteBrowserEnv).toHaveBeenCalledTimes(1);
expect(mockWriteBrowserEnv).toHaveBeenCalledWith({
NEXT_PUBLIC_FOO: 'foo',
expect(mockWriteBrowserEnv).toHaveBeenCalledWith(
{
NEXT_PUBLIC_FOO: 'foo',
},
undefined
);
});

it('should call the helper methods with options', () => {
configureRuntimeEnv({
subdirectory: 'subdirectory/',
});

expect(mockGetPublicEnv).toHaveBeenCalledTimes(1);

expect(mockWriteBrowserEnv).toHaveBeenCalledTimes(1);
expect(mockWriteBrowserEnv).toHaveBeenCalledWith(
{
NEXT_PUBLIC_FOO: 'foo',
},
'subdirectory/'
);
});
});
20 changes: 18 additions & 2 deletions src/configure.ts
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);
}
29 changes: 24 additions & 5 deletions src/helpers/write-browser-env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ beforeAll(() => {
afterAll(() => {
infoSpy.mockRestore();

fs.rmdirSync(path);
fs.rmSync(path, { recursive: true, force: true });
});

describe('writeBrowserEnv()', () => {
afterEach(() => {
fs.rmSync(file);
});

it('should write an empty env', () => {
writeBrowserEnv({});

Expand All @@ -37,6 +33,8 @@ describe('writeBrowserEnv()', () => {
const content = fs.readFileSync(file).toString();

expect(content).toEqual('window.__ENV = {};');

fs.rmSync(file);
});

it('should write and env with a value', () => {
Expand All @@ -49,6 +47,8 @@ describe('writeBrowserEnv()', () => {
const content = fs.readFileSync(file).toString();

expect(content).toEqual('window.__ENV = {"NEXT_PUBLIC_FOO":"foo"};');

fs.rmSync(file);
});

it('should write and env with multiple values', () => {
Expand All @@ -65,5 +65,24 @@ describe('writeBrowserEnv()', () => {
expect(content).toEqual(
'window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};'
);

fs.rmSync(file);
});

it('should write to a subdirectory', () => {
const fileInSubdirectory = `${path}/subdirectory/__ENV.js`;
const messageWithSubdirectory = `${chalk.cyan(
`info`
)} - [next-runtime-env] - Wrote browser runtime environment variables to '${fileInSubdirectory}'.`;

writeBrowserEnv({}, 'subdirectory/');

expect(infoSpy).toHaveBeenCalledWith(messageWithSubdirectory);

const content = fs.readFileSync(fileInSubdirectory).toString();

expect(content).toEqual('window.__ENV = {};');

fs.rmSync(fileInSubdirectory);
});
});
15 changes: 11 additions & 4 deletions src/helpers/write-browser-env.ts
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}'.`);
}

0 comments on commit 822e377

Please sign in to comment.