@crown3/repo-rules 0.0.0 → 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.
@@ -0,0 +1,17 @@
1
+ /** One comment of a source file. */
2
+ export interface Comment {
3
+ line: number;
4
+ text: string;
5
+ /** An own-line comment, which can continue an external(...) comment on the line above. */
6
+ chains: boolean;
7
+ /** A tool directive: biome-ignore, shellcheck, a triple-slash reference, `//go:`, `//nolint:`. */
8
+ directive: boolean;
9
+ /** A JSDoc block on an exported declaration of a public API. */
10
+ publicDoc: boolean;
11
+ }
12
+ /** The lines that hold an external(...) comment or one of its own-line continuations. */
13
+ export declare function externalLines(comments: Comment[]): Set<number>;
14
+ /** The comments of a script, shell or Go file; undefined for a file of another kind. */
15
+ export declare function commentsOf(file: string, source: string, publicApi: boolean): Comment[] | undefined;
16
+ /** The lines of the comments the policy does not allow; none in a generated file. */
17
+ export declare function disallowedLines(file: string, source: string, publicApi?: boolean): number[];
@@ -0,0 +1,85 @@
1
+ /** Which CI system's pipeline files check-runs reads. */
2
+ export type Platform = 'github' | 'codebase';
3
+ /** The CI pipelines check-runs reads, and what a step in them may run. */
4
+ export interface CiConfig {
5
+ platform: Platform;
6
+ /** Pipeline files, or globs, relative to the repo root. */
7
+ files: string[];
8
+ /**
9
+ * A CI command passes when it matches this; group 1 names the package.json script it runs.
10
+ * Default for github: `pnpm <script>`. Required for codebase.
11
+ */
12
+ entry?: RegExp;
13
+ /** Scripts a step may run as setup, which the "Where checks run" table does not list. */
14
+ setup?: string[];
15
+ /** How a failing CI step should be written instead, in the error message. */
16
+ hint?: string;
17
+ }
18
+ /** check-runs: hooks and CI call only package.json scripts, and the docs table says where. */
19
+ export interface RunsConfig {
20
+ /** The hk config, relative to the repo root. Default `hk.pkl`. */
21
+ hooks?: string;
22
+ ci: CiConfig;
23
+ /** The page with the "## Where checks run" table. Default `docs/subsystems/checks-and-ci.md`. */
24
+ table?: string;
25
+ }
26
+ /** Where AGENTS.md and CLAUDE.md may sit besides the repo root and skill trees. */
27
+ export type AgentFiles = 'root' | 'workspaces' | 'anywhere';
28
+ /** check-comments and review-comments. */
29
+ export interface CommentsConfig {
30
+ /** Paths, relative to the repo root, check-comments skips besides `node_modules/` and `fixtures/`. */
31
+ exclude?: RegExp[];
32
+ /** Directory prefixes whose exported declarations may carry JSDoc, besides the `src/` of every package under `packages/` not marked private. */
33
+ publicApi?: string[];
34
+ }
35
+ /** check-docs. */
36
+ export interface DocsConfig {
37
+ /** Directories walked for Markdown besides `docs/`; their files are link- and overlap-checked only. */
38
+ trees?: string[];
39
+ /** Also read each workspace's AGENTS.md, GLOSSARY.md, README.md and docs/. Default false. */
40
+ workspaceDocs?: boolean;
41
+ /** Default `root`: only the repo root and skill trees. `workspaces` also allows a workspace root; `anywhere` turns the check off. */
42
+ agentFiles?: AgentFiles;
43
+ /** Directories no doc may link into, such as archives the docs check does not read. */
44
+ unlinked?: string[];
45
+ /** Directory names the walks skip, besides the built-in build and cache directories. */
46
+ skip?: string[];
47
+ }
48
+ /** The config a repo writes in repo-rules.config.mjs. */
49
+ export interface Config {
50
+ /** The ref review-comments diffs against when none is given. Default `origin/main`. */
51
+ baseRef?: string;
52
+ /** Directories whose children are workspaces. Default `['apps', 'packages']`. */
53
+ workspaces?: string[];
54
+ comments?: CommentsConfig;
55
+ docs?: DocsConfig;
56
+ /** Omit to leave check-runs off; check-docs runs it when set. */
57
+ runs?: RunsConfig;
58
+ }
59
+ export interface Settings {
60
+ baseRef: string;
61
+ workspaces: string[];
62
+ comments: {
63
+ exclude: RegExp[];
64
+ publicApi: string[];
65
+ };
66
+ docs: {
67
+ trees: string[];
68
+ workspaceDocs: boolean;
69
+ agentFiles: AgentFiles;
70
+ unlinked: string[];
71
+ skip: string[];
72
+ };
73
+ runs?: Runs;
74
+ }
75
+ export interface Runs {
76
+ hooks: string;
77
+ table: string;
78
+ ci: Required<CiConfig>;
79
+ }
80
+ /** Identity, so a config module gets type checking: `export default defineConfig({ ... })`. */
81
+ export declare const defineConfig: (config: Config) => Config;
82
+ export declare class ConfigError extends Error {
83
+ }
84
+ /** Checks a loaded config and fills in the defaults. */
85
+ export declare function settings(config?: unknown): Settings;
@@ -0,0 +1,6 @@
1
+ /** A `.sh` file, or an extensionless one whose first line is a sh or bash shebang. */
2
+ export declare function isShell(file: string, firstLine: string): boolean;
3
+ /** Every file git does not ignore, tracked or new, relative to the repo root. */
4
+ export declare const repoFiles: (root: string) => string[];
5
+ /** During a merge, drops the files that arrive unchanged from the merged branch; otherwise returns files as given. */
6
+ export declare function ownChanges(root: string, files: string[]): string[];
@@ -0,0 +1,6 @@
1
+ export interface FormatResult {
2
+ changed: string[];
3
+ errors: string[];
4
+ }
5
+ /** Runs shfmt on the shell files among files: lists those it would change, and rewrites them with write. */
6
+ export declare function formatFiles(root: string, files: string[], write: boolean): FormatResult;
package/dist/g.js ADDED
@@ -0,0 +1,120 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import node_path from "node:path";
3
+ const defineConfig = (config)=>config;
4
+ class ConfigError extends Error {
5
+ }
6
+ const GITHUB_ENTRY = /^pnpm (\S+)[^\n]*$/;
7
+ const GITHUB_HINT = "call a package.json script instead, or move setup into .github/actions/setup";
8
+ const isString = (v)=>'string' == typeof v;
9
+ const isStrings = (v)=>Array.isArray(v) && v.every(isString);
10
+ const isRegExps = (v)=>Array.isArray(v) && v.every((r)=>r instanceof RegExp);
11
+ const isBoolean = (v)=>'boolean' == typeof v;
12
+ const oneOf = (...values)=>(v)=>values.includes(v);
13
+ function field(object, key, check, what, fallback) {
14
+ const value = object[key];
15
+ if (void 0 === value) return fallback;
16
+ if (!check(value)) throw new ConfigError(`${key} must be ${what}`);
17
+ return value;
18
+ }
19
+ const section = (value, key)=>{
20
+ if (void 0 === value) return {};
21
+ if ('object' != typeof value || null === value || Array.isArray(value)) throw new ConfigError(`${key} must be an object`);
22
+ return value;
23
+ };
24
+ function ci(raw) {
25
+ const platform = field(raw, 'platform', oneOf('github', 'codebase'), '"github" or "codebase"', '');
26
+ if (!platform) throw new ConfigError('runs.ci.platform must be "github" or "codebase"');
27
+ const github = 'github' === platform;
28
+ const entry = field(raw, 'entry', (v)=>v instanceof RegExp, 'a RegExp', github ? GITHUB_ENTRY : void 0);
29
+ const hint = field(raw, 'hint', isString, 'a string', github ? GITHUB_HINT : void 0);
30
+ if (!entry || !hint) throw new ConfigError('runs.ci for codebase needs entry and hint');
31
+ return {
32
+ platform: platform,
33
+ files: field(raw, 'files', isStrings, 'a list of paths', []),
34
+ entry,
35
+ setup: field(raw, 'setup', isStrings, "a list of script names", []),
36
+ hint
37
+ };
38
+ }
39
+ function runs(value) {
40
+ if (void 0 === value) return;
41
+ const raw = section(value, 'runs');
42
+ return {
43
+ hooks: field(raw, 'hooks', isString, 'a path', 'hk.pkl'),
44
+ table: field(raw, 'table', isString, 'a path', 'docs/subsystems/checks-and-ci.md'),
45
+ ci: ci(section(raw.ci, 'runs.ci'))
46
+ };
47
+ }
48
+ function settings(config = {}) {
49
+ const raw = section(config, 'the config');
50
+ const comments = section(raw.comments, 'comments');
51
+ const docs = section(raw.docs, 'docs');
52
+ return {
53
+ baseRef: field(raw, 'baseRef', isString, 'a string', 'origin/main'),
54
+ workspaces: field(raw, 'workspaces', isStrings, 'a list of directories', [
55
+ 'apps',
56
+ 'packages'
57
+ ]),
58
+ comments: {
59
+ exclude: field(comments, 'exclude', isRegExps, 'a list of RegExps', []),
60
+ publicApi: field(comments, 'publicApi', isStrings, 'a list of directories', [])
61
+ },
62
+ docs: {
63
+ trees: field(docs, 'trees', isStrings, 'a list of directories', []),
64
+ workspaceDocs: field(docs, 'workspaceDocs', isBoolean, 'true or false', false),
65
+ agentFiles: field(docs, 'agentFiles', oneOf('root', 'workspaces', 'anywhere'), '"root", "workspaces" or "anywhere"', 'root'),
66
+ unlinked: field(docs, 'unlinked', isStrings, 'a list of directories', []),
67
+ skip: field(docs, 'skip', isStrings, 'a list of directory names', [])
68
+ },
69
+ runs: runs(raw.runs)
70
+ };
71
+ }
72
+ const SHEBANG = /^#!.*\b(?:ba)?sh(?:\s|$)/;
73
+ function isShell(file, firstLine) {
74
+ if (file.endsWith('.sh')) return true;
75
+ return '' === node_path.extname(file) && SHEBANG.test(firstLine);
76
+ }
77
+ const git = (root, args)=>execFileSync('git', args, {
78
+ cwd: root,
79
+ encoding: 'utf8',
80
+ stdio: [
81
+ 'ignore',
82
+ 'pipe',
83
+ 'ignore'
84
+ ],
85
+ maxBuffer: 1073741824
86
+ });
87
+ const lines = (out)=>out.split('\n').filter(Boolean);
88
+ const repoFiles = (root)=>lines(git(root, [
89
+ 'ls-files',
90
+ '--cached',
91
+ '--others',
92
+ '--exclude-standard'
93
+ ]));
94
+ function merging(root) {
95
+ try {
96
+ git(root, [
97
+ 'rev-parse',
98
+ '-q',
99
+ '--verify',
100
+ 'MERGE_HEAD'
101
+ ]);
102
+ return true;
103
+ } catch {
104
+ return false;
105
+ }
106
+ }
107
+ function ownChanges(root, files) {
108
+ if (!files.length || !merging(root)) return files;
109
+ const differ = new Set(lines(git(root, [
110
+ 'diff',
111
+ '--cached',
112
+ '--name-only',
113
+ '--no-renames',
114
+ 'MERGE_HEAD',
115
+ '--',
116
+ ...files
117
+ ])));
118
+ return files.filter((f)=>differ.has(f));
119
+ }
120
+ export { ConfigError, defineConfig, isShell, ownChanges, repoFiles, settings };
@@ -0,0 +1,7 @@
1
+ export interface GoComment {
2
+ line: number;
3
+ text: string;
4
+ chains: boolean;
5
+ }
6
+ /** Each comment in a Go source, its text untrimmed so a `//go:` directive stays recognizable. */
7
+ export declare function goComments(source: string): GoComment[];
@@ -0,0 +1,4 @@
1
+ export type { AgentFiles, CiConfig, CommentsConfig, Config, DocsConfig, Platform, RunsConfig, } from './config.js';
2
+ export { defineConfig } from './config.js';
3
+ export { isShell, ownChanges, repoFiles } from './files.js';
4
+ export { runIfMain } from './run-if-main.js';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import * as __rspack_external_node_fs_1b05aee1 from "node:fs";
3
+ function isEntry(moduleUrl) {
4
+ try {
5
+ return __rspack_external_node_fs_1b05aee1.realpathSync(process.argv[1] ?? '') === fileURLToPath(moduleUrl);
6
+ } catch {
7
+ return false;
8
+ }
9
+ }
10
+ function runIfMain(moduleUrl, main) {
11
+ if (!isEntry(moduleUrl)) return;
12
+ const code = main(process.argv);
13
+ if (code) process.exit(code);
14
+ }
15
+ export { defineConfig, isShell, ownChanges, repoFiles } from "./g.js";
16
+ export { runIfMain };
@@ -0,0 +1,14 @@
1
+ export interface Added {
2
+ line: number;
3
+ text: string;
4
+ }
5
+ export interface FileAdded {
6
+ file: string;
7
+ added: Added[];
8
+ }
9
+ /** Of the added lines, those holding an external(...) comment, whose claim a person must check. */
10
+ export declare function reviewLines(file: string, source: string, added: number[]): number[];
11
+ /** The added lines of each file in a `git diff --unified=0`, with their new line numbers. */
12
+ export declare function parseDiff(diff: string): FileAdded[];
13
+ /** `file:line` for each external(...) comment added since the merge base with against, new files included. */
14
+ export declare function reviewComments(root: string, against: string): string[];
@@ -0,0 +1,2 @@
1
+ /** Runs main with process.argv when moduleUrl is the entry script, and exits with its code when not 0. */
2
+ export declare function runIfMain(moduleUrl: string, main: (argv: string[]) => number): void;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ for (const key of Object.keys(process.env))if (key.startsWith('GIT_')) delete process.env[key];
2
+ export { };
package/package.json CHANGED
@@ -1,14 +1,60 @@
1
1
  {
2
2
  "name": "@crown3/repo-rules",
3
- "version": "0.0.0",
4
- "description": "Placeholder for trusted publishing; the first real release follows.",
3
+ "version": "0.1.0",
4
+ "description": "Repo checks shared across repos: the comment policy, shell formatting, agent docs and where hooks and CI run each script, driven by one config file.",
5
5
  "license": "0BSD",
6
+ "type": "module",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+https://github.com/crown3/cc-origins.git",
9
10
  "directory": "packages/repo-rules"
10
11
  },
