@houserules/payload 0.0.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 +30 -0
- package/package.json +87 -0
- package/payload-dist/scripts/lib/backlog-id.d.mts +13 -0
- package/payload-dist/scripts/lib/backlog-id.mjs +20 -0
- package/payload-dist/scripts/lib/comment-scan.d.mts +30 -0
- package/payload-dist/scripts/lib/comment-scan.mjs +244 -0
- package/payload-dist/scripts/lib/config.d.mts +139 -0
- package/payload-dist/scripts/lib/config.mjs +131 -0
- package/payload-dist/scripts/lib/entry-ledger.d.mts +240 -0
- package/payload-dist/scripts/lib/entry-ledger.mjs +545 -0
- package/payload-dist/scripts/lib/findings.d.mts +78 -0
- package/payload-dist/scripts/lib/findings.mjs +67 -0
- package/payload-dist/scripts/lib/ledger-index.d.mts +77 -0
- package/payload-dist/scripts/lib/ledger-index.mjs +100 -0
- package/payload-dist/scripts/lib/markdown-segment.d.mts +52 -0
- package/payload-dist/scripts/lib/markdown-segment.mjs +115 -0
- package/payload-dist/scripts/lib/proc.d.mts +33 -0
- package/payload-dist/scripts/lib/proc.mjs +93 -0
- package/payload-dist/scripts/lib/workspaces.d.mts +41 -0
- package/payload-dist/scripts/lib/workspaces.mjs +224 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** The two ledgers houserules keeps. */
|
|
2
|
+
export type LedgerKind = 'backlog' | 'decisions';
|
|
3
|
+
/**
|
|
4
|
+
* One ledger entry, fully resolved.
|
|
5
|
+
*
|
|
6
|
+
* The shape every reader works in, whichever half it came from. It lives here rather than beside
|
|
7
|
+
* the code that fetches it because three packages consume it and none of them is the one that
|
|
8
|
+
* talks to GitHub: `decision-log`, `backlog-log`, and this package's own `ledger-inject`. Core
|
|
9
|
+
* owning the FORMAT is core owning an interface, per `AGENTKIT-116e0c`, and the dependency arrow
|
|
10
|
+
* still points from plugin to core.
|
|
11
|
+
*
|
|
12
|
+
* A backlog entry leaves `scope`, `under`, `supersedes`, and `supersededBy` empty. A decision
|
|
13
|
+
* leaves `issue` null. `itemId` and `status` are empty for an entry that has not reached a board.
|
|
14
|
+
*/
|
|
15
|
+
export interface LedgerEntry {
|
|
16
|
+
id: string;
|
|
17
|
+
itemId: string;
|
|
18
|
+
issue: number | null;
|
|
19
|
+
title: string;
|
|
20
|
+
body: string;
|
|
21
|
+
surface: string;
|
|
22
|
+
date: string;
|
|
23
|
+
chat: string | null;
|
|
24
|
+
status: string | null;
|
|
25
|
+
scope: string[];
|
|
26
|
+
under: string | null;
|
|
27
|
+
supersedes: string[];
|
|
28
|
+
supersededBy: string | null;
|
|
29
|
+
}
|
|
30
|
+
export interface LedgerIndex {
|
|
31
|
+
version: number;
|
|
32
|
+
kind: LedgerKind;
|
|
33
|
+
/** ISO timestamp of the pull that produced this file, so staleness is visible. */
|
|
34
|
+
pulledAt: string;
|
|
35
|
+
/** The board numbers this was built from. A changed board set means the index is incomplete. */
|
|
36
|
+
projects: number[];
|
|
37
|
+
entries: LedgerEntry[];
|
|
38
|
+
}
|
|
39
|
+
/** The file one kind's index lives in, inside the ledger directory. */
|
|
40
|
+
export declare function indexBasename(kind: LedgerKind): string;
|
|
41
|
+
export declare function emptyIndex(kind: LedgerKind, pulledAt: string): LedgerIndex;
|
|
42
|
+
export declare function serializeIndex(index: LedgerIndex): string;
|
|
43
|
+
/**
|
|
44
|
+
* The index in `raw`, or null when it cannot be trusted.
|
|
45
|
+
*
|
|
46
|
+
* **Never throws, for any input.** `ledger-inject.mjs` reads this on every prompt as a
|
|
47
|
+
* `UserPromptSubmit` hook, and a hook that throws costs the user their turn. A truncated write, a
|
|
48
|
+
* hand-edit, or an index from a future version all return null, and null means "no cache", which
|
|
49
|
+
* every caller already has to handle for a fresh clone.
|
|
50
|
+
*/
|
|
51
|
+
export declare function parseIndex(raw: string, kind: LedgerKind): LedgerIndex | null;
|
|
52
|
+
/**
|
|
53
|
+
* The index for `kind` in `ledgerDirectory`, or null when there is not a usable one.
|
|
54
|
+
*
|
|
55
|
+
* **Never throws, for any state of the disk.** Null covers every failure the same way: no file, an
|
|
56
|
+
* unreadable file, a truncated write, a hand-edit, a version from the future. Callers already have
|
|
57
|
+
* to handle null for a fresh clone that has never pulled, so there is no second path to get wrong.
|
|
58
|
+
* `ledger-inject.mjs` calls this on every prompt, and a hook that throws costs the user their turn.
|
|
59
|
+
*
|
|
60
|
+
* This index is the other half of the queue-and-index split: the `.jsonl` ledgers hold what has
|
|
61
|
+
* not reached a board yet, and this index is a projection of the boards so `scope`, `show`, and
|
|
62
|
+
* prompt injection answer instantly and offline. It is a CACHE and never a record of truth.
|
|
63
|
+
* Deleting it loses nothing a `pull` cannot rebuild, so nothing that cannot be rebuilt from a
|
|
64
|
+
* board belongs in it. This function is the one read every consumer needs, kept here rather than
|
|
65
|
+
* copied into each of them. Writing the file stays the sync plugin's job.
|
|
66
|
+
*/
|
|
67
|
+
export declare function loadIndex(ledgerDirectory: string, kind: LedgerKind): LedgerIndex | null;
|
|
68
|
+
/** The entry for `id`, or null. The lookup prompt injection and `show` are built on. */
|
|
69
|
+
export declare function findEntry(index: LedgerIndex | null, id: string): LedgerEntry | null;
|
|
70
|
+
/**
|
|
71
|
+
* Merges an index with the queue's own entries, queue winning on any id in both.
|
|
72
|
+
*
|
|
73
|
+
* The queue is newer than the index by construction: it holds what has not been pushed, which
|
|
74
|
+
* includes edits to entries the board already has. An index-first merge would show a stale body
|
|
75
|
+
* for an entry someone just revised.
|
|
76
|
+
*/
|
|
77
|
+
export declare function mergeWithQueue(index: LedgerIndex | null, queued: readonly LedgerEntry[]): LedgerEntry[];
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Bumped whenever the stored shape changes.
|
|
5
|
+
*
|
|
6
|
+
* An index written by an older version is discarded rather than migrated. It costs one `pull` to
|
|
7
|
+
* rebuild and a migration path for a cache is work that buys nothing.
|
|
8
|
+
*/
|
|
9
|
+
const INDEX_VERSION = 1;
|
|
10
|
+
/** The file one kind's index lives in, inside the ledger directory. */
|
|
11
|
+
export function indexBasename(kind) {
|
|
12
|
+
return `${kind}.index.json`;
|
|
13
|
+
}
|
|
14
|
+
export function emptyIndex(kind, pulledAt) {
|
|
15
|
+
return { version: INDEX_VERSION, kind, pulledAt, projects: [], entries: [] };
|
|
16
|
+
}
|
|
17
|
+
export function serializeIndex(index) {
|
|
18
|
+
return `${JSON.stringify(index, null, 2)}\n`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The index in `raw`, or null when it cannot be trusted.
|
|
22
|
+
*
|
|
23
|
+
* **Never throws, for any input.** `ledger-inject.mjs` reads this on every prompt as a
|
|
24
|
+
* `UserPromptSubmit` hook, and a hook that throws costs the user their turn. A truncated write, a
|
|
25
|
+
* hand-edit, or an index from a future version all return null, and null means "no cache", which
|
|
26
|
+
* every caller already has to handle for a fresh clone.
|
|
27
|
+
*/
|
|
28
|
+
export function parseIndex(raw, kind) {
|
|
29
|
+
let parsed;
|
|
30
|
+
try {
|
|
31
|
+
parsed = JSON.parse(raw);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
37
|
+
return null;
|
|
38
|
+
const candidate = parsed;
|
|
39
|
+
if (candidate.version !== INDEX_VERSION)
|
|
40
|
+
return null;
|
|
41
|
+
if (candidate.kind !== kind)
|
|
42
|
+
return null;
|
|
43
|
+
if (!Array.isArray(candidate.entries))
|
|
44
|
+
return null;
|
|
45
|
+
return candidate;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The index for `kind` in `ledgerDirectory`, or null when there is not a usable one.
|
|
49
|
+
*
|
|
50
|
+
* **Never throws, for any state of the disk.** Null covers every failure the same way: no file, an
|
|
51
|
+
* unreadable file, a truncated write, a hand-edit, a version from the future. Callers already have
|
|
52
|
+
* to handle null for a fresh clone that has never pulled, so there is no second path to get wrong.
|
|
53
|
+
* `ledger-inject.mjs` calls this on every prompt, and a hook that throws costs the user their turn.
|
|
54
|
+
*
|
|
55
|
+
* This index is the other half of the queue-and-index split: the `.jsonl` ledgers hold what has
|
|
56
|
+
* not reached a board yet, and this index is a projection of the boards so `scope`, `show`, and
|
|
57
|
+
* prompt injection answer instantly and offline. It is a CACHE and never a record of truth.
|
|
58
|
+
* Deleting it loses nothing a `pull` cannot rebuild, so nothing that cannot be rebuilt from a
|
|
59
|
+
* board belongs in it. This function is the one read every consumer needs, kept here rather than
|
|
60
|
+
* copied into each of them. Writing the file stays the sync plugin's job.
|
|
61
|
+
*/
|
|
62
|
+
export function loadIndex(ledgerDirectory, kind) {
|
|
63
|
+
const path = resolve(ledgerDirectory, indexBasename(kind));
|
|
64
|
+
if (!existsSync(path))
|
|
65
|
+
return null;
|
|
66
|
+
try {
|
|
67
|
+
return parseIndex(readFileSync(path, 'utf8'), kind);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** The entry for `id`, or null. The lookup prompt injection and `show` are built on. */
|
|
74
|
+
export function findEntry(index, id) {
|
|
75
|
+
if (index === null)
|
|
76
|
+
return null;
|
|
77
|
+
return index.entries.find((entry) => entry.id === id) ?? null;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Merges an index with the queue's own entries, queue winning on any id in both.
|
|
81
|
+
*
|
|
82
|
+
* The queue is newer than the index by construction: it holds what has not been pushed, which
|
|
83
|
+
* includes edits to entries the board already has. An index-first merge would show a stale body
|
|
84
|
+
* for an entry someone just revised.
|
|
85
|
+
*/
|
|
86
|
+
export function mergeWithQueue(index, queued) {
|
|
87
|
+
if (index === null)
|
|
88
|
+
return [...queued];
|
|
89
|
+
const queuedById = new Map(queued.map((entry) => [entry.id, entry]));
|
|
90
|
+
const seenIds = new Set();
|
|
91
|
+
const merged = index.entries.map((entry) => {
|
|
92
|
+
seenIds.add(entry.id);
|
|
93
|
+
return queuedById.get(entry.id) ?? entry;
|
|
94
|
+
});
|
|
95
|
+
for (const entry of queued) {
|
|
96
|
+
if (!seenIds.has(entry.id))
|
|
97
|
+
merged.push(entry);
|
|
98
|
+
}
|
|
99
|
+
return merged;
|
|
100
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Separates markdown prose from code, so a prose checker never mistakes a code token for a
|
|
3
|
+
* voice violation.
|
|
4
|
+
*
|
|
5
|
+
* A checker built on a naive fence toggle (`` ``` `` flips a boolean, then a same-line
|
|
6
|
+
* backtick regex strips inline spans) measured 0% precision in `PROBE-regex.md`'s b1
|
|
7
|
+
* candidate: both of its two findings were false positives, one from an inline-code span
|
|
8
|
+
* that opens on one line and closes on the next (the toggle only matched a span with no
|
|
9
|
+
* embedded newline), the other from a blockquoted "before" example that the document quotes
|
|
10
|
+
* on purpose to forbid, not a violation of its own voice. This module fixes the first as a
|
|
11
|
+
* lexing bug and treats the second as a naming decision, documented below.
|
|
12
|
+
*
|
|
13
|
+
* DECISION: a blockquoted line (`^ {0,3}>`) is classified `'quoted'`, never `'prose'`. A
|
|
14
|
+
* checker built on `classifyLines` should skip `'quoted'` lines the same way it skips
|
|
15
|
+
* `'code'` ones. The cost: a genuine callout or admonition written with `>` (not a quoted
|
|
16
|
+
* bad example) also goes unchecked. That trade favors the false negative over the false
|
|
17
|
+
* positive, because `prose-voice.md` itself is the corpus that motivated this fix, and it
|
|
18
|
+
* blockquotes bad examples specifically to forbid them without committing them.
|
|
19
|
+
*/
|
|
20
|
+
export type LineKind = 'prose' | 'code' | 'quoted';
|
|
21
|
+
/**
|
|
22
|
+
* One `LineKind` per line of `markdown`, in `split('\n')` order.
|
|
23
|
+
*
|
|
24
|
+
* Fenced blocks win over everything else once opened, so a line that looks like a
|
|
25
|
+
* blockquote or an indented block inside a fence still reads as `'code'`. An unclosed fence
|
|
26
|
+
* runs `'code'` to end of file, since a reader has no later boundary to trust either.
|
|
27
|
+
*/
|
|
28
|
+
export declare function classifyLines(markdown: string): LineKind[];
|
|
29
|
+
/**
|
|
30
|
+
* Returns `markdown` with fenced blocks, indented code blocks, and inline spans replaced by
|
|
31
|
+
* filler of the same length, one line for one line.
|
|
32
|
+
*
|
|
33
|
+
* Line numbers of the surviving prose are unchanged, since a checker built on top reports
|
|
34
|
+
* `file:line` and a stripper that shifts a line number sends the reader to the wrong place.
|
|
35
|
+
* This does NOT strip blockquoted (`'quoted'`) lines. See the module doc for why, and use
|
|
36
|
+
* `classifyLines` to exclude them too if a caller wants that.
|
|
37
|
+
*/
|
|
38
|
+
export declare function stripCode(markdown: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* This is the function a prose checker should call. It returns `markdown` reduced to its
|
|
41
|
+
* surviving prose: `stripCode`'s fenced blocks, indented blocks, and inline spans, PLUS
|
|
42
|
+
* blockquoted (`'quoted'`) lines, all replaced by same-length or same-line-count filler.
|
|
43
|
+
* Line numbers are unchanged, same guarantee as `stripCode`.
|
|
44
|
+
*
|
|
45
|
+
* Calling `stripCode` alone reintroduces the blockquote false positive this module exists
|
|
46
|
+
* to eliminate: a document that quotes a bad example to forbid it, such as
|
|
47
|
+
* `prose-voice.md`'s own "Punctuation doing work that sentences should do" section, still
|
|
48
|
+
* contains the semicolon `stripCode` leaves behind. Reach for this function unless the
|
|
49
|
+
* caller has a genuine reason to treat blockquoted text as checkable prose, in which case
|
|
50
|
+
* use `classifyLines` and `stripCode` directly and encode that different policy explicitly.
|
|
51
|
+
*/
|
|
52
|
+
export declare function stripToProse(markdown: string): string;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Separates markdown prose from code, so a prose checker never mistakes a code token for a
|
|
3
|
+
* voice violation.
|
|
4
|
+
*
|
|
5
|
+
* A checker built on a naive fence toggle (`` ``` `` flips a boolean, then a same-line
|
|
6
|
+
* backtick regex strips inline spans) measured 0% precision in `PROBE-regex.md`'s b1
|
|
7
|
+
* candidate: both of its two findings were false positives, one from an inline-code span
|
|
8
|
+
* that opens on one line and closes on the next (the toggle only matched a span with no
|
|
9
|
+
* embedded newline), the other from a blockquoted "before" example that the document quotes
|
|
10
|
+
* on purpose to forbid, not a violation of its own voice. This module fixes the first as a
|
|
11
|
+
* lexing bug and treats the second as a naming decision, documented below.
|
|
12
|
+
*
|
|
13
|
+
* DECISION: a blockquoted line (`^ {0,3}>`) is classified `'quoted'`, never `'prose'`. A
|
|
14
|
+
* checker built on `classifyLines` should skip `'quoted'` lines the same way it skips
|
|
15
|
+
* `'code'` ones. The cost: a genuine callout or admonition written with `>` (not a quoted
|
|
16
|
+
* bad example) also goes unchecked. That trade favors the false negative over the false
|
|
17
|
+
* positive, because `prose-voice.md` itself is the corpus that motivated this fix, and it
|
|
18
|
+
* blockquotes bad examples specifically to forbid them without committing them.
|
|
19
|
+
*/
|
|
20
|
+
const FENCE_OPEN_RE = /^ {0,3}(`{3,}|~{3,})/;
|
|
21
|
+
const INDENTED_CODE_RE = /^(?: {4}|\t)/;
|
|
22
|
+
const BLOCKQUOTE_RE = /^ {0,3}>/;
|
|
23
|
+
/**
|
|
24
|
+
* One `LineKind` per line of `markdown`, in `split('\n')` order.
|
|
25
|
+
*
|
|
26
|
+
* Fenced blocks win over everything else once opened, so a line that looks like a
|
|
27
|
+
* blockquote or an indented block inside a fence still reads as `'code'`. An unclosed fence
|
|
28
|
+
* runs `'code'` to end of file, since a reader has no later boundary to trust either.
|
|
29
|
+
*/
|
|
30
|
+
export function classifyLines(markdown) {
|
|
31
|
+
const lines = markdown.split('\n');
|
|
32
|
+
const kinds = new Array(lines.length);
|
|
33
|
+
let fenceChar = null;
|
|
34
|
+
let fenceLen = 0;
|
|
35
|
+
for (let i = 0; i < lines.length; i++) {
|
|
36
|
+
const line = lines[i];
|
|
37
|
+
if (fenceChar) {
|
|
38
|
+
kinds[i] = 'code';
|
|
39
|
+
const closeRe = new RegExp(`^ {0,3}[${fenceChar}]{${fenceLen},}\\s*$`);
|
|
40
|
+
if (closeRe.test(line)) {
|
|
41
|
+
fenceChar = null;
|
|
42
|
+
fenceLen = 0;
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const open = line.match(FENCE_OPEN_RE);
|
|
47
|
+
if (open) {
|
|
48
|
+
kinds[i] = 'code';
|
|
49
|
+
fenceChar = open[1][0];
|
|
50
|
+
fenceLen = open[1].length;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (INDENTED_CODE_RE.test(line) && line.trim().length > 0) {
|
|
54
|
+
kinds[i] = 'code';
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (BLOCKQUOTE_RE.test(line)) {
|
|
58
|
+
kinds[i] = 'quoted';
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
kinds[i] = 'prose';
|
|
62
|
+
}
|
|
63
|
+
return kinds;
|
|
64
|
+
}
|
|
65
|
+
/** Replaces every non-newline character in `text` with a space, so line breaks survive. */
|
|
66
|
+
function blankKeepingNewlines(text) {
|
|
67
|
+
return text.replace(/[^\n]/g, ' ');
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A backtick run of any length opens an inline span, and only the SAME run length closes
|
|
71
|
+
* it, matching CommonMark's rule and letting `` `` code with a literal backtick `` ``
|
|
72
|
+
* nest safely. `[\s\S]*?` lets the span cross a line break, which is exactly the case the
|
|
73
|
+
* naive same-line regex in `PROBE-regex.md` missed.
|
|
74
|
+
*/
|
|
75
|
+
const INLINE_SPAN_RE = /(`+)([\s\S]*?)\1/g;
|
|
76
|
+
function stripInlineCode(text) {
|
|
77
|
+
return text.replace(INLINE_SPAN_RE, (match) => blankKeepingNewlines(match));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns `markdown` with fenced blocks, indented code blocks, and inline spans replaced by
|
|
81
|
+
* filler of the same length, one line for one line.
|
|
82
|
+
*
|
|
83
|
+
* Line numbers of the surviving prose are unchanged, since a checker built on top reports
|
|
84
|
+
* `file:line` and a stripper that shifts a line number sends the reader to the wrong place.
|
|
85
|
+
* This does NOT strip blockquoted (`'quoted'`) lines. See the module doc for why, and use
|
|
86
|
+
* `classifyLines` to exclude them too if a caller wants that.
|
|
87
|
+
*/
|
|
88
|
+
export function stripCode(markdown) {
|
|
89
|
+
const lines = markdown.split('\n');
|
|
90
|
+
const kinds = classifyLines(markdown);
|
|
91
|
+
const withCodeBlanked = lines
|
|
92
|
+
.map((line, i) => (kinds[i] === 'code' ? ' '.repeat(line.length) : line))
|
|
93
|
+
.join('\n');
|
|
94
|
+
return stripInlineCode(withCodeBlanked);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* This is the function a prose checker should call. It returns `markdown` reduced to its
|
|
98
|
+
* surviving prose: `stripCode`'s fenced blocks, indented blocks, and inline spans, PLUS
|
|
99
|
+
* blockquoted (`'quoted'`) lines, all replaced by same-length or same-line-count filler.
|
|
100
|
+
* Line numbers are unchanged, same guarantee as `stripCode`.
|
|
101
|
+
*
|
|
102
|
+
* Calling `stripCode` alone reintroduces the blockquote false positive this module exists
|
|
103
|
+
* to eliminate: a document that quotes a bad example to forbid it, such as
|
|
104
|
+
* `prose-voice.md`'s own "Punctuation doing work that sentences should do" section, still
|
|
105
|
+
* contains the semicolon `stripCode` leaves behind. Reach for this function unless the
|
|
106
|
+
* caller has a genuine reason to treat blockquoted text as checkable prose, in which case
|
|
107
|
+
* use `classifyLines` and `stripCode` directly and encode that different policy explicitly.
|
|
108
|
+
*/
|
|
109
|
+
export function stripToProse(markdown) {
|
|
110
|
+
const lines = stripCode(markdown).split('\n');
|
|
111
|
+
const kinds = classifyLines(markdown);
|
|
112
|
+
return lines
|
|
113
|
+
.map((line, i) => (kinds[i] === 'quoted' ? ' '.repeat(line.length) : line))
|
|
114
|
+
.join('\n');
|
|
115
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs git and returns its stdout, or null on any failure (not a repo, git absent,
|
|
3
|
+
* non-zero exit). Never throws.
|
|
4
|
+
*
|
|
5
|
+
* Returns RAW output — callers that parse `status --porcelain` depend on the leading
|
|
6
|
+
* two status characters, so a blanket trim here would corrupt the first line. Trim at
|
|
7
|
+
* the call site when you want a single value.
|
|
8
|
+
*/
|
|
9
|
+
export declare function git(root: string, args: string[]): string | null;
|
|
10
|
+
/**
|
|
11
|
+
* The repo root, memoized for the life of the process.
|
|
12
|
+
*
|
|
13
|
+
* The memo is the point: `loadConfigSafe()` resolves the root internally and the hook
|
|
14
|
+
* calling it resolved the root too, so every hook invocation spawned
|
|
15
|
+
* `git rev-parse --show-toplevel` twice. That is one wasted process per hook, on every
|
|
16
|
+
* session start and every turn end.
|
|
17
|
+
*
|
|
18
|
+
* Falls back to `process.cwd()` when git can't answer, so a hook outside a repo still runs.
|
|
19
|
+
*/
|
|
20
|
+
export declare function repoRoot(): string;
|
|
21
|
+
/**
|
|
22
|
+
* The hook payload Claude Code writes to stdin, or `{}` when there is none or it is
|
|
23
|
+
* malformed. A hook must run even when invoked by hand with no stdin.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readStdinJson<T = Record<string, unknown>>(): T;
|
|
26
|
+
/** The last `n` lines of `text` — used to cap command output in reports. */
|
|
27
|
+
export declare function tail(text: string, n: number): string;
|
|
28
|
+
/**
|
|
29
|
+
* A path glob compiled to an anchored RegExp: `*` stays within one segment, `**` spans
|
|
30
|
+
* separators — and when a doublestar is followed by a slash it also matches zero
|
|
31
|
+
* directories — `?` is one non-separator character. Every other metacharacter is literal.
|
|
32
|
+
*/
|
|
33
|
+
export declare function globToRe(glob: string): RegExp;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { execFileSync, execSync } from 'node:child_process';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
/**
|
|
4
|
+
* Runs git and returns its stdout, or null on any failure (not a repo, git absent,
|
|
5
|
+
* non-zero exit). Never throws.
|
|
6
|
+
*
|
|
7
|
+
* Returns RAW output — callers that parse `status --porcelain` depend on the leading
|
|
8
|
+
* two status characters, so a blanket trim here would corrupt the first line. Trim at
|
|
9
|
+
* the call site when you want a single value.
|
|
10
|
+
*/
|
|
11
|
+
export function git(root, args) {
|
|
12
|
+
try {
|
|
13
|
+
return execFileSync('git', args, {
|
|
14
|
+
cwd: root,
|
|
15
|
+
encoding: 'utf8',
|
|
16
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
let cachedRoot;
|
|
24
|
+
/**
|
|
25
|
+
* The repo root, memoized for the life of the process.
|
|
26
|
+
*
|
|
27
|
+
* The memo is the point: `loadConfigSafe()` resolves the root internally and the hook
|
|
28
|
+
* calling it resolved the root too, so every hook invocation spawned
|
|
29
|
+
* `git rev-parse --show-toplevel` twice. That is one wasted process per hook, on every
|
|
30
|
+
* session start and every turn end.
|
|
31
|
+
*
|
|
32
|
+
* Falls back to `process.cwd()` when git can't answer, so a hook outside a repo still runs.
|
|
33
|
+
*/
|
|
34
|
+
export function repoRoot() {
|
|
35
|
+
if (cachedRoot === undefined) {
|
|
36
|
+
try {
|
|
37
|
+
cachedRoot = execSync('git rev-parse --show-toplevel', {
|
|
38
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
39
|
+
})
|
|
40
|
+
.toString()
|
|
41
|
+
.trim();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
cachedRoot = null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return cachedRoot || process.cwd();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The hook payload Claude Code writes to stdin, or `{}` when there is none or it is
|
|
51
|
+
* malformed. A hook must run even when invoked by hand with no stdin.
|
|
52
|
+
*/
|
|
53
|
+
export function readStdinJson() {
|
|
54
|
+
try {
|
|
55
|
+
return JSON.parse(readFileSync(0, 'utf8') || '{}');
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** The last `n` lines of `text` — used to cap command output in reports. */
|
|
62
|
+
export function tail(text, n) {
|
|
63
|
+
const lines = text.split('\n');
|
|
64
|
+
return lines.length <= n ? text : lines.slice(-n).join('\n');
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A path glob compiled to an anchored RegExp: `*` stays within one segment, `**` spans
|
|
68
|
+
* separators — and when a doublestar is followed by a slash it also matches zero
|
|
69
|
+
* directories — `?` is one non-separator character. Every other metacharacter is literal.
|
|
70
|
+
*/
|
|
71
|
+
export function globToRe(glob) {
|
|
72
|
+
let re = '';
|
|
73
|
+
for (let i = 0; i < glob.length; i++) {
|
|
74
|
+
const c = glob[i];
|
|
75
|
+
if (c === '*') {
|
|
76
|
+
if (glob[i + 1] === '*') {
|
|
77
|
+
re += '.*';
|
|
78
|
+
i++;
|
|
79
|
+
if (glob[i + 1] === '/')
|
|
80
|
+
i++; // `**/` also matches zero dirs
|
|
81
|
+
}
|
|
82
|
+
else
|
|
83
|
+
re += '[^/]*';
|
|
84
|
+
}
|
|
85
|
+
else if ('.+^${}()|[]\\'.includes(c))
|
|
86
|
+
re += `\\${c}`;
|
|
87
|
+
else if (c === '?')
|
|
88
|
+
re += '[^/]';
|
|
89
|
+
else
|
|
90
|
+
re += c;
|
|
91
|
+
}
|
|
92
|
+
return new RegExp(`^${re}$`);
|
|
93
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { PackageJson } from 'type-fest';
|
|
2
|
+
export type { PackageJson };
|
|
3
|
+
export interface WorkspacePackage {
|
|
4
|
+
name: string;
|
|
5
|
+
/** Absolute path to the package directory. */
|
|
6
|
+
dir: string;
|
|
7
|
+
/** Package directory relative to the repo root, forward-slashed. */
|
|
8
|
+
relDir: string;
|
|
9
|
+
pkg: PackageJson;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Parsed JSON, or null when the file is missing or unparseable. Defaults to
|
|
13
|
+
* `PackageJson` because that is the overwhelming caller; pass `T` for the others.
|
|
14
|
+
*/
|
|
15
|
+
export declare function readJson<T = PackageJson>(path: string): T | null;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts the `packages:` list from pnpm-workspace.yaml. Hand-rolled on purpose: the
|
|
18
|
+
* payload ships into user repos and runs on bare node, so it cannot import a YAML
|
|
19
|
+
* library. This is not a YAML parser. It reads one key in the shapes real workspace files
|
|
20
|
+
* use, and never throws. `__test__/workspaces.test.ts` pins that contract case by case.
|
|
21
|
+
*
|
|
22
|
+
* Sibling top-level blocks such as `catalog:` and `catalogMode:` must not confuse it, so
|
|
23
|
+
* only lines inside the packages block are read and the block ends at the next
|
|
24
|
+
* non-indented line. Both YAML list forms occur in the wild and both are read, block
|
|
25
|
+
* sequences and flow sequences, and a flow sequence may span lines.
|
|
26
|
+
*/
|
|
27
|
+
export declare function parsePnpmWorkspaceGlobs(yamlText: string): string[];
|
|
28
|
+
/**
|
|
29
|
+
* Every workspace member that has a named package.json. Empty scaffold directories
|
|
30
|
+
* holding only a `.gitkeep` are silently skipped. Shared by the payload scripts and by
|
|
31
|
+
* the installer's detection, so validation and detection can never disagree.
|
|
32
|
+
*
|
|
33
|
+
* Negated globs are exclusions rather than patterns to expand, so they filter the set the
|
|
34
|
+
* positive globs produced. That is why they are applied here rather than in `expandGlob`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function listWorkspacePackages(root: string): WorkspacePackage[];
|
|
37
|
+
/**
|
|
38
|
+
* Every valid `--pkg` name for changeset-write: the workspace members, or for a
|
|
39
|
+
* single-package repo the root package itself.
|
|
40
|
+
*/
|
|
41
|
+
export declare function listPublishablePackageNames(root: string): string[];
|