Home Manual Reference Source
import {DotEnvUtils} from 'projext/src/services/common/dotEnvUtils.js'
public class | source

DotEnvUtils

A utility class/service to work with .env files for environment variables.

Constructor Summary

Public Constructor
public

constructor(environmentUtils: EnvironmentUtils, appLogger: AppLogger, pathUtils: PathUtils)

Method Summary

Public Methods
public

inject(variables: Object, overwrite: boolean)

Given a dictionary of variables, this method will inject all of them on the environment.

public

load(files: Array, extend: boolean): Object

Given a list of .env files (relative to the project root directory), this method will validate if they exist, load them, merge them (if extend is true) and return an object with all the variable declarations it found.

Public Constructors

public constructor(environmentUtils: EnvironmentUtils, appLogger: AppLogger, pathUtils: PathUtils) source

Params:

NameTypeAttributeDescription
environmentUtils EnvironmentUtils

To set variables in the environment.

appLogger AppLogger

To log information messages when files are loaded or when there's a problem loading them.

pathUtils PathUtils

To get paths relative to the project root.

Public Methods

public inject(variables: Object, overwrite: boolean) source

Given a dictionary of variables, this method will inject all of them on the environment.

Params:

NameTypeAttributeDescription
variables Object

A dictionary with the variables to inject.

overwrite boolean
  • optional
  • default: true

If true, and a variable is already declared, it will overwrite it, otherwise, it will skip it.

public load(files: Array, extend: boolean): Object source

Given a list of .env files (relative to the project root directory), this method will validate if they exist, load them, merge them (if extend is true) and return an object with all the variable declarations it found.

Params:

NameTypeAttributeDescription
files Array

The list of file names relative to the project root directory.

extend boolean
  • optional
  • default: true

Whether or not the variables found on the files should be merged on a single object. If this is true, it will reverse the list of files and merge all the variables in top of each other (to ensure that the final overwrite is from the file that was first on the list). If this is false, even if multiple files were found, it will only use the first one.

Return:

Object

Return Properties:

NameTypeAttributeDescription
loaded boolean

Whether or not variables were loaded.

variables Object

A dictionary with all the loaded variables.