12
+ "keywords": [
13
+ "lint",
14
+ "comments",
15
+ "docs",
16
+ "hk",
17
+ "ci"
18
+ ],
19
+ "engines": {
20
+ "node": ">=24"
21
+ },
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "./scrub-git-hook-env": "./dist/scrub-git-hook-env.js"
28
+ },
29
+ "bin": {
30
+ "repo-rules": "bin/repo-rules.mjs"
31
+ },
32
+ "files": [
33
+ "bin",
34
+ "dist",
35
+ "README.md"
36
+ ],
37
+ "sideEffects": [
38
+ "./dist/scrub-git-hook-env.js"
39
+ ],
11
40
  "publishConfig": {
12
41
  "access": "public"
42
+ },
43
+ "dependencies": {
44
+ "@wasm-fmt/shfmt": "0.2.7",
45
+ "oxc-parser": "0.156.0",
46
+ "yaml": "2.9.1"
47
+ },
48
+ "devDependencies": {
49
+ "@rslib/core": "1.0.7",
50
+ "@rstest/core": "0.12.6",
51
+ "@types/node": "24.19.2",
52
+ "typescript": "7.0.2"
53
+ },
54
+ "scripts": {
55
+ "build": "rslib build",
56
+ "typecheck": "tsc -p . && tsc -p tsconfig.build.json --noEmit",
57
+ "test": "rstest",
58
+ "coverage": "rstest --coverage"
13
59
  }
14
- }
60
+ }