Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decomposition proposal #690

Closed
krutoo opened this issue Oct 28, 2022 · 13 comments
Closed

Decomposition proposal #690

krutoo opened this issue Oct 28, 2022 · 13 comments

Comments

@krutoo
Copy link

krutoo commented Oct 28, 2022

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`);
      }
    }
  }
}
@krutoo krutoo changed the title More Decomposition proposal Oct 28, 2022
@motdotla
Copy link
Owner

motdotla commented May 4, 2023

This is a good suggestion. Putting on todos.

@deepam-kapur
Copy link
Contributor

Can I work on this @motdotla ? And can you assign me this?

@motdotla
Copy link
Owner

motdotla commented May 6, 2023

Let's give @krutoo the chance to do so. @krutoo, I agree with this proposal. It's a great one. Want to knock it out and I'll review and maybe make some small modifications?

@deepam-kapur
Copy link
Contributor

ok sure. let me know If I could help too! @motdotla & @krutoo

@motdotla
Copy link
Owner

motdotla commented May 9, 2023

@deepam-kapur if we don't here from @krutoo in 10 days then go ahead and start on the PR and we'll credit him in the changelog.

@krutoo
Copy link
Author

krutoo commented May 10, 2023

I'm here, sorry, was on vacation =)

@krutoo
Copy link
Author

krutoo commented May 10, 2023

@deepam-kapur you can do it if you want

I do not have sufficient knowledge of the package

I also do not impose any specific variant, just would like to be able to separately read and apply the read data to the process.env

@motdotla
Copy link
Owner

Ok, great. @deepam-kapur go ahead and work on a PR for that

@deepam-kapur
Copy link
Contributor

sure thanks!

deepam-kapur added a commit to deepam-kapur/dotenv that referenced this issue May 11, 2023
…ptions to parse and fill the data. Closes - motdotla#690

motdotla#690 Apply function added
@deepam-kapur
Copy link
Contributor

deepam-kapur commented May 11, 2023

@motdotla I've created a PR please review and let me know if any change is required.

PR - #733

@deepam-kapur
Copy link
Contributor

@motdotla have you got chance to review this?

@motdotla
Copy link
Owner

@deepam-kapur reviewed and merged. it will be released as part of 16.1.0 at the end of the month.

@motdotla
Copy link
Owner

Renamed to populate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants