@jonmilner/local-ci 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,9 +17,17 @@ Add the hook, for example as `.githooks/pre-push` with `git config core.hooksPat
17
17
  exec pnpm exec local-ci pre-push "$@"
18
18
  ```
19
19
 
20
+ To lint commit messages as you write them, add `.githooks/commit-msg`:
21
+
22
+ ```sh
23
+ #!/bin/sh
24
+ exec local-ci commit-msg "$1"
25
+ ```
26
+
20
27
  ## Commands
21
28
 
22
29
  - `local-ci pre-push`: the hook. For each commit pushed to a branch (deletes and tags are skipped) that has not already passed, runs the check command. The pushed commit must be `HEAD` with a clean working tree (`git status --porcelain` empty), so the result reflects the commit; otherwise the push is refused. A failing check blocks the push. When every commit passes, it starts `local-ci post <sha>` in the background, aimed at the pushed remote, and lets the push go ahead.
30
+ - `local-ci commit-msg <file>`: lints the commit message in `<file>` with `@jonmilner/commitlint-config`, for a git `commit-msg` hook (git passes `.git/COMMIT_EDITMSG`). `local-ci commit-msg -` reads the message from stdin, for PR titles. Comment lines (starting with `#`) and everything after a `# ------------------------ >8 ------------------------` scissors line are stripped first; merge commits, `fixup!`/`squash!` and reverts are skipped. Each error or warning goes to stderr as `commit-msg: <message> [<rule>]`, followed by the offending header; it exits 1 when there are errors (warnings alone exit 0), and 2 when no file is given. It makes no network, git or GitHub calls.
23
31
  - `local-ci post <sha>`: waits up to 3 minutes for GitHub to have the commit, posts status `check` = `success` (`<command> passed on <hostname>`), then posts `pr-title` on it for an open PR that contains it (waiting up to 30s for GitHub to link them). A PR opened after the push, or a retitled one, needs `local-ci pr-title` run by hand.
