|
| 1 | +import { promises as fsp } from 'node:fs' |
| 2 | +import process from 'node:process' |
| 3 | +import { resolve } from 'pathe' |
| 4 | +import { execaSync } from 'execa' |
| 5 | +import { |
| 6 | + determineSemverChange, |
| 7 | + getGitDiff, |
| 8 | + loadChangelogConfig, |
| 9 | + parseCommits, |
| 10 | +} from 'changelogen' |
| 11 | + |
| 12 | +export interface Dep { |
| 13 | + name: string |
| 14 | + range: string |
| 15 | + type: string |
| 16 | +} |
| 17 | + |
| 18 | +type ThenArg<T> = T extends PromiseLike<infer U> ? U : T |
| 19 | +export type Package = ThenArg<ReturnType<typeof loadPackage>> |
| 20 | + |
| 21 | +export async function loadPackage(dir: string) { |
| 22 | + const pkgPath = resolve(dir, 'package.json') |
| 23 | + const data = JSON.parse( |
| 24 | + await fsp.readFile(pkgPath, 'utf-8').catch(() => '{}'), |
| 25 | + ) |
| 26 | + const save = () => |
| 27 | + fsp.writeFile(pkgPath, `${JSON.stringify(data, null, 2)}\n`) |
| 28 | + |
| 29 | + const updateDeps = (reviver: (dep: Dep) => Dep | void) => { |
| 30 | + for (const type of [ |
| 31 | + 'dependencies', |
| 32 | + 'devDependencies', |
| 33 | + 'optionalDependencies', |
| 34 | + 'peerDependencies', |
| 35 | + ]) { |
| 36 | + if (!data[type]) |
| 37 | + continue |
| 38 | + for (const e of Object.entries(data[type])) { |
| 39 | + const dep: Dep = { name: e[0], range: e[1] as string, type } |
| 40 | + delete data[type][dep.name] |
| 41 | + const updated = reviver(dep) || dep |
| 42 | + data[updated.type] = data[updated.type] || {} |
| 43 | + data[updated.type][updated.name] = updated.range |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + dir, |
| 50 | + data, |
| 51 | + save, |
| 52 | + updateDeps, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export async function loadWorkspace(dir: string) { |
| 57 | + const workspacePkg = await loadPackage(dir) |
| 58 | + |
| 59 | + const rename = (from: string, to: string) => { |
| 60 | + workspacePkg.data._name = workspacePkg.data.name |
| 61 | + workspacePkg.data.name = to |
| 62 | + workspacePkg.updateDeps((dep) => { |
| 63 | + if (dep.name === from && !dep.range.startsWith('npm:')) |
| 64 | + dep.range = `npm:${to}@${dep.range}` |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + const setVersion = ( |
| 69 | + newVersion: string, |
| 70 | + // eslint-disable-next-line unused-imports/no-unused-vars |
| 71 | + opts: { updateDeps?: boolean } = {}, |
| 72 | + ) => { |
| 73 | + workspacePkg.data.version = newVersion |
| 74 | + } |
| 75 | + |
| 76 | + const save = () => workspacePkg.save() |
| 77 | + |
| 78 | + return { |
| 79 | + dir, |
| 80 | + workspacePkg, |
| 81 | + save, |
| 82 | + rename, |
| 83 | + setVersion, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export async function determineBumpType() { |
| 88 | + const config = await loadChangelogConfig(process.cwd()) |
| 89 | + const commits = await getLatestCommits() |
| 90 | + |
| 91 | + const bumpType = determineSemverChange(commits, config) |
| 92 | + |
| 93 | + return bumpType === 'major' ? 'minor' : bumpType |
| 94 | +} |
| 95 | + |
| 96 | +export async function getLatestCommits() { |
| 97 | + const config = await loadChangelogConfig(process.cwd()) |
| 98 | + const latestTag = execaSync('git', [ |
| 99 | + 'describe', |
| 100 | + '--tags', |
| 101 | + '--abbrev=0', |
| 102 | + ]).stdout |
| 103 | + |
| 104 | + return parseCommits(await getGitDiff(latestTag), config) |
| 105 | +} |
0 commit comments