diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index f55ba5d0..5245343e 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -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: diff --git a/scripts/bump-version.js b/scripts/bump-version.js new file mode 100755 index 00000000..e0bef9cb --- /dev/null +++ b/scripts/bump-version.js @@ -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); +});