forked from Nikaple/nest-typed-config
-
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.
- Loading branch information
Showing
9 changed files
with
190 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { OptionsSync } from 'cosmiconfig'; | ||
import { readdirSync } from 'fs'; | ||
import { fileLoader } from './file-loader'; | ||
import fromPairs from 'lodash.frompairs'; | ||
|
||
export interface DirectoryLoaderOptions extends OptionsSync { | ||
/** | ||
* The directory containing all configuration files. | ||
*/ | ||
directory: string; | ||
} | ||
|
||
/** | ||
* Directory loader loads configuration in a specific folder. | ||
* The basename of file will be used as configuration key, for the directory below: | ||
* | ||
* ``` | ||
* . | ||
* └─config | ||
* ├── app.toml | ||
* └── db.toml | ||
* ``` | ||
* | ||
* The parsed config will be `{ app: "config in app.toml", db: "config in db.toml" }` | ||
* @param options directory loader options. | ||
*/ | ||
export const directoryLoader = ({ | ||
directory, | ||
...options | ||
}: DirectoryLoaderOptions) => { | ||
return (): Record<string, any> => { | ||
const files = readdirSync(directory); | ||
const fileNames = [ | ||
...new Set(files.map(file => file.replace(/\.\w+$/, ''))), | ||
]; | ||
const configs = fromPairs( | ||
fileNames.map(name => [ | ||
name, | ||
fileLoader({ | ||
basename: name, | ||
searchFrom: directory, | ||
...options, | ||
})(), | ||
]), | ||
); | ||
|
||
return configs; | ||
}; | ||
}; |
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,3 +1,4 @@ | ||
export * from './file-loader'; | ||
export * from './remote-loader'; | ||
export * from './dotenv-loader'; | ||
export * from './directory-loader'; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { AppModule } from '../src/app.module'; | ||
import { DirectoryConfig, DatabaseConfig } from '../src/config.model'; | ||
|
||
describe('Directory loader', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [AppModule.withDirectory()], | ||
}).compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
it(`should be able to config from specific folder`, () => { | ||
const config = app.get(DirectoryConfig); | ||
expect(config.table.name).toEqual('table2'); | ||
|
||
const databaseConfig = app.get(DatabaseConfig); | ||
expect(databaseConfig.port).toBe(3000); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app?.close(); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
host = '127.0.0.1' | ||
port = 3000 | ||
|
||
[table] | ||
name = 'test' |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
name = 'table2' |