Skip to content

Commit 2796fde

Browse files
committed
add support for dotenv
1 parent a30d819 commit 2796fde

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"stack-traces"
2828
],
2929
"scripts": {
30-
"prebuild": "cti create ./src && rimraf **/index.ts.bak",
30+
"prebuild": "cti --withoutbackup create ./src && rimraf **/index.ts.bak",
3131
"build": "npm run format:prettier && npm run lint && tsc -sourcemap",
3232
"test": "mocha \"dist/test/**/*.js\"",
3333
"build:watch": "tsc -w",
@@ -48,6 +48,7 @@
4848
"dependencies": {
4949
"app-root-path": "^3.0.0",
5050
"callsites": "^3.1.0",
51+
"dotenv": "^8.2.0",
5152
"json-stringify-safe": "^5.0.1",
5253
"source-map-support": "^0.5.19",
5354
"winston": "^3.3.3"

src/env/env.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export class Env {
5+
private findOptionalEnvFile(startPath: string): string | null {
6+
if (!fs.existsSync(startPath) || startPath === '/') {
7+
return null;
8+
}
9+
10+
const files = fs.readdirSync(startPath);
11+
// tslint:disable-next-line:prefer-for-of
12+
for (let i = 0; i < files.length; i++) {
13+
const filename = path.join(startPath, files[i]);
14+
const stat = fs.lstatSync(filename);
15+
if (!stat.isDirectory()) {
16+
if (filename.toLowerCase().endsWith('.env')) {
17+
return filename;
18+
}
19+
}
20+
}
21+
return this.findOptionalEnvFile(path.resolve(startPath, '../'));
22+
}
23+
24+
public loadDotEnv() {
25+
const optionalEnvFile = this.findOptionalEnvFile(__dirname);
26+
if (optionalEnvFile != null && optionalEnvFile.length < 0) {
27+
require('dotenv').config({ path: optionalEnvFile });
28+
} else {
29+
require('dotenv').config();
30+
}
31+
}
32+
}

src/env/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// created from 'create-ts-index'
2+
3+
export * from './env';

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// created from 'create-ts-index'
22

3+
export * from './env';
34
export * from './capture-nested-stack-trace';
45
export * from './error-with-context';
56
export * from './format-stack-trace';

src/logger.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getCallingFilename } from './get-calling-filename';
1010
import { safeObjectAssign } from './safe-object-assign';
1111
import { sortObject } from './sort-object';
1212
import { ToOneLine } from './to-one-line';
13+
import { Env } from './env';
1314

1415
// tslint:disable-next-line:no-var-requires
1516
require('source-map-support').install({
@@ -247,6 +248,9 @@ function ifEverythingFailsLogger(functionName: string, err: Error) {
247248
let logParams!: { logLevel: LOG_LEVEL; debugString: boolean };
248249

249250
export function LoggerAdaptToConsole(options?: { logLevel?: LOG_LEVEL; debugString?: boolean }) {
251+
const env = new Env();
252+
env.loadDotEnv();
253+
250254
const defaultOptions = {
251255
logLevel: LOG_LEVEL.info,
252256
debugString: false,

0 commit comments

Comments
 (0)