|
| 1 | +import { promises as fsp } from 'node:fs' |
| 2 | +import { execSync } from 'node:child_process' |
| 3 | +import { resolve } from 'pathe' |
| 4 | +import { execaSync } from 'execa' |
| 5 | +import { determineSemverChange, getGitDiff, loadChangelogConfig, parseCommits } from 'changelogen' |
| 6 | + |
| 7 | +export interface Dep { |
| 8 | + name: string, |
| 9 | + range: string, |
| 10 | + type: string |
| 11 | +} |
| 12 | + |
| 13 | +type ThenArg<T> = T extends PromiseLike<infer U> ? U : T |
| 14 | +export type Package = ThenArg<ReturnType<typeof loadPackage>> |
| 15 | + |
| 16 | +export async function loadPackage (dir: string) { |
| 17 | + const pkgPath = resolve(dir, 'package.json') |
| 18 | + const data = JSON.parse(await fsp.readFile(pkgPath, 'utf-8').catch(() => '{}')) |
| 19 | + const save = () => fsp.writeFile(pkgPath, JSON.stringify(data, null, 2) + '\n') |
| 20 | + |
| 21 | + const updateDeps = (reviver: (dep: Dep) => Dep | void) => { |
| 22 | + for (const type of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) { |
| 23 | + if (!data[type]) { continue } |
| 24 | + for (const e of Object.entries(data[type])) { |
| 25 | + const dep: Dep = { name: e[0], range: e[1] as string, type } |
| 26 | + delete data[type][dep.name] |
| 27 | + const updated = reviver(dep) || dep |
| 28 | + data[updated.type] = data[updated.type] || {} |
| 29 | + data[updated.type][updated.name] = updated.range |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return { |
| 35 | + dir, |
| 36 | + data, |
| 37 | + save, |
| 38 | + updateDeps |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +export async function loadWorkspace (dir: string) { |
| 43 | + const workspacePkg = await loadPackage(dir) |
| 44 | + |
| 45 | + const packages = [await loadPackage(process.cwd())] |
| 46 | + |
| 47 | + const find = (name: string) => { |
| 48 | + const pkg = packages.find(pkg => pkg.data.name === name) |
| 49 | + if (!pkg) { |
| 50 | + throw new Error('Workspace package not found: ' + name) |
| 51 | + } |
| 52 | + return pkg |
| 53 | + } |
| 54 | + |
| 55 | + const rename = (from: string, to: string) => { |
| 56 | + find(from).data._name = find(from).data.name |
| 57 | + find(from).data.name = to |
| 58 | + for (const pkg of packages) { |
| 59 | + pkg.updateDeps((dep) => { |
| 60 | + if (dep.name === from && !dep.range.startsWith('npm:')) { |
| 61 | + dep.range = 'npm:' + to + '@' + dep.range |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const setVersion = (name: string, newVersion: string, opts: { updateDeps?: boolean } = {}) => { |
| 68 | + find(name).data.version = newVersion |
| 69 | + if (!opts.updateDeps) { return } |
| 70 | + |
| 71 | + for (const pkg of packages) { |
| 72 | + pkg.updateDeps((dep) => { |
| 73 | + if (dep.name === name) { |
| 74 | + dep.range = newVersion |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const save = () => Promise.all(packages.map(pkg => pkg.save())) |
| 81 | + |
| 82 | + return { |
| 83 | + dir, |
| 84 | + workspacePkg, |
| 85 | + packages, |
| 86 | + save, |
| 87 | + find, |
| 88 | + rename, |
| 89 | + setVersion |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export async function determineBumpType () { |
| 94 | + const config = await loadChangelogConfig(process.cwd()) |
| 95 | + const commits = await getLatestCommits() |
| 96 | + |
| 97 | + const bumpType = determineSemverChange(commits, config) |
| 98 | + |
| 99 | + return bumpType === 'major' ? 'minor' : bumpType |
| 100 | +} |
| 101 | + |
| 102 | +export async function getLatestCommits () { |
| 103 | + const config = await loadChangelogConfig(process.cwd()) |
| 104 | + const latestTag = execaSync('git', ['describe', '--tags', '--abbrev=0']).stdout |
| 105 | + |
| 106 | + return parseCommits(await getGitDiff(latestTag), config) |
| 107 | +} |
| 108 | + |
| 109 | +export async function getContributors () { |
| 110 | + const contributors = [] as Array<{ name: string, username: string }> |
| 111 | + const emails = new Set<string>() |
| 112 | + const latestTag = execSync('git describe --tags --abbrev=0').toString().trim() |
| 113 | + const rawCommits = await getGitDiff(latestTag) |
| 114 | + for (const commit of rawCommits) { |
| 115 | + if (emails.has(commit.author.email) || commit.author.name === 'renovate[bot]') { continue } |
| 116 | + const { author } = await $fetch<{ author: { login: string, email: string } }>(`https://api.github.com/repos/nuxt/image/commits/${commit.shortHash}`, { |
| 117 | + headers: { |
| 118 | + 'User-Agent': 'nuxt/image', |
| 119 | + Accept: 'application/vnd.github.v3+json', |
| 120 | + Authorization: `token ${process.env.GITHUB_TOKEN}` |
| 121 | + } |
| 122 | + }) |
| 123 | + if (!contributors.some(c => c.username === author.login)) { |
| 124 | + contributors.push({ name: commit.author.name, username: author.login }) |
| 125 | + } |
| 126 | + emails.add(author.email) |
| 127 | + } |
| 128 | + return contributors |
| 129 | +} |
0 commit comments