@whil/guardrails 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +208 -2
- package/catalog-assets/claude-hooks/guardrails-check.sh +37 -0
- package/catalog-assets/eslint-guardrails/config.mjs +21 -0
- package/catalog-assets/eslint-rules/_rule-tester.mjs +21 -0
- package/catalog-assets/eslint-rules/cache-policy-constant.mjs +35 -0
- package/catalog-assets/eslint-rules/client-imports-server.mjs +26 -0
- package/catalog-assets/eslint-rules/effects-in-hooks.mjs +46 -0
- package/catalog-assets/eslint-rules/env-through-config.mjs +26 -0
- package/catalog-assets/eslint-rules/hook-body-order.mjs +169 -0
- package/catalog-assets/eslint-rules/import-boundaries.mjs +97 -0
- package/catalog-assets/eslint-rules/no-comments.mjs +35 -0
- package/catalog-assets/eslint-rules/no-default-export.mjs +20 -0
- package/catalog-assets/eslint-rules/no-delete.mjs +16 -0
- package/catalog-assets/eslint-rules/no-enum.mjs +14 -0
- package/catalog-assets/eslint-rules/no-heavy-inline-styles.mjs +20 -0
- package/catalog-assets/eslint-rules/no-let.mjs +14 -0
- package/catalog-assets/eslint-rules/no-logic-in-jsx.mjs +23 -0
- package/catalog-assets/eslint-rules/no-manual-memoization.mjs +45 -0
- package/catalog-assets/eslint-rules/no-member-assignment.mjs +28 -0
- package/catalog-assets/eslint-rules/no-module-cache.mjs +40 -0
- package/catalog-assets/eslint-rules/no-mutating-array-methods.mjs +85 -0
- package/catalog-assets/eslint-rules/no-process-exit.mjs +15 -0
- package/catalog-assets/eslint-rules/no-state-mutation.mjs +62 -0
- package/catalog-assets/eslint-rules/no-unnecessary-use-client.mjs +40 -0
- package/catalog-assets/eslint-rules/pure-components.mjs +48 -0
- package/catalog-assets/eslint-rules/query-key-constant.mjs +54 -0
- package/catalog-assets/eslint-rules/require-disable-reason.mjs +29 -0
- package/catalog-assets/eslint-rules/restrict-timers.mjs +35 -0
- package/catalog-assets/eslint-rules/rules-of-hooks.mjs +51 -0
- package/catalog-assets/eslint-rules/thin-api-routes.mjs +18 -0
- package/catalog-assets/eslint-rules/thin-routes.mjs +18 -0
- package/catalog-assets/eslint-rules/virtualized-lists.mjs +32 -0
- package/catalog-assets/githooks/commit-msg +13 -0
- package/catalog-assets/githooks/pre-commit +26 -0
- package/catalog-assets/scripts/check-exact-deps.mjs +26 -0
- package/dist/cli.js +54 -0
- package/dist/core/app/analysis.js +6 -0
- package/dist/core/app/args.js +44 -0
- package/dist/core/app/catalog.js +20 -0
- package/dist/core/app/ci-plan.js +14 -0
- package/dist/core/app/claude.js +37 -0
- package/dist/core/app/commands/add-rule.js +32 -0
- package/dist/core/app/commands/agents-md.js +13 -0
- package/dist/core/app/commands/apply.js +74 -0
- package/dist/core/app/commands/audit.js +38 -0
- package/dist/core/app/commands/check.js +33 -0
- package/dist/core/app/commands/detect.js +16 -0
- package/dist/core/app/commands/init.js +124 -0
- package/dist/core/app/commands/learn.js +48 -0
- package/dist/core/app/commands/plan.js +40 -0
- package/dist/core/app/commands/pr-status.js +66 -0
- package/dist/core/app/commands/self-check.js +17 -0
- package/dist/core/app/git.js +75 -0
- package/dist/core/app/github.js +146 -0
- package/dist/core/app/hooks-script.js +27 -0
- package/dist/core/app/output.js +17 -0
- package/dist/core/app/run.js +32 -0
- package/dist/core/app/session.js +18 -0
- package/dist/core/app/timing.js +11 -0
- package/dist/core/domain/agents-md.js +92 -0
- package/dist/core/domain/audit-report.js +7 -0
- package/dist/core/domain/changes.js +8 -0
- package/dist/core/domain/check-report.js +23 -0
- package/dist/core/domain/claude-settings.js +42 -0
- package/dist/core/domain/constraint.js +7 -0
- package/dist/core/domain/diagnostics.js +3 -0
- package/dist/core/domain/errors.js +20 -0
- package/dist/core/domain/facts.js +18 -0
- package/dist/core/domain/init-report.js +28 -0
- package/dist/core/domain/json.js +43 -0
- package/dist/core/domain/learn.js +78 -0
- package/dist/core/domain/log-parse.js +99 -0
- package/dist/core/domain/pack-defaults.js +17 -0
- package/dist/core/domain/pack.js +1 -0
- package/dist/core/domain/pr-comment.js +45 -0
- package/dist/core/domain/pr-loop.js +85 -0
- package/dist/core/domain/profile.js +25 -0
- package/dist/core/domain/protection.js +43 -0
- package/dist/core/domain/ratchet.js +40 -0
- package/dist/core/domain/registry.js +32 -0
- package/dist/core/domain/remote.js +4 -0
- package/dist/core/domain/result.js +25 -0
- package/dist/core/domain/thresholds.js +9 -0
- package/dist/core/domain/types.js +1 -0
- package/dist/core/domain/workflows.js +252 -0
- package/dist/core/infra/assets.js +7 -0
- package/dist/core/infra/backup.js +31 -0
- package/dist/core/infra/baseline.js +8 -0
- package/dist/core/infra/changes.js +17 -0
- package/dist/core/infra/exec.js +14 -0
- package/dist/core/infra/facts.js +61 -0
- package/dist/core/infra/fs.js +87 -0
- package/dist/core/infra/gh.js +32 -0
- package/dist/core/infra/hooks.js +31 -0
- package/dist/core/infra/learn-sources.js +40 -0
- package/dist/core/infra/pr.js +48 -0
- package/dist/packs/index.js +6 -0
- package/dist/packs/js-ts/app/add-rule.js +116 -0
- package/dist/packs/js-ts/app/analysis.js +104 -0
- package/dist/packs/js-ts/app/bootstrap.js +16 -0
- package/dist/packs/js-ts/app/eslint-constraints.js +155 -0
- package/dist/packs/js-ts/app/eslint-files.js +56 -0
- package/dist/packs/js-ts/app/fixes.js +3 -0
- package/dist/packs/js-ts/app/pr-rules.js +30 -0
- package/dist/packs/js-ts/app/project.js +87 -0
- package/dist/packs/js-ts/app/tests.js +63 -0
- package/dist/packs/js-ts/domain/catalog-specs.js +147 -0
- package/dist/packs/js-ts/domain/ci.js +47 -0
- package/dist/packs/js-ts/domain/data.js +34 -0
- package/dist/packs/js-ts/domain/diagnostics-js.js +17 -0
- package/dist/packs/js-ts/domain/disables.js +11 -0
- package/dist/packs/js-ts/domain/eslint-output.js +46 -0
- package/dist/packs/js-ts/domain/eslint-state.js +80 -0
- package/dist/packs/js-ts/domain/hooks.js +49 -0
- package/dist/packs/js-ts/domain/pr-diagnostics.js +33 -0
- package/dist/packs/js-ts/domain/pr-tests.js +27 -0
- package/dist/packs/js-ts/domain/profile-pure.js +51 -0
- package/dist/packs/js-ts/domain/profile.js +112 -0
- package/dist/packs/js-ts/domain/report.js +74 -0
- package/dist/packs/js-ts/domain/rule-templates.js +44 -0
- package/dist/packs/js-ts/domain/signals.js +14 -0
- package/dist/packs/js-ts/domain/testing.js +33 -0
- package/dist/packs/js-ts/domain/ts-options.js +13 -0
- package/dist/packs/js-ts/index.js +50 -0
- package/dist/packs/js-ts/infra/eslint.js +50 -0
- package/dist/packs/js-ts/infra/scan.js +25 -0
- package/dist/packs/js-ts/infra/tsc.js +21 -0
- package/dist/packs/nextjs/domain/data.js +9 -0
- package/dist/packs/nextjs/domain/profile.js +21 -0
- package/dist/packs/nextjs/domain/specs.js +33 -0
- package/dist/packs/nextjs/index.js +23 -0
- package/dist/packs/node/domain/specs.js +23 -0
- package/dist/packs/node/index.js +26 -0
- package/dist/packs/react/domain/data.js +9 -0
- package/dist/packs/react/domain/specs.js +63 -0
- package/dist/packs/react/index.js +30 -0
- package/dist/packs/react-native/domain/specs.js +29 -0
- package/dist/packs/react-native/domain/testing.js +20 -0
- package/dist/packs/react-native/index.js +30 -0
- package/package.json +60 -4
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { CORE_PROBE, mergeProbes } from "../domain/facts.js";
|
|
3
|
+
import { exists, listDir, readJsonRecord, readText } from "./fs.js";
|
|
4
|
+
import { runSync } from "./exec.js";
|
|
5
|
+
function git(root, args) {
|
|
6
|
+
const result = runSync("git", args, root);
|
|
7
|
+
return result.status === 0 ? result.stdout.trim() : null;
|
|
8
|
+
}
|
|
9
|
+
const DEFAULT_BRANCHES = ["main", "master"];
|
|
10
|
+
function branchExists(root, ref) {
|
|
11
|
+
return git(root, ["rev-parse", "--verify", "--quiet", ref]) !== null;
|
|
12
|
+
}
|
|
13
|
+
function baseBranchOf(root) {
|
|
14
|
+
const remote = DEFAULT_BRANCHES.find((name) => branchExists(root, `refs/remotes/origin/${name}`));
|
|
15
|
+
if (remote !== undefined)
|
|
16
|
+
return remote;
|
|
17
|
+
const head = git(root, ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"]);
|
|
18
|
+
if (head !== null && head.startsWith("origin/"))
|
|
19
|
+
return head.slice("origin/".length);
|
|
20
|
+
return DEFAULT_BRANCHES.find((name) => branchExists(root, `refs/heads/${name}`)) ?? "main";
|
|
21
|
+
}
|
|
22
|
+
export function readGitFacts(root) {
|
|
23
|
+
if (git(root, ["rev-parse", "--git-dir"]) === null)
|
|
24
|
+
return { isRepo: false, hooksPath: null, baseBranch: "main", originUrl: null };
|
|
25
|
+
const hooks = git(root, ["config", "--get", "core.hooksPath"]);
|
|
26
|
+
return { isRepo: true, hooksPath: hooks === null || hooks === "" ? null : hooks, baseBranch: baseBranchOf(root), originUrl: git(root, ["remote", "get-url", "origin"]) };
|
|
27
|
+
}
|
|
28
|
+
const SKIPPED_DIRS = ["node_modules", ".git", "dist", "build", ".next"];
|
|
29
|
+
function dirsUnder(root, rel, depth) {
|
|
30
|
+
if (depth === 0)
|
|
31
|
+
return [];
|
|
32
|
+
return listDir(root, rel)
|
|
33
|
+
.filter((name) => !SKIPPED_DIRS.includes(name) && !name.includes("."))
|
|
34
|
+
.map((name) => `${rel}/${name}`)
|
|
35
|
+
.flatMap((child) => [child, ...dirsUnder(root, child, depth - 1)]);
|
|
36
|
+
}
|
|
37
|
+
function jsonEntries(root, paths) {
|
|
38
|
+
return Object.fromEntries(paths.flatMap((path) => {
|
|
39
|
+
const value = readJsonRecord(root, path);
|
|
40
|
+
return value === null ? [] : [[path, value]];
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
function textEntries(root, paths) {
|
|
44
|
+
return Object.fromEntries(paths.flatMap((path) => {
|
|
45
|
+
const value = readText(root, path);
|
|
46
|
+
return value === null ? [] : [[path, value]];
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
export function readFacts(root, packProbes) {
|
|
50
|
+
const probe = mergeProbes([CORE_PROBE, ...packProbes]);
|
|
51
|
+
return {
|
|
52
|
+
rootName: basename(root),
|
|
53
|
+
topLevel: listDir(root, "."),
|
|
54
|
+
present: probe.paths.filter((path) => exists(root, path)),
|
|
55
|
+
json: jsonEntries(root, probe.json),
|
|
56
|
+
text: textEntries(root, probe.text),
|
|
57
|
+
tree: probe.tree.flatMap((entry) => exists(root, entry.root) ? [entry.root, ...dirsUnder(root, entry.root, entry.depth)] : []),
|
|
58
|
+
workflows: listDir(root, ".github/workflows").filter((file) => /\.ya?ml$/.test(file)),
|
|
59
|
+
git: readGitFacts(root),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { appError } from "../domain/errors.js";
|
|
4
|
+
import { detectIndent, isRecord, parseJsoncRecord, parseStrictJson, stringifyWithIndent } from "../domain/json.js";
|
|
5
|
+
import { err, ok } from "../domain/result.js";
|
|
6
|
+
function createReadCache() {
|
|
7
|
+
const store = new Map();
|
|
8
|
+
return { has: (key) => store.has(key), get: (key) => store.get(key), set: (key, value) => void store.set(key, value) };
|
|
9
|
+
}
|
|
10
|
+
const reads = createReadCache();
|
|
11
|
+
function keyOf(root, rel) {
|
|
12
|
+
return join(root, rel);
|
|
13
|
+
}
|
|
14
|
+
export function exists(root, rel) {
|
|
15
|
+
return existsSync(keyOf(root, rel));
|
|
16
|
+
}
|
|
17
|
+
export function readText(root, rel) {
|
|
18
|
+
const key = keyOf(root, rel);
|
|
19
|
+
if (reads.has(key))
|
|
20
|
+
return reads.get(key) ?? null;
|
|
21
|
+
const text = existsSync(key) ? readFileSync(key, "utf8") : null;
|
|
22
|
+
reads.set(key, text);
|
|
23
|
+
return text;
|
|
24
|
+
}
|
|
25
|
+
export function readJsonRecord(root, rel) {
|
|
26
|
+
const text = readText(root, rel);
|
|
27
|
+
return text === null ? null : parseJsoncRecord(text);
|
|
28
|
+
}
|
|
29
|
+
export function listDir(root, rel) {
|
|
30
|
+
const path = keyOf(root, rel);
|
|
31
|
+
return existsSync(path) ? readdirSync(path).toSorted() : [];
|
|
32
|
+
}
|
|
33
|
+
function write(root, rel, content, mode) {
|
|
34
|
+
const path = keyOf(root, rel);
|
|
35
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
36
|
+
writeFileSync(path, content);
|
|
37
|
+
if (mode !== undefined)
|
|
38
|
+
chmodSync(path, mode);
|
|
39
|
+
reads.set(path, content);
|
|
40
|
+
}
|
|
41
|
+
export function removeFile(root, rel) {
|
|
42
|
+
const path = keyOf(root, rel);
|
|
43
|
+
unlinkSync(path);
|
|
44
|
+
reads.set(path, null);
|
|
45
|
+
}
|
|
46
|
+
export function writeTemp(root, rel, content) {
|
|
47
|
+
if (exists(root, rel))
|
|
48
|
+
return false;
|
|
49
|
+
write(root, rel, content, undefined);
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
export function writeManaged(root, rel, content, mode) {
|
|
53
|
+
const current = readText(root, rel);
|
|
54
|
+
if (current === content)
|
|
55
|
+
return { path: rel, action: "unchanged" };
|
|
56
|
+
write(root, rel, content, mode);
|
|
57
|
+
return { path: rel, action: current === null ? "created" : "updated" };
|
|
58
|
+
}
|
|
59
|
+
export function writeIfAbsent(root, rel, content, rule, mode) {
|
|
60
|
+
const current = readText(root, rel);
|
|
61
|
+
if (current === content)
|
|
62
|
+
return ok({ path: rel, action: "unchanged" });
|
|
63
|
+
if (current !== null) {
|
|
64
|
+
return err(appError("conflict", `${rel} already exists with different content; guardrails does not overwrite files it does not own`, {
|
|
65
|
+
file: rel,
|
|
66
|
+
rule,
|
|
67
|
+
fix: `Merge the guardrails version into ${rel} by hand, then run the command again. Nothing was changed.`,
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
write(root, rel, content, mode);
|
|
71
|
+
return ok({ path: rel, action: "created" });
|
|
72
|
+
}
|
|
73
|
+
export function editJsonFile(root, rel, mutate, create) {
|
|
74
|
+
const raw = readText(root, rel);
|
|
75
|
+
if (raw === null && !create)
|
|
76
|
+
return err(appError("unsupported-config", `${rel} does not exist`, { file: rel, fix: `Create ${rel} first.` }));
|
|
77
|
+
const parsed = raw === null ? ok({}) : parseStrictJson(raw);
|
|
78
|
+
if (!parsed.ok) {
|
|
79
|
+
return err(appError("unsupported-config", `${rel} is not strict JSON (comments or trailing commas); guardrails will not rewrite it`, {
|
|
80
|
+
file: rel,
|
|
81
|
+
fix: `Remove comments and trailing commas from ${rel} (or make the change by hand), then run the command again.`,
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
if (!isRecord(parsed.value))
|
|
85
|
+
return err(appError("unsupported-config", `${rel} is not a JSON object`, { file: rel, fix: `Make ${rel} a JSON object.` }));
|
|
86
|
+
return ok(writeManaged(root, rel, stringifyWithIndent(mutate(parsed.value), raw === null ? " " : detectIndent(raw))));
|
|
87
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { evaluateClassic, evaluateRuleset, protectionUnknown } from "../domain/protection.js";
|
|
2
|
+
import { isRecord, parseStrictJson } from "../domain/json.js";
|
|
3
|
+
import { runSync } from "./exec.js";
|
|
4
|
+
function ghJson(root, path) {
|
|
5
|
+
const result = runSync("gh", ["api", path], root);
|
|
6
|
+
const parsed = result.status === 0 ? parseStrictJson(result.stdout) : null;
|
|
7
|
+
return parsed !== null && parsed.ok ? parsed.value : null;
|
|
8
|
+
}
|
|
9
|
+
function rulesetProtections(root, slug, base, list) {
|
|
10
|
+
return list.flatMap((item) => {
|
|
11
|
+
const id = isRecord(item) ? item["id"] : null;
|
|
12
|
+
const detail = typeof id === "number" ? ghJson(root, `${slug}/rulesets/${id}`) : null;
|
|
13
|
+
return isRecord(detail) ? [evaluateRuleset(detail, base)] : [];
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export function checkProtection(root, remote, base) {
|
|
17
|
+
if (remote === null)
|
|
18
|
+
return protectionUnknown("no GitHub remote named origin");
|
|
19
|
+
if (runSync("gh", ["--version"], root).status !== 0)
|
|
20
|
+
return protectionUnknown("gh CLI not available");
|
|
21
|
+
const slug = `repos/${remote.owner}/${remote.repo}`;
|
|
22
|
+
const list = ghJson(root, `${slug}/rulesets`);
|
|
23
|
+
const active = Array.isArray(list) ? rulesetProtections(root, slug, base, list).find((item) => item.status === "active") : undefined;
|
|
24
|
+
if (active !== undefined)
|
|
25
|
+
return active;
|
|
26
|
+
const classic = ghJson(root, `${slug}/branches/${base}/protection`);
|
|
27
|
+
if (isRecord(classic))
|
|
28
|
+
return evaluateClassic(classic);
|
|
29
|
+
if (Array.isArray(list))
|
|
30
|
+
return { status: "missing", source: null, missing: ["no active ruleset or branch protection on the base branch"], reason: null };
|
|
31
|
+
return protectionUnknown("gh api failed (not authenticated or no access)");
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { runSync } from "./exec.js";
|
|
5
|
+
export function runCommitMsgHook(root, hookPath, subject) {
|
|
6
|
+
const file = join(mkdtempSync(join(tmpdir(), "guardrails-")), "msg");
|
|
7
|
+
writeFileSync(file, `${subject}\n`);
|
|
8
|
+
return runSync("sh", [join(root, hookPath), file], root).status;
|
|
9
|
+
}
|
|
10
|
+
export function shellSyntax(root, rel) {
|
|
11
|
+
const result = runSync("sh", ["-n", join(root, rel)], root);
|
|
12
|
+
return { ok: result.status === 0, detail: result.status === 0 ? "sh -n passed" : result.stderr.trim() };
|
|
13
|
+
}
|
|
14
|
+
export function gitConfigGet(root, key) {
|
|
15
|
+
return runSync("git", ["config", "--get", key], root).stdout.trim();
|
|
16
|
+
}
|
|
17
|
+
export function gitConfigSet(root, key, value) {
|
|
18
|
+
const result = runSync("git", ["config", key, value], root);
|
|
19
|
+
return { ok: result.status === 0, detail: result.stderr.trim() };
|
|
20
|
+
}
|
|
21
|
+
export function runNodeScript(root, rel) {
|
|
22
|
+
return runSync("node", [join(root, rel)], root).status;
|
|
23
|
+
}
|
|
24
|
+
export function runShellScript(root, rel) {
|
|
25
|
+
const result = runSync("sh", [join(root, rel)], root);
|
|
26
|
+
return { ok: result.status === 0, detail: result.stderr.trim() };
|
|
27
|
+
}
|
|
28
|
+
export function runNodeTest(root, rel) {
|
|
29
|
+
const result = runSync("node", ["--test", rel], root);
|
|
30
|
+
return { ok: result.status === 0, output: `${result.stdout}${result.stderr}`.trim().split("\n").slice(-25).join("\n") };
|
|
31
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { HISTORY_PATH, appendHistory, parseHistory, serializeHistory } from "../domain/learn.js";
|
|
2
|
+
import { runSync } from "./exec.js";
|
|
3
|
+
import { readText, writeManaged } from "./fs.js";
|
|
4
|
+
const FIX_COMMITS = 200;
|
|
5
|
+
const REVIEW_PRS = 30;
|
|
6
|
+
const CI_RUNS = 10;
|
|
7
|
+
const LOG_CAP = 400;
|
|
8
|
+
export function readHistory(root) {
|
|
9
|
+
return parseHistory(readText(root, HISTORY_PATH));
|
|
10
|
+
}
|
|
11
|
+
export function recordHistory(root, record) {
|
|
12
|
+
writeManaged(root, HISTORY_PATH, serializeHistory(appendHistory(readHistory(root), record)));
|
|
13
|
+
}
|
|
14
|
+
export function fixCommitSubjects(root) {
|
|
15
|
+
const result = runSync("git", ["log", `-${String(FIX_COMMITS)}`, "--pretty=%s%n%b", "-i", "--grep=^fix", "--grep=^revert"], root);
|
|
16
|
+
return result.status === 0 ? result.stdout.split("\n").filter((line) => line.trim() !== "") : [];
|
|
17
|
+
}
|
|
18
|
+
function ghJson(root, args) {
|
|
19
|
+
const result = runSync("gh", args, root);
|
|
20
|
+
return result.status === 0 ? result.stdout : null;
|
|
21
|
+
}
|
|
22
|
+
export function reviewComments(root, repo) {
|
|
23
|
+
const list = ghJson(root, ["pr", "list", "--state", "all", "--limit", String(REVIEW_PRS), "--json", "number"]);
|
|
24
|
+
if (list === null)
|
|
25
|
+
return { texts: [], available: false };
|
|
26
|
+
const numbers = [...list.matchAll(/"number":\s*(\d+)/g)].map((match) => match[1] ?? "");
|
|
27
|
+
const bodies = numbers.flatMap((number) => {
|
|
28
|
+
const out = ghJson(root, ["api", `repos/${repo}/pulls/${number}/comments`, "--jq", ".[].body"]);
|
|
29
|
+
return out === null ? [] : out.split("\n").filter((line) => line.trim() !== "");
|
|
30
|
+
});
|
|
31
|
+
return { texts: bodies, available: true };
|
|
32
|
+
}
|
|
33
|
+
export function ciFailureLines(root) {
|
|
34
|
+
const list = ghJson(root, ["run", "list", "--status", "failure", "--limit", String(CI_RUNS), "--json", "databaseId"]);
|
|
35
|
+
if (list === null)
|
|
36
|
+
return { texts: [], available: false };
|
|
37
|
+
const ids = [...list.matchAll(/"databaseId":\s*(\d+)/g)].map((match) => match[1] ?? "");
|
|
38
|
+
const lines = ids.flatMap((id) => (ghJson(root, ["run", "view", id, "--log-failed"]) ?? "").split("\n").slice(0, LOG_CAP));
|
|
39
|
+
return { texts: lines, available: true };
|
|
40
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { relative, resolve } from "node:path";
|
|
2
|
+
import { appError } from "../domain/errors.js";
|
|
3
|
+
import { parseStrictJson, stringifyJson } from "../domain/json.js";
|
|
4
|
+
import { EMPTY_STATE, MARKER, parseState, parsePr } from "../domain/pr-loop.js";
|
|
5
|
+
import { err, ok } from "../domain/result.js";
|
|
6
|
+
import { runAsync, runSync } from "./exec.js";
|
|
7
|
+
import { readText, writeManaged } from "./fs.js";
|
|
8
|
+
const LOG_LIMIT = 400000;
|
|
9
|
+
const FIELDS = "number,url,headRefOid,headRefName,baseRefName,statusCheckRollup";
|
|
10
|
+
const ghMissing = (detail) => appError("missing-dependency", `gh could not read the pull request: ${detail}`, { fix: "Run from the PR branch with `gh auth status` green, or pass --pr <number>." });
|
|
11
|
+
export function readPr(root, number) {
|
|
12
|
+
const result = runSync("gh", ["pr", "view", ...(number === undefined ? [] : [number]), "--json", FIELDS], root);
|
|
13
|
+
if (result.status !== 0)
|
|
14
|
+
return err(ghMissing(result.stderr.trim() || "no pull request for this branch"));
|
|
15
|
+
const parsed = parseStrictJson(result.stdout);
|
|
16
|
+
const pr = parsed.ok ? parsePr(parsed.value) : null;
|
|
17
|
+
return pr === null ? err(ghMissing("unexpected gh output")) : ok(pr);
|
|
18
|
+
}
|
|
19
|
+
export function jobLog(root, jobId) {
|
|
20
|
+
const result = runSync("gh", ["api", "--allow-escape-sequences", `repos/{owner}/{repo}/actions/jobs/${jobId}/logs`], root);
|
|
21
|
+
return result.status === 0 ? result.stdout.slice(-LOG_LIMIT) : `could not download the log of job ${jobId}: ${result.stderr.trim()}`;
|
|
22
|
+
}
|
|
23
|
+
export async function waitForChecks(root, number) {
|
|
24
|
+
await runAsync("gh", ["pr", "checks", String(number), "--watch"], root);
|
|
25
|
+
}
|
|
26
|
+
function commentId(root, number) {
|
|
27
|
+
const query = `.[] | select(.body | contains("${MARKER}")) | .id`;
|
|
28
|
+
const result = runSync("gh", ["api", `repos/{owner}/{repo}/issues/${String(number)}/comments`, "--paginate", "--jq", query], root);
|
|
29
|
+
return result.status === 0 ? (result.stdout.split("\n").find((line) => line.trim() !== "")?.trim() ?? null) : null;
|
|
30
|
+
}
|
|
31
|
+
export function upsertComment(root, number, body) {
|
|
32
|
+
const id = commentId(root, number);
|
|
33
|
+
const target = id === null ? `repos/{owner}/{repo}/issues/${String(number)}/comments` : `repos/{owner}/{repo}/issues/comments/${id}`;
|
|
34
|
+
return runSync("gh", ["api", "--method", id === null ? "POST" : "PATCH", target, "--input", "-"], root, JSON.stringify({ body })).status === 0;
|
|
35
|
+
}
|
|
36
|
+
function statePath(root) {
|
|
37
|
+
const result = runSync("git", ["rev-parse", "--git-path", "guardrails-pr-loop.json"], root);
|
|
38
|
+
return result.status === 0 ? relative(root, resolve(root, result.stdout.trim())) : null;
|
|
39
|
+
}
|
|
40
|
+
export function readLoopState(root) {
|
|
41
|
+
const path = statePath(root);
|
|
42
|
+
return path === null ? EMPTY_STATE : parseState(readText(root, path));
|
|
43
|
+
}
|
|
44
|
+
export function writeLoopState(root, state) {
|
|
45
|
+
const path = statePath(root);
|
|
46
|
+
if (path !== null)
|
|
47
|
+
writeManaged(root, path, stringifyJson(state));
|
|
48
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { jsTsPack } from "./js-ts/index.js";
|
|
2
|
+
import { nextjsPack } from "./nextjs/index.js";
|
|
3
|
+
import { nodePack } from "./node/index.js";
|
|
4
|
+
import { reactPack } from "./react/index.js";
|
|
5
|
+
import { reactNativePack } from "./react-native/index.js";
|
|
6
|
+
export const PACKS = Object.freeze([jsTsPack, nodePack, reactPack, reactNativePack, nextjsPack]);
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { OWN_PREFIX } from "../domain/catalog-specs.js";
|
|
2
|
+
import { capped } from "../../../core/domain/diagnostics.js";
|
|
3
|
+
import { eslintDiagnostic } from "../domain/diagnostics-js.js";
|
|
4
|
+
import { violationsOf } from "../domain/eslint-output.js";
|
|
5
|
+
import { PLUGIN_DIR, withLevel, withRule } from "../domain/eslint-state.js";
|
|
6
|
+
import { appError, EXIT } from "../../../core/domain/errors.js";
|
|
7
|
+
import { mergeChanges } from "../../../core/domain/changes.js";
|
|
8
|
+
import { UNIMPLEMENTED_MARKER, renderRule, renderRuleTest } from "../domain/rule-templates.js";
|
|
9
|
+
import { andThen, err, ok } from "../../../core/domain/result.js";
|
|
10
|
+
import { readAsset } from "../../../core/infra/assets.js";
|
|
11
|
+
import { hasEslint, lintRepo } from "../infra/eslint.js";
|
|
12
|
+
import { exists, readText, writeManaged } from "../../../core/infra/fs.js";
|
|
13
|
+
import { runNodeTest } from "../../../core/infra/hooks.js";
|
|
14
|
+
import { jsTs } from "../domain/data.js";
|
|
15
|
+
import { fixesFor } from "./fixes.js";
|
|
16
|
+
import { readState, wireEslintConfig, writeIndexAndConfig, writeState } from "./eslint-files.js";
|
|
17
|
+
const VIOLATION_LIMIT = 100;
|
|
18
|
+
function requireEslint(root) {
|
|
19
|
+
return hasEslint(root) ? null : appError("missing-dependency", "eslint is not installed in node_modules", { file: "package.json", fix: "Install eslint and typescript-eslint as exact devDependencies, then run add-rule again." });
|
|
20
|
+
}
|
|
21
|
+
function readSpec(request) {
|
|
22
|
+
const { name } = request;
|
|
23
|
+
const message = request.message === "" ? request.from : request.message;
|
|
24
|
+
if (message === undefined || message === "") {
|
|
25
|
+
return err(appError("usage", "add-rule needs --message <how to fix> (or --from <the correction you were given>)", {
|
|
26
|
+
rule: name,
|
|
27
|
+
fix: 'Example: guardrails add-rule no-fetch-in-components --selector "CallExpression[callee.name=\'fetch\']" --message "Call the API through the service layer" --bad "fetch(url);" --good "getUser(id);"',
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
const orNull = (value) => (value === "" ? null : value);
|
|
31
|
+
return ok({ name, message, selector: orNull(request.selector), bad: orNull(request.bad), good: orNull(request.good) });
|
|
32
|
+
}
|
|
33
|
+
function finish(ctx, name, changes, level, extra) {
|
|
34
|
+
const key = `${OWN_PREFIX}${name}`;
|
|
35
|
+
const { root, profile } = ctx;
|
|
36
|
+
return andThen(ok(profile), () => {
|
|
37
|
+
const lint = lintRepo(root, jsTs(profile));
|
|
38
|
+
if (!lint.ran)
|
|
39
|
+
return err(appError("verify-failed", `eslint could not run: ${lint.error ?? "unknown error"}`, { file: jsTs(profile).eslint.config ?? "eslint.config.mjs", rule: key, fix: "Fix the ESLint error above, then run add-rule again." }));
|
|
40
|
+
return andThen(readState(root), (state) => {
|
|
41
|
+
const violations = violationsOf(lint, key).map((violation) => eslintDiagnostic(violation, fixesFor(ctx.packs)));
|
|
42
|
+
const final = state.rules[key] === undefined || level === "off" ? level : violations.length === 0 ? "error" : "warn";
|
|
43
|
+
const written = state.rules[key] === undefined ? [] : [writeState(root, withLevel(state, key, final))];
|
|
44
|
+
const shown = capped(violations, VIOLATION_LIMIT);
|
|
45
|
+
return ok({
|
|
46
|
+
exit: violations.length > 0 ? EXIT.findings : EXIT.ok,
|
|
47
|
+
data: {
|
|
48
|
+
rule: key,
|
|
49
|
+
level: final,
|
|
50
|
+
changes: mergeChanges([...changes, ...written]),
|
|
51
|
+
violations: { count: violations.length, truncated: shown.truncated, items: shown.items },
|
|
52
|
+
...extra,
|
|
53
|
+
next: violations.length > 0 ? `Fix the ${violations.length} violations, then run \`guardrails add-rule ${name} --promote\`.` : "guardrails check --json",
|
|
54
|
+
},
|
|
55
|
+
text: [`${key}: level ${final}, ${violations.length} existing violations`, ...shown.items.slice(0, 10).map((item) => ` ${item.file}:${item.line} ${item.message}`)].join("\n"),
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function promote(ctx, name) {
|
|
61
|
+
const { root } = ctx;
|
|
62
|
+
const key = `${OWN_PREFIX}${name}`;
|
|
63
|
+
const missing = requireEslint(root);
|
|
64
|
+
if (missing !== null)
|
|
65
|
+
return err(missing);
|
|
66
|
+
return andThen(readState(root), (state) => {
|
|
67
|
+
if (state.rules[key] === undefined)
|
|
68
|
+
return err(appError("unknown-constraint", `${key} is not installed`, { rule: key, fix: `Create it first: guardrails add-rule ${name} --message "<how to fix>" --selector "<esquery>"` }));
|
|
69
|
+
if ((readText(root, `${PLUGIN_DIR}/rules/${name}.mjs`) ?? "").includes(UNIMPLEMENTED_MARKER)) {
|
|
70
|
+
return err(appError("not-applicable", `${key} is still a skeleton`, { file: `${PLUGIN_DIR}/rules/${name}.mjs`, rule: key, fix: "Implement create() or pass --selector when creating it; then add --bad/--good cases to its test." }));
|
|
71
|
+
}
|
|
72
|
+
const test = runNodeTest(root, `${PLUGIN_DIR}/rules/${name}.test.mjs`);
|
|
73
|
+
if (!test.ok)
|
|
74
|
+
return err(appError("verify-failed", `the rule test for ${key} fails`, { file: `${PLUGIN_DIR}/rules/${name}.test.mjs`, rule: key, fix: `Fix the rule or its test until \`node --test ${PLUGIN_DIR}/rules/${name}.test.mjs\` passes.\n${test.output}` }));
|
|
75
|
+
writeState(root, withLevel(state, key, "warn"));
|
|
76
|
+
return finish(ctx, name, [], "warn", { ruleTest: { ok: true } });
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function create(ctx, request) {
|
|
80
|
+
const { root, profile } = ctx;
|
|
81
|
+
const { name } = request;
|
|
82
|
+
const ruleFile = `${PLUGIN_DIR}/rules/${name}.mjs`;
|
|
83
|
+
const key = `${OWN_PREFIX}${name}`;
|
|
84
|
+
if (exists(root, ruleFile)) {
|
|
85
|
+
return err(appError("conflict", `${ruleFile} already exists`, { file: ruleFile, rule: key, fix: `Edit the rule in place, or run \`guardrails add-rule ${name} --promote\` to re-check and promote it. guardrails never overwrites a rule.` }));
|
|
86
|
+
}
|
|
87
|
+
return andThen(readSpec(request), (spec) => andThen(readState(root), (state) => {
|
|
88
|
+
const files = request.files.map((glob) => glob.trim()).filter((glob) => glob !== "");
|
|
89
|
+
const entry = { level: spec.selector === null ? "off" : "warn", files: files.length > 0 ? files : jsTs(profile).paths.sourceGlobs };
|
|
90
|
+
const written = [
|
|
91
|
+
writeManaged(root, ruleFile, renderRule(spec)),
|
|
92
|
+
writeManaged(root, `${PLUGIN_DIR}/rules/${name}.test.mjs`, renderRuleTest(spec)),
|
|
93
|
+
writeManaged(root, `${PLUGIN_DIR}/rules/_rule-tester.mjs`, readAsset("eslint-rules/_rule-tester.mjs")),
|
|
94
|
+
writeState(root, withRule(state, key, entry)),
|
|
95
|
+
...writeIndexAndConfig(root),
|
|
96
|
+
];
|
|
97
|
+
return andThen(wireEslintConfig(ctx), (wiring) => {
|
|
98
|
+
const test = runNodeTest(root, `${PLUGIN_DIR}/rules/${name}.test.mjs`);
|
|
99
|
+
if (!test.ok) {
|
|
100
|
+
return err(appError("verify-failed", `the generated rule test fails: ${PLUGIN_DIR}/rules/${name}.test.mjs`, { file: `${PLUGIN_DIR}/rules/${name}.test.mjs`, rule: key, fix: `Make --selector match the --bad snippet and not the --good one, then edit the rule or its test.\n${test.output}` }));
|
|
101
|
+
}
|
|
102
|
+
const skeleton = spec.selector === null;
|
|
103
|
+
return finish(ctx, name, [...written, ...wiring], entry.level, {
|
|
104
|
+
status: skeleton ? "skeleton" : "created",
|
|
105
|
+
ruleTest: { ok: true },
|
|
106
|
+
needs: skeleton ? [`Implement create() in ${ruleFile} (pass --selector to generate it), then add --bad/--good cases to its test.`] : [],
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
export function addRuleJsTs(ctx, request) {
|
|
112
|
+
if (request.promote)
|
|
113
|
+
return promote(ctx, request.name);
|
|
114
|
+
const missing = requireEslint(ctx.root);
|
|
115
|
+
return missing !== null ? err(missing) : create(ctx, request);
|
|
116
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { objectAt } from "../../../core/domain/json.js";
|
|
2
|
+
import { timed, timedSync } from "../../../core/app/timing.js";
|
|
3
|
+
import { readJsonRecord, exists } from "../../../core/infra/fs.js";
|
|
4
|
+
import { jsTs } from "../domain/data.js";
|
|
5
|
+
import { EXACT_DEPS_RULE, REASON_RULE, dependencyDiagnostics, disableDiagnostics, failingReasons, ratchetDiagnostic, rulesAt, skippedTools, toolDiagnostics, topRules } from "../domain/report.js";
|
|
6
|
+
import { withLevel } from "../domain/eslint-state.js";
|
|
7
|
+
import { evaluateRatchet } from "../../../core/domain/ratchet.js";
|
|
8
|
+
import { readBaseline, writeBaseline } from "../../../core/infra/baseline.js";
|
|
9
|
+
import { missingStrictOptions } from "../domain/ts-options.js";
|
|
10
|
+
import { hasEslint, lintRepoAsync } from "../infra/eslint.js";
|
|
11
|
+
import { scanDisableDirectives } from "../infra/scan.js";
|
|
12
|
+
import { runTscAsync } from "../infra/tsc.js";
|
|
13
|
+
import { fixesFor } from "./fixes.js";
|
|
14
|
+
import { prRuleDiagnostics } from "./pr-rules.js";
|
|
15
|
+
import { readState, writeState } from "./eslint-files.js";
|
|
16
|
+
import { installDevCommand } from "../domain/profile-pure.js";
|
|
17
|
+
function missingFix(data) {
|
|
18
|
+
return `Install the tools (${installDevCommand(data.packageManager, ["eslint", "typescript-eslint", "typescript"])}) and run \`guardrails plan\` to see which constraints to apply.`;
|
|
19
|
+
}
|
|
20
|
+
async function runTools(root, data, paths) {
|
|
21
|
+
const runs = await timed(async () => {
|
|
22
|
+
const lint = timed(() => (hasEslint(root) ? lintRepoAsync(root, data, paths) : Promise.resolve(null)));
|
|
23
|
+
const tsc = timed(() => (data.typescript.present ? runTscAsync(root) : Promise.resolve(null)));
|
|
24
|
+
return Promise.all([lint, tsc]);
|
|
25
|
+
});
|
|
26
|
+
const [lint, tsc] = runs.value;
|
|
27
|
+
return { lint, tsc };
|
|
28
|
+
}
|
|
29
|
+
function typeMetric(root, runs) {
|
|
30
|
+
const tsc = runs.tsc.value;
|
|
31
|
+
const options = exists(root, "tsconfig.json") ? objectAt(readJsonRecord(root, "tsconfig.json") ?? {}, "compilerOptions") : null;
|
|
32
|
+
return { errors: tsc?.ran === true ? tsc.diagnostics.length : null, missingStrictOptions: options === null ? [] : missingStrictOptions(options) };
|
|
33
|
+
}
|
|
34
|
+
function lintMetric(runs) {
|
|
35
|
+
const lint = runs.lint.value;
|
|
36
|
+
const violations = lint?.violations ?? [];
|
|
37
|
+
const errors = violations.filter((item) => item.severity === "error").length;
|
|
38
|
+
return { ran: lint?.ran === true, errors, warnings: violations.length - errors, byRule: topRules(violations), skipped: lint === null ? "eslint is not installed" : lint.error };
|
|
39
|
+
}
|
|
40
|
+
function ratchetOf(ctx, runs) {
|
|
41
|
+
const state = readState(ctx.root);
|
|
42
|
+
const data = jsTs(ctx.profile);
|
|
43
|
+
const baseline = readBaseline(ctx.root);
|
|
44
|
+
const loose = data.exactDeps.script ? data.exactDeps.loose.length : 0;
|
|
45
|
+
const exact = loose > 0 || baseline.rules[EXACT_DEPS_RULE] !== undefined ? [EXACT_DEPS_RULE] : [];
|
|
46
|
+
const warnRules = [...(state.ok ? rulesAt(state.value, "warn") : []), ...exact];
|
|
47
|
+
const lintCounts = (runs.lint.value?.violations ?? []).reduce((acc, item) => ({ ...acc, [item.rule]: (acc[item.rule] ?? 0) + 1 }), {});
|
|
48
|
+
const counts = { ...lintCounts, [EXACT_DEPS_RULE]: loose };
|
|
49
|
+
return { counts, warnRules, result: evaluateRatchet({ baseline, counts, warnRules }) };
|
|
50
|
+
}
|
|
51
|
+
function settle(ctx, runs, paths, ratchet) {
|
|
52
|
+
if (paths.length > 0 || runs.lint.value?.ran !== true)
|
|
53
|
+
return [];
|
|
54
|
+
const state = readState(ctx.root);
|
|
55
|
+
const promoted = ratchet.result.promotable.filter((rule) => state.ok && state.value.rules[rule] !== undefined);
|
|
56
|
+
if (state.ok && promoted.length > 0)
|
|
57
|
+
writeState(ctx.root, promoted.reduce((next, rule) => withLevel(next, rule, "error"), state.value));
|
|
58
|
+
const before = readBaseline(ctx.root);
|
|
59
|
+
const changed = JSON.stringify(before.rules) !== JSON.stringify(ratchet.result.next.rules);
|
|
60
|
+
if (changed && ctx.readOnly !== true)
|
|
61
|
+
writeBaseline(ctx.root, ratchet.result.next);
|
|
62
|
+
return promoted;
|
|
63
|
+
}
|
|
64
|
+
function metricsOf(ctx, runs, scan, ratchet, promoted) {
|
|
65
|
+
const lint = lintMetric(runs);
|
|
66
|
+
const types = typeMetric(ctx.root, runs);
|
|
67
|
+
const withoutReason = scan.filter((item) => !item.hasReason);
|
|
68
|
+
const debtCount = ratchet.warnRules.reduce((sum, rule) => sum + (ratchet.counts[rule] ?? 0), 0);
|
|
69
|
+
const regressions = ratchet.result.regressions.map((entry) => `${entry.rule} ${String(entry.count)} > baseline ${String(entry.baseline)}`);
|
|
70
|
+
return {
|
|
71
|
+
failing: failingReasons({ errors: lint.errors, regressions, disablesWithoutReason: withoutReason.length, typeErrors: types.errors }),
|
|
72
|
+
metrics: {
|
|
73
|
+
lint,
|
|
74
|
+
eslintDisables: { total: scan.length, withoutReason: withoutReason.length, locations: withoutReason.slice(0, 50).map((item) => `${item.file}:${item.line}`) },
|
|
75
|
+
ratchet: { rulesAtWarn: ratchet.warnRules, debt: debtCount, regressions: ratchet.result.regressions, tolerated: ratchet.result.tolerated, promoted },
|
|
76
|
+
types,
|
|
77
|
+
},
|
|
78
|
+
lines: [
|
|
79
|
+
`lint: ${lint.errors} errors, ${lint.warnings} warnings`,
|
|
80
|
+
`eslint-disable: ${scan.length} total, ${withoutReason.length} without reason`,
|
|
81
|
+
`ratchet: ${ratchet.warnRules.length} rules at warn, ${debtCount} violations of debt${promoted.length > 0 ? `, promoted to error: ${promoted.join(", ")}` : ""}`,
|
|
82
|
+
`types: ${types.errors === null ? "not checked" : `${types.errors} errors`}`,
|
|
83
|
+
],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export async function analyzeJsTs(ctx, paths) {
|
|
87
|
+
const data = jsTs(ctx.profile);
|
|
88
|
+
const runs = await runTools(ctx.root, data, paths);
|
|
89
|
+
const scan = timedSync(() => scanDisableDirectives(ctx.root, data));
|
|
90
|
+
const base = toolDiagnostics(runs.lint.value, runs.tsc.value, fixesFor(ctx.packs));
|
|
91
|
+
const handled = new Set(base.filter((item) => item.rule === REASON_RULE).map((item) => `${item.file}:${item.line}`));
|
|
92
|
+
const ratchet = ratchetOf(ctx, runs);
|
|
93
|
+
const promoted = settle(ctx, runs, paths, ratchet);
|
|
94
|
+
return {
|
|
95
|
+
pack: "js-ts",
|
|
96
|
+
ran: runs.lint.value?.ran === true || runs.tsc.value?.ran === true,
|
|
97
|
+
debt: Object.fromEntries(ratchet.warnRules.map((rule) => [rule, ratchet.counts[rule] ?? 0])),
|
|
98
|
+
diagnostics: [...base, ...ratchet.result.regressions.map(ratchetDiagnostic), ...prRuleDiagnostics(ctx, paths), ...dependencyDiagnostics(data), ...disableDiagnostics(scan.value, handled)],
|
|
99
|
+
skipped: skippedTools(runs.lint.value, runs.tsc.value),
|
|
100
|
+
missingFix: missingFix(data),
|
|
101
|
+
phasesMs: { eslint: runs.lint.ms, tsc: runs.tsc.ms, scan: scan.ms },
|
|
102
|
+
...metricsOf(ctx, runs, scan.value, ratchet, promoted),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { objectAt } from "../../../core/domain/json.js";
|
|
2
|
+
import { map, ok } from "../../../core/domain/result.js";
|
|
3
|
+
import { editJsonFile, readJsonRecord } from "../../../core/infra/fs.js";
|
|
4
|
+
const PREPARE = "git config core.hooksPath .githooks || true";
|
|
5
|
+
const prepareOf = (root) => objectAt(readJsonRecord(root, "package.json") ?? {}, "scripts")["prepare"];
|
|
6
|
+
export const JS_TS_BOOTSTRAP = {
|
|
7
|
+
active: ({ root }) => {
|
|
8
|
+
const prepare = prepareOf(root);
|
|
9
|
+
return typeof prepare === "string" && prepare.includes("core.hooksPath");
|
|
10
|
+
},
|
|
11
|
+
ensure: ({ root }) => {
|
|
12
|
+
if (typeof prepareOf(root) === "string")
|
|
13
|
+
return ok([]);
|
|
14
|
+
return map(editJsonFile(root, "package.json", (value) => ({ ...value, scripts: { ...objectAt(value, "scripts"), prepare: PREPARE } }), false), (change) => [change]);
|
|
15
|
+
},
|
|
16
|
+
};
|