Skip to content

Commit a548415

Browse files
committed
fix: fix error handling when no npm token is defined
1 parent f7b73e5 commit a548415

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

lib/definitions/errors.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Your configuration for the \`tarballDir\` option is \`${tarballDir}\`.`,
2222
details: `The [pkgRoot option](${linkify('README.md#pkgroot')}) option, if defined, must be a \`String\`.
2323
2424
Your configuration for the \`pkgRoot\` option is \`${pkgRoot}\`.`,
25+
}),
26+
ENONPMTOKEN: ({registry}) => ({
27+
message: 'No npm token specified.',
28+
details: `An [npm token](${linkify(
29+
'README.md#npm-registry-authentication'
30+
)}) must be created and set in the \`NPM_TOKEN\` environment variable on your CI environment.
31+
32+
Please make sure to create an [npm token](https://docs.npmjs.com/getting-started/working_with_tokens#how-to-create-new-tokens) and to set it in the \`NPM_TOKEN\` environment variable on your CI environment. The token must allow to publish to the registry \`${registry}\`.`,
2533
}),
2634
EINVALIDNPMTOKEN: ({registry}) => ({
2735
message: 'Invalid npm token.',
@@ -33,7 +41,6 @@ If you are using Two-Factor Authentication, make configure the \`auth-only\` [le
3341
3442
Please make sure to set the \`NPM_TOKEN\` environment variable in your CI with the exact value of the npm token.`,
3543
}),
36-
3744
ENOPKGNAME: () => ({
3845
message: 'Missing `name` property in `package.json`.',
3946
details: `The \`package.json\`'s [name](https://docs.npmjs.com/files/package.json#name) property is required in order to publish a package to the npm registry.

lib/set-npmrc-auth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const {appendFile} = require('fs-extra');
22
const getAuthToken = require('registry-auth-token');
33
const nerfDart = require('nerf-dart');
4-
const SemanticReleaseError = require('@semantic-release/error');
4+
const AggregateError = require('aggregate-error');
5+
const getError = require('./get-error');
56

67
module.exports = async (registry, logger) => {
78
logger.log('Verify authentication for registry %s', registry);
@@ -17,6 +18,6 @@ module.exports = async (registry, logger) => {
1718
await appendFile('./.npmrc', `\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`);
1819
logger.log('Wrote NPM_TOKEN to .npmrc.');
1920
} else {
20-
throw new SemanticReleaseError('No npm token specified.', 'ENONPMTOKEN');
21+
throw new AggregateError([getError('ENONPMTOKEN', {registry})]);
2122
}
2223
};

test/set-npmrc-auth.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test.serial('Do not modify ".npmrc" if auth is already configured for a scoped p
6868
});
6969

7070
test.serial('Throw error if "NPM_TOKEN" is missing', async t => {
71-
const error = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
71+
const [error] = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
7272

7373
t.is(error.name, 'SemanticReleaseError');
7474
t.is(error.message, 'No npm token specified.');
@@ -78,7 +78,7 @@ test.serial('Throw error if "NPM_TOKEN" is missing', async t => {
7878
test.serial('Throw error if "NPM_USERNAME" is missing', async t => {
7979
process.env.NPM_PASSWORD = 'npm_pasword';
8080
process.env.NPM_EMAIL = 'npm_email';
81-
const error = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
81+
const [error] = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
8282

8383
t.is(error.name, 'SemanticReleaseError');
8484
t.is(error.message, 'No npm token specified.');
@@ -88,7 +88,7 @@ test.serial('Throw error if "NPM_USERNAME" is missing', async t => {
8888
test.serial('Throw error if "NPM_PASSWORD" is missing', async t => {
8989
process.env.NPM_USERNAME = 'npm_username';
9090
process.env.NPM_EMAIL = 'npm_email';
91-
const error = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
91+
const [error] = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
9292

9393
t.is(error.name, 'SemanticReleaseError');
9494
t.is(error.message, 'No npm token specified.');
@@ -98,7 +98,7 @@ test.serial('Throw error if "NPM_PASSWORD" is missing', async t => {
9898
test.serial('Throw error if "NPM_EMAIL" is missing', async t => {
9999
process.env.NPM_USERNAME = 'npm_username';
100100
process.env.NPM_PASSWORD = 'npm_password';
101-
const error = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
101+
const [error] = await t.throws(setNpmrcAuth('http://custom.registry.com', t.context.logger));
102102

103103
t.is(error.name, 'SemanticReleaseError');
104104
t.is(error.message, 'No npm token specified.');

0 commit comments

Comments
 (0)