Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(makePublic): Enable using empty process.env based on options #159

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/utils/make-env-public.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ describe('makeEnvPublic()', () => {
);
});

it('should warn when prefixing a variable that is not available in process.env', () => {
it('should warn when prefixing a variable that is not available in process.env and default options', () => {
makeEnvPublic('FOO', {skipNonExistingVar: false});

expect(process.env.FOO).toEqual("");
expect(process.env.NEXT_PUBLIC_FOO).toEqual("");
});

it('should prefixi a variable that is not available in process.env if skipNonExistingVar option is false', () => {
makeEnvPublic('FOO');

expect(warnMock).toHaveBeenCalledWith(
Expand Down
24 changes: 17 additions & 7 deletions src/utils/make-env-public.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { event, LogOptions, warn } from '../helpers/log';

export interface MakeEnvPublicOptions extends LogOptions {}
export interface MakeEnvPublicOptions extends LogOptions {
skipNonExistingVar?: boolean
}

function prefixKey(key: string, options?: MakeEnvPublicOptions) {
const {skipNonExistingVar = true} = options || {};
// Check if key is available in process.env.
if (!process.env[key]) {
warn(
`Skipped prefixing environment variable '${key}'. Variable not in process.env`,
options,
);
if (skipNonExistingVar) {
warn(
`Skipped prefixing environment variable '${key}'. Variable not in process.env`,
options,
);

return;
}

return;
process.env[key] = ''
}

// Check if key is already public.
Expand All @@ -20,7 +27,7 @@ function prefixKey(key: string, options?: MakeEnvPublicOptions) {

const prefixedKey = `NEXT_PUBLIC_${key}`;

process.env[prefixedKey] = process.env[key];
process.env[prefixedKey] = process.env[key] || "";

// eslint-disable-next-line no-console
event(`Prefixed environment variable '${key}'`, options);
Expand All @@ -38,6 +45,9 @@ function prefixKey(key: string, options?: MakeEnvPublicOptions) {
* // Make multiple variables public.
* makeEnvPublic(['FOO', 'BAR', 'BAZ']);
*
* // Enable prefixing vars not present on process.env
* makeEnvPublic('FOO', { skipNonExistingVar: false });
*
* // Disable logging.
* makeEnvPublic('FOO', { logLevel: 'silent' });
*
Expand Down