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

Allow token replacement of .npmrc configuration with env vars #1207

Merged
merged 16 commits into from
Nov 14, 2016
Merged

Allow token replacement of .npmrc configuration with env vars #1207

merged 16 commits into from
Nov 14, 2016

Conversation

darthtrevino
Copy link
Contributor

@darthtrevino darthtrevino commented Oct 18, 2016

Summary

NPM allows for clients to inject environment variables into .npmrc configuration files. e.g.

//registry.npmjs.org/:_authToken=${NPM_TOKEN}
registry=http://registry.npmjs.org

This commit allows yarn to understand .npmrc files using this feature.

Test plan

  • Build the yarn executable
  • execute yarn config list to view injected variables

Fixes #1206

@darthtrevino
Copy link
Contributor Author

dwapkyphuw

@holm
Copy link

holm commented Oct 19, 2016

We use this to inject our NPM token into the nvmrc file, so we can avoid checking in the token, and also avoid having to deal with global npm configuration on machines.

@darthtrevino darthtrevino changed the title Update NPM-Registry Class to inject Env Vars into NPM Config Allow token replacement of .npmrc configuration with env vars Oct 19, 2016
@samccone samccone self-assigned this Oct 25, 2016
@samccone
Copy link
Member

Thanks for the PR @darthtrevino I will review

@darthtrevino
Copy link
Contributor Author

Sweet, thank you sir!

@@ -36,6 +36,24 @@ function getGlobalPrefix(): string {
}
}

function envReplace(f: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be more descriptive variable here than f.

@darthtrevino
Copy link
Contributor Author

Word, thanks @wyze; in the last couple of commits I split out the envReplace function into a separate module in utils/ and added a few basic tests

@maybeec
Copy link

maybeec commented Nov 1, 2016

Any chance to get this in the next release? Would love to see this as it is annoying me in my project :)

@@ -118,6 +119,9 @@ export default class NpmRegistry extends Registry {

for (const [, loc, file] of await this.getPossibleConfigLocations('.npmrc')) {
const config = ini.parse(file);
Object.keys(config).forEach((key: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use for...in here please.

import assert from 'assert';

describe('environment variable replacement', () => {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty newline.

return value;
}

const envExpr = /(\\*)\$\{([^}]+)\}/g;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this at the top level and capitalise it as ENV_EXPR.

@darthtrevino
Copy link
Contributor Author

darthtrevino commented Nov 2, 2016

The CircleCI issue looks like it's affecting newer PRs too. Let me know when I can pull a fix for it.

@darthtrevino darthtrevino reopened this Nov 2, 2016
@maybeec
Copy link

maybeec commented Nov 3, 2016

Does this PR also include the ability to replace any env variable? NPM, e.g., does not replace env vars in keys like this:

@scope:registry=${ARTIFACTORY_REGISTRY}
//${ARTIFACTORY_REGISTRY}:_password=${PASSWORD}
//${ARTIFACTORY_REGISTRY}:username=${USERNAME}

So ${ARTIFACTORY_REGISTRY} is replaced in a value expression but not in a key expression, which is quite odd.

@darthtrevino
Copy link
Contributor Author

@maybeec Nope, this change acts the same way that NPM does. It performs token replacement in value expressions

@maybeec
Copy link

maybeec commented Nov 4, 2016

@darthtrevino maybe not in near future: npm/npm#14513
So you might want to think of supporting this directly. 👍

But might be also in a separate PR by another guy if this is merged here. As you prefer.

@darthtrevino
Copy link
Contributor Author

@maybeec When NPM changes, I'll gladly implement it here (provided this PR actually makes it in)

@darthtrevino
Copy link
Contributor Author

@kittens The changes you requested have been implemented and the CI looks good now.

@darthtrevino
Copy link
Contributor Author

63122063

@PAkerstrand
Copy link

Does this PR require further work/changes? Would love to see it merged...

@darthtrevino
Copy link
Contributor Author

darthtrevino commented Nov 9, 2016

¯_(ツ)_/¯ - I've addressed the review issues; it's up to the yarn team now

@sebmck sebmck merged commit b4e42e3 into yarnpkg:master Nov 14, 2016
@darthtrevino
Copy link
Contributor Author

💃

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

Successfully merging this pull request may close these issues.

8 participants