-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
release.ts
177 lines (147 loc) · 3.72 KB
/
release.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Octokit } from "@octokit/core";
import dotenv from "dotenv";
import log from "loglevel";
import { join } from "path";
import { changelogData } from "./changelogData";
import { clean } from "./clean";
import { projectRoot } from "./constants";
import { libsize } from "./libsize";
import {
getLernaVersion,
git,
replaceTag,
run,
uncommittedFiles,
verify,
} from "./utils";
import { initBlog } from "./utils/initBlog";
import { variables } from "./variables";
export type ReleaseType =
| "major"
| "minor"
| "patch"
| "premajor"
| "preminor"
| "prepatch"
| "prerelease"
| "";
export const RELEASE_TYPES: readonly ReleaseType[] = [
"major",
"minor",
"patch",
"premajor",
"preminor",
"prepatch",
"prerelease",
];
export function toReleaseType(value: string): ReleaseType {
if (RELEASE_TYPES.includes(value as ReleaseType)) {
return value as ReleaseType;
}
return "";
}
async function rollback(): Promise<never> {
log.error("Cancelling this release...");
const version = await getLernaVersion();
git(`reset HEAD^`);
git(`tag -d v${version}`);
git("checkout .");
return process.exit(1);
}
async function continueOrRollback(autoConfirm: boolean): Promise<void> {
const complete = await verify("Continue the release?", autoConfirm);
if (!complete) {
await rollback();
}
log.info();
}
interface Options {
clean: boolean;
type: ReleaseType;
blog: boolean;
autoYes: boolean;
}
export async function release({
autoYes,
blog,
clean: enableClean,
type,
}: Options): Promise<void> {
const localDotEnv = join(projectRoot, ".env.local");
dotenv.config({ path: localDotEnv });
const { GITHUB_TOKEN } = process.env;
if (!GITHUB_TOKEN) {
log.error(
`Missing a \`GITHUB_TOKEN\` environment variable. This should be located at:
- ${localDotEnv}
A token can be created at:
- https://github.com/settings/tokens/new?scopes=repo
`
);
process.exit(1);
}
const yes = autoYes ? " --yes" : "";
if (enableClean) {
log.info("Cleaning all the old dists and `.tsbuildinfo` files...");
await clean();
}
log.info("Updating scssVariables files...");
// ensure the latest `dist/scssVariables` have been created
await variables();
// have to run the build before updating the changelog data since it pulls
// the variables from the react-md package
run("yarn build");
const updatedVariables = uncommittedFiles();
if (updatedVariables) {
git("stash");
}
await changelogData();
run(`npx lerna version ${type} --no-push${yes}`);
const changelog = await initBlog(autoYes);
const percentChanged = await libsize({
umd: true,
themes: true,
// have to force the themes to be updated since they are always stored in
// git now
forceThemes: true,
stageChanges: true,
});
git("add themes");
if (updatedVariables) {
git("stash pop");
}
if (blog) {
log.info("Update the blog...");
await continueOrRollback(autoYes);
}
git("add -u");
await replaceTag();
let distTag = "";
if (type.startsWith("pre")) {
distTag = " --dist-tag next";
}
run(`npx lerna publish from-package${distTag}${yes}`);
await continueOrRollback(autoYes);
git("push origin main");
git("push --tags");
log.info("Creating github release");
const body = `${changelog}
### Library Size Changes
\`\`\`sh
${percentChanged}
\`\`\`
`;
const version = await getLernaVersion();
const octokit = new Octokit({ auth: GITHUB_TOKEN });
const response = await octokit.request(
"POST /repos/{owner}/{repo}/releases",
{
owner: "mlaursen",
repo: "react-md",
tag_name: `v${version}`,
body,
prerelease: type.startsWith("pre"),
}
);
log.info(`Created release: ${response.data.html_url}`);
}