@candlerip/shared3 0.0.53 → 0.0.55

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.53",
3
+ "version": "0.0.55",
4
4
  "type": "module",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -1,7 +1,7 @@
1
1
  import { CustomError } from '../../../../error/index.js';
2
2
  import { EnvironmentMode } from '../../../environment-mode/index.js';
3
3
  import { EnvironmentVariables } from '../../../environment-variables/index.js';
4
- export declare const loadEnvFile: <K extends keyof EnvironmentVariables = never, D = EnvironmentVariables[K]>(environmentMode: EnvironmentMode) => {
4
+ export declare const loadEnvFile: <P extends keyof EnvironmentVariables, D = EnvironmentVariables[P]>(environmentMode: EnvironmentMode, projectName: P) => {
5
5
  data: D;
6
6
  } | {
7
7
  customError: CustomError;
@@ -1,7 +1,8 @@
1
1
  import * as dotenv from 'dotenv';
2
2
  import { customErrorWorker } from '../../../../error/index.js';
3
+ import { ENVIRONMENT_VARIABLES } from '../../../environment-variables/index.js';
3
4
  import { ENVIRONMENT_VARIABLE } from '../../../environment-variable/index.js';
4
- export const loadEnvFile = (environmentMode) => {
5
+ export const loadEnvFile = (environmentMode, projectName) => {
5
6
  const { composeCustomError } = customErrorWorker({ environmentMode });
6
7
  const fileName = `.env.${environmentMode}`;
7
8
  const envFile = dotenv.config({ path: `./${fileName}` });
@@ -9,19 +10,20 @@ export const loadEnvFile = (environmentMode) => {
9
10
  if (err || !parsed) {
10
11
  return composeCustomError(`Error loading ${fileName}`, { err });
11
12
  }
12
- const data = {};
13
- Object.keys(parsed).forEach((key) => {
14
- if (ENVIRONMENT_VARIABLE[key] === 'boolean') {
15
- data[key] = Boolean(parsed[key] === 'true');
16
- return;
17
- }
18
- if (ENVIRONMENT_VARIABLE[key] === 'number') {
19
- data[key] = parseInt(parsed[key]);
20
- return;
21
- }
22
- data[key] = parsed[key];
23
- });
13
+ const environmentVariables = ENVIRONMENT_VARIABLES[projectName];
24
14
  return {
25
- data: data,
15
+ data: environmentVariables.reduce((acc, key) => {
16
+ const data = {};
17
+ if (ENVIRONMENT_VARIABLE[key] === 'boolean') {
18
+ data[key] = Boolean(parsed[key] === 'true');
19
+ }
20
+ else if (ENVIRONMENT_VARIABLE[key] === 'number') {
21
+ data[key] = parseInt(parsed[key]);
22
+ }
23
+ else {
24
+ data[key] = parsed[key];
25
+ }
26
+ return { ...acc, ...data };
27
+ }, {}),
26
28
  };
27
29
  };
@@ -4,8 +4,8 @@ export const EnvironmentVariablesWorker = (() => {
4
4
  const NODE_ENV = process.env.NODE_ENV;
5
5
  let envs;
6
6
  const get = () => envs;
7
- const init = () => {
8
- const resp = loadEnvFile(NODE_ENV);
7
+ const init = (projectName) => {
8
+ const resp = loadEnvFile(NODE_ENV, projectName);
9
9
  if ('customError' in resp) {
10
10
  return resp;
11
11
  }
@@ -7,7 +7,7 @@ export type IEnvironmentVariablesWorker = {
7
7
  isProduction: IsProduction;
8
8
  };
9
9
  export type Get = <T extends keyof EnvironmentVariables = never>() => EnvironmentVariables[T];
10
- export type Init = <T extends keyof EnvironmentVariables = never>() => {
10
+ export type Init = <T extends keyof EnvironmentVariables>(projectName: T) => {
11
11
  data: EnvironmentVariables[T];
12
12
  } | {
13
13
  customError: CustomError;