@crown3/repo-rules 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 ADDED
@@ -0,0 +1,7 @@
1
+ BSD Zero Clause License
2
+
3
+ Copyright (C) 2026 by Crown
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,104 @@
1
- # Temporary Holding Version
1
+ # @crown3/repo-rules
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Repo checks shared across repos, as one CLI driven by a per-repo config: the comment policy, shell formatting, the agent docs, and where git hooks and CI run each `package.json` script.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ pnpm add -D @crown3/repo-rules
9
+ ```
10
+
11
+ Needs Node.js >= 24 and git.
12
+
13
+ ## Use
14
+
15
+ ```sh
16
+ repo-rules check-comments [files...] # no files: every file git does not ignore, new ones included
17
+ repo-rules format-sh [--check] [files...] # shfmt (2-space indent, indented case) on shell files
18
+ repo-rules check-docs [root] # the Markdown checks below, then check-runs when configured
19
+ repo-rules check-runs [root] # hooks and CI call only package.json scripts, as the docs table says
20
+ repo-rules review-comments [ref] # external(...) comments added since the merge base with ref (default: baseRef)
21
+ ```
22
+
23
+ `--config <file>` loads another config module, resolved from the working directory; the default is `repo-rules.config.mjs` in the root.
24
+
25
+ Exit codes: 0 pass, 1 findings, 64 a usage or config error.
26
+
27
+ ### check-comments
28
+
29
+ A comment in a TypeScript, JavaScript, shell or Go file is allowed only as an `external(<dependency>): why` comment (own-line comments right below continue it), a tool directive (`biome-ignore`, `shellcheck`, `/// <reference`, `//go:`, `//nolint:`, a shebang), or a JSDoc block on an exported declaration, or a member of an exported interface or type, in a public API: the `src/` of each `packages/<name>/` whose `package.json` is not `"private": true`, plus `comments.publicApi`. Files under `node_modules/` and `fixtures/`, `comments.exclude`, and files with a `// Code generated ... DO NOT EDIT.` line are skipped. With file names during a merge, files that arrive unchanged from the merged branch are skipped too, so a hook does not fail on someone else's code; `format-sh` does the same.
30
+
31
+ ### check-docs
32
+
33
+ Reads `AGENTS.md`, `GLOSSARY-MAP.md`, `GLOSSARY.md`, `LESSONS.md` and `README.md` at the root, everything under `docs/`, each workspace's `AGENTS.md`, `GLOSSARY.md`, `README.md` and `docs/` with `docs.workspaceDocs`, and `docs.trees`. It fails on:
34
+
35
+ - a relative link that does not resolve, or a `#fragment` into a `.md` file that names none of its headings (GitHub's slug, `-1` for a repeat) or `<a id>`/`<a name>`; a link into `docs.unlinked`;
36
+ - a document outside `docs.trees`, `README.md` aside, that no link chain from `AGENTS.md` reaches;
37
+ - an `AGENTS.md` or `CLAUDE.md` outside the root and the `skills/`, `.agents/skills/` and `.claude/skills/` trees (`docs.agentFiles`);
38
+ - CJK text outside code in an `AGENTS.md`, `GLOSSARY-MAP.md`, `GLOSSARY.md`, `LESSONS.md` or a file under a `docs/` directory;
39
+ - a run of 10 words that two documents share (an inline code span counts as one word, a link breaks the run).
40
+
41
+ ### check-runs
42
+
43
+ Every hk step command (`check`, `fix`, `check_diff`, `check_list_files` in `hk.pkl`) must be `pnpm <script>`, and every CI command must match `runs.ci.entry`; `runs.ci.setup` scripts are exempt. Each script they run needs a row in the `## Where checks run` table of `runs.table`, whose second and third cells name, in backticks, the hooks and CI jobs that run it:
44
+
45
+ ```md
46
+ | Script | hk | CI job |
47
+ |---|---|---|
48
+ | `check:docs` | `pre-commit` | `verify` |
49
+ ```
50
+
51
+ GitHub workflows are read for `run:` under `jobs.<job>`, Codebase pipelines for `jobs.<job>.steps[].commands[]`.
52
+
53
+ ## Config
54
+
55
+ `repo-rules.config.mjs` exports the config as a plain object, so it loads before the package is built in its own repo:
56
+
57
+ ```js
58
+ export default {
59
+ baseRef: 'origin/master', // review-comments without a ref
60
+ workspaces: ['apps', 'packages'], // directories whose children are workspaces
61
+ comments: {
62
+ exclude: [/^vendor\//],
63
+ publicApi: ['packages/plugin/src/'], // a private package whose exports are still an API
64
+ },
65
+ docs: {
66
+ trees: ['skills'], // also link- and overlap-checked, not held to reachability
67
+ workspaceDocs: true,
68
+ agentFiles: 'workspaces', // 'root' (default), 'workspaces' or 'anywhere' (off)
69
+ unlinked: ['archives', '.scratch'],
70
+ skip: ['generated'], // directory names, besides node_modules, dist, coverage and other build output
71
+ },
72
+ runs: {
73
+ ci: {
74
+ platform: 'codebase', // or 'github'
75
+ files: ['.codebase/pipelines/*.yaml'],
76
+ entry: /^npx -y @ecom\/codebase-ci@\S+ run (\S+)(?: [a-z][\w-]*)?$/, // group 1: the script
77
+ setup: ['install'],
78
+ hint: 'call `npx -y @ecom/codebase-ci@<version> run <package.json script>` instead',
79
+ },
80
+ },
81
+ };
82
+ ```
83
+
84
+ `defineConfig` and the `Config` type are exported for a config that imports the built package. For `github`, `entry` defaults to `pnpm <script>` and `hint` to moving setup into `.github/actions/setup`; `codebase` needs both. `runs.hooks` (default `hk.pkl`) and `runs.table` (default `docs/subsystems/checks-and-ci.md`) move those files.
85
+
86
+ ## Library
87
+
88
+ ```js
89
+ import { isShell, ownChanges, repoFiles, runIfMain } from '@crown3/repo-rules';
90
+ ```
91
+
92
+ `runIfMain(import.meta.url, main)` runs `main(process.argv)` when the module is the entry script and exits with its code when not 0.
93
+
94
+ `@crown3/repo-rules/scrub-git-hook-env` deletes every `GIT_*` variable. git exports `GIT_DIR` and `GIT_INDEX_FILE` to hooks, so tests that call git from a hook would act on the real repo; load it as an rstest setup file:
95
+
96
+ ```js
97
+ import { fileURLToPath } from 'node:url';
98
+
99
+ export default {
100
+ setupFiles: [
101
+ fileURLToPath(import.meta.resolve('@crown3/repo-rules/scrub-git-hook-env')),
102
+ ],
103
+ };
104
+ ```
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync } from 'node:fs';
3
+
4
+ const source = new URL('../src/cli.ts', import.meta.url);
5
+ const entry = existsSync(source)
6
+ ? source
7
+ : new URL('../dist/cli.js', import.meta.url);
8
+ const { main } = await import(entry.href);
9
+ process.exitCode = await main(process.argv.slice(2));
@@ -0,0 +1,3 @@
1
+ import type { Settings } from './config.js';
2
+ /** One error per comment the policy does not allow in files, as `file:line: message`. */
3
+ export declare function checkComments(root: string, files: string[], { exclude, publicApi }: Settings['comments']): string[];
@@ -0,0 +1,13 @@
1
+ import type { Settings } from './config.js';
2
+ /** The targets of the Markdown links outside code. */
3
+ export declare function links(markdown: string): string[];
4
+ /** GitHub's anchor for a heading. */
5
+ export declare function slug(heading: string): string;
6
+ /** Every anchor a link can name in markdown: heading slugs, with `-1` on for a repeat, and `<a id>` or `<a name>`. */
7
+ export declare function anchors(markdown: string): Set<string>;
8
+ export interface DocsResult {
9
+ errors: string[];
10
+ count: number;
11
+ }
12
+ /** Checks the repo's Markdown: links and anchors, reachability from AGENTS.md, stray agent files, English, shared runs of words; and check-runs when configured. */
13
+ export declare function checkDocs(root: string, settings: Settings): DocsResult;
@@ -0,0 +1,23 @@
1
+ import type { Runs } from './config.js';
2
+ /** A command a hook or CI step runs, under the keys that lead to it. */
3
+ export interface Run {
4
+ path: string[];
5
+ command: string;
6
+ }
7
+ /** Every `run:` command in a GitHub Actions workflow, with the keys above it. */
8
+ export declare function runs(yaml: string): Run[];
9
+ /** Every step command of a Codebase pipeline, under its job. */
10
+ export declare function commands(yaml: string): Run[];
11
+ /** Every step command in an hk.pkl, under its hook and step. */
12
+ export declare function hookRuns(pkl: string): Run[];
13
+ type Kind = 'hooks' | 'jobs';
14
+ type Where = Record<Kind, string[]>;
15
+ /** The rows of the "Where checks run" table: per script, the hooks and CI jobs it lists. */
16
+ export declare function table(markdown: string): Map<string, Where>;
17
+ /** The pipeline files the patterns name, sorted within each pattern. */
18
+ export declare function pipelineFiles(root: string, patterns: string[]): string[];
19
+ /** How the error messages name the pipeline files: the one pattern, or `dir/{a,b}.ext`. */
20
+ export declare function describeFiles(patterns: string[]): string;
21
+ /** Hooks and CI steps call only package.json scripts, and the "Where checks run" table lists each where it runs. */
22
+ export declare function checkRuns(root: string, config: Runs): string[];
23
+ export {};
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const USAGE = "usage: repo-rules <command> [--config <file>] [args...]\n\ncommands:\n check-comments [files...] comments the policy allows; no files: every file git does not ignore\n format-sh [--check] [files...] shfmt on shell files; --check only lists them\n check-docs [root] links, reachability, agent files, English, shared phrases, and check-runs\n check-runs [root] hooks and CI call package.json scripts the docs table lists\n review-comments [ref] external(...) comments added since the merge base with ref\n\noptions:\n --config <file> config module (default: repo-rules.config.mjs in the root)";
2
+ /** Runs one command with argv after the program name; resolves to its exit code. */
3
+ export declare function main(argv: string[], cwd?: string): Promise<number>;