dotenv#DotenvParseOutput TypeScript Examples

The following examples show how to use dotenv#DotenvParseOutput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: config.service.ts    From nest-react with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
   * Extract the configuration from a `dotenv` file
   * @param env The environment name. Corresponding `name.env` file will be used. Default to `local`
   */
  private getConfigFromEnvFile(env = 'local'): DotenvParseOutput {
    const envFilePath = join('env', `.env.${env}`);
    try {
      const config = parse(readFileSync(envFilePath));
      return config;
    } catch (err) {
      const msg = `Configuration error, see below:

/!\\ No environment definition found at ${envFilePath}. Please choose one of the following options (in preference order):
  1. Set both the CONFIG_PATH and the SECRETS_PATH environment variables and fill their respective folders with corresponding environment values.
  2. Set the NODE_ENV environment variable and attach the corresponding "dotenv" file to the server.

`;
      this.logger.error(msg);
      throw new InternalServerErrorException();
    }
  }
Example #2
Source File: config.service.ts    From nest-react with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
   * Extract the configuration from both the `config` and `secrets` folders.
   * @param configPath Path to the open `configurations` directory
   * @param secretsPath Path to the `secrets` directory
   */
  private getConfigFromVolumes(
    configPath: string,
    secretsPath: string
  ): DotenvParseOutput {
    const configFiles = readdirSync(configPath).filter(
      file => !file.includes('..')
    );
    const secretsFiles = readdirSync(secretsPath).filter(
      file => !file.includes('..')
    );
    const config: DotenvParseOutput = {};
    configFiles.reduce((partialConfig, file) => {
      partialConfig[file] = this.extractConfigFromFile(join(configPath, file));
      return partialConfig;
    }, config);
    secretsFiles.reduce((partialConfig, file) => {
      partialConfig[file] = this.extractConfigFromFile(join(secretsPath, file));
      return partialConfig;
    }, config);
    return config;
  }
Example #3
Source File: config.service.ts    From nest-react with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
   * Validate the configuration object against the required configuration schema
   * using the Joi library.
   * @param envConfig The config object
   */
  private validateInput(envConfig: DotenvParseOutput): DotenvParseOutput {
    const { error, value: validatedEnvConfig } = this.configSchema.validate(
      envConfig
    );
    if (error) {
      throw new Error(`Config validation error: ${error.message}`);
    }
    this.printConfig(validatedEnvConfig);
    return validatedEnvConfig;
  }
Example #4
Source File: config.service.ts    From nest-react with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
   * Safely prints the server configuration. All secret values will be hidden.
   * @param envConfig
   */
  private printConfig(envConfig: DotenvParseOutput): void {
    const config = Object.keys(envConfig)
      .filter(key => !key.includes('SECRET'))
      .reduce((obj, key) => {
        obj[key] = envConfig[key];
        return obj;
      }, {} as DotenvParseOutput);
    const secrets = Object.keys(envConfig).filter(key =>
      key.includes('SECRET')
    );
    this.logger.log(
      `Server configuration:\n${JSON.stringify(config, null, 2)}`
    );
    this.logger.log(`Server secrets:\n${JSON.stringify(secrets, null, 2)}`);
  }
Example #5
Source File: config.service.ts    From nest-react with GNU Lesser General Public License v3.0 6 votes vote down vote up
constructor() {
    this.rootDir = `${join(process.cwd())}`;
    this.runningDir = `${join(this.rootDir, process.env.baseUrl || '')}`;

    // extract and validate config from ${NODE_ENV}.env file or CONFIG_PATH & SECRETS_PATH
    let config: DotenvParseOutput;
    const { CONFIG_PATH: configPath, SECRETS_PATH: secretsPath } = process.env;
    if (configPath && secretsPath) {
      config = this.getConfigFromVolumes(configPath, secretsPath);
    } else {
      config = this.getConfigFromEnvFile(process.env.NODE_ENV);
    }
    this.envConfig = this.validateInput(config);
  }
Example #6
Source File: config.service.ts    From nest-react with GNU Lesser General Public License v3.0 5 votes vote down vote up
private envConfig: DotenvParseOutput;