Skip to content

Commit 74804f3

Browse files
committed
fix: allow _auth to be set via environment
When using legacy auth, the env var `NPM_CONFIG__AUTH` can be used. Fixes #324
1 parent 5e0cd43 commit 74804f3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ Both the [token](https://docs.npmjs.com/getting-started/working_with_tokens) and
5353
| `NPM_USERNAME` | Npm username created via [npm adduser](https://docs.npmjs.com/cli/adduser) or on [npmjs.com](https://www.npmjs.com) |
5454
| `NPM_PASSWORD` | Password of the npm user. |
5555
| `NPM_EMAIL` | Email address associated with the npm user |
56-
| `NPM_CONFIG_USERCONFIG` | Path to non-default .npmrc file |
56+
| `NPM_CONFIG_USERCONFIG` | Path to non-default .npmrc file |
57+
| `NPM_CONFIG__AUTH` | Npm `_auth` configuration parameter set as an [environment variable](https://docs.npmjs.com/cli/v7/using-npm/config) |
5758

58-
Use either `NPM_TOKEN` for token authentication or `NPM_USERNAME`, `NPM_PASSWORD` and `NPM_EMAIL` for legacy authentication
59+
Use either `NPM_TOKEN` for token authentication or `NPM_USERNAME`, `NPM_PASSWORD` and `NPM_EMAIL` (or alternatively `NPM_CONFIG__AUTH` and `NPM_EMAIL`) for legacy authentication
5960

6061
### Options
6162

lib/set-npmrc-auth.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const getError = require('./get-error');
99
module.exports = async (
1010
npmrc,
1111
registry,
12-
{cwd, env: {NPM_TOKEN, NPM_CONFIG_USERCONFIG, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL}, logger}
12+
{cwd, env: {NPM_TOKEN, NPM_CONFIG__AUTH, NPM_CONFIG_USERCONFIG, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL}, logger}
1313
) => {
1414
logger.log('Verify authentication for registry %s', registry);
1515
const {configs, ...rcConfig} = rc(
@@ -35,6 +35,12 @@ module.exports = async (
3535
`${currentConfig ? `${currentConfig}\n` : ''}_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`
3636
);
3737
logger.log(`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`);
38+
} else if (NPM_CONFIG__AUTH && NPM_EMAIL) {
39+
await outputFile(
40+
npmrc,
41+
`${currentConfig ? `${currentConfig}\n` : ''}_auth = \${NPM_CONFIG__AUTH}\nemail = \${NPM_EMAIL}`
42+
);
43+
logger.log(`Wrote NPM_CONFIG__AUTH and NPM_EMAIL to ${npmrc}`);
3844
} else if (NPM_TOKEN) {
3945
await outputFile(
4046
npmrc,

test/set-npmrc-auth.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ test.serial('Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"', asyn
4848
t.deepEqual(t.context.log.args[1], [`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`]);
4949
});
5050

51+
test.serial('Set auth with "NPM_CONFIG__AUTH" and "NPM_EMAIL"', async (t) => {
52+
process.env.HOME = tempy.directory();
53+
const cwd = tempy.directory();
54+
process.chdir(cwd);
55+
const npmrc = tempy.file({name: '.npmrc'});
56+
const env = {NPM_CONFIG__AUTH: 'npm_config__auth', NPM_EMAIL: 'npm_email'};
57+
58+
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
59+
60+
t.is((await readFile(npmrc)).toString(), `_auth = \${NPM_CONFIG__AUTH}\nemail = \${NPM_EMAIL}`);
61+
t.deepEqual(t.context.log.args[1], [`Wrote NPM_CONFIG__AUTH and NPM_EMAIL to ${npmrc}`]);
62+
});
63+
5164
test.serial('Preserve home ".npmrc"', async (t) => {
5265
process.env.HOME = tempy.directory();
5366
const cwd = tempy.directory();
@@ -147,6 +160,22 @@ test.serial('Throw error if "NPM_TOKEN" is missing', async (t) => {
147160
t.is(error.code, 'ENONPMTOKEN');
148161
});
149162

163+
test.serial('Throw error if "NPM_EMAIL" is missing when used with "NPM_CONFIG__AUTH"', async (t) => {
164+
process.env.HOME = tempy.directory();
165+
const cwd = tempy.directory();
166+
process.chdir(cwd);
167+
const npmrc = tempy.file({name: '.npmrc'});
168+
const env = {NPM_CONFIG__AUTH: 'npm_config__auth'};
169+
170+
const [error] = await t.throwsAsync(
171+
require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
172+
);
173+
174+
t.is(error.name, 'SemanticReleaseError');
175+
t.is(error.message, 'No npm token specified.');
176+
t.is(error.code, 'ENONPMTOKEN');
177+
});
178+
150179
test.serial('Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set', async (t) => {
151180
process.env.HOME = tempy.directory();
152181
const cwd = tempy.directory();

0 commit comments

Comments
 (0)