Skip to content

Commit

Permalink
fix(README): correct docs on authentication (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
swain committed Oct 20, 2021
1 parent d47164d commit f089d9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

| Step | Description |
|--------------------|-------------|
| `verifyConditions` | Verify the presence of the `NPM_TOKEN` environment variable, create or update the `.npmrc` file with the token and verify the token is valid. |
| `verifyConditions` | Verify the presence of the `NPM_TOKEN` environment variable, or an `.npmrc` file, and verify the authentication method is valid. |
| `prepare` | Update the `package.json` version and [create](https://docs.npmjs.com/cli/pack) the npm package tarball. |
| `addChannel` | [Add a release to a dist-tag](https://docs.npmjs.com/cli/dist-tag). |
| `publish` | [Publish the npm package](https://docs.npmjs.com/cli/publish) to the registry. |
Expand Down Expand Up @@ -41,7 +41,9 @@ The npm authentication configuration is **required** and can be set via [environ

Both the [token](https://docs.npmjs.com/getting-started/working_with_tokens) and the legacy (`username`, `password` and `email`) authentication are supported. It is recommended to use the [token](https://docs.npmjs.com/getting-started/working_with_tokens) authentication. The legacy authentication is supported as the alternative npm registries [Artifactory](https://www.jfrog.com/open-source/#os-arti) and [npm-registry-couchapp](https://github.com/npm/npm-registry-couchapp) only supports that form of authentication.

**Note**: Only the `auth-only` [level of npm two-factor authentication](https://docs.npmjs.com/getting-started/using-two-factor-authentication#levels-of-authentication) is supported, **semantic-release** will not work with the default `auth-and-writes` level.
**Notes**:
- Only the `auth-only` [level of npm two-factor authentication](https://docs.npmjs.com/getting-started/using-two-factor-authentication#levels-of-authentication) is supported, **semantic-release** will not work with the default `auth-and-writes` level.
- The presence of an `.npmrc` file will override any specified environment variables.

### Environment variables

Expand Down
27 changes: 27 additions & 0 deletions test/set-npmrc-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,30 @@ test.serial('Throw error if "NPM_EMAIL" is missing', async (t) => {
t.is(error.message, 'No npm token specified.');
t.is(error.code, 'ENONPMTOKEN');
});

test.serial('Prefer .npmrc over environment variables', async (t) => {
process.env.HOME = tempy.directory();
const cwd = tempy.directory();
process.chdir(cwd);
const npmrc = tempy.file({name: '.npmrc'});
// Specify an NPM token environment variable
const env = {NPM_TOKEN: 'env_npm_token'};

await appendFile(path.resolve(cwd, '.npmrc'), '//registry.npmjs.org/:_authToken=npmrc_npm_token');

await require('../lib/set-npmrc-auth')(npmrc, 'http://registry.npmjs.org', {cwd, env, logger: t.context.logger});

t.is(
(await readFile(npmrc)).toString(),
// Assert did not write the token from environment variable
`//registry.npmjs.org/:_authToken=npmrc_npm_token`
);

// Assert reads from config
t.deepEqual(t.context.log.args[1], ['Reading npm config from %s', path.resolve(cwd, '.npmrc')]);

// Assert does not write NPM_TOKEN
for (const log of t.context.log.args) {
t.false(log.includes('Wrote NPM_TOKEN'));
}
});

0 comments on commit f089d9d

Please sign in to comment.