|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import { fileURLToPath, pathToFileURL } from "url"; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +const PROJECT_DATA_PATH = path.join(__dirname, "../../apps/web/projectData.ts"); |
| 11 | + |
| 12 | +function incrementVersion(version) { |
| 13 | + // Handle different version formats: |
| 14 | + // "0.1.10-canery-1" -> "0.1.10-canery-2" |
| 15 | + // "0.1.10" -> "0.1.11" |
| 16 | + // "1.2.3-alpha-5" -> "1.2.3-alpha-6" |
| 17 | + |
| 18 | + // Pattern 1: ends with -number (e.g., "0.1.10-canery-1") |
| 19 | + const dashNumberPattern = /^(.+-)(\d+)$/; |
| 20 | + const dashMatch = version.match(dashNumberPattern); |
| 21 | + |
| 22 | + if (dashMatch) { |
| 23 | + const prefix = dashMatch[1]; |
| 24 | + const number = parseInt(dashMatch[2], 10); |
| 25 | + return `${prefix}${number + 1}`; |
| 26 | + } |
| 27 | + |
| 28 | + // Pattern 2: semantic version (e.g., "0.1.10" or "1.2.3") |
| 29 | + const semverPattern = /^(\d+)\.(\d+)\.(\d+)(.*)$/; |
| 30 | + const semverMatch = version.match(semverPattern); |
| 31 | + |
| 32 | + if (semverMatch) { |
| 33 | + const major = parseInt(semverMatch[1], 10); |
| 34 | + const minor = parseInt(semverMatch[2], 10); |
| 35 | + const patch = parseInt(semverMatch[3], 10); |
| 36 | + const suffix = semverMatch[4] || ""; |
| 37 | + return `${major}.${minor}.${patch + 1}${suffix}`; |
| 38 | + } |
| 39 | + |
| 40 | + // Fallback: find the last number in the string and increment it |
| 41 | + const lastNumberPattern = /^(.*?)(\d+)([^\d]*)$/; |
| 42 | + const lastNumberMatch = version.match(lastNumberPattern); |
| 43 | + |
| 44 | + if (lastNumberMatch) { |
| 45 | + const prefix = lastNumberMatch[1]; |
| 46 | + const number = parseInt(lastNumberMatch[2], 10); |
| 47 | + const suffix = lastNumberMatch[3]; |
| 48 | + return `${prefix}${number + 1}${suffix}`; |
| 49 | + } |
| 50 | + |
| 51 | + // If no number found, append .1 |
| 52 | + return `${version}.1`; |
| 53 | +} |
| 54 | + |
| 55 | +function updateProjectData() { |
| 56 | + try { |
| 57 | + // Read the current file |
| 58 | + const content = fs.readFileSync(PROJECT_DATA_PATH, "utf8"); |
| 59 | + |
| 60 | + // Extract current version using regex |
| 61 | + const versionPattern = /version:\s*["']([^"']+)["']/; |
| 62 | + const match = content.match(versionPattern); |
| 63 | + |
| 64 | + if (!match) { |
| 65 | + throw new Error("Could not find version property in projectData.ts"); |
| 66 | + } |
| 67 | + |
| 68 | + const currentVersion = match[1]; |
| 69 | + const newVersion = incrementVersion(currentVersion); |
| 70 | + |
| 71 | + // Replace the version in the content |
| 72 | + const newContent = content.replace( |
| 73 | + versionPattern, |
| 74 | + `version: "${newVersion}"`, |
| 75 | + ); |
| 76 | + |
| 77 | + // Write back to file |
| 78 | + fs.writeFileSync(PROJECT_DATA_PATH, newContent, "utf8"); |
| 79 | + |
| 80 | + console.log(`Version updated from ${currentVersion} to ${newVersion}`); |
| 81 | + |
| 82 | + // Output for GitHub Actions (using modern format) |
| 83 | + if (process.env.GITHUB_OUTPUT) { |
| 84 | + fs.appendFileSync( |
| 85 | + process.env.GITHUB_OUTPUT, |
| 86 | + `current=${currentVersion}\n`, |
| 87 | + ); |
| 88 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `new=${newVersion}\n`); |
| 89 | + } else { |
| 90 | + // Fallback for local testing |
| 91 | + console.log(`current=${currentVersion}`); |
| 92 | + console.log(`new=${newVersion}`); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + console.error("Error updating version:", error.message); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Run if this script is executed directly |
| 101 | +if (import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 102 | + updateProjectData(); |
| 103 | +} |
| 104 | + |
| 105 | +export { incrementVersion, updateProjectData }; |
0 commit comments