Skip to content

Commit 5737aec

Browse files
committed
ci: rewrite bump resolver
1 parent f4b8a8b commit 5737aec

15 files changed

+164
-86
lines changed
Binary file not shown.

packages/_aggregate/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
{
3232
"path": "../ci"
3333
},
34+
{
35+
"path": "../semver-bump"
36+
},
3437
{
3538
"path": "../ci-github-release"
3639
}

packages/ci/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "module",
55
"packageManager": "[email protected]",
66
"devDependencies": {
7+
"@conventional-changelog/git-client": "^1",
78
"@tsconfig/node20": "^20",
89
"@types/conventional-changelog": "^3",
910
"@types/conventional-changelog-core": "^4",
@@ -19,9 +20,9 @@
1920
},
2021
"dependencies": {
2122
"@wroud/ci-github-release": "workspace:^",
23+
"@wroud/semver-bump": "workspace:^",
2224
"conventional-changelog": "^6",
2325
"conventional-changelog-conventionalcommits": "^8",
24-
"conventional-recommended-bump": "^10",
2526
"execa": "^9",
2627
"gulp": "^5",
2728
"tempy": "^3"
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
declare module "conventional-changelog-conventionalcommits" {
2-
export type Preset = import("conventional-changelog-core").Options.Config;
2+
import type { ParserStreamOptions } from "conventional-commits-parser";
3+
import type {
4+
GetCommitsParams,
5+
Params,
6+
} from "@conventional-changelog/git-client";
7+
import type { Options } from "conventional-changelog-core";
8+
9+
export type Preset = Options.Config & {
10+
commits?: GetCommitsParams & Params;
11+
parser?: ParserStreamOptions;
12+
};
313

414
export default function createPreset(options?: any): Promise<Preset>;
515
}

packages/ci/src/gulp/RestrictEmptyCommits.ts

-48
This file was deleted.

packages/ci/src/gulp/prepublish.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ import conventionalChangelog from "conventional-changelog";
66
import createPreset, {
77
type Preset,
88
} from "conventional-changelog-conventionalcommits";
9-
import { RestrictEmptyCommits } from "./RestrictEmptyCommits.js";
109
import { pipeline } from "stream/promises";
1110
import { combineStreams } from "./combineStreams.js";
1211
import { createReadStream, createWriteStream, existsSync } from "fs";
1312
import { githubRelease } from "@wroud/ci-github-release";
14-
import { releaseCommitRegex } from "./releaseCommitRegex.js";
1513
import type { IPackageJson } from "./IPackageJson.js";
14+
import { getBump } from "@wroud/semver-bump";
15+
import type { GetCommitsParams } from "@conventional-changelog/git-client";
1616

1717
const tagPrefix = "di-v";
1818
const commitPath = ".";
1919
const changeLogFile = "CHANGELOG.md";
2020
// print output of commands into the terminal
2121
const stdio = "inherit";
22-
const commitsConfig = { path: commitPath /*, ignore: releaseCommitRegex*/ };
22+
const commitsConfig: GetCommitsParams = {
23+
path: commitPath,
24+
};
2325

2426
async function readPackageJson(): Promise<IPackageJson> {
2527
return await readFile("package.json", "utf8").then((data) =>
@@ -28,20 +30,16 @@ async function readPackageJson(): Promise<IPackageJson> {
2830
}
2931

3032
async function bumpVersion(preset: Preset): Promise<string | null> {
31-
const bumper = new RestrictEmptyCommits(process.cwd())
32-
.loadPreset(preset)
33-
.tag({
34-
prefix: tagPrefix,
35-
})
36-
.commits({ ...commitsConfig, ignore: releaseCommitRegex });
37-
38-
const recommendation = await bumper.bump();
33+
const bump = await getBump(
34+
{ prefix: tagPrefix },
35+
{ ...preset.commits, ...commitsConfig },
36+
);
3937

40-
if (!recommendation.releaseType) {
38+
if (!bump) {
4139
return null;
4240
}
4341

44-
await execa("yarn", ["version", recommendation.releaseType], {
42+
await execa("yarn", ["version", bump], {
4543
stdio,
4644
});
4745

@@ -59,7 +57,7 @@ async function changelog(preset: Preset, version: string) {
5957
tagPrefix,
6058
},
6159
undefined,
62-
commitsConfig,
60+
{ path: commitPath },
6361
);
6462

6563
const combinedStream = combineStreams(
@@ -99,7 +97,7 @@ async function publishGithubRelease(preset: Preset, packageName: string) {
9997
{ type: "oauth", token },
10098
{ config: preset, tagPrefix },
10199
{ owner, repository },
102-
commitsConfig,
100+
{ path: commitPath },
103101
);
104102
}
105103

packages/ci/src/gulp/releaseCommitRegex.ts

-1
This file was deleted.

packages/ci/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"references": [
1414
{
1515
"path": "../ci-github-release"
16+
},
17+
{
18+
"path": "../semver-bump"
1619
}
1720
]
1821
}

packages/di/tsconfig.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
"composite": true
1010
},
1111
"include": ["src"],
12-
"references": []
12+
"references": [
13+
{
14+
"path": "../ci"
15+
}
16+
]
1317
}

packages/semver-bump/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@wroud/semver-bump",
3+
"private": true,
4+
"version": "0.0.1",
5+
"type": "module",
6+
"packageManager": "[email protected]",
7+
"devDependencies": {
8+
"@tsconfig/node20": "^20",
9+
"@types/node": "^20",
10+
"@wroud/tsconfig": "workspace:^",
11+
"conventional-commits-parser": "^6",
12+
"typescript": "^5"
13+
},
14+
"exports": {
15+
".": "./lib/getBump.js"
16+
},
17+
"dependencies": {
18+
"@conventional-changelog/git-client": "^1",
19+
"conventional-commits-filter": "^5"
20+
}
21+
}

packages/semver-bump/src/Bump.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Bump = "major" | "minor" | "patch" | null;

packages/semver-bump/src/getBump.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {
2+
type GetSemverTagsParams,
3+
type GetCommitsParams,
4+
type Params,
5+
ConventionalGitClient,
6+
} from "@conventional-changelog/git-client";
7+
import type { ParserStreamOptions } from "conventional-commits-parser";
8+
import { getBumpRecommendation } from "./getBumpRecommendation.js";
9+
import type { Bump } from "./Bump.js";
10+
11+
export async function getBump(
12+
params?: GetSemverTagsParams & { cwd?: string } & Params,
13+
gitCommitsParams?: GetCommitsParams,
14+
parserOptions?: ParserStreamOptions,
15+
): Promise<Bump> {
16+
const cwd = params?.cwd || process.cwd();
17+
const gitClient = new ConventionalGitClient(cwd);
18+
const lastSemverTag = await gitClient.getLastSemverTag(params);
19+
20+
const commits = gitClient.getCommits(
21+
{
22+
format: "%B%n-hash-%n%H",
23+
from: lastSemverTag || "",
24+
filterReverts: true,
25+
...gitCommitsParams,
26+
},
27+
parserOptions,
28+
);
29+
30+
return await getBumpRecommendation(commits);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Commit } from "conventional-commits-parser";
2+
import type { Bump } from "./Bump.js";
3+
4+
export const breakingHeaderPattern = /^\w+?(?:\([^\(\)]+?\))?!:/gi;
5+
6+
export async function getBumpRecommendation(
7+
commits: AsyncIterable<Commit>,
8+
): Promise<Bump> {
9+
let bump: Bump = null;
10+
11+
for await (const commit of commits) {
12+
let type = commit["type"];
13+
const major =
14+
commit.header?.match(breakingHeaderPattern) ||
15+
commit.footer?.includes("BREAKING CHANGE") ||
16+
commit.footer?.includes("BREAKING-CHANGE");
17+
18+
if (major) {
19+
type = "BREAKING CHANGE";
20+
}
21+
22+
switch (type) {
23+
case "BREAKING CHANGE":
24+
return "major";
25+
26+
case "feat":
27+
if (bump === null || bump === "patch") {
28+
bump = "minor";
29+
}
30+
break;
31+
32+
case "fix":
33+
if (bump === null) {
34+
bump = "patch";
35+
}
36+
break;
37+
}
38+
}
39+
40+
return bump;
41+
}

packages/semver-bump/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "@wroud/tsconfig/tsconfig.json",
3+
"compilerOptions": {
4+
"tsBuildInfoFile": "./lib/.tsbuildinfo",
5+
"rootDir": "src",
6+
"rootDirs": ["src"],
7+
"outDir": "lib",
8+
"types": ["node"],
9+
"incremental": true,
10+
"composite": true
11+
},
12+
"include": ["src"],
13+
"references": []
14+
}

yarn.lock

+19-19
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ __metadata:
893893
languageName: node
894894
linkType: hard
895895

896-
"@conventional-changelog/git-client@npm:^1.0.0":
896+
"@conventional-changelog/git-client@npm:^1, @conventional-changelog/git-client@npm:^1.0.0":
897897
version: 1.0.1
898898
resolution: "@conventional-changelog/git-client@npm:1.0.1"
899899
dependencies:
@@ -3295,6 +3295,7 @@ __metadata:
32953295
version: 0.0.0-use.local
32963296
resolution: "@wroud/ci@workspace:packages/ci"
32973297
dependencies:
3298+
"@conventional-changelog/git-client": "npm:^1"
32983299
"@tsconfig/node20": "npm:^20"
32993300
"@types/conventional-changelog": "npm:^3"
33003301
"@types/conventional-changelog-core": "npm:^4"
@@ -3303,10 +3304,10 @@ __metadata:
33033304
"@types/gulp": "npm:^4"
33043305
"@types/node": "npm:^20"
33053306
"@wroud/ci-github-release": "workspace:^"
3307+
"@wroud/semver-bump": "workspace:^"
33063308
"@wroud/tsconfig": "workspace:^"
33073309
conventional-changelog: "npm:^6"
33083310
conventional-changelog-conventionalcommits: "npm:^8"
3309-
conventional-recommended-bump: "npm:^10"
33103311
execa: "npm:^9"
33113312
gulp: "npm:^5"
33123313
tempy: "npm:^3"
@@ -3399,6 +3400,20 @@ __metadata:
33993400
languageName: unknown
34003401
linkType: soft
34013402

3403+
"@wroud/semver-bump@workspace:^, @wroud/semver-bump@workspace:packages/semver-bump":
3404+
version: 0.0.0-use.local
3405+
resolution: "@wroud/semver-bump@workspace:packages/semver-bump"
3406+
dependencies:
3407+
"@conventional-changelog/git-client": "npm:^1"
3408+
"@tsconfig/node20": "npm:^20"
3409+
"@types/node": "npm:^20"
3410+
"@wroud/tsconfig": "workspace:^"
3411+
conventional-commits-filter: "npm:^5"
3412+
conventional-commits-parser: "npm:^6"
3413+
typescript: "npm:^5"
3414+
languageName: unknown
3415+
linkType: soft
3416+
34023417
"@wroud/tests-runner@workspace:^, @wroud/tests-runner@workspace:packages/tests-runner":
34033418
version: 0.0.0-use.local
34043419
resolution: "@wroud/tests-runner@workspace:packages/tests-runner"
@@ -4376,7 +4391,7 @@ __metadata:
43764391
languageName: node
43774392
linkType: hard
43784393

4379-
"conventional-commits-filter@npm:^5.0.0":
4394+
"conventional-commits-filter@npm:^5, conventional-commits-filter@npm:^5.0.0":
43804395
version: 5.0.0
43814396
resolution: "conventional-commits-filter@npm:5.0.0"
43824397
checksum: 10c0/678900d6c589bbe1739929071ea0ca89c872b9f3cc6974994726eb7a197ca04243e9ea65cae39a55e41fdc20f27fdfc43060588750d828e0efab41f309a42934
@@ -4397,7 +4412,7 @@ __metadata:
43974412
languageName: node
43984413
linkType: hard
43994414

4400-
"conventional-commits-parser@npm:^6.0.0":
4415+
"conventional-commits-parser@npm:^6, conventional-commits-parser@npm:^6.0.0":
44014416
version: 6.0.0
44024417
resolution: "conventional-commits-parser@npm:6.0.0"
44034418
dependencies:
@@ -4408,21 +4423,6 @@ __metadata:
44084423
languageName: node
44094424
linkType: hard
44104425

4411-
"conventional-recommended-bump@npm:^10":
4412-
version: 10.0.0
4413-
resolution: "conventional-recommended-bump@npm:10.0.0"
4414-
dependencies:
4415-
"@conventional-changelog/git-client": "npm:^1.0.0"
4416-
conventional-changelog-preset-loader: "npm:^5.0.0"
4417-
conventional-commits-filter: "npm:^5.0.0"
4418-
conventional-commits-parser: "npm:^6.0.0"
4419-
meow: "npm:^13.0.0"
4420-
bin:
4421-
conventional-recommended-bump: dist/cli/index.js
4422-
checksum: 10c0/f2a2486693689a431d0810b66fbbb3bad2344c5ae5bddd1680194c7edc9ff66785ab8d69f4234bc373dcde981a642dbe74df4aa944fe2dcde17854542dbfb88b
4423-
languageName: node
4424-
linkType: hard
4425-
44264426
"convert-source-map@npm:^2.0.0":
44274427
version: 2.0.0
44284428
resolution: "convert-source-map@npm:2.0.0"

0 commit comments

Comments
 (0)