|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync, writeFileSync } from "fs"; |
| 4 | +import { execSync } from "child_process"; |
| 5 | +import { createInterface } from "readline"; |
| 6 | +import { resolve, dirname } from "path"; |
| 7 | +import { fileURLToPath } from "url"; |
| 8 | + |
| 9 | +// ANSI colors |
| 10 | +const bold = (s) => `\x1b[1m${s}\x1b[0m`; |
| 11 | +const dim = (s) => `\x1b[2m${s}\x1b[0m`; |
| 12 | +const cyan = (s) => `\x1b[36m${s}\x1b[0m`; |
| 13 | +const green = (s) => `\x1b[32m${s}\x1b[0m`; |
| 14 | +const _yellow = (s) => `\x1b[33m${s}\x1b[0m`; |
| 15 | +const magenta = (s) => `\x1b[35m${s}\x1b[0m`; |
| 16 | +const red = (s) => `\x1b[31m${s}\x1b[0m`; |
| 17 | + |
| 18 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 19 | +const pkgPath = resolve(__dirname, "../package.json"); |
| 20 | +const pkg = JSON.parse(readFileSync(pkgPath, "utf-8")); |
| 21 | +const currentVersion = pkg.version; |
| 22 | + |
| 23 | +function parseVersion(version) { |
| 24 | + const match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-beta\.(\d+))?$/); |
| 25 | + if (!match) { |
| 26 | + console.error(red(`\n Error: Cannot parse version: ${version}\n`)); |
| 27 | + process.exit(1); |
| 28 | + } |
| 29 | + return { |
| 30 | + major: parseInt(match[1]), |
| 31 | + minor: parseInt(match[2]), |
| 32 | + patch: parseInt(match[3]), |
| 33 | + beta: match[4] != null ? parseInt(match[4]) : null, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function formatVersion(v) { |
| 38 | + const base = `${v.major}.${v.minor}.${v.patch}`; |
| 39 | + return v.beta != null ? `${base}-beta.${v.beta}` : base; |
| 40 | +} |
| 41 | + |
| 42 | +const parsed = parseVersion(currentVersion); |
| 43 | + |
| 44 | +const options = []; |
| 45 | + |
| 46 | +// Current version stable: drop beta prerelease tag |
| 47 | +options.push({ |
| 48 | + label: "Current version stable", |
| 49 | + version: formatVersion({ |
| 50 | + major: parsed.major, |
| 51 | + minor: parsed.minor, |
| 52 | + patch: parsed.patch, |
| 53 | + beta: null, |
| 54 | + }), |
| 55 | +}); |
| 56 | + |
| 57 | +// Next version-beta: bump minor, start at beta.1 |
| 58 | +options.push({ |
| 59 | + label: "Next version beta", |
| 60 | + version: formatVersion({ |
| 61 | + major: parsed.major, |
| 62 | + minor: parsed.minor + 1, |
| 63 | + patch: 0, |
| 64 | + beta: 1, |
| 65 | + }), |
| 66 | +}); |
| 67 | + |
| 68 | +// Next beta: keep version, bump beta number (only if currently a beta) |
| 69 | +if (parsed.beta != null) { |
| 70 | + options.push({ |
| 71 | + label: "Next beta", |
| 72 | + version: formatVersion({ ...parsed, beta: parsed.beta + 1 }), |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +console.log(); |
| 77 | +console.log(bold(" Dyad Version Bump")); |
| 78 | +console.log(dim(" ─────────────────")); |
| 79 | +console.log(` Current version: ${cyan(`v${currentVersion}`)}`); |
| 80 | +console.log(); |
| 81 | +options.forEach((opt, i) => { |
| 82 | + const num = bold(` ${i + 1})`); |
| 83 | + const label = opt.label.padEnd(24); |
| 84 | + const ver = magenta(`v${opt.version}`); |
| 85 | + console.log(`${num} ${label} ${dim("→")} ${ver}`); |
| 86 | +}); |
| 87 | +console.log(); |
| 88 | + |
| 89 | +const rl = createInterface({ input: process.stdin, output: process.stdout }); |
| 90 | + |
| 91 | +rl.question(` ${bold("Select option:")} `, (answer) => { |
| 92 | + rl.close(); |
| 93 | + const index = parseInt(answer) - 1; |
| 94 | + if (isNaN(index) || index < 0 || index >= options.length) { |
| 95 | + console.error(red("\n Invalid selection.\n")); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + const selected = options[index]; |
| 100 | + const newVersion = selected.version; |
| 101 | + const tag = `v${newVersion}`; |
| 102 | + const branchTag = tag.replaceAll(".", "-"); |
| 103 | + const branch = `bump-to-${branchTag}`; |
| 104 | + |
| 105 | + console.log(); |
| 106 | + console.log(dim(" ─────────────────")); |
| 107 | + console.log(` Bumping to ${green(tag)}`); |
| 108 | + console.log(); |
| 109 | + |
| 110 | + // Update package.json |
| 111 | + pkg.version = newVersion; |
| 112 | + writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n"); |
| 113 | + step("Updated package.json"); |
| 114 | + |
| 115 | + // Git operations |
| 116 | + run(`git checkout -b ${branch}`); |
| 117 | + step("Created branch " + cyan(branch)); |
| 118 | + |
| 119 | + run(`git add package.json`); |
| 120 | + run(`git commit -m "Bump to ${tag}"`); |
| 121 | + step("Committed changes"); |
| 122 | + |
| 123 | + run(`git push -u origin ${branch}`); |
| 124 | + step("Pushed to remote"); |
| 125 | + |
| 126 | + const prUrl = run( |
| 127 | + `gh pr create --title "Bump to ${tag}" --body "#skip-bb"`, |
| 128 | + ).trim(); |
| 129 | + step("Created pull request"); |
| 130 | + |
| 131 | + console.log(); |
| 132 | + console.log(green(bold(` Done!`)) + ` PR created for ${green(tag)}`); |
| 133 | + console.log(` ${cyan(prUrl)}`); |
| 134 | + console.log(); |
| 135 | +}); |
| 136 | + |
| 137 | +function step(msg) { |
| 138 | + console.log(` ${green("✔")} ${msg}`); |
| 139 | +} |
| 140 | + |
| 141 | +function run(cmd) { |
| 142 | + return execSync(cmd, { |
| 143 | + stdio: "pipe", |
| 144 | + cwd: resolve(__dirname, ".."), |
| 145 | + }).toString(); |
| 146 | +} |
0 commit comments