Skip to content

Commit e75c620

Browse files
committed
refactor(verify-auth): extract token verification logic to intent-revealing functions
to improve readability for #958
1 parent 739f655 commit e75c620

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

lib/verify-auth.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,48 @@ function registryIsDefault(registry, DEFAULT_NPM_REGISTRY) {
1111
return normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY);
1212
}
1313

14+
async function verifyAuthContextAgainstRegistry(npmrc, registry, cwd, env, stdout, stderr) {
15+
try {
16+
const whoamiResult = execa("npm", ["whoami", "--userconfig", npmrc, "--registry", registry], {
17+
cwd,
18+
env,
19+
preferLocal: true,
20+
});
21+
22+
whoamiResult.stdout.pipe(stdout, { end: false });
23+
whoamiResult.stderr.pipe(stderr, { end: false });
24+
25+
await whoamiResult;
26+
} catch {
27+
throw new AggregateError([getError("EINVALIDNPMTOKEN", { registry })]);
28+
}
29+
}
30+
31+
async function attemptPublishDryRun(npmrc, registry, cwd, env) {
32+
const publishDryRunResult = execa(
33+
"npm",
34+
["publish", "--dry-run", "--tag=semantic-release-auth-check", "--userconfig", npmrc, "--registry", registry],
35+
{ cwd, env, preferLocal: true, lines: true }
36+
);
37+
38+
// publishDryRunResult.stdout.pipe(stdout, { end: false });
39+
// publishDryRunResult.stderr.pipe(stderr, { end: false });
40+
41+
(await publishDryRunResult).stderr.forEach((line) => {
42+
if (line.includes("This command requires you to be logged in to ")) {
43+
throw new AggregateError([getError("EINVALIDNPMAUTH", { registry })]);
44+
}
45+
});
46+
}
47+
48+
async function verifyTokenAuth(registry, DEFAULT_NPM_REGISTRY, npmrc, cwd, env, stdout, stderr) {
49+
if (registryIsDefault(registry, DEFAULT_NPM_REGISTRY)) {
50+
await verifyAuthContextAgainstRegistry(npmrc, registry, cwd, env, stdout, stderr);
51+
} else {
52+
await attemptPublishDryRun(npmrc, registry, cwd, env);
53+
}
54+
}
55+
1456
export default async function (npmrc, pkg, context) {
1557
const {
1658
cwd,
@@ -26,33 +68,5 @@ export default async function (npmrc, pkg, context) {
2668

2769
await setNpmrcAuth(npmrc, registry, context);
2870

29-
if (registryIsDefault(registry, DEFAULT_NPM_REGISTRY)) {
30-
try {
31-
const whoamiResult = execa("npm", ["whoami", "--userconfig", npmrc, "--registry", registry], {
32-
cwd,
33-
env,
34-
preferLocal: true,
35-
});
36-
whoamiResult.stdout.pipe(stdout, { end: false });
37-
whoamiResult.stderr.pipe(stderr, { end: false });
38-
await whoamiResult;
39-
} catch {
40-
throw new AggregateError([getError("EINVALIDNPMTOKEN", { registry })]);
41-
}
42-
} else {
43-
const publishDryRunResult = execa(
44-
"npm",
45-
["publish", "--dry-run", "--tag=semantic-release-auth-check", "--userconfig", npmrc, "--registry", registry],
46-
{ cwd, env, preferLocal: true, lines: true }
47-
);
48-
49-
// publishDryRunResult.stdout.pipe(stdout, { end: false });
50-
// publishDryRunResult.stderr.pipe(stderr, { end: false });
51-
52-
(await publishDryRunResult).stderr.forEach((line) => {
53-
if (line.includes("This command requires you to be logged in to ")) {
54-
throw new AggregateError([getError("EINVALIDNPMAUTH", { registry })]);
55-
}
56-
});
57-
}
71+
await verifyTokenAuth(registry, DEFAULT_NPM_REGISTRY, npmrc, cwd, env, stdout, stderr);
5872
}

0 commit comments

Comments
 (0)