diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100644 index 00000000..9ba1f4fa --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1 @@ +node scripts/check-commit-msg.mjs "$1" diff --git a/.npmrc b/.npmrc index fb2e0fa0..9a340df4 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1 @@ -message=chore(release): %s +message=chore: release %s diff --git a/scripts/check-commit-msg.mjs b/scripts/check-commit-msg.mjs new file mode 100644 index 00000000..cb9febb1 --- /dev/null +++ b/scripts/check-commit-msg.mjs @@ -0,0 +1,62 @@ +#!/usr/bin/env node +// scripts/check-commit-msg.mjs +// +// Commit-msg hook. The scope is optional; when present it must be a +// directory name under lib/, or "styles"/"widgets". Merge, revert, fixup, +// squash and amend commits are always accepted. + +import { readdirSync, readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const SCOPES = readdirSync(join(root, "lib"), { withFileTypes: true }) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + .concat(["styles", "widgets"]) + .sort(); + +const file = process.argv[2]; + +if (!file) { + console.error( + "usage: node scripts/check-commit-msg.mjs " + ); + process.exit(2); +} + +const subject = readFileSync(file, "utf8") + .replace(/\r\n/g, "\n") + .split("\n")[0] + .trim(); + +if ( + !subject || + /^(Merge |Revert "|fixup! |squash! |amend! )/.test(subject) +) { + process.exit(0); +} + +const match = subject.match(/^[a-z]+\(([^)]+)\)/); + +if (!match) { + process.exit(0); +} + +const invalid = match[1] + .split(",") + .map((scope) => scope.trim()) + .filter((scope) => !SCOPES.includes(scope)); + +if (invalid.length === 0) { + process.exit(0); +} + +console.error(`Invalid commit scope: ${invalid.join(", ")}`); +console.error(""); +console.error(`Allowed scopes: ${SCOPES.join(", ")}`); +console.error(""); +console.error( + 'Scope can be omitted; otherwise use a lib/ directory name, "styles" or "widgets".' +); +process.exit(1);