-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): improve .env type detection by loadConfigSync
- Loading branch information
Showing
6 changed files
with
303 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.