Skip to content

Commit 0df36a7

Browse files
committed
build(release): updated release behavior
1 parent 348937f commit 0df36a7

14 files changed

+608
-150
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
/dist
12
/.next
23
/public
34
/node_modules
4-
/dist

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@ module.exports = {
33
rules: {
44
'react/react-in-jsx-scope': 0,
55
},
6+
overrides: [
7+
{
8+
files: ['scripts/*.ts'],
9+
rules: {
10+
'no-console': 0,
11+
},
12+
},
13+
],
614
};

.gitignore

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ coverage
1212

1313
# Dependency directories
1414
node_modules/
15+
1516
# TypeScript cache
1617
*.tsbuildinfo
1718

19+
# For creating github releases
20+
.env.github
21+
22+
# For vercel deployments
1823
.env.production.local
1924

2025
# Output of 'npm pack'
2126
*.tgz
2227

2328
# generated output
29+
/dist
2430
/.next
2531
/public/tsdocs
26-
/es
27-
/lib
28-
/dist
29-
/types

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CHANGELOG.md
2+
/node_modules

changelog.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

createEnv.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
testEnvironment: 'jsdom',
55
globals: {
66
'ts-jest': {
7-
tsconfig: './tsconfig.jest.json',
7+
tsconfig: './tsconfig.node.json',
88
},
99
},
1010
moduleNameMapper: {

package.json

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@
1717
"scripts": {
1818
"prepare": "husky install",
1919
"prebuild": "npm-run-all -p lint typecheck api-docs create-env",
20-
"clean-dist": "rimraf dist",
21-
"ci": "npm-run-all lint typecheck test",
22-
"create-env": "node createEnv.js",
23-
"api-docs": "node ./typedoc.js",
24-
"bundle": "rollup -c rollup.config.js",
25-
"update-version": "standard-version",
26-
"do-publish": "npm publish",
27-
"push-tag": "git push --follow-tags origin main",
28-
"release": "npm-run-all build-pkg update-version do-publish push-tag",
29-
"typedefs": "tsc -p tsconfig.typedefs.json",
30-
"build-pkg": "npm-run-all clean-dist bundle typedefs",
31-
"typecheck": "tsc -p tsconfig.check.json",
20+
"run-script": "ts-node -T -P tsconfig.node.json",
21+
"create-env": "yarn run-script scripts/createEnv.ts",
22+
"api-docs": "yarn run-script scripts/typedoc.ts",
23+
"release": "yarn run-script scripts/release.ts",
3224
"lint": "eslint \"**/*.{js,jsx,ts,tsx}\"",
25+
"typecheck": "tsc -p tsconfig.check.json",
3326
"test": "jest",
3427
"start": "next dev",
3528
"build": "next build"
@@ -43,24 +36,28 @@
4336
"@babel/preset-env": "^7.15.4",
4437
"@babel/preset-react": "^7.14.5",
4538
"@babel/preset-typescript": "^7.15.0",
46-
"@mlaursen/changelog-preset": "^1.1.0",
47-
"@mlaursen/eslint-config": "^1.1.4",
39+
"@mlaursen/eslint-config": "^1.1.5",
4840
"@next/eslint-plugin-next": "^11.1.2",
41+
"@octokit/core": "^3.5.1",
4942
"@rollup/plugin-babel": "^5.3.0",
5043
"@rollup/plugin-commonjs": "^20.0.0",
5144
"@rollup/plugin-node-resolve": "^13.0.4",
5245
"@rollup/plugin-replace": "^3.0.0",
5346
"@testing-library/jest-dom": "^5.14.1",
5447
"@testing-library/react": "^12.0.0",
48+
"@types/inquirer": "^8.1.1",
5549
"@types/jest": "^27.0.1",
50+
"@types/node": "^16.7.13",
5651
"@types/prismjs": "^1.16.6",
5752
"@types/react": "^17.0.19",
5853
"@types/react-dom": "^17.0.9",
5954
"babel-plugin-prismjs": "^2.1.0",
55+
"dotenv": "^10.0.0",
6056
"eslint": "^7.32.0",
6157
"filesize": "^8.0.0",
6258
"husky": "^7.0.2",
6359
"identity-obj-proxy": "^3.0.0",
60+
"inquirer": "^8.1.2",
6461
"jest": "^27.1.0",
6562
"jest-watch-typeahead": "^0.6.4",
6663
"lint-staged": "^11.1.2",
@@ -77,6 +74,7 @@
7774
"sass": "^1.39.0",
7875
"standard-version": "^9.3.1",
7976
"ts-jest": "^27.0.5",
77+
"ts-node": "^10.2.1",
8078
"typedoc": "^0.21.9",
8179
"typedoc-plugin-sourcefile-url": "^1.0.6",
8280
"typescript": "^4.4.2"

scripts/createEnv.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// vi: ft=javascript
2+
import { execSync } from "child_process";
3+
import { writeFile } from "fs";
4+
5+
const commitSha = execSync("git rev-parse HEAD").toString().trim();
6+
const gaCode = "G-JSF6GSWE18";
7+
8+
const contents = `NEXT_PUBLIC_GA_CODE=${gaCode}
9+
NEXT_PUBLIC_COMMIT_SHA=${commitSha}
10+
`;
11+
12+
writeFile(".env.production.local", contents, (error) => {
13+
if (error) {
14+
throw error;
15+
}
16+
17+
// eslint-disable-next-line no-console
18+
console.log(contents);
19+
});

scripts/release.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { Octokit } from "@octokit/core";
2+
import { execSync } from "child_process";
3+
import dotenv from "dotenv";
4+
import { readFileSync } from "fs";
5+
import inquirer from "inquirer";
6+
import { join } from "path";
7+
8+
function loggedCommand(command: string): void {
9+
console.log(command);
10+
execSync(command, { stdio: "inherit" });
11+
console.log("");
12+
}
13+
14+
function undo(version?: string): void {
15+
loggedCommand("git reset HEAD^");
16+
if (typeof version === "string") {
17+
loggedCommand(`git tag -d v${version}`);
18+
}
19+
}
20+
21+
const NEW_ENTRY = /^#{1,3}\s+\[\d/;
22+
23+
async function getReleaseNotes(version: string): Promise<string> {
24+
console.log("Update the CHANGELOG.md with any additional changes.");
25+
const { confirmed } = await inquirer.prompt<{ confirmed: boolean }>([
26+
{
27+
type: "confirm",
28+
name: "confirmed",
29+
message: "Continue the release?",
30+
},
31+
]);
32+
if (!confirmed) {
33+
console.error("Canceling the release.");
34+
undo(version);
35+
36+
process.exit(1);
37+
}
38+
39+
const changelog = readFileSync(join(process.cwd(), "CHANGELOG.md"), "utf8");
40+
const lines = changelog.split(/\r?\n/);
41+
let lastEntryStart = -1;
42+
let nextEntryStart = -1;
43+
44+
for (let i = 0; i < lines.length; i += 1) {
45+
if (NEW_ENTRY.test(lines[i])) {
46+
if (lastEntryStart === -1) {
47+
lastEntryStart = i + 1;
48+
} else if (nextEntryStart === -1) {
49+
nextEntryStart = i;
50+
break;
51+
}
52+
}
53+
}
54+
55+
if (lastEntryStart === -1 || nextEntryStart === -1) {
56+
console.error("Unable to find a release block.");
57+
process.exit(1);
58+
}
59+
60+
return lines.slice(lastEntryStart, nextEntryStart).join("\n");
61+
}
62+
63+
const RELEASE_TYPES = ["major", "minor", "patch"];
64+
65+
async function run(): Promise<void> {
66+
let type = "";
67+
let prerelease = "";
68+
for (let i = 0; i < process.argv.length; i += 1) {
69+
const flag = process.argv[i];
70+
const value = process.argv[i + 1];
71+
if (
72+
flag === "-t" &&
73+
typeof value === "string" &&
74+
RELEASE_TYPES.includes(value)
75+
) {
76+
type = ` --release-as ${value}`;
77+
} else if (flag === "-p" || flag === "--prerelease") {
78+
prerelease = " --prerelease";
79+
}
80+
}
81+
82+
const githubDotEnv = join(process.cwd(), ".env.github");
83+
dotenv.config({ path: githubDotEnv });
84+
85+
const { GITHUB_TOKEN } = process.env;
86+
if (!GITHUB_TOKEN) {
87+
console.error(
88+
`Missing a \`GITHUB_TOKEN\` environment variable. This should be located at:
89+
- ${githubDotEnv}
90+
91+
A token can be created at:
92+
- https://github.com/settings/tokens/new?scopes=repo
93+
`
94+
);
95+
96+
process.exit(1);
97+
}
98+
99+
loggedCommand("yarn lint");
100+
loggedCommand("yarn typecheck");
101+
loggedCommand("yarn test");
102+
loggedCommand("yarn rimraf dist");
103+
loggedCommand("yarn rollup -c rollup.config.js");
104+
loggedCommand("yarn tsc -p tsconfig.typedefs.json");
105+
loggedCommand(`yarn standard-version --preset angular${type}${prerelease}`);
106+
107+
const { version } = JSON.parse(
108+
readFileSync(join(process.cwd(), "package.json"), "utf8")
109+
);
110+
if (typeof version !== "string") {
111+
console.error("Unable to get the package version.");
112+
undo();
113+
process.exit(1);
114+
}
115+
116+
const releaseNotes = await getReleaseNotes(version);
117+
118+
loggedCommand("npm publish");
119+
loggedCommand("git push --follow-tags origin main");
120+
const octokit = new Octokit({ auth: GITHUB_TOKEN });
121+
const response = await octokit.request(
122+
"POST /repos/{owner}/{repo}/releases",
123+
{
124+
owner: "mlaursen",
125+
repo: "react-marked-renderer",
126+
tag_name: `v${version}`,
127+
body: releaseNotes,
128+
}
129+
);
130+
131+
console.log(`Created release: ${response.data.html_url}`);
132+
}
133+
134+
run();

0 commit comments

Comments
 (0)