Skip to content

Commit 8edab7f

Browse files
committed
feat: ✨ next.js style terminal string styling
1 parent c159421 commit 8edab7f

File tree

7 files changed

+59
-22
lines changed

7 files changed

+59
-22
lines changed

src/helpers/get-public-env.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import chalk from 'chalk';
2+
13
import { getPublicEnv } from './get-public-env';
24

35
const infoSpy = jest.spyOn(console, 'info');
@@ -20,7 +22,17 @@ describe('getPublicEnv()', () => {
2022
delete process.env.NEXT_PUBLIC_BAZ;
2123
});
2224

23-
it('should return a allow-listed value', () => {
25+
it('should show an info message after reading the environment', () => {
26+
getPublicEnv();
27+
28+
expect(infoSpy).toHaveBeenCalledWith(
29+
`${chalk.cyan(
30+
`info`
31+
)} - [next-runtime-env] - Read environment variables prefixed with 'NEXT_PUBLIC_' from process.env.`
32+
);
33+
});
34+
35+
it('should return an allow-listed value', () => {
2436
process.env.NEXT_PUBLIC_FOO = 'foo';
2537

2638
expect(getPublicEnv()).toEqual({

src/helpers/get-public-env.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import * as log from '../output/log';
1+
import * as log from '../utils/log';
22

33
/**
44
* Gets a list of environment variables that start with `NEXT_PUBLIC_`.
55
*/
66
export function getPublicEnv() {
7-
log.info(
8-
`Get environment variables prefixed with 'NEXT_PUBLIC_' from process.env`
9-
);
10-
11-
return Object.keys(process.env)
7+
const publicEnv = Object.keys(process.env)
128
.filter((key) => /^NEXT_PUBLIC_/i.test(key))
139
.reduce(
1410
(env, key) => ({
@@ -17,4 +13,10 @@ export function getPublicEnv() {
1713
}),
1814
{} as NodeJS.ProcessEnv
1915
);
16+
17+
log.info(
18+
`Read environment variables prefixed with 'NEXT_PUBLIC_' from process.env.`
19+
);
20+
21+
return publicEnv;
2022
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const path = `${base}/public`;
1010
const file = `${path}/__ENV.js`;
1111
const message = `${chalk.cyan(
1212
`info`
13-
)} - [next-runtime-env] - Write browser runtime environment variables to ${file}`;
13+
)} - [next-runtime-env] - Wrote browser runtime environment variables to ${file}`;
1414

1515
beforeAll(() => {
1616
infoSpy.mockImplementation();

src/helpers/write-browser-env.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22

3-
import * as log from '../output/log';
3+
import * as log from '../utils/log';
44

55
/**
66
* Writes the environment variables to the public __ENV.js file and make them
@@ -10,9 +10,9 @@ export function writeBrowserEnv(env: NodeJS.ProcessEnv) {
1010
const base = fs.realpathSync(process.cwd());
1111
const path = `${base}/public/__ENV.js`;
1212

13-
log.info(`Write browser runtime environment variables to ${path}`);
14-
1513
const content = `window.__ENV = ${JSON.stringify(env)};`;
1614

1715
fs.writeFileSync(path, content);
16+
17+
log.info(`Wrote browser runtime environment variables to ${path}`);
1818
}

src/make-env-public.spec.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ describe('makeEnvPublic()', () => {
3030

3131
makeEnvPublic('FOO');
3232

33-
expect(infoSpy).toHaveBeenCalledWith(
34-
`${chalk.cyan(
35-
`info`
36-
)} - [next-runtime-env] - Prefix environment variable 'FOO'.`
37-
);
38-
3933
expect(process.env.FOO).toEqual('foo');
4034
expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo');
4135
});
@@ -49,12 +43,41 @@ describe('makeEnvPublic()', () => {
4943

5044
expect(process.env.FOO).toEqual('foo');
5145
expect(process.env.NEXT_PUBLIC_FOO).toEqual('foo');
46+
5247
expect(process.env.BAR).toEqual('bar');
5348
expect(process.env.NEXT_PUBLIC_BAR).toEqual('bar');
49+
5450
expect(process.env.BAZ).toEqual('baz');
5551
expect(process.env.NEXT_PUBLIC_BAZ).toEqual('baz');
5652
});
5753

54+
it('should show an info message when env vars are prefixed', () => {
55+
process.env.FOO = 'foo';
56+
process.env.BAR = 'bar';
57+
process.env.BAZ = 'baz';
58+
59+
makeEnvPublic('FOO');
60+
makeEnvPublic(['BAR', 'BAZ']);
61+
62+
expect(infoSpy).toHaveBeenCalledWith(
63+
`${chalk.cyan(
64+
`info`
65+
)} - [next-runtime-env] - Prefixed environment variable 'FOO'.`
66+
);
67+
68+
expect(infoSpy).toHaveBeenCalledWith(
69+
`${chalk.cyan(
70+
`info`
71+
)} - [next-runtime-env] - Prefixed environment variable 'BAR'.`
72+
);
73+
74+
expect(infoSpy).toHaveBeenCalledWith(
75+
`${chalk.cyan(
76+
`info`
77+
)} - [next-runtime-env] - Prefixed environment variable 'BAZ'.`
78+
);
79+
});
80+
5881
it('should warn when the env var already starts with NEXT_PUBLIC_', () => {
5982
process.env.NEXT_PUBLIC_FOO = 'foo';
6083

@@ -63,7 +86,7 @@ describe('makeEnvPublic()', () => {
6386
expect(warnSpy).toHaveBeenCalledWith(
6487
`${chalk.yellow(
6588
`warn`
66-
)} - [next-runtime-env] - Prefix environment variable 'NEXT_PUBLIC_FOO' is already public.`
89+
)} - [next-runtime-env] - Environment variable 'NEXT_PUBLIC_FOO' is already public.`
6790
);
6891
});
6992
});

src/make-env-public.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import * as log from './output/log';
1+
import * as log from './utils/log';
22

33
function prefixKey(key: string) {
44
// Check if the key already is already public.
55
if (/^NEXT_PUBLIC_/i.test(key)) {
6-
log.warn(`Prefix environment variable '${key}' is already public.`);
6+
log.warn(`Environment variable '${key}' is already public.`);
77
}
88

9-
log.info(`Prefix environment variable '${key}'.`);
10-
119
const prefixedKey = `NEXT_PUBLIC_${key}`;
1210

1311
process.env[prefixedKey] = process.env[key];
12+
13+
log.info(`Prefixed environment variable '${key}'.`);
1414
}
1515

1616
/**
File renamed without changes.

0 commit comments

Comments
 (0)