Skip to content
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
2 changes: 1 addition & 1 deletion dotenv-extended.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* The result of a call to load() or parse()
*/
export interface IEnvironmentMap {
[name: string]: string;
[name: string]: string | undefined;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"auto-parse": "^1.3.0",
"camelcase": "^5.3.1",
"cross-spawn": "^7.0.1",
"dotenv": "^8.2.0"
"dotenv": "^8.2.0",
"typescript": "^3.8.3"
}
}
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dotenv from 'dotenv';
import getConfigFromEnv from './utils/config-from-env';
import loadEnvironmentFile from './utils/load-environment-file';
import loadEnvTypeDeclaration from './utils/load-env-type-declaration';

export const parse = dotenv.parse.bind(dotenv);
export const config = options => {
Expand Down Expand Up @@ -35,7 +36,9 @@ export const config = options => {
const configKeys = Object.keys(config);

if (options.errorOnMissing || options.errorOnExtra || options.errorOnRegex) {
const schema = loadEnvironmentFile(options.schema, options.encoding, options.silent);
const schema = options.schema.endsWith('.d.ts') ?
loadEnvTypeDeclaration(options.schema, options.encoding, options.silent)
: loadEnvironmentFile(options.schema, options.encoding, options.silent);
const schemaKeys = Object.keys(schema);

let missingKeys = schemaKeys.filter(function (key) {
Expand Down
25 changes: 25 additions & 0 deletions src/utils/load-env-type-declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs';
import ts from 'typescript';

export const loadEnvTypeDeclaration = (path, encoding, silent) => {
try {
const data = fs.readFileSync(path, encoding);
const source = ts.createSourceFile(path, data, ts.ScriptTarget.ES2015, true);
const schema = {};
source.getChildren().find(c => c.kind === ts.SyntaxKind.SyntaxList).getChildren()
.find(c => c.kind === ts.SyntaxKind.ModuleDeclaration && c.name.text === 'dotenv-extended').body.getChildren()
.find(c => c.kind === ts.SyntaxKind.SyntaxList).getChildren()
.find(c => c.kind === ts.SyntaxKind.InterfaceDeclaration && c.name.escapedText === 'IEnvironmentMap').getChildren()
.find(c => c.kind === ts.SyntaxKind.SyntaxList).getChildren()
// Only accepts members of type "string"
.filter(c => c.type.kind === ts.SyntaxKind.StringKeyword)
.forEach(c => schema[c.name.escapedText] = '');
return schema;
} catch (err) {
if (!silent) {
console.error(err.message);
}
return {};
}
};
export default loadEnvTypeDeclaration;
15 changes: 15 additions & 0 deletions test/EnvSchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IEnvironmentMap } from "dotenv-extended";

declare module "dotenv-extended" {
interface IEnvironmentMap {
TEST_ONE: string;
TEST_TWO: string;
TEST_THREE: string;
}
}

declare global{
namespace NodeJS {
interface ProcessEnv extends IEnvironmentMap {}
}
}
16 changes: 16 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ describe('dotenv-extended tests', () => {
dotenvex.load({silent: false});
expect(console.error).to.have.been.calledOnce;
});

it('Should load .d.ts schema, defaults and env into correct values in process.env and returned object', function () {
const config = dotenvex.load({
schema: 'EnvSchema.d.ts',
defaults: '.env.defaults.example',
path: '.env.override',
errorOnExtra: true,
errorOnMissing: true
});
expect(config.TEST_ONE).to.equal('one overridden');
expect(config.TEST_TWO).to.equal('two');
expect(config.TEST_THREE).to.equal('three');
expect(process.env.TEST_ONE).to.equal('one overridden');
expect(process.env.TEST_TWO).to.equal('two');
expect(process.env.TEST_THREE).to.equal('three');
});
});

describe('Supporting libraries tests', () => {
Expand Down