Repository navigation
Expand file tree
/
Copy pathdesign-docs.ts
More file actions
282 lines (250 loc) · 8.07 KB
/
Copy pathdesign-docs.ts
File metadata and controls
282 lines (250 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env bun
import { statSync } from "node:fs";
import { isAbsolute, relative, resolve, sep } from "node:path";
export interface DesignDocument {
path: string;
patterns: string[];
}
export interface DesignMatch {
document: DesignDocument;
matches: Array<{ target: string; pattern: string }>;
}
interface CliOptions {
all: boolean;
changed: boolean;
explain: boolean;
help: boolean;
paths: string[];
}
const usage = `Usage: bun design-docs [options] [path ...]
Find design documents applicable to project files or directories.
Options:
--changed Match all staged, unstaged, and untracked Git changes
--all List every design document
--explain Show which paths and patterns caused each match
-h, --help Show this help
Examples:
bun design-docs inference/crates/icn-engine/src/scheduler.rs
bun design-docs inference/crates/icn-engine
bun design-docs --changed
bun design-docs --all`;
function decode(output: Uint8Array): string {
return new TextDecoder().decode(output);
}
function runGit(root: string, args: string[]): string {
const result = Bun.spawnSync(["git", ...args], {
cwd: root,
stdout: "pipe",
stderr: "pipe",
});
if (result.exitCode !== 0) {
const detail = decode(result.stderr).trim();
throw new Error(detail || `git ${args.join(" ")} failed`);
}
return decode(result.stdout);
}
function nulPaths(output: string): string[] {
return output
.split("\0")
.filter(Boolean)
.map((path) => path.replaceAll("\\", "/"));
}
export function findProjectRoot(cwd = process.cwd()): string {
return runGit(cwd, ["rev-parse", "--show-toplevel"]).trim();
}
export function parseDesignDocument(path: string, source: string): DesignDocument {
const frontMatter = source.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/);
if (!frontMatter) {
throw new Error(`${path}: missing YAML front matter`);
}
let parsed: unknown;
try {
parsed = Bun.YAML.parse(frontMatter[1]);
} catch (error) {
throw new Error(`${path}: invalid YAML front matter: ${String(error)}`);
}
const appliesTo =
parsed && typeof parsed === "object" && "applies_to" in parsed
? (parsed as { applies_to?: unknown }).applies_to
: undefined;
if (!Array.isArray(appliesTo) || appliesTo.length === 0) {
throw new Error(`${path}: applies_to must be a non-empty list`);
}
const patterns = appliesTo.map((pattern, index) => {
if (typeof pattern !== "string" || pattern.length === 0) {
throw new Error(`${path}: applies_to[${index}] must be a non-empty string`);
}
if (isAbsolute(pattern) || pattern.startsWith("../") || pattern.includes("\\")) {
throw new Error(
`${path}: applies_to[${index}] must be project-root-relative and use forward slashes`,
);
}
try {
new Bun.Glob(pattern);
} catch (error) {
throw new Error(`${path}: invalid applies_to glob ${JSON.stringify(pattern)}: ${String(error)}`);
}
return pattern;
});
return { path, patterns };
}
export async function loadDesignDocuments(root: string): Promise<DesignDocument[]> {
const paths = Array.from(
new Bun.Glob("design/**/*.md").scanSync({ cwd: root, onlyFiles: true }),
)
.map((path) => path.replaceAll("\\", "/"))
.filter((path) => !path.endsWith("/AGENTS.md"))
.sort();
return Promise.all(
paths.map(async (path) => parseDesignDocument(path, await Bun.file(resolve(root, path)).text())),
);
}
export function normalizeProjectPath(root: string, input: string): string {
const absolute = resolve(root, input);
const projectRelative = relative(root, absolute);
if (projectRelative === ".." || projectRelative.startsWith(`..${sep}`) || isAbsolute(projectRelative)) {
throw new Error(`path is outside the project root: ${input}`);
}
return projectRelative.replaceAll(sep, "/") || ".";
}
function gitPaths(root: string, args: string[]): string[] {
return nulPaths(runGit(root, args));
}
export function collectChangedPaths(root: string): string[] {
return Array.from(
new Set([
...gitPaths(root, ["diff", "--name-only", "-z", "--"]),
...gitPaths(root, ["diff", "--cached", "--name-only", "-z", "--"]),
...gitPaths(root, ["ls-files", "--others", "--exclude-standard", "-z", "--"]),
]),
).sort();
}
export function expandInputPaths(root: string, inputs: string[]): string[] {
const targets = new Set<string>();
for (const input of inputs) {
const path = normalizeProjectPath(root, input);
const absolute = resolve(root, path);
let directory = false;
try {
directory = statSync(absolute).isDirectory();
} catch {
// A missing path may still describe a planned or deleted file and is matched directly.
}
if (!directory) {
targets.add(path);
continue;
}
targets.add(path === "." ? "." : `${path}/`);
const pathspec = path === "." ? "." : path;
for (const child of gitPaths(root, ["ls-files", "-co", "--exclude-standard", "-z", "--", pathspec])) {
targets.add(child);
}
}
return Array.from(targets).sort();
}
export function matchDesignDocuments(
documents: DesignDocument[],
targets: string[],
): DesignMatch[] {
const targetSet = new Set(targets);
return documents
.map((document): DesignMatch => {
const matches: DesignMatch["matches"] = [];
if (targetSet.has(document.path)) {
matches.push({ target: document.path, pattern: "(document itself)" });
}
for (const pattern of document.patterns) {
const glob = new Bun.Glob(pattern);
for (const target of targets) {
if (glob.match(target)) {
matches.push({ target, pattern });
}
}
}
return { document, matches };
})
.filter((match) => match.matches.length > 0)
.sort((left, right) => left.document.path.localeCompare(right.document.path));
}
function parseArgs(args: string[]): CliOptions {
const options: CliOptions = {
all: false,
changed: false,
explain: false,
help: false,
paths: [],
};
let positionalOnly = false;
for (const arg of args) {
if (positionalOnly) {
options.paths.push(arg);
} else if (arg === "--") {
positionalOnly = true;
} else if (arg === "--all") {
options.all = true;
} else if (arg === "--changed") {
options.changed = true;
} else if (arg === "--explain") {
options.explain = true;
} else if (arg === "--help" || arg === "-h") {
options.help = true;
} else if (arg.startsWith("-")) {
throw new Error(`unknown option: ${arg}`);
} else {
options.paths.push(arg);
}
}
return options;
}
export async function main(args = process.argv.slice(2)): Promise<number> {
let options: CliOptions;
try {
options = parseArgs(args);
} catch (error) {
console.error(String(error instanceof Error ? error.message : error));
console.error(usage);
return 2;
}
if (options.help) {
console.log(usage);
return 0;
}
const selectorCount = Number(options.all) + Number(options.changed) + Number(options.paths.length > 0);
if (selectorCount !== 1) {
console.error("provide paths, --changed, or --all (exactly one selection mode)");
console.error(usage);
return 2;
}
try {
const root = findProjectRoot();
const documents = await loadDesignDocuments(root);
if (options.all) {
for (const document of documents) {
console.log(document.path);
if (options.explain) {
console.log(" selected by --all");
}
}
return 0;
}
const targets = options.changed
? collectChangedPaths(root)
: expandInputPaths(root, options.paths);
for (const match of matchDesignDocuments(documents, targets)) {
console.log(match.document.path);
if (options.explain) {
for (const reason of match.matches) {
console.log(` matched ${reason.target}`);
console.log(` via ${reason.pattern}`);
}
}
}
return 0;
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
return 1;
}
}
if (import.meta.main) {
process.exitCode = await main();
}