@zenithbuild/plugins 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,554 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * =============================================================================
4
+ * Zenith Release Script
5
+ * =============================================================================
6
+ *
7
+ * Automated release script using Bun for all Zenith repositories.
8
+ * Handles:
9
+ * - Conventional Commit parsing for automatic version determination
10
+ * - CHANGELOG.md generation
11
+ * - package.json version updates
12
+ * - Release notes generation for GitHub releases
13
+ *
14
+ * Usage:
15
+ * bun run scripts/release.ts # Normal release
16
+ * bun run scripts/release.ts --dry-run # Test without making changes
17
+ * bun run scripts/release.ts --bump=major # Force major version bump
18
+ * bun run scripts/release.ts --bump=minor # Force minor version bump
19
+ * bun run scripts/release.ts --bump=patch # Force patch version bump
20
+ *
21
+ * Environment Variables:
22
+ * DRY_RUN - Set to 'true' for dry run mode
23
+ * BUMP_TYPE - Force bump type (patch, minor, major)
24
+ * GITHUB_TOKEN - GitHub token for API calls (optional)
25
+ *
26
+ * =============================================================================
27
+ */
28
+
29
+ import { $ } from "bun";
30
+ import { existsSync } from "fs";
31
+ import { join } from "path";
32
+
33
+ // =============================================================================
34
+ // Types
35
+ // =============================================================================
36
+
37
+ interface Commit {
38
+ hash: string;
39
+ type: string;
40
+ scope: string | null;
41
+ subject: string;
42
+ body: string;
43
+ breaking: boolean;
44
+ raw: string;
45
+ }
46
+
47
+ interface PackageJson {
48
+ name: string;
49
+ version: string;
50
+ private?: boolean;
51
+ [key: string]: unknown;
52
+ }
53
+
54
+ interface ReleaseConfig {
55
+ types: {
56
+ [key: string]: {
57
+ title: string;
58
+ bump: "patch" | "minor" | "major" | null;
59
+ hidden?: boolean;
60
+ };
61
+ };
62
+ skipCI: string[];
63
+ tagPrefix: string;
64
+ }
65
+
66
+ type BumpType = "patch" | "minor" | "major";
67
+
68
+ // =============================================================================
69
+ // Configuration
70
+ // =============================================================================
71
+
72
+ const DEFAULT_CONFIG: ReleaseConfig = {
73
+ types: {
74
+ feat: { title: "✨ Features", bump: "minor" },
75
+ fix: { title: "šŸ› Bug Fixes", bump: "patch" },
76
+ perf: { title: "⚔ Performance Improvements", bump: "patch" },
77
+ refactor: { title: "ā™»ļø Code Refactoring", bump: "patch" },
78
+ docs: { title: "šŸ“š Documentation", bump: null },
79
+ style: { title: "šŸ’„ Styles", bump: null },
80
+ test: { title: "āœ… Tests", bump: null },
81
+ build: { title: "šŸ“¦ Build System", bump: "patch" },
82
+ ci: { title: "šŸ”§ CI Configuration", bump: null },
83
+ chore: { title: "šŸ”Ø Chores", bump: null },
84
+ revert: { title: "āŖ Reverts", bump: "patch" },
85
+ },
86
+ skipCI: ["[skip ci]", "[ci skip]", "[no ci]"],
87
+ tagPrefix: "v",
88
+ };
89
+
90
+ // =============================================================================
91
+ // Utility Functions
92
+ // =============================================================================
93
+
94
+ function log(message: string, type: "info" | "success" | "warn" | "error" = "info"): void {
95
+ const colors = {
96
+ info: "\x1b[36m", // Cyan
97
+ success: "\x1b[32m", // Green
98
+ warn: "\x1b[33m", // Yellow
99
+ error: "\x1b[31m", // Red
100
+ };
101
+ const reset = "\x1b[0m";
102
+ const prefix = {
103
+ info: "ℹ",
104
+ success: "āœ“",
105
+ warn: "⚠",
106
+ error: "āœ–",
107
+ };
108
+ console.log(`${colors[type]}${prefix[type]}${reset} ${message}`);
109
+ }
110
+
111
+ function parseArgs(): { dryRun: boolean; bumpType: BumpType | null } {
112
+ const args = process.argv.slice(2);
113
+ let dryRun = process.env.DRY_RUN === "true";
114
+ let bumpType: BumpType | null = (process.env.BUMP_TYPE as BumpType) || null;
115
+
116
+ for (const arg of args) {
117
+ if (arg === "--dry-run") {
118
+ dryRun = true;
119
+ } else if (arg.startsWith("--bump=")) {
120
+ const bump = arg.split("=")[1] as BumpType;
121
+ if (["patch", "minor", "major"].includes(bump)) {
122
+ bumpType = bump;
123
+ }
124
+ }
125
+ }
126
+
127
+ return { dryRun, bumpType };
128
+ }
129
+
130
+ // =============================================================================
131
+ // Git Functions
132
+ // =============================================================================
133
+
134
+ async function getLastTag(): Promise<string | null> {
135
+ try {
136
+ const result = await $`git describe --tags --abbrev=0 2>/dev/null`.text();
137
+ return result.trim() || null;
138
+ } catch {
139
+ return null;
140
+ }
141
+ }
142
+
143
+ async function tagExists(version: string): Promise<boolean> {
144
+ try {
145
+ const result = await $`git tag -l "v${version}"`.text();
146
+ return result.trim() !== "";
147
+ } catch {
148
+ return false;
149
+ }
150
+ }
151
+
152
+
153
+ async function getCommitsSinceTag(tag: string | null): Promise<string[]> {
154
+ try {
155
+ let result: string;
156
+ if (tag) {
157
+ result = await $`git log ${tag}..HEAD --pretty=format:"%H|%s|%b|||"`.text();
158
+ } else {
159
+ result = await $`git log --pretty=format:"%H|%s|%b|||"`.text();
160
+ }
161
+ return result.split("|||").filter((c) => c.trim());
162
+ } catch {
163
+ return [];
164
+ }
165
+ }
166
+
167
+ function parseCommit(rawCommit: string, config: ReleaseConfig): Commit | null {
168
+ const parts = rawCommit.trim().split("|");
169
+ if (parts.length < 2) return null;
170
+
171
+ const hash = parts[0];
172
+ const subject = parts[1];
173
+ const body = parts.slice(2).join("|").trim();
174
+
175
+ // Skip CI commits
176
+ if (config.skipCI.some((skip) => subject.includes(skip))) {
177
+ return null;
178
+ }
179
+
180
+ // Parse conventional commit format: type(scope): subject
181
+ const conventionalMatch = subject.match(/^(\w+)(?:\(([^)]+)\))?!?:\s*(.+)$/);
182
+
183
+ if (!conventionalMatch) {
184
+ // Non-conventional commit, treat as misc
185
+ return {
186
+ hash,
187
+ type: "other",
188
+ scope: null,
189
+ subject,
190
+ body,
191
+ breaking: false,
192
+ raw: rawCommit,
193
+ };
194
+ }
195
+
196
+ const [, type, scope, message] = conventionalMatch;
197
+ const breaking = subject.includes("!:") ||
198
+ body.toLowerCase().includes("breaking change") ||
199
+ body.toLowerCase().includes("breaking-change");
200
+
201
+ return {
202
+ hash,
203
+ type: type.toLowerCase(),
204
+ scope: scope || null,
205
+ subject: message.trim(),
206
+ body,
207
+ breaking,
208
+ raw: rawCommit,
209
+ };
210
+ }
211
+
212
+ // =============================================================================
213
+ // Version Functions
214
+ // =============================================================================
215
+
216
+ function bumpVersion(version: string, bumpType: BumpType): string {
217
+ const parts = version.replace(/^v/, "").split(".");
218
+ const major = parseInt(parts[0] || "0", 10);
219
+ const minor = parseInt(parts[1] || "0", 10);
220
+ const patch = parseInt(parts[2] || "0", 10);
221
+
222
+ switch (bumpType) {
223
+ case "major":
224
+ return `${major + 1}.0.0`;
225
+ case "minor":
226
+ return `${major}.${minor + 1}.0`;
227
+ case "patch":
228
+ return `${major}.${minor}.${patch + 1}`;
229
+ }
230
+ }
231
+
232
+ function determineBumpType(commits: Commit[], config: ReleaseConfig): BumpType | null {
233
+ let bump: BumpType | null = null;
234
+ const priority: Record<BumpType, number> = { patch: 1, minor: 2, major: 3 };
235
+
236
+ for (const commit of commits) {
237
+ // Breaking changes always result in major bump
238
+ if (commit.breaking) {
239
+ return "major";
240
+ }
241
+
242
+ const typeConfig = config.types[commit.type];
243
+ if (typeConfig?.bump) {
244
+ if (!bump || priority[typeConfig.bump] > priority[bump]) {
245
+ bump = typeConfig.bump;
246
+ }
247
+ }
248
+ }
249
+
250
+ return bump;
251
+ }
252
+
253
+ // =============================================================================
254
+ // Changelog Generation
255
+ // =============================================================================
256
+
257
+ function generateChangelog(
258
+ commits: Commit[],
259
+ newVersion: string,
260
+ config: ReleaseConfig
261
+ ): string {
262
+ const date = new Date().toISOString().split("T")[0];
263
+ const groupedCommits: Record<string, Commit[]> = {};
264
+
265
+ // Group commits by type
266
+ for (const commit of commits) {
267
+ const type = commit.type;
268
+ if (!groupedCommits[type]) {
269
+ groupedCommits[type] = [];
270
+ }
271
+ groupedCommits[type].push(commit);
272
+ }
273
+
274
+ let changelog = `## [${newVersion}] - ${date}\n\n`;
275
+
276
+ // Breaking changes section
277
+ const breakingChanges = commits.filter((c) => c.breaking);
278
+ if (breakingChanges.length > 0) {
279
+ changelog += `### āš ļø BREAKING CHANGES\n\n`;
280
+ for (const commit of breakingChanges) {
281
+ const scope = commit.scope ? `**${commit.scope}**: ` : "";
282
+ changelog += `- ${scope}${commit.subject} (${commit.hash.slice(0, 7)})\n`;
283
+ }
284
+ changelog += "\n";
285
+ }
286
+
287
+ // Regular changes by type
288
+ for (const [type, typeConfig] of Object.entries(config.types)) {
289
+ if (typeConfig.hidden) continue;
290
+
291
+ const typeCommits = groupedCommits[type];
292
+ if (!typeCommits || typeCommits.length === 0) continue;
293
+
294
+ changelog += `### ${typeConfig.title}\n\n`;
295
+ for (const commit of typeCommits) {
296
+ const scope = commit.scope ? `**${commit.scope}**: ` : "";
297
+ changelog += `- ${scope}${commit.subject} (${commit.hash.slice(0, 7)})\n`;
298
+ }
299
+ changelog += "\n";
300
+ }
301
+
302
+ // Other/uncategorized commits
303
+ if (groupedCommits.other && groupedCommits.other.length > 0) {
304
+ changelog += `### šŸ“ Other Changes\n\n`;
305
+ for (const commit of groupedCommits.other) {
306
+ changelog += `- ${commit.subject} (${commit.hash.slice(0, 7)})\n`;
307
+ }
308
+ changelog += "\n";
309
+ }
310
+
311
+ return changelog;
312
+ }
313
+
314
+ function generateReleaseNotes(
315
+ commits: Commit[],
316
+ newVersion: string,
317
+ packageName: string,
318
+ config: ReleaseConfig
319
+ ): string {
320
+ const changelog = generateChangelog(commits, newVersion, config);
321
+
322
+ return `# ${packageName} v${newVersion}
323
+
324
+ ${changelog}
325
+
326
+ ## Installation
327
+
328
+ \`\`\`bash
329
+ bun add ${packageName}@${newVersion}
330
+ \`\`\`
331
+
332
+ or with npm:
333
+
334
+ \`\`\`bash
335
+ npm install ${packageName}@${newVersion}
336
+ \`\`\`
337
+ `;
338
+ }
339
+
340
+ async function updateChangelogFile(
341
+ newChangelog: string,
342
+ dryRun: boolean
343
+ ): Promise<void> {
344
+ const changelogPath = join(process.cwd(), "CHANGELOG.md");
345
+ let existingChangelog = "";
346
+
347
+ if (existsSync(changelogPath)) {
348
+ existingChangelog = await Bun.file(changelogPath).text();
349
+ // Remove the header if it exists
350
+ existingChangelog = existingChangelog.replace(
351
+ /^# Changelog\n\n(?:.*\n)*?(?=## \[)/,
352
+ ""
353
+ );
354
+ }
355
+
356
+ const fullChangelog = `# Changelog
357
+
358
+ All notable changes to this project will be documented in this file.
359
+
360
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
361
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
362
+
363
+ ${newChangelog}${existingChangelog}`;
364
+
365
+ if (dryRun) {
366
+ log("Would write CHANGELOG.md:", "info");
367
+ console.log(fullChangelog.slice(0, 500) + "...");
368
+ } else {
369
+ await Bun.write(changelogPath, fullChangelog);
370
+ log("Updated CHANGELOG.md", "success");
371
+ }
372
+ }
373
+
374
+ // =============================================================================
375
+ // Package.json Functions
376
+ // =============================================================================
377
+
378
+ async function readPackageJson(): Promise<PackageJson> {
379
+ const packagePath = join(process.cwd(), "package.json");
380
+ const content = await Bun.file(packagePath).text();
381
+ return JSON.parse(content);
382
+ }
383
+
384
+ async function updatePackageJson(
385
+ newVersion: string,
386
+ dryRun: boolean
387
+ ): Promise<void> {
388
+ const packagePath = join(process.cwd(), "package.json");
389
+ const packageJson = await readPackageJson();
390
+ packageJson.version = newVersion;
391
+
392
+ if (dryRun) {
393
+ log(`Would update package.json version to ${newVersion}`, "info");
394
+ } else {
395
+ await Bun.write(packagePath, JSON.stringify(packageJson, null, 2) + "\n");
396
+ log(`Updated package.json version to ${newVersion}`, "success");
397
+ }
398
+ }
399
+
400
+ // =============================================================================
401
+ // GitHub Actions Output
402
+ // =============================================================================
403
+
404
+ async function setGitHubOutput(name: string, value: string): Promise<void> {
405
+ const outputFile = process.env.GITHUB_OUTPUT;
406
+ if (outputFile) {
407
+ const output = `${name}=${value}\n`;
408
+ // Use Node.js appendFileSync for reliable GitHub Actions output
409
+ const { appendFileSync } = await import("fs");
410
+ appendFileSync(outputFile, output);
411
+ log(`Set output: ${name}=${value}`, "info");
412
+ }
413
+ }
414
+
415
+ // =============================================================================
416
+ // Load Custom Config
417
+ // =============================================================================
418
+
419
+ async function loadConfig(): Promise<ReleaseConfig> {
420
+ const configPaths = [
421
+ join(process.cwd(), ".releaserc.json"),
422
+ join(process.cwd(), "release.config.json"),
423
+ join(process.cwd(), ".release.json"),
424
+ ];
425
+
426
+ for (const configPath of configPaths) {
427
+ if (existsSync(configPath)) {
428
+ const content = await Bun.file(configPath).text();
429
+ const customConfig = JSON.parse(content);
430
+ return { ...DEFAULT_CONFIG, ...customConfig };
431
+ }
432
+ }
433
+
434
+ return DEFAULT_CONFIG;
435
+ }
436
+
437
+ // =============================================================================
438
+ // Main Release Function
439
+ // =============================================================================
440
+
441
+ async function main(): Promise<void> {
442
+ console.log("\nšŸš€ Zenith Release Script\n");
443
+ console.log("=".repeat(50) + "\n");
444
+
445
+ const { dryRun, bumpType: forcedBumpType } = parseArgs();
446
+
447
+ if (dryRun) {
448
+ log("Running in DRY RUN mode - no changes will be made", "warn");
449
+ console.log();
450
+ }
451
+
452
+ // Load configuration
453
+ const config = await loadConfig();
454
+ log("Loaded release configuration", "success");
455
+
456
+ // Read package.json
457
+ const packageJson = await readPackageJson();
458
+ log(`Package: ${packageJson.name}`, "info");
459
+ log(`Current version: ${packageJson.version}`, "info");
460
+
461
+ // Get last tag
462
+ const lastTag = await getLastTag();
463
+ log(`Last tag: ${lastTag || "none"}`, "info");
464
+
465
+ // Get commits since last tag
466
+ const rawCommits = await getCommitsSinceTag(lastTag);
467
+ log(`Found ${rawCommits.length} commits since last release`, "info");
468
+
469
+ if (rawCommits.length === 0) {
470
+ log("No commits since last release. Nothing to do.", "warn");
471
+ await setGitHubOutput("should_release", "false");
472
+ return;
473
+ }
474
+
475
+ // Parse commits
476
+ const commits = rawCommits
477
+ .map((c) => parseCommit(c, config))
478
+ .filter((c): c is Commit => c !== null);
479
+
480
+ log(`Parsed ${commits.length} conventional commits`, "info");
481
+
482
+ // Determine bump type
483
+ const determinedBumpType = forcedBumpType || determineBumpType(commits, config);
484
+
485
+ if (!determinedBumpType) {
486
+ log("No version bump required based on commits", "warn");
487
+ await setGitHubOutput("should_release", "false");
488
+ return;
489
+ }
490
+
491
+ log(`Version bump type: ${determinedBumpType}`, "info");
492
+
493
+ // Calculate new version
494
+ const newVersion = bumpVersion(packageJson.version, determinedBumpType);
495
+ log(`New version: ${newVersion}`, "success");
496
+
497
+ // Check if this version already exists as a tag (fallback to prevent duplicates)
498
+ if (await tagExists(newVersion)) {
499
+ log(`Version v${newVersion} already exists as a tag. Skipping release.`, "warn");
500
+ await setGitHubOutput("should_release", "false");
501
+ return;
502
+ }
503
+
504
+ console.log("\n" + "-".repeat(50) + "\n");
505
+
506
+ // Generate changelog
507
+ const changelog = generateChangelog(commits, newVersion, config);
508
+ log("Generated changelog", "success");
509
+
510
+ // Generate release notes
511
+ const releaseNotes = generateReleaseNotes(
512
+ commits,
513
+ newVersion,
514
+ packageJson.name,
515
+ config
516
+ );
517
+
518
+ // Update files
519
+ await updateChangelogFile(changelog, dryRun);
520
+ await updatePackageJson(newVersion, dryRun);
521
+
522
+ // Write release notes for GitHub Action
523
+ if (!dryRun) {
524
+ await Bun.write(join(process.cwd(), "RELEASE_NOTES.md"), releaseNotes);
525
+ log("Written RELEASE_NOTES.md", "success");
526
+ }
527
+
528
+ // Set GitHub Actions outputs
529
+ await setGitHubOutput("should_release", "true");
530
+ await setGitHubOutput("new_version", newVersion);
531
+ await setGitHubOutput("bump_type", determinedBumpType);
532
+ await setGitHubOutput("package_name", packageJson.name);
533
+
534
+ console.log("\n" + "=".repeat(50));
535
+ console.log("\nāœ… Release preparation complete!\n");
536
+
537
+ if (dryRun) {
538
+ console.log("DRY RUN - Summary of what would happen:");
539
+ console.log(` • Version: ${packageJson.version} → ${newVersion}`);
540
+ console.log(` • CHANGELOG.md would be updated`);
541
+ console.log(` • Release notes would be created`);
542
+ console.log(` • Git tag: v${newVersion} would be created`);
543
+ console.log(` • GitHub release would be published`);
544
+ if (!packageJson.private) {
545
+ console.log(` • Package would be published to NPM`);
546
+ }
547
+ }
548
+ }
549
+
550
+ // Run the script
551
+ main().catch((error) => {
552
+ log(`Release failed: ${error.message}`, "error");
553
+ process.exit(1);
554
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": [
4
+ "ESNext",
5
+ "DOM"
6
+ ],
7
+ "target": "ESNext",
8
+ "module": "ESNext",
9
+ "moduleDetection": "force",
10
+ "allowJs": true,
11
+ "moduleResolution": "bundler",
12
+ "allowImportingTsExtensions": true,
13
+ "verbatimModuleSyntax": true,
14
+ "noEmit": true,
15
+ "strict": true,
16
+ "skipLibCheck": true,
17
+ "noImplicitAny": true,
18
+ "types": [
19
+ "node"
20
+ ],
21
+ "baseUrl": ".",
22
+ "paths": {
23
+ "@zenithbuild/core": [
24
+ "./content/types.ts"
25
+ ]
26
+ }
27
+ },
28
+ "include": [
29
+ "**/*.ts"
30
+ ]
31
+ }