Skip to content

Commit

Permalink
feat: add commit parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Jun 22, 2023
1 parent 9919762 commit 7f20a07
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
33 changes: 33 additions & 0 deletions packages/release/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@ijsblokje/release",
"version": "0.0.0",
"author": "ijsKoud <[email protected]>",
"license": "MIT",
"type": "module",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"build": "tsc --build",
"build:watch": "tsc --watch > /dev/null",
"lint": "TIMING=1 eslint",
"test": "vitest run"
},
"dependencies": {
"@ijsblokje/octokit": "*",
"@ijsblokje/utils": "*",
"@octokit/types": "10.0.0",
"semver": "7.5.2"
},
"devDependencies": {
"@types/node": "^18.16.18",
"@types/semver": "7.5.0",
"eslint": "^8.43.0",
"prettier": "^2.8.8",
"typescript": "5.1.3",
"vitest": "^0.32.2"
},
"engines": {
"node": ">= v18.16.0"
},
"packageManager": "[email protected]"
}
37 changes: 37 additions & 0 deletions packages/release/src/lib/Commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Endpoints } from "@octokit/types";

export class Commit {
public constructor(public data: GitHubCommit) {}

public parse(): CommitParserResult | null {
const commitRegex =
/^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|types|¯\\_\(ツ\)_\/¯)(?<scope>\(\w+\)?((?=:\s)|(?=!:\s)))?(?<breaking>!)?(?<subject>:\s.*)?|^(?<merge>Merge \w+)/gm;
const [message] = this.data.commit.message.split("\n");

const parsed = commitRegex.exec(message);
if (!parsed?.groups) return null;

const { type, scope, breaking, subject, merge } = parsed.groups;
if (!type || !subject) return null;

const commitMessage = Boolean(merge) ? merge : subject.slice(2);
return {
type: type as CommitType,
scope: scope?.slice(1, -1),
message: commitMessage.trim(),
breaking: Boolean(breaking),
merge: Boolean(merge)
};
}
}

export interface CommitParserResult {
type: CommitType;
scope: string;
message: string;
breaking: boolean;
merge: boolean;
}

export type CommitType = "build" | "chore" | "ci" | "docs" | "feat" | "fix" | "perf" | "refactor" | "revert" | "style" | "test" | "types";
export type GitHubCommit = Endpoints["GET /repos/{owner}/{repo}/compare/{base}...{head}"]["response"]["data"]["commits"][0];
4 changes: 4 additions & 0 deletions packages/release/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["./src/**/*"]
}
5 changes: 5 additions & 0 deletions packages/release/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.json",
"include": ["./src/**/*.ts"],
"compilerOptions": { "outDir": "./dist", "composite": true }
}
13 changes: 13 additions & 0 deletions packages/release/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
globals: true,
cache: {
dir: "../../node_modules/.vitest"
}
},
esbuild: {
target: "ES2020"
}
});
28 changes: 26 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,23 @@ __metadata:
languageName: unknown
linkType: soft

"@ijsblokje/release@workspace:packages/release":
version: 0.0.0-use.local
resolution: "@ijsblokje/release@workspace:packages/release"
dependencies:
"@ijsblokje/octokit": "*"
"@ijsblokje/utils": "*"
"@octokit/types": 10.0.0
"@types/node": ^18.16.18
"@types/semver": 7.5.0
eslint: ^8.43.0
prettier: ^2.8.8
semver: 7.5.2
typescript: 5.1.3
vitest: ^0.32.2
languageName: unknown
linkType: soft

"@ijsblokje/server@*, @ijsblokje/server@workspace:packages/server":
version: 0.0.0-use.local
resolution: "@ijsblokje/server@workspace:packages/server"
Expand Down Expand Up @@ -856,7 +873,7 @@ __metadata:
languageName: node
linkType: hard

"@octokit/types@npm:^10.0.0":
"@octokit/types@npm:10.0.0, @octokit/types@npm:^10.0.0":
version: 10.0.0
resolution: "@octokit/types@npm:10.0.0"
dependencies:
Expand Down Expand Up @@ -1194,6 +1211,13 @@ __metadata:
languageName: node
linkType: hard

"@types/semver@npm:7.5.0":
version: 7.5.0
resolution: "@types/semver@npm:7.5.0"
checksum: 0a64b9b9c7424d9a467658b18dd70d1d781c2d6f033096a6e05762d20ebbad23c1b69b0083b0484722aabf35640b78ccc3de26368bcae1129c87e9df028a22e2
languageName: node
linkType: hard

"@types/semver@npm:^7.3.12":
version: 7.3.13
resolution: "@types/semver@npm:7.3.13"
Expand Down Expand Up @@ -5152,7 +5176,7 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.8":
"semver@npm:7.5.2, semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.8":
version: 7.5.2
resolution: "semver@npm:7.5.2"
dependencies:
Expand Down

0 comments on commit 7f20a07

Please sign in to comment.