Skip to content

Commit 7cd002d

Browse files
authored
build(release-script): npm logout after publishing (#15170)
1 parent c30fa9f commit 7cd002d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

tools/release/npm/npm-client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function isNpmAuthenticated(): boolean {
2020
}
2121

2222
/** Runs "npm login" interactively by piping stdin/stderr/stdout to the current tty. */
23-
export function runInteractiveNpmLogin(): boolean {
23+
export function npmLoginInteractive(): boolean {
2424
return spawnSync('npm', ['login'], {
2525
stdio: 'inherit',
2626
shell: true,
@@ -29,7 +29,7 @@ export function runInteractiveNpmLogin(): boolean {
2929
}
3030

3131
/** Runs NPM publish within a specified directory */
32-
export function runNpmPublish(packagePath: string, distTag: string): string | null {
32+
export function npmPublish(packagePath: string, distTag: string): string | null {
3333
const result = spawnSync('npm', ['publish', '--access', 'public', '--tag', distTag], {
3434
cwd: packagePath,
3535
shell: true,
@@ -42,3 +42,11 @@ export function runNpmPublish(packagePath: string, distTag: string): string | nu
4242
return result.stderr.toString();
4343
}
4444
}
45+
46+
/** Log out of npm. */
47+
export function npmLogout(): boolean {
48+
return spawnSync('npm', ['logout'], {
49+
shell: true,
50+
env: npmClientEnvironment,
51+
}).status === 0;
52+
}

tools/release/publish-release.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {checkReleaseOutput} from './check-release-output';
77
import {extractReleaseNotes} from './extract-release-notes';
88
import {GitClient} from './git/git-client';
99
import {getGithubNewReleaseUrl} from './git/github-urls';
10-
import {isNpmAuthenticated, runInteractiveNpmLogin, runNpmPublish} from './npm/npm-client';
10+
import {
11+
isNpmAuthenticated,
12+
npmLogout,
13+
npmLoginInteractive,
14+
npmPublish,
15+
} from './npm/npm-client';
1116
import {promptForNpmDistTag} from './prompt/npm-dist-tag-prompt';
1217
import {promptForUpstreamRemote} from './prompt/upstream-remote-prompt';
1318
import {releasePackages} from './release-output/release-packages';
@@ -125,6 +130,12 @@ class PublishReleaseTask extends BaseReleaseTask {
125130

126131
console.log();
127132
console.info(green(bold(` ✓ Published all packages successfully`)));
133+
134+
// Always log out of npm after releasing to prevent unintentional changes to
135+
// any packages.
136+
npmLogout();
137+
console.info(green(bold(` ✓ Logged out of npm`)));
138+
128139
console.info(yellow(` ⚠ Please draft a new release of the version on Github.`));
129140
console.info(yellow(` ${newReleaseUrl}`));
130141
}
@@ -194,7 +205,7 @@ class PublishReleaseTask extends BaseReleaseTask {
194205
console.log(yellow(` ⚠ NPM is currently not authenticated. Running "npm login"..`));
195206

196207
for (let i = 0; i < MAX_NPM_LOGIN_TRIES; i++) {
197-
if (runInteractiveNpmLogin()) {
208+
if (npmLoginInteractive()) {
198209
// In case the user was able to login properly, we want to exit the loop as we
199210
// don't need to ask for authentication again.
200211
break;
@@ -217,7 +228,7 @@ class PublishReleaseTask extends BaseReleaseTask {
217228
private publishPackageToNpm(packageName: string, npmDistTag: string) {
218229
console.info(green(` ⭮ Publishing "${packageName}"..`));
219230

220-
const errorOutput = runNpmPublish(join(this.releaseOutputPath, packageName), npmDistTag);
231+
const errorOutput = npmPublish(join(this.releaseOutputPath, packageName), npmDistTag);
221232

222233
if (errorOutput) {
223234
console.error(red(` ✘ An error occurred while publishing "${packageName}".`));

0 commit comments

Comments
 (0)