-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better env variable(s) handling
- Loading branch information
Showing
6 changed files
with
112 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright (c) 2023-2023. | ||
* Author Peter Placzek (tada5hi) | ||
* For the full copyright and license information, | ||
* view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
export enum EnvironmentName { | ||
DEVELOPMENT = 'development', | ||
PRODUCTION = 'production', | ||
TEST = '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,4 @@ | ||
export * from './constants'; | ||
export * from './module'; | ||
export * from './type'; | ||
export * from './utils'; |
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 type { EnvironmentName } from './constants'; | ||
import type { Environment } from './type'; | ||
import { hasProcessEnv, readFromProcessEnv } from './utils'; | ||
|
||
let instance : Environment | undefined; | ||
|
||
export function useEnv() : Environment; | ||
export function useEnv<K extends keyof Environment>(key: K) : Environment[K]; | ||
export function useEnv(key?: string) : any { | ||
if (typeof instance !== 'undefined') { | ||
if (typeof key === 'string') { | ||
return instance[key as keyof Environment]; | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
const seeds : string[] = []; | ||
const seedKeys = ['DATABASE_SEEDER_SEEDS', 'TYPEORM_SEEDING_SEEDS']; | ||
for (let i = 0; i < seedKeys.length; i++) { | ||
if (!hasProcessEnv(seedKeys[i])) { | ||
continue; | ||
} | ||
|
||
seeds.push(...readFromProcessEnv(seedKeys[i], '').split(',')); | ||
} | ||
|
||
const factories : string[] = []; | ||
const factoryKeys = ['DATABASE_SEEDER_FACTORIES', 'TYPEORM_SEEDING_FACTORIES']; | ||
for (let i = 0; i < factoryKeys.length; i++) { | ||
if (!hasProcessEnv(factoryKeys[i])) { | ||
continue; | ||
} | ||
|
||
factories.push(...readFromProcessEnv(factoryKeys[i], '').split(',')); | ||
} | ||
|
||
instance = { | ||
env: readFromProcessEnv('NODE_ENV', 'development') as `${EnvironmentName}`, | ||
seeds: seeds.filter((el) => el), | ||
factories: factories.filter((el) => el), | ||
}; | ||
|
||
if (typeof key === 'string') { | ||
return instance[key as keyof Environment]; | ||
} | ||
|
||
return instance; | ||
} |
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,7 @@ | ||
import type { EnvironmentName } from './constants'; | ||
|
||
export interface Environment { | ||
env: `${EnvironmentName}`, | ||
seeds: string[], | ||
factories: string[] | ||
} |
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,16 @@ | ||
import process from 'node:process'; | ||
import { hasOwnProperty } from '../utils'; | ||
|
||
export function hasProcessEnv(key: string) : boolean { | ||
return hasOwnProperty(process.env, key); | ||
} | ||
|
||
export function readFromProcessEnv(key: string) : string | undefined; | ||
export function readFromProcessEnv<T>(key: string, alt: T) : T | string; | ||
export function readFromProcessEnv<T>(key: string, alt?: T): any { | ||
if (hasOwnProperty(process.env, key)) { | ||
return process.env[key]; | ||
} | ||
|
||
return alt; | ||
} |
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