Skip to content

Commit

Permalink
workflow: update versions in docs when releasing
Browse files Browse the repository at this point in the history
  • Loading branch information
cloydlau committed Sep 16, 2023
1 parent 0d5aa1e commit 1986a64
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion scripts/release.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fs from 'node:fs'
import prompts from 'prompts'
import * as semver from 'semver'
import type { SemVer } from 'semver'
import spawn from 'cross-spawn'
import { cyan } from 'kolorist'

const docsPath = ['./README.md', './docs/README.zh-CN.md']

async function release() {
console.log(cyan('Fetching origin...'))
spawn.sync('git', ['pull'], { stdio: 'inherit' })
Expand All @@ -30,7 +33,7 @@ async function release() {
spawn.sync('npm', ['pack'], { stdio: 'inherit' })

const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
const { version: currentVersion } = pkg
const { name, version: currentVersion } = pkg

const choices = Array.from(['patch', 'minor', 'major'], title => ({
title,
Expand Down Expand Up @@ -84,6 +87,16 @@ async function release() {
return
}

if (!['patch', 'prerelease'].includes(t)) {
const parsedCurrentVersion = semver.parse(currentVersion) as SemVer
const parsedTargetVersion = semver.parse(targetVersion) as SemVer
const pattern = new RegExp(`${name}@${parsedCurrentVersion.major}.${parsedCurrentVersion.minor}`, 'g')
const replacement = `${name}@${parsedTargetVersion.major}.${parsedTargetVersion.minor}`
docsPath.forEach((docPath) => {
fs.writeFileSync(docPath, fs.readFileSync(docPath, 'utf-8').replace(pattern, replacement))
})
}

pkg.version = targetVersion
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))

Expand Down

0 comments on commit 1986a64

Please sign in to comment.