Skip to content

Commit 822e377

Browse files
authored
Merge pull request #13 from expatfile/development
chore: 🔖 release stable version
2 parents b307621 + aeb9c1d commit 822e377

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

src/configure.spec.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,39 @@ const mockWriteBrowserEnv = writeBrowserEnv as jest.MockedFunction<
1919
typeof writeBrowserEnv
2020
>;
2121

22+
afterEach(() => {
23+
mockGetPublicEnv.mockClear();
24+
mockWriteBrowserEnv.mockClear();
25+
});
26+
2227
describe('configureRuntimeEnv()', () => {
2328
it('should call the helper methods', () => {
2429
configureRuntimeEnv();
2530

2631
expect(mockGetPublicEnv).toHaveBeenCalledTimes(1);
2732

2833
expect(mockWriteBrowserEnv).toHaveBeenCalledTimes(1);
29-
expect(mockWriteBrowserEnv).toHaveBeenCalledWith({
30-
NEXT_PUBLIC_FOO: 'foo',
34+
expect(mockWriteBrowserEnv).toHaveBeenCalledWith(
35+
{
36+
NEXT_PUBLIC_FOO: 'foo',
37+
},
38+
undefined
39+
);
40+
});
41+
42+
it('should call the helper methods with options', () => {
43+
configureRuntimeEnv({
44+
subdirectory: 'subdirectory/',
3145
});
46+
47+
expect(mockGetPublicEnv).toHaveBeenCalledTimes(1);
48+
49+
expect(mockWriteBrowserEnv).toHaveBeenCalledTimes(1);
50+
expect(mockWriteBrowserEnv).toHaveBeenCalledWith(
51+
{
52+
NEXT_PUBLIC_FOO: 'foo',
53+
},
54+
'subdirectory/'
55+
);
3256
});
3357
});

src/configure.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import { getPublicEnv } from './helpers/get-public-env';
22
import { writeBrowserEnv } from './helpers/write-browser-env';
33

4+
export type ConfigureRuntimeEnvOptions = {
5+
/**
6+
* The subdirectory of `/public` where the `__ENV.js` file should be written
7+
* eg. `subdirectory/`to.
8+
*/
9+
subdirectory?: string;
10+
};
11+
412
/**
513
* Reads all environment variables that start with `NEXT_PUBLIC_` and writes
614
* them to the public `__ENV.js` file. This makes them accessible under the
715
* `window.__ENV` object.
16+
*
17+
* Options:
18+
* ```ts
19+
* type ConfigureRuntimeEnvOptions = {
20+
* // The subdirectory of `/public` where the `__ENV.js` file should be written to. eg. `subdirectory/`
21+
* subdirectory?: string;
22+
* };
23+
* ```
824
*/
9-
export function configureRuntimeEnv() {
25+
export function configureRuntimeEnv(options?: ConfigureRuntimeEnvOptions) {
1026
const publicEnv = getPublicEnv();
1127

12-
writeBrowserEnv(publicEnv);
28+
writeBrowserEnv(publicEnv, options?.subdirectory);
1329
}

src/helpers/write-browser-env.spec.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ beforeAll(() => {
2121
afterAll(() => {
2222
infoSpy.mockRestore();
2323

24-
fs.rmdirSync(path);
24+
fs.rmSync(path, { recursive: true, force: true });
2525
});
2626

2727
describe('writeBrowserEnv()', () => {
28-
afterEach(() => {
29-
fs.rmSync(file);
30-
});
31-
3228
it('should write an empty env', () => {
3329
writeBrowserEnv({});
3430

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

3935
expect(content).toEqual('window.__ENV = {};');
36+
37+
fs.rmSync(file);
4038
});
4139

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

5149
expect(content).toEqual('window.__ENV = {"NEXT_PUBLIC_FOO":"foo"};');
50+
51+
fs.rmSync(file);
5252
});
5353

5454
it('should write and env with multiple values', () => {
@@ -65,5 +65,24 @@ describe('writeBrowserEnv()', () => {
6565
expect(content).toEqual(
6666
'window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};'
6767
);
68+
69+
fs.rmSync(file);
70+
});
71+
72+
it('should write to a subdirectory', () => {
73+
const fileInSubdirectory = `${path}/subdirectory/__ENV.js`;
74+
const messageWithSubdirectory = `${chalk.cyan(
75+
`info`
76+
)} - [next-runtime-env] - Wrote browser runtime environment variables to '${fileInSubdirectory}'.`;
77+
78+
writeBrowserEnv({}, 'subdirectory/');
79+
80+
expect(infoSpy).toHaveBeenCalledWith(messageWithSubdirectory);
81+
82+
const content = fs.readFileSync(fileInSubdirectory).toString();
83+
84+
expect(content).toEqual('window.__ENV = {};');
85+
86+
fs.rmSync(fileInSubdirectory);
6887
});
6988
});

src/helpers/write-browser-env.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import fs from 'fs';
2+
import path from 'path';
23

34
import * as log from '../utils/log';
45

56
/**
67
* Writes the environment variables to the public __ENV.js file and make them
78
* accessible under `window.__ENV`.
89
*/
9-
export function writeBrowserEnv(env: NodeJS.ProcessEnv) {
10+
export function writeBrowserEnv(env: NodeJS.ProcessEnv, subdirectory = '') {
1011
const base = fs.realpathSync(process.cwd());
11-
const path = `${base}/public/__ENV.js`;
12+
const file = `${base}/public/${subdirectory}__ENV.js`;
1213

1314
const content = `window.__ENV = ${JSON.stringify(env)};`;
1415

15-
fs.writeFileSync(path, content);
16+
const dirname = path.dirname(file);
1617

17-
log.info(`Wrote browser runtime environment variables to '${path}'.`);
18+
if (!fs.existsSync(dirname)) {
19+
fs.mkdirSync(dirname, { recursive: true });
20+
}
21+
22+
fs.writeFileSync(file, content);
23+
24+
log.info(`Wrote browser runtime environment variables to '${file}'.`);
1825
}

0 commit comments

Comments
 (0)