Setting the file. One moment. List Blocks · Wp Block Development · WordPress/agent-skills · Skills Docsscripts/list_blocks.mjs
JavaScript·120 lines·3 KB
11
".next"
,
12 ".turbo",
13]);
14
15function statSafe(p) {
16 try {
17 return fs.statSync(p);
18 } catch {
19 return null;
20 }
21}
22
23function existsDir(p) {
24 const st = statSafe(p);
25 return Boolean(st && st.isDirectory());
26}
27
28function readJsonSafe(p) {
29 try {
30 return JSON.parse(fs.readFileSync(p, "utf8"));
31 } catch {
32 return null;
33 }
34}
35
36function findFilesRecursive(repoRoot, predicate, { maxFiles = 6000, maxDepth = 10 } = {}) {
37 const results = [];
38 const queue = [{ dir: repoRoot, depth: 0 }];
39 let visited = 0;
40
41 while (queue.length > 0) {
42 const { dir, depth } = queue.shift();
43 if (depth > maxDepth) continue;
44
45 let entries;
46 try {
47 entries = fs.readdirSync(dir, { withFileTypes: true });
48 } catch {
49 continue;
50 }
51
52 for (const ent of entries) {
53 const fullPath = path.join(dir, ent.name);
54 if (ent.isDirectory()) {
55 if (DEFAULT_IGNORES.has(ent.name)) continue;
56 queue.push({ dir: fullPath, depth: depth + 1 });
57 continue;
58 }
59 if (!ent.isFile()) continue;
60
61 visited += 1;
62 if (visited > maxFiles) return { results, truncated: true };
63 if (predicate(fullPath)) results.push(fullPath);
64 }
65 }
66
67 return { results, truncated: false };
68}
69
70function summarizeBlockJson(repoRoot, blockJsonPath) {
71 const json = readJsonSafe(blockJsonPath);
72 if (!json) {
73 return {
74 path: path.relative(repoRoot, blockJsonPath),
75 error: "invalid-json",
76 };
77 }
78
79 const rel = path.relative(repoRoot, blockJsonPath);
80 const blockRoot = path.dirname(rel);
81
82 return {
83 path: rel,
84 blockRoot,
85 name: typeof json?.name === "string" ? json.name : null,
86 title: typeof json?.title === "string" ? json.title : null,
87 apiVersion: typeof json?.apiVersion === "number" ? json.apiVersion : null,
88 render: typeof json?.render === "string" ? json.render : null,
89 viewScript: json?.viewScript ?? null,
90 viewScriptModule: json?.viewScriptModule ?? null,
91 editorScript: json?.editorScript ?? null,
92 script: json?.script ?? null,
93 style: json?.style ?? null,
94 editorStyle: json?.editorStyle ?? null,
95 attributes: json?.attributes ? Object.keys(json.attributes).slice(0, 50) : [],
96 };
97}
98
99function main() {
100 const repoRoot = process.cwd();
101
102 const { results: blockJsonFiles, truncated } = findFilesRecursive(repoRoot, (p) => path.basename(p) === "block.json", {
103 maxFiles: 8000,
104 maxDepth: 12,
105 });
106
107 const blocks = blockJsonFiles.map((p) => summarizeBlockJson(repoRoot, p));
108
109 const report = {
110 tool: { name: "list_blocks", version: "0.1.0" },
111 repoRoot,
112 truncated,
113 count: blocks.length,
114 blocks,
115 };
116
117 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
118}
119
120main();
121