vite/scripts/releaseUtils.ts
2023-04-05 09:43:16 +00:00

70 lines
1.9 KiB
TypeScript

import { readdirSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import colors from 'picocolors'
import type { Options as ExecaOptions, ExecaReturnValue } from 'execa'
import { execa } from 'execa'
import fs from 'fs-extra'
export async function run(
bin: string,
args: string[],
opts: ExecaOptions<string> = {},
): Promise<ExecaReturnValue<string>> {
return execa(bin, args, { stdio: 'inherit', ...opts })
}
export async function getLatestTag(pkgName: string): Promise<string> {
const tags = (await run('git', ['tag'], { stdio: 'pipe' })).stdout
.split(/\n/)
.filter(Boolean)
const prefix = pkgName === 'vite' ? 'v' : `${pkgName}@`
return tags
.filter((tag) => tag.startsWith(prefix))
.sort()
.reverse()[0]
}
export async function logRecentCommits(pkgName: string): Promise<void> {
const tag = await getLatestTag(pkgName)
if (!tag) return
const sha = await run('git', ['rev-list', '-n', '1', tag], {
stdio: 'pipe',
}).then((res) => res.stdout.trim())
console.log(
colors.bold(
`\n${colors.blue(`i`)} Commits of ${colors.green(
pkgName,
)} since ${colors.green(tag)} ${colors.gray(`(${sha.slice(0, 5)})`)}`,
),
)
await run(
'git',
[
'--no-pager',
'log',
`${sha}..HEAD`,
'--oneline',
'--',
`packages/${pkgName}`,
],
{ stdio: 'inherit' },
)
console.log()
}
export async function updateTemplateVersions(): Promise<void> {
const viteVersion = fs.readJSONSync('packages/vite/package.json').version
if (/beta|alpha|rc/.test(viteVersion)) return
const dir = 'packages/create-vite'
const templates = readdirSync(dir).filter((dir) =>
dir.startsWith('template-'),
)
for (const template of templates) {
const pkgPath = path.join(dir, template, `package.json`)
const pkg = fs.readJSONSync(pkgPath)
pkg.devDependencies.vite = `^` + viteVersion
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
}