@candlerip/shared3 0.0.108 → 0.0.109

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@candlerip/shared3",
3
- "version": "0.0.108",
3
+ "version": "0.0.109",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.js",
@@ -0,0 +1 @@
1
+ export * from './load-env-file/index.js';
@@ -0,0 +1 @@
1
+ export * from './load-env-file/index.js';
@@ -0,0 +1 @@
1
+ export * from './util.js';
@@ -0,0 +1 @@
1
+ export * from './util.js';
@@ -0,0 +1,6 @@
1
+ import { CustomError, EnvironmentMode, EnvironmentVariables } from '@candlerip/shared3';
2
+ export declare const loadEnvFile: <P extends keyof EnvironmentVariables, D = EnvironmentVariables[P]>(environmentMode: EnvironmentMode, projectName: P) => {
3
+ data: D;
4
+ } | {
5
+ customError: CustomError;
6
+ };
@@ -0,0 +1,27 @@
1
+ import { customErrorWorker, ENVIRONMENT_VARIABLES_CONFIG, ENVIRONMENT_VARIABLE_CONFIG, } from '@candlerip/shared3';
2
+ import * as dotenv from 'dotenv';
3
+ export const loadEnvFile = (environmentMode, projectName) => {
4
+ const { composeCustomError } = customErrorWorker({ environmentMode });
5
+ const fileName = `.env.${environmentMode}`;
6
+ const envFile = dotenv.config({ path: `./${fileName}` });
7
+ const { error: err, parsed } = envFile;
8
+ if (err || !parsed) {
9
+ return composeCustomError(`Error loading ${fileName}`, { err });
10
+ }
11
+ const environmentVariables = ENVIRONMENT_VARIABLES_CONFIG[projectName];
12
+ return {
13
+ data: environmentVariables.reduce((acc, key) => {
14
+ const data = {};
15
+ if (ENVIRONMENT_VARIABLE_CONFIG[key] === 'boolean') {
16
+ data[key] = Boolean(parsed[key] === 'true');
17
+ }
18
+ else if (ENVIRONMENT_VARIABLE_CONFIG[key] === 'number') {
19
+ data[key] = parseInt(parsed[key]);
20
+ }
21
+ else {
22
+ data[key] = parsed[key];
23
+ }
24
+ return { ...acc, ...data };
25
+ }, {}),
26
+ };
27
+ };
@@ -1,5 +1,6 @@
1
1
  export * from './cache/index.js';
2
2
  export * from './database/index.js';
3
3
  export * from './dictionary/index.js';
4
+ export * from './environment/index.js';
4
5
  export * from './express/index.js';
5
6
  export * from './message-broker/index.js';
@@ -1,5 +1,6 @@
1
1
  export * from './cache/index.js';
2
2
  export * from './database/index.js';
3
3
  export * from './dictionary/index.js';
4
+ export * from './environment/index.js';
4
5
  export * from './express/index.js';
5
6
  export * from './message-broker/index.js';