Skip to content

Commit

Permalink
feat(config): improve .env type detection by loadConfigSync
Browse files Browse the repository at this point in the history
  • Loading branch information
isuvorov committed Feb 19, 2024
1 parent 10da8e3 commit 2df79e3
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 322 deletions.
13 changes: 7 additions & 6 deletions libs/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
},
"dependencies": {
"@lsk4/algos": "^4.9.0",
"@lsk4/config": "^4.9.0",
"@lsk4/err": "^4.9.0",
"@lsk4/log": "^4.9.0"
"@lsk4/config": "^4.10.0",
"@lsk4/err": "^4.10.0",
"@lsk4/log": "^4.10.0",
"dotenv": "^16.4.4"
},
"devDependencies": {
"@nestjs/common": "^10.3.0",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.3.0"
"@nestjs/common": "^10.3.3",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.3"
},
"peerDependencies": {
"@nestjs/common": "*",
Expand Down
77 changes: 32 additions & 45 deletions libs/config/src/ConfigModule.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,56 @@
import { existsSync } from 'node:fs';

import { loadConfigSync } from '@lsk4/config';
import { Err } from '@lsk4/err';
import { DynamicModule, Global, Module } from '@nestjs/common';
import {
ConfigModule as NestConfigModule,
ConfigService as NestConfigService,
} from '@nestjs/config';

// import { ConfigModule } from '@nestlib/config';
import { ConfigService } from './ConfigService';
import { loadConfigEnvs } from './loadConfigEnvs';
import { ConfigModuleOptions } from './types';
import { getConfigServiceToken } from './utils';

@Global()
@Module({
// import: [],
// controllers: [AuthController],
// providers: [CryptoService, AuthOtpService, AuthService],
// exports: [ConfigService],
// exports: [
// // ConfigService,
// // NestConfigService,
// {
// provide: getConfigServiceToken(),
// useExisting: ConfigService,
// },
// ],
})
@Module({})
export class ConfigModule {
static forRoot(options: ConfigModuleOptions = {}): DynamicModule {
const rootDir = options.cwd || process.cwd();
const envFiles = ['.env', '../.env', '../../.env'].map((f) => `${rootDir}/${f}`);
const configEnvs = [
'.env.js',
'.env.json',
'../.env.js',
'../.env.json',
'../../.env.js',
'../../.env.json',
].map((f) => `${rootDir}/${f}`);
const { ns, name, key, ...loadConfigOptions } = options;

const cwd = options.cwd || process.cwd();
const envFilePath = ['.env', '../.env', '../../.env']
.map((f) => `${cwd}/${f}`)
.find((f) => existsSync(f));

// console.log('[CnfModule]', { envFiles, configEnvs });
// TODO: may be
// dotenv.config({ path: envFilePath });

const ConfigService2 = {
provide: getConfigServiceToken(options.connection),
const _ConfigService = {
provide: getConfigServiceToken(ns),
useExisting: ConfigService,
};

return {
imports: [
NestConfigModule.forRoot({ envFilePath: envFiles }),
NestConfigModule.forRoot(loadConfigEnvs(['process.env.ENV_JSON', ...configEnvs])),
// ConfigModule.forRoot(loadConfigEnvs(['process.env.ENV_JSON', ...configEnvs])),
NestConfigModule.forRoot({ envFilePath }),
NestConfigModule.forRoot({
load: [
() => {
const { config } = loadConfigSync<any>(name || '.env', loadConfigOptions);
if (!key) return config;
if (typeof key === 'string') return config?.[key];
if (typeof key === 'function') return key(config);
throw new Err('Invalid keyOrFn');
},
],
isGlobal: true,
expandVariables: true,
}),
],
module: ConfigModule,
// controllers: [AuthController],
providers: [ConfigService, NestConfigService, ConfigService2],
exports: [ConfigService, NestConfigService, ConfigService2],
providers: [ConfigService, _ConfigService, NestConfigService],
exports: [ConfigService, _ConfigService, NestConfigService],
};
}
static getExports() {
return [
ConfigService,
NestConfigService,
{
provide: getConfigServiceToken(),
useExisting: ConfigService,
},
];
}
}
1 change: 0 additions & 1 deletion libs/config/src/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export class ConfigService {
get(key: string) {
const res = this.configService.get(key);
this.log.trace('get', key, res);
// console.log('configService', this.configService);
return res;
}
}
Expand Down
40 changes: 0 additions & 40 deletions libs/config/src/getEnvConfig.ts

This file was deleted.

16 changes: 6 additions & 10 deletions libs/config/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import type { LoadConfigOptions } from '@lsk4/config';

export type PropsFn = (res: Record<string, unknown>) => Record<string, unknown>;

export interface ConfigModuleOptions {
connection?: string;
type Fn = (a: any) => any;
export interface ConfigModuleOptions extends LoadConfigOptions {
ns?: string;
key?: string | Fn;

name?: string;
cwd?: string;
files?: string[];
exts?: string[];
stopDir?: string;
throwError?: boolean;
silent?: boolean;
packageKey?: string;
processEnvKey?: string;
}
Loading

0 comments on commit 2df79e3

Please sign in to comment.