-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathcreate-release.ts
More file actions
99 lines (76 loc) · 2.98 KB
/
Copy pathcreate-release.ts
File metadata and controls
99 lines (76 loc) · 2.98 KB
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
#!/usr/bin/env bun
import { readFileSync } from "node:fs";
import { join } from "node:path";
function extractChangelogNotes(): string {
const changelogPath = join(process.cwd(), "CHANGELOG.md");
const changelogContent = readFileSync(changelogPath, "utf-8");
const lines = changelogContent.split("\n");
const releaseNotes: string[] = [];
let foundFirstHeader = false;
for (const line of lines) {
if (line.startsWith("## ")) {
if (foundFirstHeader) {
// We've hit the second header, stop collecting notes
break;
}
foundFirstHeader = true;
continue; // Skip the first header line itself
}
if (foundFirstHeader) {
releaseNotes.push(line);
}
}
// Remove trailing empty lines
while (releaseNotes.length > 0 && releaseNotes[releaseNotes.length - 1].trim() === "") {
releaseNotes.pop();
}
return releaseNotes.join("\n").trim();
}
async function createRelease() {
try {
// Read package.json to get version
const packagePath = join(process.cwd(), "package.json");
const packageContent = readFileSync(packagePath, "utf-8");
const packageJson = JSON.parse(packageContent);
const version = packageJson.version;
// Extract release notes from changelog
const releaseNotes = extractChangelogNotes();
if (!releaseNotes) {
console.error("Error: No release notes found in CHANGELOG.md");
process.exit(1);
}
const tagName = `v${version}`;
const title = `v${version}`;
const commitMessage = `Version ${version}`;
// Commit and push all changes with version message before tagging the release
console.log("Committing and pushing changes...");
// Add all files
const addProc = Bun.spawn(["git", "add", "."]);
await addProc.exited;
// Commit with version message
const commitProc = Bun.spawn(["git", "commit", "-m", commitMessage]);
await commitProc.exited;
// Push changes
const pushProc = Bun.spawn(["git", "push"]);
await pushProc.exited;
console.log(`✅ Successfully committed and pushed changes with message: "${commitMessage}"`);
console.log(`Creating release ${tagName}...`);
console.log("Release notes:");
console.log(releaseNotes);
// Create the GitHub release using stdin for release notes to handle special characters
const proc = Bun.spawn(
["gh", "release", "create", tagName, "--title", title, "--notes-file", "-", "--latest"],
{
stdin: "pipe",
},
);
proc.stdin?.write(releaseNotes);
proc.stdin?.end();
await proc.exited;
console.log(`✅ Successfully created release ${tagName}`);
} catch (error) {
console.error("Error creating release:", error);
process.exit(1);
}
}
createRelease();