Skip to content

Commit

Permalink
build(version): Removed merge-release, use bump-version.js instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCoder committed Dec 17, 2024
1 parent 5f5e05b commit 9e549aa
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 4 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ jobs:
node-version: "18"
cache: pnpm
- run: pnpm install
- name: Bump Version
id: bump
run: ./scripts/bump-version.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Build
run: pnpm build
- name: Publish
uses: mrcoder/merge-release@master
- name: Publish to npm
run: npm publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

# Build the site for embed view and other demo
build-site:
Expand Down
119 changes: 119 additions & 0 deletions scripts/bump-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env node
const fs = require("fs");
const { execSync } = require("child_process");
const path = require("path");

const getCurrentVersion = () => {
try {
// Get latest version from npm
const pkg = require("../package.json");
const npmVersion = execSync(`npm view ${pkg.name} version`)
.toString()
.trim();
console.log(`Latest version on npm: ${npmVersion}`);
return npmVersion;
} catch (e) {
// Package not published yet, use package.json as fallback
const pkg = require("../package.json");
console.log("Package not found on npm, using version from package.json");
return pkg.version;
}
};

const getCommitMessages = (currentVersion) => {
try {
// Try to fetch tags first
try {
execSync("git fetch --tags");
} catch (e) {
console.log("Warning: Could not fetch tags:", e.message);
}

// Get commits since the last version tag
const messages = execSync(
`git log v${currentVersion}..HEAD --pretty=format:%s%n%b`,
).toString();
console.log("\nCommit messages since", `v${currentVersion}:`);
console.log(messages || "(no commits)");
return messages;
} catch (e) {
console.log(
"Warning: Could not get commits since last version, falling back to recent commits",
);
// Fallback to recent commits if tag not found
const messages = execSync("git log -10 --pretty=format:%s%n%b").toString();
console.log("\nRecent commit messages:");
console.log(messages || "(no commits)");
return messages;
}
};

const determineVersionBump = (messages) => {
const lines = messages.split("\n").filter(Boolean);
if (
lines.some((msg) => msg.includes("BREAKING CHANGE") || msg.includes("!:"))
) {
return "major";
}
if (lines.some((msg) => msg.toLowerCase().startsWith("feat"))) {
return "minor";
}
return "patch";
};

const getNewVersion = (currentVersion, bump) => {
const [major, minor, patch] = currentVersion.split(".").map(Number);
switch (bump) {
case "major":
return `${major + 1}.0.0`;
case "minor":
return `${major}.${minor + 1}.0`;
case "patch":
return `${major}.${minor}.${patch + 1}`;
default:
throw new Error(`Invalid version bump type: ${bump}`);
}
};

const run = async () => {
const dryRun = process.argv.includes("--dry-run");
if (dryRun) {
console.log("DRY RUN: No changes will be made\n");
}

const currentVersion = getCurrentVersion();
console.log(`Current version: ${currentVersion}`);

const messages = getCommitMessages(currentVersion);
const versionBump = determineVersionBump(messages);
console.log(`\nDetermined version bump: ${versionBump}`);

const newVersion = getNewVersion(currentVersion, versionBump);
console.log(`New version will be: ${newVersion}`);

if (!dryRun) {
// Update package.json
const pkg = require("../package.json");
pkg.version = newVersion;
fs.writeFileSync(
path.join(__dirname, "../package.json"),
JSON.stringify(pkg, null, 2) + "\n",
);

// Create git tag
execSync(`git tag v${newVersion}`);
console.log(`\nCreated git tag: v${newVersion}`);

// Set output for GitHub Actions
if (process.env.GITHUB_ACTIONS) {
fs.appendFileSync(process.env.GITHUB_OUTPUT, `version=v${newVersion}\n`);
}
} else {
console.log("\nDRY RUN: No changes were made");
}
};

run().catch((err) => {
console.error("Error:", err.message);
process.exit(1);
});

0 comments on commit 9e549aa

Please sign in to comment.