24
32
  - `local-ci pr-title [<pr>]`: lints the PR title (the current branch's PR by default) with `@jonmilner/commitlint-config` and posts status `pr-title` on its head commit, `failure` with the first error when the title does not pass.
25
33
  - `local-ci status [<sha>]`: posts `check` for a commit (default `HEAD`) that already passed, or runs the check first. When the check command fails, it posts `check` = `failure` (`<command> failed on <hostname>`) and exits non-zero; a later passing run posts `success` over it. A dirty tree, a commit that is not `HEAD`, or a `gh` error posts nothing. Use it after a push that skipped hooks (`--no-verify`) or a lost background poster.
@@ -1,2 +1,2 @@
1
- // lintTitle loads this file, so the shared config and what it extends resolve from this package.
1
+ // lintTitle loads this file, so the shared config resolves from this package.
2
2
  export { default } from "@jonmilner/commitlint-config";
package/dist/cli.js CHANGED
@@ -4,6 +4,7 @@ import { closeSync, openSync, readFileSync } from "node:fs";
4
4
  import { hostname } from "node:os";
5
5
  import { basename, dirname, resolve } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
+ import { commitMsg } from "./commit-msg.js";
7
8
  import { appendLog, checkRunMessage, logLine, passedCommand, recordPass, releaseEnabled, resolveCommand, statePaths, } from "./config.js";
8
9
  import { exec, run } from "./exec.js";
9
10
  import { assertGhAuth, checkStatus, postStatus, prTitleStatus, viewPr, waitForCommit, waitForOpenPr, } from "./github.js";
@@ -17,6 +18,8 @@ const USAGE = `Usage:
17
18
  local-ci pre-push run the check for each pushed commit (git pre-push hook)
18
19
  local-ci post <sha> wait for <sha> on GitHub, then post its check and pr-title statuses
19
20
  local-ci pr-title [<pr>] lint the PR title and post the pr-title status
21
+ local-ci commit-msg <file|->
22
+ lint a commit message file (git commit-msg hook) or stdin (-)
20
23
  local-ci status [<sha>] post check for <sha> (default HEAD), running the check if needed
21
24
  local-ci release version pending changesets through a PR, publish to npm and push tags
22
25
  local-ci renovate check each open Renovate PR in your repos and post check and pr-title
@@ -293,6 +296,19 @@ async function runRenovate([flag, ...extra]) {
293
296
  async function main([command, ...args]) {
294
297
  if (command === "pre-push")
295
298
  return prePush(args[1]);
299
+ if (command === "commit-msg") {
300
+ if (!args[0] || args.length > 1) {
301
+ err(USAGE);
302
+ process.exitCode = 2;
303
+ return;
304
+ }
305
+ const { lines, ok } = await commitMsg(args[0]);
306
+ for (const line of lines)
307
+ err(line);
308
+ if (!ok)
309
+ process.exitCode = 1;
310
+ return;
311
+ }
296
312
  if (command === "release")
297
313
  return runRelease();
298
314
  if (command === "renovate")
@@ -0,0 +1,10 @@
1
+ /** Drops what git strips from a commit message: `#` comment lines, and everything after a scissors line. */
2
+ export declare function stripComments(message: string): string;
3
+ /**
4
+ * Lints the commit message in `source` (a file path, or `-` for stdin). Returns the lines for
5
+ * stderr and whether it passed; warnings alone still pass.
6
+ */
7
+ export declare function commitMsg(source: string): Promise<{
8
+ lines: string[];
9
+ ok: boolean;
10
+ }>;
@@ -0,0 +1,25 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { lintMessage } from "./title.js";
3
+ const SCISSORS = /^# -+ >8 -+$/;
4
+ /** Drops what git strips from a commit message: `#` comment lines, and everything after a scissors line. */
5
+ export function stripComments(message) {
6
+ const lines = message.split(/\r?\n/);
7
+ const end = lines.findIndex((line) => SCISSORS.test(line));
8
+ return (end === -1 ? lines : lines.slice(0, end))
9
+ .filter((line) => !line.startsWith("#"))
10
+ .join("\n")
11
+ .trim();
12
+ }
13
+ /**
14
+ * Lints the commit message in `source` (a file path, or `-` for stdin). Returns the lines for
15
+ * stderr and whether it passed; warnings alone still pass.
16
+ */
17
+ export async function commitMsg(source) {
18
+ const raw = readFileSync(source === "-" ? 0 : source, "utf8");
19
+ const message = stripComments(raw);
20
+ const { errors, warnings } = await lintMessage(message);
21
+ const lines = [...errors, ...warnings].map(({ message, rule }) => `commit-msg: ${message} [${rule}]`);
22
+ if (errors.length)
23
+ lines.push(`commit-msg: in "${message.split("\n")[0]}"`);
24
+ return { lines, ok: errors.length === 0 };
25
+ }
package/dist/title.d.ts CHANGED
@@ -1,2 +1,11 @@
1
+ export interface Problem {
2
+ message: string;
3
+ rule: string;
4
+ }
5
+ /** Lints a message with @jonmilner/commitlint-config, keeping commitlint's default ignores (merges, fixup!, reverts). */
6
+ export declare function lintMessage(message: string): Promise<{
7
+ errors: Problem[];
8
+ warnings: Problem[];
9
+ }>;
1
10
  /** Lints a PR title with @jonmilner/commitlint-config and returns its error messages. */
2
11
  export declare function lintTitle(title: string): Promise<string[]>;
package/dist/title.js CHANGED
@@ -1,12 +1,25 @@
1
1
  import { fileURLToPath } from "node:url";
2
2
  import lint from "@commitlint/lint";
3
3
  import load from "@commitlint/load";
4
- /** Lints a PR title with @jonmilner/commitlint-config and returns its error messages. */
5
- export async function lintTitle(title) {
4
+ /** Lints a message with @jonmilner/commitlint-config, keeping commitlint's default ignores (merges, fixup!, reverts). */
5
+ export async function lintMessage(message) {
6
6
  // Load this package's own config file, so it resolves the same in any repo being checked.
7
7
  const cwd = fileURLToPath(new URL("..", import.meta.url));
8
8
  const config = await load({}, { cwd, file: "commitlint.config.js" });
9
9
  const parserOpts = config.parserPreset?.parserOpts;
10
- const result = await lint(title, config.rules, parserOpts ? { parserOpts } : {});
11
- return result.errors.map((error) => `${error.message} [${error.name}]`);
10
+ const result = await lint(message, config.rules, {
11
+ ...(parserOpts ? { parserOpts } : {}),
12
+ defaultIgnores: config.defaultIgnores,
13
+ ignores: config.ignores,
14
+ });
15
+ const problem = ({ message, name }) => ({
16
+ message,
17
+ rule: name,
18
+ });
19
+ return { errors: result.errors.map(problem), warnings: result.warnings.map(problem) };
20
+ }
21
+ /** Lints a PR title with @jonmilner/commitlint-config and returns its error messages. */
22
+ export async function lintTitle(title) {
23
+ const { errors } = await lintMessage(title);
24
+ return errors.map((error) => `${error.message} [${error.rule}]`);
12
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jonmilner/local-ci",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Run a repo's check in a pre-push hook and post the result as GitHub commit statuses",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -21,10 +21,9 @@
21
21
  "access": "public"
22
22
  },
23
23
  "dependencies": {
24
- "@commitlint/config-conventional": "^20.0.0",
25
24
  "@commitlint/lint": "^21.2.4",
26
25
  "@commitlint/load": "^21.2.4",
27
- "@jonmilner/commitlint-config": "^0.1.1"
26
+ "@jonmilner/commitlint-config": "^0.1.2"
28
27
  },
29
28
  "engines": {
30
29
  "node": ">=22"