Skip to content

Commit

Permalink
feat(expand): added expand flag that expands variable like in dotenv-…
Browse files Browse the repository at this point in the history
…expand
  • Loading branch information
rubiin committed Mar 27, 2022
1 parent 0025c39 commit 389e33f
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 865 deletions.
2 changes: 1 addition & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ KEYSTR=hello
#this is a comment
TEST=d
ARR=1,foo,true,false
KEYBOOLFALSE=false
KEYBOOLFALSE=false
15 changes: 15 additions & 0 deletions .env.expand
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
KEYINT=100
KEYBOOLTRUE=true
KEYSTR=hello
#this is a comment
TEST=d
ARR=1,foo,true,false
KEYBOOLFALSE=false
PASSWORD=s1mpl3
DB_PASS=$PASSWORD
MONGODB_USERNAME=testuser
MONGODB_PASSWORD=testpassword
MONGODB_HOST=localhost
MONGODB_PORT=27017
MONGODB_SERVERNAME=my-mongodb-server
MONGODB_URI=mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_SERVERNAME}
1 change: 0 additions & 1 deletion .env.test

This file was deleted.

42 changes: 25 additions & 17 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
jest: true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
createDefaultProgram: true,
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['off'],
},
};
481 changes: 0 additions & 481 deletions VersionHistory.md

This file was deleted.

37 changes: 17 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-easyconfig",
"version": "2.4.5",
"version": "2.5.0",
"description": "Add configs to nestjs with auto type parsing",
"author": "Rubin Bhandari <[email protected]>",
"license": "MIT",
Expand All @@ -27,40 +27,37 @@
},
"bugs": "https://github.com/rubiin/nestjs-easyconfig/issues",
"dependencies": {
"dotenv": "^16.0.0"
"dotenv": "^16.0.0",
"dotenv-expand": "^8.0.3"
},
"peerDependencies": {
"@nestjs/common": ">7.0.0"
},
"devDependencies": {
"@nestjs/common": "^8.4.0",
"@nestjs/core": "^8.4.0",
"@nestjs/testing": "8.4.0",
"@nestjs/common": "^8.4.3",
"@nestjs/core": "^8.4.3",
"@nestjs/testing": "8.4.3",
"@types/jest": "27.4.1",
"@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "5.13.0",
"@typescript-eslint/parser": "5.13.0",
"@types/node": "17.0.23",
"@typescript-eslint/eslint-plugin": "5.16.0",
"@typescript-eslint/parser": "5.16.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "^8.10.0",
"eslint-config-prettier": "8.4.0",
"eslint": "^8.12.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.25.4",
"husky": "^7.0.4",
"jest": "27.5.1",
"lint-staged": "^12.3.4",
"prettier": "2.5.1",
"lint-staged": "^12.3.7",
"prettier": "2.6.1",
"reflect-metadata": "0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.4",
"supertest": "6.2.2",
"ts-jest": "27.1.3",
"ts-node": "10.6.0",
"tsc-watch": "4.6.0",
"tsconfig-paths": "3.12.0",
"typescript": "4.6.2"
"rxjs": "^7.5.5",
"ts-jest": "27.1.4",
"typescript": "4.6.3"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --cache --fix",
"eslint --fix",
"prettier --write"
],
"*.{json,md,scss,yaml,yml}": [
Expand Down
3 changes: 2 additions & 1 deletion src/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Config extends DotenvConfigOptions {
*/

sampleFilePath?: string;
expand?: boolean;

/**
* checks whether the used env file is missing some keys.
Expand Down Expand Up @@ -61,7 +62,7 @@ export interface Config extends DotenvConfigOptions {

/**
*
* This option allows you overide a value on process.env if its alreadt set . Defaults to false
* This option allows you overide a value on process.env if its already set . Defaults to false
*
*/

Expand Down
15 changes: 11 additions & 4 deletions src/easyconfig.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as dotenv from 'dotenv';
import * as dotenvExpand from 'dotenv-expand';
import dotenvParseVariables from './parseEnv';
import * as fs from 'fs';
import { Config } from './config.interface';
Expand Down Expand Up @@ -30,12 +31,18 @@ export class EasyconfigService {
* @returns {Record<string, any>}
* @memberof EasyconfigService
*/
returnEnvs(config: Config): Record<string, any> {
return dotenv.config({
returnEnvs(config: Config): Record<string, string> {
const thisEnv = dotenv.config({
debug: config.debug,
encoding: config.encoding,
path: config.path,
}).parsed;
});

if (config.expand) {
return dotenvExpand.expand(thisEnv).parsed;
}

return thisEnv.parsed;
}

/**
Expand Down Expand Up @@ -104,7 +111,7 @@ export class EasyconfigService {
'Failed to load configs. Either pass file or NODE_ENV :(',
);
} else {
this.envConfig = this.envConfig = this.returnEnvs({
this.envConfig = this.returnEnvs({
...config,
path: path.resolve(config.path),
});
Expand Down
73 changes: 66 additions & 7 deletions test/unit/easyconfig.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,67 @@ describe('EasyconfigService', () => {
safe: true,
});

Logger.log(anotherService.get('KEYSTR'))
Logger.log(anotherService.get('KEYSTR'));
} catch (err) {
expect(err).toBeInstanceOf(EasyconfigError);
}
});
});

describe('EasyconfigService with expand true', () => {
const service: EasyconfigService = new EasyconfigService({
path: '.env.expand',
safe: true,
expand: true,
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should be return int', () => {
expect(service.get('KEYINT')).toEqual(100);
});

it('should be return a true boolean', () => {
expect(service.get('KEYBOOLTRUE')).toEqual(true);
});

it('should be return a false boolean', () => {
expect(service.get('KEYBOOLFALSE')).toEqual(false);
});

it('should be return string', () => {
expect(service.get('KEYSTR')).toEqual('hello');
});

it('should be return array', () => {
expect(service.get('ARR')).toEqual([1, 'foo', true, false]);
});

it('should throw error when something goes wrong', () => {
try {
const anotherService: EasyconfigService = new EasyconfigService({
path: '.env.dev',
safe: true,
});

Logger.log(anotherService.get('KEYSTR'));
} catch (err) {
expect(err).toBeInstanceOf(EasyconfigError);
}
});
it('should be return string', () => {
expect(service.get('DB_PASS')).toEqual('s1mpl3');
});

it('should be return string', () => {
expect(service.get('MONGODB_URI')).toEqual(
'mongodb://testuser:testpassword@localhost:27017/my-mongodb-server',
);
});
});

describe('EasyconfigService with NODE_ENV', () => {
process.env.NODE_ENV = 'dev';
const service: EasyconfigService = new EasyconfigService({});
Expand All @@ -64,10 +118,15 @@ describe('EasyconfigService without NODE_ENV', () => {
});

describe('it should work with the logger option', () => {
it.each([
new Logger(EasyconfigService.name)
])('it should work with any logger', (logger: LoggerService) => {
const service = new EasyconfigService({ logger, path: '.env.dev', safe: true });
expect(service).toBeDefined();
});
it.each([new Logger(EasyconfigService.name)])(
'it should work with any logger',
(logger: LoggerService) => {
const service = new EasyconfigService({
logger,
path: '.env.dev',
safe: true,
});
expect(service).toBeDefined();
},
);
});
Loading

0 comments on commit 389e33f

Please sign in to comment.