Skip to content

Commit

Permalink
feat(git-tools): Added call to @commitlint/lint in the commitlint
Browse files Browse the repository at this point in the history
… CLI command
  • Loading branch information
sullivanpj committed Jan 10, 2025
1 parent 6964e97 commit 7c10076
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 138 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
"@typescript-eslint/parser": "7.16.0",
"c12": "catalog:",
"chalk": "catalog:",
"conventional-changelog-conventionalcommits": "8.0.0",
"copyfiles": "catalog:",
"esbuild": "catalog:",
"esbuild-register": "3.5.0",
Expand Down
126 changes: 62 additions & 64 deletions packages/git-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "@storm-software/git-tools",
"version": "2.79.1",
"type": "module",
"description": "Tools for managing Git repositories within a Nx workspace.",
"repository": {
"type": "github",
Expand Down Expand Up @@ -46,46 +45,46 @@
"pnpm": ">=9.15.2"
},
"bin": {
"storm-git": "./bin/git/index.cjs",
"storm-git-esm": "./bin/git/index.js",
"storm-git-cjs": "./bin/git/index.cjs",
"storm-post-checkout": "./bin/post-checkout/index.cjs",
"storm-post-checkout-esm": "./bin/post-checkout/index.js",
"storm-post-checkout-cjs": "./bin/post-checkout/index.cjs",
"storm-post-commit": "./bin/post-commit/index.cjs",
"storm-post-commit-esm": "./bin/post-commit/index.js",
"storm-post-commit-cjs": "./bin/post-commit/index.cjs",
"storm-post-merge": "./bin/post-merge/index.cjs",
"storm-post-merge-esm": "./bin/post-merge/index.js",
"storm-post-merge-cjs": "./bin/post-merge/index.cjs",
"storm-pre-commit": "./bin/pre-commit/index.cjs",
"storm-pre-commit-esm": "./bin/pre-commit/index.js",
"storm-pre-commit-cjs": "./bin/pre-commit/index.cjs",
"storm-pre-install": "./bin/pre-install/index.cjs",
"storm-pre-install-esm": "./bin/pre-install/index.js",
"storm-pre-install-cjs": "./bin/pre-install/index.cjs",
"storm-pre-push": "./bin/pre-push/index.cjs",
"storm-pre-push-esm": "./bin/pre-push/index.js",
"storm-pre-push-cjs": "./bin/pre-push/index.cjs",
"storm-prepare": "./bin/prepare/index.cjs",
"storm-prepare-esm": "./bin/prepare/index.js",
"storm-prepare-cjs": "./bin/prepare/index.cjs",
"storm-version-warning": "./bin/version-warning/index.cjs",
"storm-version-warning-esm": "./bin/version-warning/index.js",
"storm-version-warning-cjs": "./bin/version-warning/index.cjs"
"storm-git": "./bin/git/index.js",
"storm-git-esm": "./bin/git/index.mjs",
"storm-git-cjs": "./bin/git/index.js",
"storm-post-checkout": "./bin/post-checkout/index.js",
"storm-post-checkout-esm": "./bin/post-checkout/index.mjs",
"storm-post-checkout-cjs": "./bin/post-checkout/index.js",
"storm-post-commit": "./bin/post-commit/index.js",
"storm-post-commit-esm": "./bin/post-commit/index.mjs",
"storm-post-commit-cjs": "./bin/post-commit/index.js",
"storm-post-merge": "./bin/post-merge/index.js",
"storm-post-merge-esm": "./bin/post-merge/index.mjs",
"storm-post-merge-cjs": "./bin/post-merge/index.js",
"storm-pre-commit": "./bin/pre-commit/index.js",
"storm-pre-commit-esm": "./bin/pre-commit/index.mjs",
"storm-pre-commit-cjs": "./bin/pre-commit/index.js",
"storm-pre-install": "./bin/pre-install/index.js",
"storm-pre-install-esm": "./bin/pre-install/index.mjs",
"storm-pre-install-cjs": "./bin/pre-install/index.js",
"storm-pre-push": "./bin/pre-push/index.js",
"storm-pre-push-esm": "./bin/pre-push/index.mjs",
"storm-pre-push-cjs": "./bin/pre-push/index.js",
"storm-prepare": "./bin/prepare/index.js",
"storm-prepare-esm": "./bin/prepare/index.mjs",
"storm-prepare-cjs": "./bin/prepare/index.js",
"storm-version-warning": "./bin/version-warning/index.js",
"storm-version-warning-esm": "./bin/version-warning/index.mjs",
"storm-version-warning-cjs": "./bin/version-warning/index.js"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"default": {
"types": "./dist/index.d.ts",
Expand All @@ -94,12 +93,12 @@
},
"./index": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"default": {
"types": "./dist/index.d.ts",
Expand All @@ -108,12 +107,12 @@
},
"./types": {
"import": {
"types": "./dist/types.d.ts",
"default": "./dist/types.js"
"types": "./dist/types.d.mts",
"default": "./dist/types.mjs"
},
"require": {
"types": "./dist/types.d.cts",
"default": "./dist/types.cjs"
"types": "./dist/types.d.ts",
"default": "./dist/types.js"
},
"default": {
"types": "./dist/types.d.ts",
Expand All @@ -122,12 +121,12 @@
},
"./commit/config": {
"import": {
"types": "./dist/commit/config.d.ts",
"default": "./dist/commit/config.js"
"types": "./dist/commit/config.d.mts",
"default": "./dist/commit/config.mjs"
},
"require": {
"types": "./dist/commit/config.d.cts",
"default": "./dist/commit/config.cjs"
"types": "./dist/commit/config.d.ts",
"default": "./dist/commit/config.js"
},
"default": {
"types": "./dist/commit/config.d.ts",
Expand All @@ -136,12 +135,12 @@
},
"./commit/*": {
"import": {
"types": "./dist/commit/*.d.ts",
"default": "./dist/commit/*.js"
"types": "./dist/commit/*.d.mts",
"default": "./dist/commit/*.mjs"
},
"require": {
"types": "./dist/commit/*.d.cts",
"default": "./dist/commit/*.cjs"
"types": "./dist/commit/*.d.ts",
"default": "./dist/commit/*.js"
},
"default": {
"types": "./dist/commit/*.d.ts",
Expand All @@ -150,12 +149,12 @@
},
"./release/config": {
"import": {
"types": "./dist/release/config.d.ts",
"default": "./dist/release/config.js"
"types": "./dist/release/config.d.mts",
"default": "./dist/release/config.mjs"
},
"require": {
"types": "./dist/release/config.d.cts",
"default": "./dist/release/config.cjs"
"types": "./dist/release/config.d.ts",
"default": "./dist/release/config.js"
},
"default": {
"types": "./dist/release/config.d.ts",
Expand All @@ -164,12 +163,12 @@
},
"./release/*": {
"import": {
"types": "./dist/release/*.d.ts",
"default": "./dist/release/*.js"
"types": "./dist/release/*.d.mts",
"default": "./dist/release/*.mjs"
},
"require": {
"types": "./dist/release/*.d.cts",
"default": "./dist/release/*.cjs"
"types": "./dist/release/*.d.ts",
"default": "./dist/release/*.js"
},
"default": {
"types": "./dist/release/*.d.ts",
Expand All @@ -178,12 +177,12 @@
},
"./changelog-renderer": {
"import": {
"types": "./dist/release/changelog-renderer.d.ts",
"default": "./dist/release/changelog-renderer.js"
"types": "./dist/release/changelog-renderer.d.mts",
"default": "./dist/release/changelog-renderer.mjs"
},
"require": {
"types": "./dist/release/changelog-renderer.d.cts",
"default": "./dist/release/changelog-renderer.cjs"
"types": "./dist/release/changelog-renderer.d.ts",
"default": "./dist/release/changelog-renderer.js"
},
"default": {
"types": "./dist/release/changelog-renderer.d.ts",
Expand Down Expand Up @@ -217,7 +216,7 @@
"tsconfig-paths": "4.2.0"
},
"devDependencies": {
"@commitlint/types": "19.0.3",
"@commitlint/lint": "^19.6.0",
"@humanfs/core": "^0.19.1",
"@humanfs/node": "^0.16.6",
"@humanwhocodes/retry": "0.4.1",
Expand Down Expand Up @@ -245,7 +244,6 @@
"chalk": "catalog:",
"chalk-template": "1.1.0",
"commander": "catalog:",
"conventional-changelog-conventionalcommits": "8.0.0",
"defu": "catalog:",
"htmlparser2": "10.0.0",
"jsonc-parser": "3.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/git-tools/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"pnpm copyfiles LICENSE dist/packages/git-tools",
"pnpm copyfiles --up=2 ./packages/git-tools/*.md ./packages/git-tools/package.json dist/packages/git-tools",
"pnpm copyfiles --up=4 ./packages/git-tools/dist/src/* dist/packages/git-tools/dist",
"pnpm copyfiles --up=5 ./packages/git-tools/dist/src/commit/config.* dist/packages/git-tools/dist/commit",
"pnpm copyfiles --up=5 ./packages/git-tools/dist/src/commit/* dist/packages/git-tools/dist/commit",
"pnpm copyfiles --up=5 ./packages/git-tools/dist/src/release/* dist/packages/git-tools/dist/release",
"pnpm copyfiles --up=5 ./packages/git-tools/src/readme/templates/* dist/packages/git-tools/dist/readme/templates",
"pnpm copyfiles --up=4 ./packages/git-tools/src/lefthook/* dist/packages/git-tools/dist/lefthook",
Expand Down
16 changes: 5 additions & 11 deletions packages/git-tools/src/commit/commit-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { joinPaths, writeInfo } from "@storm-software/config-tools";
import chalkTemplate from "chalk-template";
import defu from "defu";
import { execSync } from "node:child_process";
import { getScopeEnum, getScopeEnumRule } from "../commitlint/scope";
import { getScopeEnum } from "../commitlint/scope";
import type {
CommitConfig,
CommitQuestionAnswers,
Expand All @@ -19,8 +19,7 @@ import type {
CommitState,
CommitTypeProps,
CommitTypesEnum,
DefaultCommitQuestionKeys,
DefaultResolvedCommitRulesEnum
DefaultCommitQuestionKeys
} from "../types";
import { DEFAULT_COMMIT_CONFIG } from "./config";

Expand All @@ -44,14 +43,10 @@ export const getGitRootDir = () => {

const resolveCommitOptions = async (
config: CommitConfig
): Promise<CommitResolvedConfig<DefaultResolvedCommitRulesEnum>> => {
): Promise<CommitResolvedConfig> => {
return {
utils: { getScopeEnum },
parserPreset: "conventional-changelog-conventionalcommits",
rules: {
...config.rules,
"scope-enum": getScopeEnumRule
},
prompt: {
settings: config.settings,
messages: config.messages,
Expand All @@ -63,9 +58,8 @@ const resolveCommitOptions = async (
};
};

const resolveDefaultCommitOptions = async (): Promise<
CommitResolvedConfig<DefaultResolvedCommitRulesEnum>
> => resolveCommitOptions(DEFAULT_COMMIT_CONFIG);
const resolveDefaultCommitOptions = async (): Promise<CommitResolvedConfig> =>
resolveCommitOptions(DEFAULT_COMMIT_CONFIG);

export const createState = async (
config: StormConfig,
Expand Down
7 changes: 2 additions & 5 deletions packages/git-tools/src/commit/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import {
CommitQuestionProps,
DEFAULT_COMMIT_PROMPT_MESSAGES,
DEFAULT_COMMIT_QUESTIONS,
DEFAULT_COMMIT_RULES,
DEFAULT_COMMIT_SETTINGS,
DEFAULT_COMMIT_TYPES,
DefaultCommitQuestionKeys,
DefaultCommitRulesEnum
DefaultCommitQuestionKeys
} from "../types";

export const DEFAULT_COMMIT_CONFIG: CommitConfig<DefaultCommitRulesEnum> = {
rules: DEFAULT_COMMIT_RULES,
export const DEFAULT_COMMIT_CONFIG: CommitConfig = {
settings: DEFAULT_COMMIT_SETTINGS,
messages: DEFAULT_COMMIT_PROMPT_MESSAGES,
questions: DEFAULT_COMMIT_QUESTIONS as CommitQuestionEnum<
Expand Down
31 changes: 31 additions & 0 deletions packages/git-tools/src/commitlint/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
DEFAULT_COMMIT_TYPES,
DefaultCommitRulesEnum,
RuleConfigCondition,
RuleConfigSeverity
} from "../types";

export const DEFAULT_COMMIT_RULES: DefaultCommitRulesEnum = {
"body-leading-blank": [RuleConfigSeverity.Warning, "always"],
"body-max-length": [RuleConfigSeverity.Error, "always", 600],
"footer-leading-blank": [RuleConfigSeverity.Warning, "always"],
"footer-max-line-length": [RuleConfigSeverity.Error, "always", 150],
"header-max-length": [RuleConfigSeverity.Error, "always", 150],
"header-trim": [RuleConfigSeverity.Error, "always"],
"subject-case": [RuleConfigSeverity.Error, "always", ["sentence-case"]],
"subject-empty": [RuleConfigSeverity.Error, "never"],
"subject-full-stop": [RuleConfigSeverity.Error, "never", "."],
"subject-max-length": [RuleConfigSeverity.Error, "always", 150],
"subject-min-length": [RuleConfigSeverity.Error, "always", 3],
"type-case": [RuleConfigSeverity.Error, "always", "kebab-case"],
"type-empty": [RuleConfigSeverity.Error, "never"],
"type-enum": [
RuleConfigSeverity.Error,
"always",
Object.keys(DEFAULT_COMMIT_TYPES)
] as [RuleConfigSeverity, RuleConfigCondition, string[]],
"type-max-length": [RuleConfigSeverity.Error, "always", 20],
"type-min-length": [RuleConfigSeverity.Error, "always", 3],
"scope-case": [RuleConfigSeverity.Error, "always", ["kebab-case"]],
"scope-empty": [RuleConfigSeverity.Error, "never"]
};
23 changes: 19 additions & 4 deletions packages/git-tools/src/commitlint/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { existsSync } from "fs";
import { readFile } from "fs/promises";
import childProcess from "node:child_process";
import { DEFAULT_COMMIT_TYPES } from "../types";
import { getNxScopes } from "./scope";
import { DEFAULT_COMMIT_RULES } from "./config";
import { getNxScopes, getRuleFromScopeEnum } from "./scope";

const COMMIT_EDITMSG_PATH = ".git/COMMIT_EDITMSG";

Expand All @@ -33,7 +34,7 @@ export const runCommitLint = async (
} else {
const commitFile = joinPaths(
config.workspaceRoot,
params.file || COMMIT_EDITMSG_PATH
params.file || params.message || COMMIT_EDITMSG_PATH
);
if (existsSync(commitFile)) {
commitMessage = (await readFile(commitFile, "utf8"))?.trim();
Expand Down Expand Up @@ -88,15 +89,26 @@ export const runCommitLint = async (
}

const allowedTypes = Object.keys(DEFAULT_COMMIT_TYPES).join("|");
const allowedScopes = (await getNxScopes()).join("|");
const allowedScopes = await getNxScopes();
// eslint-disable-next-line no-useless-escape
const commitMsgRegex = `(${allowedTypes})\\((${allowedScopes})\\)!?:\\s(([a-z0-9:\-\s])+)`;

const matchCommit = new RegExp(commitMsgRegex, "g").test(commitMessage);
const matchRevert = /Revert/gi.test(commitMessage);
const matchRelease = /Release/gi.test(commitMessage);

if (+!(matchRelease || matchRevert || matchCommit) === 0) {
const lint = (await import("@commitlint/lint")).default;

const report = await lint(commitMessage, {
...DEFAULT_COMMIT_RULES,
"scope-enum": getRuleFromScopeEnum(allowedScopes)
} as any);

if (
+!(matchRelease || matchRevert || matchCommit) === 0 ||
report.errors.length ||
report.warnings.length
) {
writeSuccess(`Commit was processing completed successfully!`, config);
} else {
let errorMessage =
Expand All @@ -113,6 +125,9 @@ export const runCommitLint = async (
"\nEXAMPLE: \n" +
"feat(nx): add an option to generate lazy-loadable modules\n" +
"fix(core)!: breaking change should have exclamation mark\n";
errorMessage += `\n\nCommitLint Errors: ${report.errors.length ? report.errors.map(error => ` - ${error.message}`).join("\n") : "None"}`;
errorMessage += `\nCommitLint Warnings: ${report.warnings.length ? report.warnings.map(warning => ` - ${warning.message}`).join("\n") : "None"}`;
errorMessage += "\n\nPlease fix the commit message and try again.";

throw new Error(errorMessage);
}
Expand Down
Loading

0 comments on commit 7c10076

Please sign in to comment.