Skip to content

Commit f5c8d85

Browse files
committed
fix(auth): throw appropriate error when auth context fails to enable publishing
1 parent e24967d commit f5c8d85

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

lib/definitions/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Please make sure to create an [npm token](https://docs.npmjs.com/getting-started
4545
};
4646
}
4747

48-
export function EINVALIDNPMAUTH({ registry }) {
48+
export function EINVALIDNPMAUTH() {
4949
return {
5050
message: "Invalid npm authentication.",
5151
details: `The [authentication required to publish](${linkify(

lib/verify-auth.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { execa } from "execa";
22
import normalizeUrl from "normalize-url";
33
import AggregateError from "aggregate-error";
44
import getRegistry from "./get-registry.js";
5+
import setNpmrcAuth from "./set-npmrc-auth.js";
6+
import getError from "./get-error.js";
7+
8+
function registryIsDefault(registry, DEFAULT_NPM_REGISTRY) {
9+
return normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY);
10+
}
511

612
export default async function (npmrc, pkg, context) {
713
const {
@@ -14,7 +20,7 @@ export default async function (npmrc, pkg, context) {
1420

1521
// await setNpmrcAuth(npmrc, registry, context);
1622

17-
if (normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)) {
23+
if (registryIsDefault(registry, DEFAULT_NPM_REGISTRY)) {
1824
// try {
1925
const publishDryRunResult = execa("npm", ["publish", "--dry-run", "--tag=semantic-release-auth-check"], {cwd, env, preferLocal: true, lines: true});
2026
// const whoamiResult = execa("npm", ["whoami", "--userconfig", npmrc, "--registry", registry], {
@@ -27,14 +33,14 @@ export default async function (npmrc, pkg, context) {
2733
// await whoamiResult;
2834
publishDryRunResult.stdout.pipe(stdout, { end: false });
2935
publishDryRunResult.stderr.pipe(stderr, { end: false });
30-
const {stderr: execaStderr} = await publishDryRunResult;
31-
execaStderr.forEach((line) => {
32-
if (line.includes("This command requires you to be logged in to https://registry.npmjs.org/")) {
33-
throw new AggregateError([new Error('no auth context')]);
36+
37+
(await publishDryRunResult).stderr.forEach((line) => {
38+
if (line.includes("This command requires you to be logged in to ")) {
39+
throw new AggregateError([getError("EINVALIDNPMAUTH" )]);
3440
}
3541
})
3642
// } catch {
37-
// throw new AggregateError([getError("EINVALIDNPMTOKEN", { registry })]);
43+
// throw new AggregateError([getError("EINVALIDNPMAUTH", { registry })]);
3844
// }
3945
}
4046
}

test/integration.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ test("Throws error if NPM token is invalid", async (t) => {
107107
);
108108

109109
t.is(error.name, "SemanticReleaseError");
110-
t.is(error.code, "EINVALIDNPMTOKEN");
111-
t.is(error.message, "Invalid npm token.");
110+
t.is(error.code, "EINVALIDNPMAUTH");
111+
t.is(error.message, "Invalid npm authentication.");
112112
});
113113

114114
test("Skip Token validation if the registry configured is not the default one", async (t) => {

0 commit comments

Comments
 (0)