File size: 2,208 Bytes
a5101af
 
 
 
 
 
 
 
 
 
d1dc0e3
 
 
 
 
 
 
 
 
 
a5101af
 
 
 
d1dc0e3
a5101af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bf7657
a5101af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { exec as execCallback } from 'node:child_process'
import { readFile, writeFile } from 'node:fs/promises'
import { promisify } from 'node:util'
import path from 'node:path'

const exec = promisify(execCallback)
const root = path.resolve(__dirname, '..')
const execOpts = { cwd: root, maxBuffer: 10 * 1024 * 1024 }

async function main() {
  let bumpedVersion = process.argv[2]?.trim()

  if (bumpedVersion) {
    console.log(`Using provided version: ${bumpedVersion}`)
  } else {
    console.log('Calculating bumped version with git-cliff...')
    bumpedVersion = (
      await exec('bun git-cliff --unreleased --bumped-version', execOpts)
    ).stdout.trim()
  }

  if (!bumpedVersion) {
    throw new Error('git-cliff did not return a bumped version')
  }

  console.log(`Bumped version: ${bumpedVersion}`)

  const cargoTomlPath = path.join(root, 'Cargo.toml')
  const cargoToml = await readFile(cargoTomlPath, 'utf8')
  const versionPattern =
    /(\[workspace\.package\][\s\S]*?version\s*=\s*")([^"]+)(")/

  if (!versionPattern.test(cargoToml)) {
    throw new Error('Could not find [workspace.package] version in Cargo.toml')
  }

  const updatedCargoToml = cargoToml.replace(
    versionPattern,
    `$1${bumpedVersion}$3`,
  )
  await writeFile(cargoTomlPath, updatedCargoToml)
  console.log('Updated Cargo.toml version')

  await exec('cargo metadata --format-version 1', execOpts)
  console.log('Updated Cargo.lock')

  await exec('git add Cargo.toml Cargo.lock', execOpts)
  await exec(`git commit -m "chore(release): ${bumpedVersion}"`, execOpts)
  console.log('Created release commit')

  await exec(`git tag ${bumpedVersion}`, execOpts)
  console.log('Created git tag')

  await exec(`bun git-cliff -o CHANGELOG.md`, execOpts)
  console.log('Updated CHANGELOG.md')

  await exec('git add CHANGELOG.md', execOpts)
  await exec(`git commit --amend --no-edit`, execOpts)
  console.log('Amended release commit with updated CHANGELOG.md')

  await exec(`git tag -f ${bumpedVersion}`, execOpts)
  console.log('Updated git tag to include CHANGELOG.md')

  console.log(`Release commit and tag ${bumpedVersion} created.`)
}

main().catch((error) => {
  console.error(error)
  process.exit(1)
})