/
publish-to-npm.js
89 lines (78 loc) · 2.42 KB
/
publish-to-npm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import chalk from "chalk";
import outdent from "outdent";
import { execa } from "execa";
import semver from "semver";
import enquirer from "enquirer";
import {
getBlogPostInfo,
getChangelogContent,
logPromise,
waitForEnter,
} from "../utils.js";
const outdentString = outdent.string;
/**
* Retry "npm publish" when to enter OTP is failed.
*/
async function retryNpmPublish() {
const runNpmPublish = async () => {
const { otp } = await enquirer.prompt({
type: "input",
name: "otp",
message: "Please enter your npm OTP",
});
await execa("npm", ["publish", "--otp", otp], { cwd: "./dist" });
};
for (let i = 5; i > 0; i--) {
try {
return await runNpmPublish();
} catch (error) {
if (error.code === "EOTP" && i > 0) {
console.log(`To enter OTP is failed, you can retry it ${i} times.`);
continue;
}
throw error;
}
}
}
export function getReleaseUrl(version, previousVersion) {
const semverDiff = semver.diff(version, previousVersion);
const isPatch = semverDiff === "patch";
let body;
if (isPatch) {
const urlToChangelog =
"https://github.com/prettier/prettier/blob/main/CHANGELOG.md#" +
version.split(".").join("");
body = `🔗 [Changelog](${urlToChangelog})`;
} else {
const blogPostInfo = getBlogPostInfo(version);
body = getChangelogContent({
version,
previousVersion,
body: `🔗 [Release note](https://prettier.io/${blogPostInfo.path})`,
});
}
body = encodeURIComponent(body);
return `https://github.com/prettier/prettier/releases/new?tag=${version}&title=${version}&body=${body}`;
}
export default async function publishToNpm({ dry, version, previousVersion }) {
if (dry) {
return;
}
await logPromise("Publishing to npm", retryNpmPublish());
const releaseUrl = getReleaseUrl(version, previousVersion);
console.log(
outdentString(chalk`
{green.bold Prettier ${version} published!}
{yellow.bold Some manual steps are necessary.}
{bold.underline Create a GitHub Release}
- Go to {cyan.underline ${releaseUrl}}
- Press {bgGreen.black Publish release }
{bold.underline Test the new release}
- In a new session, run {yellow npm i prettier@latest} in another directory
- Test the API and CLI
After that, we can proceed to bump this repo's Prettier dependency.
Press ENTER to continue.
`)
);
await waitForEnter();
}