Skip to content

Decomposition proposal #690

@krutoo

Description

@krutoo

Hi, i want use dotenv for parse and merge objects manually, as it is done under the hood of this library.

I want to extract mechanism of modifying process.env by parsing result.

Can we figure out how to decompose config() function into for example config() and merge() or apply().

In this case config() will be looks like:

function config (options) {
  // ...
  try {
    const parsed = parse(fs.readFileSync(dotenvPath, { encoding }));

    apply({ 
      target: process.env,
      source: parsed,
      options: { debug, override },
    });

    return { parsed }
  } catch (err) {
  // ...
}

The apply() function may be looks like:

function apply ({ target, source, options }) {
  const { override, debug, log } = options;

  for (const key of Object.keys(source)) {
    if (!Object.prototype.hasOwnProperty.call(target, key)) {
      target[key] = source[key];
    } else {
      if (override === true) {
        target[key] = source[key];
        debug && log(`"${key}" is already defined and WAS overwritten`);
      } else {
        debug && log(`"${key}" is already defined and was NOT overwritten`);
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions