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

Need a hook or event to set process.env before _parseOptionsEnv #1762

Closed
hungtcs opened this issue Jun 27, 2022 · 4 comments · Fixed by #1763
Closed

Need a hook or event to set process.env before _parseOptionsEnv #1762

hungtcs opened this issue Jun 27, 2022 · 4 comments · Fixed by #1763

Comments

@hungtcs
Copy link
Contributor

hungtcs commented Jun 27, 2022

Hallo,
Is there any way to set environment variables before _parseOptionsEnv function?
my use case is to read .env file via dotenv and set environment variables before all subcommands are executed.

The dotenv file can be specified, and I am use enablePositionalOptions to process options.
The issue is preAction hook are emit after _parseOptionsEnv function, this will cause new Option().env('SOME_ENV') not to perform as expected.

program.addOption(new Option('--env <env>', 'env file').default('.env'))
  .hook('preAction', async () => {
    const { env } = program.opts();
    dotenv.config({ path: env, override: true });
  });

I also tried the option:env event, but it doesn't solve the problem perfectly either.

I searched relevant issues and stackoverflow as much as possible, but no solution found.

Thanks.

@hungtcs
Copy link
Contributor Author

hungtcs commented Jun 27, 2022

It may be a preSubCommand hook that performs some async operations before sub command parsing

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 27, 2022

I also tried the option:env event, but it doesn't solve the problem perfectly either.

For interest, what didn't work using the option:env approach? (It does not support async?)

@hungtcs
Copy link
Contributor Author

hungtcs commented Jun 28, 2022

I also tried the option:env event, but it doesn't solve the problem perfectly either.

For interest, what didn't work using the option:env approach? (It does not support async?)

In my use case, async support is not necessary.

program.addOption(new Option('--env <env>', 'env file').default('.env'))
  .on('option:env', () => {
    console.log('option:env event emit');
  });

This event will only fire when the env parameter is explicitly specified, default value does not trigger the event.

node dist/main.js --env .env.production  # trigger
node dist/main.js  # default value .env not trigger

@shadowspawn shadowspawn added enhancement pending release Merged into a branch for a future release, but not released yet labels Jul 9, 2022
@shadowspawn
Copy link
Collaborator

Released in Commander v9.4.0

@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants