Skip to content

Commit

Permalink
Adds more info for release URL (#11492)
Browse files Browse the repository at this point in the history
* Add getReleaseUrl function

* Implement

* Add emoji

* tmp

* Update jest

* Test works

* Add comment

* Empty lines to tigger netlify build

Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
sosukesuzuki and fisker committed Sep 11, 2021
1 parent 5ce28dd commit 96795dc
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 341 deletions.
9 changes: 9 additions & 0 deletions scripts/release/__tests__/setup.js
@@ -0,0 +1,9 @@
import { jest } from "@jest/globals";

jest.unstable_mockModule("../get-formatted-date", () => ({
default: () => ({
year: "2021",
month: "09",
day: "01",
}),
}));
26 changes: 26 additions & 0 deletions scripts/release/__tests__/steps/publish-to-npm.spec.js
@@ -0,0 +1,26 @@
import { getReleaseUrl } from "../../steps/publish-to-npm.js";

describe("publish-to-npm", () => {
describe("getReleaseUrl", () => {
it("returns URL for patch releasing", () => {
const result = getReleaseUrl("2.3.1", "2.3.0");
expect(result).toBe(
"https://github.com/prettier/prettier/releases/new?tag=2.3.1&title=2.3.1&body=%F0%9F%94%97%20%5BChangelog%5D(https%3A%2F%2Fgithub.com%2Fprettier%2Fprettier%2Fblob%2Fmain%2FCHANGELOG.md%23231)"
);
});

it("returns URL for minor releasing", () => {
const result = getReleaseUrl("2.4.0", "2.3.0");
expect(result).toBe(
"https://github.com/prettier/prettier/releases/new?tag=2.4.0&title=2.4.0&body=%5Bdiff%5D(https%3A%2F%2Fgithub.com%2Fprettier%2Fprettier%2Fcompare%2F2.3.0...2.4.0)%0A%0A%F0%9F%94%97%20%5BRelease%20note%5D(https%3A%2F%2Fprettier.io%2Fblog%2F2021%2F09%2F01%2F2.4.0.html)"
);
});

it("returns URL for major releasing", () => {
const result = getReleaseUrl("2.3.0", "2.2.0");
expect(result).toBe(
"https://github.com/prettier/prettier/releases/new?tag=2.3.0&title=2.3.0&body=%5Bdiff%5D(https%3A%2F%2Fgithub.com%2Fprettier%2Fprettier%2Fcompare%2F2.2.0...2.3.0)%0A%0A%F0%9F%94%97%20%5BRelease%20note%5D(https%3A%2F%2Fprettier.io%2Fblog%2F2021%2F09%2F01%2F2.3.0.html)"
);
});
});
});
8 changes: 8 additions & 0 deletions scripts/release/get-formatted-date.js
@@ -0,0 +1,8 @@
// TODO: Implement this in `utils.js` when jest.importActual is landed.
export default function getFormattedDate() {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return { year, month, day };
}
6 changes: 6 additions & 0 deletions scripts/release/jest.config.js
@@ -0,0 +1,6 @@
const config = {
testMatch: ["<rootDir>/__tests__/**/*.spec.js"],
setupFiles: ["<rootDir>/__tests__/setup.js"],
};

export default config;
3 changes: 2 additions & 1 deletion scripts/release/package.json
Expand Up @@ -14,6 +14,7 @@
"string-width": "5.0.0"
},
"devDependencies": {
"jest": "27.1.0"
"@jest/globals": "27.1.1",
"jest": "27.1.1"
}
}
35 changes: 32 additions & 3 deletions scripts/release/steps/publish-to-npm.js
@@ -1,7 +1,13 @@
import chalk from "chalk";
import outdent from "outdent";
import execa from "execa";
import { logPromise, waitForEnter } from "../utils.js";
import semver from "semver";
import {
getBlogPostInfo,
getChangelogContent,
logPromise,
waitForEnter,
} from "../utils.js";

const outdentString = outdent.string;

Expand All @@ -27,21 +33,44 @@ async function retryNpmPublish() {
}
}

export default async function publishToNpm({ dry, version }) {
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 https://github.com/prettier/prettier/releases/new?tag=${version}}
- Go to {cyan.underline ${releaseUrl}}
- Copy release notes from {yellow CHANGELOG.md}
- Press {bgGreen.black Publish release }
Expand Down
30 changes: 9 additions & 21 deletions scripts/release/steps/update-changelog.js
Expand Up @@ -3,31 +3,19 @@ import execa from "execa";
import chalk from "chalk";
import outdent from "outdent";
import semver from "semver";
import { waitForEnter, runYarn, logPromise } from "../utils.js";
import {
waitForEnter,
runYarn,
logPromise,
getBlogPostInfo,
getChangelogContent,
} from "../utils.js";

const outdentString = outdent.string;

function getBlogPostInfo(version) {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");

return {
file: `website/blog/${year}-${month}-${day}-${version}.md`,
path: `blog/${year}/${month}/${day}/${version}.html`,
};
}

function writeChangelog({ version, previousVersion, body }) {
function writeChangelog(params) {
const changelog = fs.readFileSync("CHANGELOG.md", "utf-8");
const newEntry = outdent`
# ${version}
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
${body}
`;
const newEntry = `# ${params.version}\n\n` + getChangelogContent(params);
fs.writeFileSync("CHANGELOG.md", newEntry + "\n\n" + changelog);
}

Expand Down
21 changes: 21 additions & 0 deletions scripts/release/utils.js
Expand Up @@ -4,6 +4,8 @@ import chalk from "chalk";
import execa from "execa";
import stringWidth from "string-width";
import fetch from "node-fetch";
import outdent from "outdent";
import getFormattedDate from "./get-formatted-date.js";

readline.emitKeypressEvents(process.stdin);

Expand Down Expand Up @@ -90,6 +92,23 @@ async function fetchText(url) {
return response.text();
}

function getBlogPostInfo(version) {
const { year, month, day } = getFormattedDate();

return {
file: `website/blog/${year}-${month}-${day}-${version}.md`,
path: `blog/${year}/${month}/${day}/${version}.html`,
};
}

function getChangelogContent({ version, previousVersion, body }) {
return outdent`
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
${body}
`;
}

export {
runYarn,
runGit,
Expand All @@ -99,4 +118,6 @@ export {
readJson,
writeJson,
waitForEnter,
getBlogPostInfo,
getChangelogContent,
};

0 comments on commit 96795dc

Please sign in to comment.