import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const packageJsonPath = path.join(rootDir, 'package.json')

const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
const current = String(pkg.version || '')
const matched = /^(\d+)\.(\d+)\.(\d+)$/.exec(current)

if (!matched) {
  console.error(`[version-bump] invalid semver in package.json: ${current}`)
  process.exit(1)
}

const major = Number(matched[1])
const minor = Number(matched[2])
const patch = Number(matched[3]) + 1
const next = `${major}.${minor}.${patch}`

pkg.version = next
fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2) + '\n')
console.log(`[version-bump] ${current} -> ${next}`)
