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

"Strict" flag to override error in production environment #11

Open
wants to merge 2 commits into
base: master
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
8 changes: 5 additions & 3 deletions lib/generateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { promises: fsp } = require('fs');

const runtimeConfig = {};

async function generateConfig({ envConfig, envFile }) {
async function generateConfig({ envConfig, envFile, strict = null }) {
let envConfigExists = true;

try {
Expand All @@ -25,6 +25,7 @@ async function generateConfig({ envConfig, envFile }) {
throw err;
}

const isRelaxed = strict == null ? process.env.NODE_ENV === recConstants.DEVELOPMENT : !strict;
const content = await fsp.readFile(envFile, 'utf8');

content.split(/\r?\n/)
Expand All @@ -33,12 +34,13 @@ async function generateConfig({ envConfig, envFile }) {
const equalSignIndex = line.indexOf('=');
const lengthOfString = line.length;


if (equalSignIndex !== -1) {
const key = line.slice(0, equalSignIndex);
const value = line.slice(equalSignIndex + 1, lengthOfString);

if (process.env.NODE_ENV === recConstants.DEVELOPMENT) {
runtimeConfig[key] = value;
if (isRelaxed) {
runtimeConfig[key] = process.env[key] ?? value;
} else {
if (!process.env[key])
throw new Error(`Error getting '${key}' from process.env`);
Expand Down
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node


const yargs = require('yargs');

const generateConfig = require('./generateConfig');
Expand All @@ -17,6 +18,11 @@ const argv = yargs
description: 'Location and name of your .env file.',
default: './.env',
type: 'string',
}).option('strict', {
alias: 's',
'description': "Raise error if matching environment variables for .env variable",
'default': null,
'type': 'boolean'
})
.help()
.alias('help', 'h').argv;
Expand All @@ -25,11 +31,13 @@ const argv = yargs
try {
const envConfig = argv['config-name'];
const envFile = argv['env-file'];
const strict = argv['strict'];

console.log(`
Provided flags:
--config-name = ${envConfig}
--env-file = ${envFile}
--strict = ${strict}

Your environment variables will be available on '${constants.runtimeConfigPath}'
`);
Expand All @@ -40,6 +48,7 @@ const argv = yargs
result = await generateConfig({
envConfig,
envFile,
strict
});
} catch (err) {
throw new Error(err.message);
Expand Down
61 changes: 60 additions & 1 deletion tests/generateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,50 @@ describe('runtime-env-cra', () => {
);
});

it('should throw error if NODE_ENV is production and env var is not found in shell', async () => {
it('should throw error if NODE_ENV is production, env var is not found in shell, and strict is not set', async () => {
process.env.NODE_ENV = 'production';
let config;
let error;

try {
config = await generateConfig({
envConfig: './tests/utils/runtime-config.js',
envFile: './tests/utils/.env',
});
} catch (err) {
error = err;
}

expect(config).toBeUndefined();
expect(error).toBeDefined();
expect(error.message).toEqual('Error getting \'TEST_VAR\' from process.env');
});

it('should default to env if NODE_ENV is production, env var is not found in shell, and strict is set to false', async () => {
process.env.NODE_ENV = 'production';

let config;
let error;

try {
config = await generateConfig({
envConfig: './tests/utils/runtime-config.js',
envFile: './tests/utils/.env',
strict: false
});
} catch (err) {
error = err;
}

expect(error).toBeUndefined();
expect(config).toBeDefined();
expect(config).toEqual(
'window.__RUNTIME_CONFIG__ = {"TEST_VAR":"TEST_VALUE"};',
);
});

it('should throw error if env var is not found in shell, and strict is true regardless of environment', async () => {
process.env.NODE_ENV = 'development';

let config;
let error;
Expand All @@ -76,6 +118,7 @@ describe('runtime-env-cra', () => {
config = await generateConfig({
envConfig: './tests/utils/runtime-config.js',
envFile: './tests/utils/.env',
strict: true
});
} catch (err) {
error = err;
Expand All @@ -86,6 +129,22 @@ describe('runtime-env-cra', () => {
expect(error.message).toEqual('Error getting \'TEST_VAR\' from process.env');
});

it('should generate a runtime-config.js file successfully on Production', async () => {
process.env.NODE_ENV = 'Production';
process.env.TEST_VAR = 'TEST_VALUE';


config = await generateConfig({
envConfig: './tests/utils/runtime-config.js',
envFile: './tests/utils/.env',
});

expect(config).toBeDefined();
expect(config).toEqual(
'window.__RUNTIME_CONFIG__ = {"TEST_VAR":"TEST_VALUE"};',
);
});

it('should parse the TEST_VAR value from the shell when NODE_ENV is not set to development', async () => {
process.env.TEST_VAR = 'TEST_VALUE';

Expand Down