Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 1x 1x 1x 9x 9x 1x 9x 9x 9x 1x 4x 4x 4x 4x 1x 9x 9x 5x 4x 1x 9x 8x 1x 9x 9x 9x 1x 9x 9x 1x 4x 4x 4x 4x 4x 1x 4x 4x 9x 21x 14x 9x 9x 3x 17x 4x 12x 4x 4x 4x 4x 4x | import { readFile } from "node:fs/promises";
import { matchesGlob, resolve } from "node:path";
import type { CoverageArtifact } from "#layers/snapshot/shared/types/CoverageArtifact.ts";
import type { MetricsArtifact } from "#layers/snapshot/shared/types/MetricsArtifact.ts";
import type { ProjectMetrics } from "#layers/snapshot/shared/types/ProjectMetrics.ts";
import type { ProjectNode } from "#layers/snapshot/shared/types/ProjectNode.ts";
import { collectInvariants } from "../lib/invariants.ts";
import { WORKSPACE_ROOT } from "../lib/paths.ts";
import { git, tryRun } from "../lib/run.ts";
const SPEC_SUFFIX = ".spec.ts";
// The `commit-msg` hook rewrites a conforming subject to carry the branch's issue number — `feat:
// [50] add thing` — so the optional group here keeps a numberless-branch commit from counting as a miss.
const CONVENTIONAL =
/^(?:build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(?:\([^)]+\))?!?: (?:\[\d+\] )?[a-z0-9]/;
const filesIn = async (root: string): Promise<string[]> => {
const stdout = await git(["ls-files", "--", root]);
return stdout.split("\n").filter(Boolean);
};
/** Latest `<project>@<version>` tag. Tags under an older scope never match the glob. */
const latestTag = async (name: string): Promise<string | null> => {
const stdout = await git(["tag", "--list", `${name}@*`, "--sort=-v:refname"]);
const newest = stdout.split("\n").find(Boolean);
return newest ?? null;
};
/** `nx.json`'s `release.projects`: roots or names that `nx release` versions, so they owe a first release. */
const releaseProjects = async (): Promise<string[]> => {
try {
const raw = await readFile(resolve(WORKSPACE_ROOT, "nx.json"), "utf8");
const projects =
(JSON.parse(raw) as { release?: { projects?: string | string[] } }).release?.projects ??
[];
return typeof projects === "string" ? [projects] : projects;
} catch {
return [];
}
};
const unreleasedCommits = async (
name: string,
root: string,
released: boolean,
): Promise<number | null> => {
const tag = await latestTag(name);
if (!tag && !released) return null;
const stdout = await git(["rev-list", "--count", tag ? `${tag}..HEAD` : "HEAD", "--", root]);
return Number.parseInt(stdout.trim(), 10);
};
const commitStats = async (
root: string,
): Promise<{ commits: number; commitsLastTwoWeeks: number }> => {
const [counted, recent] = await Promise.all([
git(["rev-list", "--count", "HEAD", "--", root]),
git(["log", "--since=2 weeks ago", "--format=%H", "--", root]),
]);
return {
commits: Number.parseInt(counted.trim(), 10),
commitsLastTwoWeeks: recent.split("\n").filter(Boolean).length,
};
};
const versionOf = async (root: string): Promise<string | null> => {
try {
const raw = await readFile(resolve(WORKSPACE_ROOT, root, "package.json"), "utf8");
return (JSON.parse(raw) as { version?: string }).version ?? null;
} catch {
return null;
}
};
const unreleasedFor = async (
name: string,
root: string,
released: boolean,
): Promise<number | null> => {
const result = await tryRun(() => unreleasedCommits(name, root, released));
return result.ok ? result.value : null;
};
/**
* `nx release` commits are all literally `chore(release): Publish`, which makes them exact release
* boundaries in the log. Counting from the newest one answers "how much is waiting to ship".
*/
const commitsSinceRelease = async (): Promise<number | null> => {
const stdout = await git(["log", "--format=%H", "--grep=^chore(release)", "-1"]);
const sha = stdout.split("\n").find(Boolean);
Iif (!sha) return null;
const counted = await git(["rev-list", "--count", `${sha}..HEAD`]);
return Number.parseInt(counted.trim(), 10);
};
export const collectMetrics = async (
projects: ProjectNode[],
coverage: CoverageArtifact,
generatedAt: string,
): Promise<MetricsArtifact> => {
const releasePatterns = await releaseProjects();
const measured = await Promise.all(
projects.map(async (project): Promise<ProjectMetrics> => {
const files = await filesIn(project.root);
const specs = files.filter((file) => file.endsWith(SPEC_SUFFIX));
const projectCoverage = coverage.projects.find((entry) => entry.name === project.name);
const history = await tryRun(() => commitStats(project.root));
return {
name: project.name,
root: project.root,
specs: specs.length,
commits: history.ok ? history.value.commits : null,
commitsLastTwoWeeks: history.ok ? history.value.commitsLastTwoWeeks : null,
coverageLinesPct: projectCoverage?.collected
? (projectCoverage.lines?.pct ?? null)
: null,
unreleasedCommits: await unreleasedFor(
project.name,
project.root,
releasePatterns.some(
(pattern) => pattern === project.name || matchesGlob(project.root, pattern),
),
),
currentVersion: await versionOf(project.root),
hasChangelog: files.some((file) => file.endsWith("CHANGELOG.md")),
};
}),
);
const subjects = (await git(["log", "--no-merges", "--format=%s", "-100"]))
.split("\n")
.filter(Boolean);
const conforming = subjects.filter((subject) => CONVENTIONAL.test(subject));
const release = await tryRun(commitsSinceRelease);
const [commit, branch] = await Promise.all([
git(["rev-parse", "--short", "HEAD"]).then((out) => out.trim()),
git(["rev-parse", "--abbrev-ref", "HEAD"]).then((out) => out.trim()),
]);
return {
generatedAt,
commit,
branch,
projects: measured,
invariantFindings: await collectInvariants(),
conventionalCommitRate:
subjects.length === 0 ? null : Math.round((conforming.length / subjects.length) * 100),
commitsSinceLastRelease: release.ok ? release.value : null,
};
};
|