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

Add support for loading env values #23

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cleaning up
nkahlor committed Mar 18, 2021
commit a1910ea61fc0675703850f34d1bed1f44b2db80b
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including that file was intentional; revert this


# Logs
logs
*.log
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ export default function setEnv<T extends UndefinedEnvVars, V extends UndefinedEn
...options,
};

if (_options.options.loadDotEnv) {
if (_options.options.envPath) {
parseEnvFile(_options.options);
}

2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ function parseLine(line: string): Record<string, string> {
}

export function parseEnvFile(options: ConfigOptions = {}): void {
const fullPath = options.envFile || join(process.cwd(), '.env');
const fullPath = options.envPath || join(process.cwd(), '.env');

if (!fs.existsSync(fullPath)) {
return;
78 changes: 2 additions & 76 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'jest';
import fs from 'fs';
import setEnvDefault from '..';
import parseEnvFile from '../parser';

describe('simple-env', () => {
// Fresh setEnv for each test
@@ -105,11 +104,11 @@ describe('simple-env', () => {
expect(Object.getOwnPropertyDescriptors(env)).toHaveProperty('somethingElse');
});

it('will invoke the parser is loadDotEnv is true', () => {
it('will invoke the parser is envPath is set', () => {
existsSyncSpy.mockReturnValue(true);
readFileSpy.mockImplementation(() => Buffer.from('TEST=test'));

setEnv({ optional: { something: 'SOMETHING' }, options: { loadDotEnv: true } });
setEnv({ optional: { something: 'SOMETHING' }, options: { envPath: './a/.env' } });

expect(process.env).toHaveProperty('TEST');
});
@@ -123,77 +122,4 @@ describe('simple-env', () => {
expect(process.env).not.toHaveProperty('TEST');
});
});

describe('parser', () => {
beforeEach(() => {
process.env = {};
});

afterEach(() => {
jest.resetModules();
});

it('will not overwrite vars that already exist', () => {
const originalValue = 'I already exist';
const newValue = 'I should not';
process.env = { TEST: originalValue };

readFileSpy.mockImplementation(() => Buffer.from(`TEST=${newValue}`));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.TEST).toEqual(originalValue);
});

it('will reject malformed lines', () => {
const fakeFile = `
bad
good=this
4=bad
good2='this'
good3="this"
`;
readFileSpy.mockImplementation(() => Buffer.from(fakeFile));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.good).toEqual('this');
expect(process.env.good2).toEqual('this');
expect(process.env.good3).toEqual('this');
});

it('will ignore comments in the file', () => {
const fakeFile = `
#comment\n
//comment\n
TEST=test
`;
readFileSpy.mockImplementation(() => Buffer.from(fakeFile));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.TEST).toEqual('test');
});

it('will not do anything if the .env file does not exist', () => {
readFileSpy.mockImplementation(() => Buffer.from('TEST=test'));
existsSyncSpy.mockReturnValue(false);

parseEnvFile();

expect(process.env.TEST).toBeUndefined();
});

it('will read an env variable from a .env file into process.env', () => {
readFileSpy.mockImplementation(() => Buffer.from('TEST=test'));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.TEST).toEqual('test');
});
});
});
79 changes: 79 additions & 0 deletions src/test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import fs from 'fs';
import parseEnvFile from '../parser';

describe('parser', () => {
const readFileSpy = jest.spyOn(fs, 'readFileSync');
const existsSyncSpy = jest.spyOn(fs, 'existsSync');

beforeEach(async () => {
jest.resetModules();
process.env = {};
});

afterEach(() => {
jest.resetModules();
});

it('will not overwrite vars that already exist', () => {
const originalValue = 'I already exist';
const newValue = 'I should not';
process.env = { TEST: originalValue };

readFileSpy.mockImplementation(() => Buffer.from(`TEST=${newValue}`));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.TEST).toEqual(originalValue);
});

it('will reject malformed lines', () => {
const fakeFile = `
bad
good=this
4=bad
good2='this'
good3="this"
`;
readFileSpy.mockImplementation(() => Buffer.from(fakeFile));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.good).toEqual('this');
expect(process.env.good2).toEqual('this');
expect(process.env.good3).toEqual('this');
});

it('will ignore comments in the file', () => {
const fakeFile = `
#comment\n
//comment\n
TEST=test
`;
readFileSpy.mockImplementation(() => Buffer.from(fakeFile));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.TEST).toEqual('test');
});

it('will not do anything if the .env file does not exist', () => {
readFileSpy.mockImplementation(() => Buffer.from('TEST=test'));
existsSyncSpy.mockReturnValue(false);

parseEnvFile();

expect(process.env.TEST).toBeUndefined();
});

it('will read an env variable from a .env file into process.env', () => {
readFileSpy.mockImplementation(() => Buffer.from('TEST=test'));
existsSyncSpy.mockReturnValue(true);

parseEnvFile();

expect(process.env.TEST).toEqual('test');
});
});
3 changes: 1 addition & 2 deletions src/types/Options.ts
Original file line number Diff line number Diff line change
@@ -2,8 +2,7 @@ import { DefaultEnvVars, UndefinedEnvVars } from './EnvVars';
import { RemoveKeys } from './helpers';

export interface ConfigOptions {
envFile?: string;
loadDotEnv?: boolean;
envPath?: string;
}

export interface Options<Required extends UndefinedEnvVars = DefaultEnvVars, Optional extends UndefinedEnvVars = DefaultEnvVars> {