Skip to content

Commit c95f4c8

Browse files
committed
ci: add changelogen
1 parent db59d22 commit c95f4c8

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@types/lodash.kebabcase": "^4.1.7",
3030
"@types/node": "^18.17.18",
3131
"chalk": "^4.1.2",
32+
"changelogen": "^0.5.5",
3233
"conventional-changelog-cli": "^4.1.0",
3334
"enquirer": "^2.4.1",
3435
"execa": "^8.0.1",

pnpm-lock.yaml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/_utils.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { promises as fsp } from 'node:fs'
2+
import { resolve } from 'pathe'
3+
import { globby } from 'globby'
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+
const pkgDirs = (await globby(['packages/*'], { onlyDirectories: true })).sort()
45+
46+
const packages: Package[] = []
47+
48+
for (const pkgDir of pkgDirs) {
49+
const pkg = await loadPackage(pkgDir)
50+
if (!pkg.data.name) { continue }
51+
packages.push(pkg)
52+
}
53+
54+
const find = (name: string) => {
55+
const pkg = packages.find(pkg => pkg.data.name === name)
56+
if (!pkg) {
57+
throw new Error('Workspace package not found: ' + name)
58+
}
59+
return pkg
60+
}
61+
62+
const rename = (from: string, to: string) => {
63+
find(from).data._name = find(from).data.name
64+
find(from).data.name = to
65+
for (const pkg of packages) {
66+
pkg.updateDeps((dep) => {
67+
if (dep.name === from && !dep.range.startsWith('npm:')) {
68+
dep.range = 'npm:' + to + '@' + dep.range
69+
}
70+
})
71+
}
72+
}
73+
74+
const setVersion = (name: string, newVersion: string, opts: { updateDeps?: boolean } = {}) => {
75+
find(name).data.version = newVersion
76+
if (!opts.updateDeps) { return }
77+
78+
for (const pkg of packages) {
79+
pkg.updateDeps((dep) => {
80+
if (dep.name === name) {
81+
dep.range = newVersion
82+
}
83+
})
84+
}
85+
}
86+
87+
const save = () => Promise.all(packages.map(pkg => pkg.save()))
88+
89+
return {
90+
dir,
91+
workspacePkg,
92+
packages,
93+
save,
94+
find,
95+
rename,
96+
setVersion
97+
}
98+
}
99+
100+
export async function determineBumpType () {
101+
const config = await loadChangelogConfig(process.cwd())
102+
const commits = await getLatestCommits()
103+
104+
const bumpType = determineSemverChange(commits, config)
105+
106+
return bumpType === 'major' ? 'minor' : bumpType
107+
}
108+
109+
export async function getLatestCommits () {
110+
const config = await loadChangelogConfig(process.cwd())
111+
const latestTag = execaSync('git', ['describe', '--tags', '--abbrev=0']).stdout
112+
113+
return parseCommits(await getGitDiff(latestTag), config)
114+
}

0 commit comments

Comments
 (0)