pgserve 0.1.1

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.
Files changed (158) hide show
  1. package/.genie/AGENTS.md +13 -0
  2. package/.genie/agents/README.md +110 -0
  3. package/.genie/agents/analyze.md +176 -0
  4. package/.genie/agents/forge.md +290 -0
  5. package/.genie/agents/garbage-cleaner.md +324 -0
  6. package/.genie/agents/garbage-collector.md +596 -0
  7. package/.genie/agents/github-issue-gc.md +618 -0
  8. package/.genie/agents/review.md +380 -0
  9. package/.genie/agents/semantic-analyzer/find-duplicates.md +90 -0
  10. package/.genie/agents/semantic-analyzer/find-orphans.md +99 -0
  11. package/.genie/agents/semantic-analyzer.md +101 -0
  12. package/.genie/agents/update.md +182 -0
  13. package/.genie/agents/wish.md +357 -0
  14. package/.genie/code/AGENTS.md +692 -0
  15. package/.genie/code/agents/audit/risk.md +173 -0
  16. package/.genie/code/agents/audit/security.md +189 -0
  17. package/.genie/code/agents/audit.md +145 -0
  18. package/.genie/code/agents/challenge.md +230 -0
  19. package/.genie/code/agents/change-reviewer.md +295 -0
  20. package/.genie/code/agents/code-garbage-collector.md +425 -0
  21. package/.genie/code/agents/code-quality.md +410 -0
  22. package/.genie/code/agents/commit-suggester.md +255 -0
  23. package/.genie/code/agents/commit.md +124 -0
  24. package/.genie/code/agents/consensus.md +204 -0
  25. package/.genie/code/agents/daily-standup.md +722 -0
  26. package/.genie/code/agents/docgen.md +48 -0
  27. package/.genie/code/agents/explore.md +79 -0
  28. package/.genie/code/agents/fix.md +100 -0
  29. package/.genie/code/agents/git/commit-advisory.md +219 -0
  30. package/.genie/code/agents/git/workflows/issue.md +244 -0
  31. package/.genie/code/agents/git/workflows/pr.md +179 -0
  32. package/.genie/code/agents/git/workflows/release.md +460 -0
  33. package/.genie/code/agents/git/workflows/report.md +342 -0
  34. package/.genie/code/agents/git.md +432 -0
  35. package/.genie/code/agents/implementor.md +161 -0
  36. package/.genie/code/agents/install.md +515 -0
  37. package/.genie/code/agents/issue-creator.md +344 -0
  38. package/.genie/code/agents/polish.md +116 -0
  39. package/.genie/code/agents/qa.md +653 -0
  40. package/.genie/code/agents/refactor.md +294 -0
  41. package/.genie/code/agents/release.md +1129 -0
  42. package/.genie/code/agents/roadmap.md +885 -0
  43. package/.genie/code/agents/tests.md +557 -0
  44. package/.genie/code/agents/tracer.md +50 -0
  45. package/.genie/code/agents/update/upstream-update.md +85 -0
  46. package/.genie/code/agents/update/versions/generic-update.md +305 -0
  47. package/.genie/code/agents/vibe.md +1317 -0
  48. package/.genie/code/spells/agent-configuration.md +58 -0
  49. package/.genie/code/spells/automated-rc-publishing.md +106 -0
  50. package/.genie/code/spells/branch-tracker-guidance.md +28 -0
  51. package/.genie/code/spells/debug.md +320 -0
  52. package/.genie/code/spells/emoji-naming-convention.md +303 -0
  53. package/.genie/code/spells/evidence-storage.md +26 -0
  54. package/.genie/code/spells/file-naming-rules.md +35 -0
  55. package/.genie/code/spells/forge-code-blueprints.md +195 -0
  56. package/.genie/code/spells/genie-integration.md +153 -0
  57. package/.genie/code/spells/publishing-protocol.md +61 -0
  58. package/.genie/code/spells/team-consultation-protocol.md +284 -0
  59. package/.genie/code/spells/tool-requirements.md +20 -0
  60. package/.genie/code/spells/triad-maintenance-protocol.md +154 -0
  61. package/.genie/code/teams/tech-council/council.md +328 -0
  62. package/.genie/code/teams/tech-council/jt.md +352 -0
  63. package/.genie/code/teams/tech-council/nayr.md +305 -0
  64. package/.genie/code/teams/tech-council/oettam.md +375 -0
  65. package/.genie/neurons/README.md +193 -0
  66. package/.genie/neurons/forge.md +106 -0
  67. package/.genie/neurons/genie.md +63 -0
  68. package/.genie/neurons/review.md +106 -0
  69. package/.genie/neurons/wish.md +104 -0
  70. package/.genie/product/README.md +20 -0
  71. package/.genie/product/cli-automation.md +359 -0
  72. package/.genie/product/environment.md +60 -0
  73. package/.genie/product/mission.md +60 -0
  74. package/.genie/product/roadmap.md +44 -0
  75. package/.genie/product/tech-stack.md +34 -0
  76. package/.genie/product/templates/context-template.md +218 -0
  77. package/.genie/product/templates/qa-done-report-template.md +68 -0
  78. package/.genie/product/templates/review-report-template.md +89 -0
  79. package/.genie/product/templates/wish-template.md +120 -0
  80. package/.genie/scripts/helpers/analyze-commit.js +195 -0
  81. package/.genie/scripts/helpers/bullet-counter.js +194 -0
  82. package/.genie/scripts/helpers/bullet-find.js +289 -0
  83. package/.genie/scripts/helpers/bullet-id.js +244 -0
  84. package/.genie/scripts/helpers/check-secrets.js +237 -0
  85. package/.genie/scripts/helpers/count-tokens.js +200 -0
  86. package/.genie/scripts/helpers/create-frontmatter.js +456 -0
  87. package/.genie/scripts/helpers/detect-markers.js +293 -0
  88. package/.genie/scripts/helpers/detect-todos.js +267 -0
  89. package/.genie/scripts/helpers/detect-unlabeled-blocks.js +135 -0
  90. package/.genie/scripts/helpers/embeddings.js +344 -0
  91. package/.genie/scripts/helpers/find-empty-sections.js +158 -0
  92. package/.genie/scripts/helpers/index.js +319 -0
  93. package/.genie/scripts/helpers/validate-frontmatter.js +578 -0
  94. package/.genie/scripts/helpers/validate-links.js +207 -0
  95. package/.genie/scripts/helpers/validate-paths.js +373 -0
  96. package/.genie/spells/README.md +9 -0
  97. package/.genie/spells/ace-protocol.md +118 -0
  98. package/.genie/spells/ask-one-at-a-time.md +175 -0
  99. package/.genie/spells/backup-analyzer.md +542 -0
  100. package/.genie/spells/blocker.md +12 -0
  101. package/.genie/spells/break-things-move-fast.md +56 -0
  102. package/.genie/spells/context-candidates.md +72 -0
  103. package/.genie/spells/context-critic.md +51 -0
  104. package/.genie/spells/defer-to-expertise.md +278 -0
  105. package/.genie/spells/delegate-dont-do.md +292 -0
  106. package/.genie/spells/error-investigation-protocol.md +328 -0
  107. package/.genie/spells/evidence-based-completion.md +273 -0
  108. package/.genie/spells/experiment.md +65 -0
  109. package/.genie/spells/file-creation-protocol.md +229 -0
  110. package/.genie/spells/forge-integration.md +281 -0
  111. package/.genie/spells/forge-orchestration.md +514 -0
  112. package/.genie/spells/gather-context.md +18 -0
  113. package/.genie/spells/global-health-check.md +34 -0
  114. package/.genie/spells/global-noop-roundtrip.md +25 -0
  115. package/.genie/spells/install-genie.md +1232 -0
  116. package/.genie/spells/install.md +82 -0
  117. package/.genie/spells/investigate-before-commit.md +112 -0
  118. package/.genie/spells/know-yourself.md +288 -0
  119. package/.genie/spells/learn.md +828 -0
  120. package/.genie/spells/mcp-diagnostic-protocol.md +246 -0
  121. package/.genie/spells/mcp-first.md +124 -0
  122. package/.genie/spells/multi-step-execution.md +67 -0
  123. package/.genie/spells/orchestration-boundary-protocol.md +256 -0
  124. package/.genie/spells/orchestrator-not-implementor.md +189 -0
  125. package/.genie/spells/prompt.md +746 -0
  126. package/.genie/spells/reflect.md +404 -0
  127. package/.genie/spells/routing-decision-matrix.md +368 -0
  128. package/.genie/spells/run-in-parallel.md +12 -0
  129. package/.genie/spells/session-state-updater-example.md +196 -0
  130. package/.genie/spells/session-state-updater.md +220 -0
  131. package/.genie/spells/track-long-running-tasks.md +133 -0
  132. package/.genie/spells/troubleshoot-infrastructure.md +176 -0
  133. package/.genie/spells/upgrade-genie.md +415 -0
  134. package/.genie/spells/url-presentation-protocol.md +301 -0
  135. package/.genie/spells/wish-initiation.md +158 -0
  136. package/.genie/spells/wish-issue-linkage.md +410 -0
  137. package/.genie/spells/wish-lifecycle.md +100 -0
  138. package/.genie/state/provider-status.json +3 -0
  139. package/.genie/state/version.json +16 -0
  140. package/AGENTS.md +422 -0
  141. package/CLAUDE.md +1 -0
  142. package/LICENSE +21 -0
  143. package/Makefile +235 -0
  144. package/README.md +323 -0
  145. package/bin/pglite-server.js +457 -0
  146. package/ecosystem.config.cjs +23 -0
  147. package/examples/multi-tenant-demo.js +104 -0
  148. package/package.json +47 -0
  149. package/src/detector.js +105 -0
  150. package/src/index.js +177 -0
  151. package/src/pool.js +320 -0
  152. package/src/ports.js +114 -0
  153. package/src/protocol.js +216 -0
  154. package/src/registry.js +134 -0
  155. package/src/router.js +289 -0
  156. package/src/server.js +265 -0
  157. package/tests/benchmarks/runner.js +489 -0
  158. package/tests/multi-tenant.test.js +201 -0
@@ -0,0 +1,344 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Embeddings Helper - Learning Deduplication
5
+ *
6
+ * Checks if new learning already exists in target section.
7
+ * Uses transformers.js with all-MiniLM-L6-v2 (85MB, CPU-only).
8
+ *
9
+ * Usage:
10
+ * genie helper embeddings "new learning text" file.md "Section Name"
11
+ *
12
+ * Output: Top matches with similarity scores and recommendations
13
+ * 0.85+ = DUPLICATE (merge or skip)
14
+ * 0.70-0.85 = RELATED (evaluate carefully)
15
+ * <0.70 = DIFFERENT (safe to append)
16
+ */
17
+
18
+ const fs = require('fs');
19
+ const path = require('path');
20
+ const crypto = require('crypto');
21
+
22
+ // Lazy load transformers (only when needed)
23
+ let pipeline = null;
24
+ let embedder = null;
25
+
26
+ async function initEmbedder() {
27
+ if (!embedder) {
28
+ const { pipeline: pipelineImport } = await import('@xenova/transformers');
29
+ pipeline = pipelineImport;
30
+
31
+ // Use all-MiniLM-L6-v2 for sentence embeddings
32
+ // Show progress indicator on first download (85MB model)
33
+ console.error('⏳ Loading embedding model (first time: downloads 85MB)...');
34
+ embedder = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', {
35
+ progress_callback: (progress) => {
36
+ if (progress.status === 'downloading' && progress.progress !== undefined) {
37
+ const percent = Math.round(progress.progress);
38
+ if (percent % 10 === 0) { // Log every 10%
39
+ console.error(` Downloading: ${percent}%`);
40
+ }
41
+ }
42
+ }
43
+ });
44
+ console.error('✅ Model loaded!\n');
45
+ }
46
+ return embedder;
47
+ }
48
+
49
+ /**
50
+ * Compute cosine similarity between two vectors
51
+ */
52
+ function cosineSimilarity(vecA, vecB) {
53
+ let dotProduct = 0;
54
+ let normA = 0;
55
+ let normB = 0;
56
+
57
+ for (let i = 0; i < vecA.length; i++) {
58
+ dotProduct += vecA[i] * vecB[i];
59
+ normA += vecA[i] * vecA[i];
60
+ normB += vecB[i] * vecB[i];
61
+ }
62
+
63
+ return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
64
+ }
65
+
66
+ /**
67
+ * Get embedding for text
68
+ */
69
+ async function getEmbedding(text) {
70
+ const model = await initEmbedder();
71
+ const output = await model(text, { pooling: 'mean', normalize: true });
72
+ return Array.from(output.data);
73
+ }
74
+
75
+ /**
76
+ * Validate that file path is within workspace (security)
77
+ */
78
+ function validateFilePath(filePath) {
79
+ const absPath = path.resolve(filePath);
80
+ const workspaceRoot = path.resolve(process.cwd());
81
+
82
+ if (!absPath.startsWith(workspaceRoot)) {
83
+ throw new Error(`Security: Path outside workspace not allowed: ${filePath}`);
84
+ }
85
+
86
+ return absPath;
87
+ }
88
+
89
+ /**
90
+ * Extract section content from markdown file
91
+ * @param {string} fileContent - File content (already read)
92
+ * @param {string} sectionName - Section header to extract
93
+ */
94
+ function extractSection(fileContent, sectionName) {
95
+ const lines = fileContent.split('\n');
96
+
97
+ const sectionLines = [];
98
+ let inSection = false;
99
+ let sectionLevel = 0;
100
+
101
+ for (let i = 0; i < lines.length; i++) {
102
+ const line = lines[i];
103
+ const headerMatch = line.match(/^(#{1,6})\s+(.+)$/); // Added $ anchor
104
+
105
+ if (headerMatch) {
106
+ const level = headerMatch[1].length;
107
+ const title = headerMatch[2].trim();
108
+
109
+ if (title.includes(sectionName) || title === sectionName) {
110
+ inSection = true;
111
+ sectionLevel = level;
112
+ continue;
113
+ } else if (inSection && level <= sectionLevel) {
114
+ // Hit next section at same/higher level, stop
115
+ break;
116
+ }
117
+ }
118
+
119
+ if (inSection && line.trim()) {
120
+ // Skip code blocks and markdown formatting
121
+ if (!line.startsWith('```') && !line.startsWith('---')) {
122
+ sectionLines.push({ text: line.trim(), line: i + 1 });
123
+ }
124
+ }
125
+ }
126
+
127
+ return sectionLines;
128
+ }
129
+
130
+ /**
131
+ * Get cache path for file + section
132
+ */
133
+ function getCachePath(filePath, sectionName) {
134
+ const hash = crypto.createHash('md5')
135
+ .update(filePath + ':' + sectionName)
136
+ .digest('hex');
137
+
138
+ const cacheDir = path.join(process.cwd(), '.genie', '.cache', 'embeddings');
139
+ if (!fs.existsSync(cacheDir)) {
140
+ fs.mkdirSync(cacheDir, { recursive: true });
141
+ }
142
+
143
+ return path.join(cacheDir, `${hash}.json`);
144
+ }
145
+
146
+ /**
147
+ * Get recommendation based on similarity score
148
+ */
149
+ function getRecommendation(similarity) {
150
+ if (similarity >= 0.85) return 'DUPLICATE';
151
+ if (similarity >= 0.70) return 'RELATED';
152
+ return 'DIFFERENT';
153
+ }
154
+
155
+ /**
156
+ * Compare new learning to section (with caching)
157
+ */
158
+ async function compareToSection(newText, filePath, sectionName) {
159
+ // Security: Validate file path is within workspace
160
+ const validatedPath = validateFilePath(filePath);
161
+
162
+ // Read file once (optimization: avoid dual reads)
163
+ const fileContent = fs.readFileSync(validatedPath, 'utf-8');
164
+
165
+ // Stage 1: Check for exact match with grep (fast)
166
+ if (fileContent.includes(newText)) {
167
+ return {
168
+ stage: 1,
169
+ exact_match: true,
170
+ recommendation: 'DUPLICATE (exact match via grep)'
171
+ };
172
+ }
173
+
174
+ // Stage 2: Semantic comparison (thorough)
175
+ const cachePath = getCachePath(validatedPath, sectionName);
176
+ let cached = null;
177
+
178
+ // Try to load cache
179
+ if (fs.existsSync(cachePath)) {
180
+ try {
181
+ cached = JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
182
+ } catch (err) {
183
+ // Cache invalid, will rebuild
184
+ }
185
+ }
186
+
187
+ // Extract section lines (pass content to avoid re-reading file)
188
+ const sectionLines = extractSection(fileContent, sectionName);
189
+
190
+ if (sectionLines.length === 0) {
191
+ return {
192
+ error: `Section "${sectionName}" not found in ${filePath}`,
193
+ recommendation: 'CHECK SECTION NAME'
194
+ };
195
+ }
196
+
197
+ // Compute content hash for cache invalidation
198
+ const sectionHash = crypto.createHash('md5')
199
+ .update(sectionLines.map(l => l.text).join('\n'))
200
+ .digest('hex');
201
+
202
+ // Compute embeddings (use cache if valid)
203
+ let embeddings = [];
204
+
205
+ if (cached &&
206
+ cached.hash === sectionHash &&
207
+ cached.embeddings &&
208
+ cached.embeddings.length === sectionLines.length) {
209
+ embeddings = cached.embeddings;
210
+ } else {
211
+ for (const item of sectionLines) {
212
+ const emb = await getEmbedding(item.text);
213
+ embeddings.push({ text: item.text, line: item.line, embedding: emb });
214
+ }
215
+
216
+ // Save cache with content hash
217
+ fs.writeFileSync(cachePath, JSON.stringify({
218
+ file: validatedPath,
219
+ section: sectionName,
220
+ model: 'Xenova/all-MiniLM-L6-v2',
221
+ hash: sectionHash,
222
+ updated: new Date().toISOString(),
223
+ embeddings
224
+ }));
225
+ }
226
+
227
+ // Get new text embedding
228
+ const newEmbedding = await getEmbedding(newText);
229
+
230
+ // Calculate similarities
231
+ const similarities = embeddings.map(item => ({
232
+ text: item.text,
233
+ line: item.line,
234
+ similarity: cosineSimilarity(newEmbedding, item.embedding)
235
+ }));
236
+
237
+ // Sort by similarity and get top matches (threshold 0.65)
238
+ similarities.sort((a, b) => b.similarity - a.similarity);
239
+ const topMatches = similarities.slice(0, 5).filter(m => m.similarity >= 0.65);
240
+
241
+ if (topMatches.length === 0) {
242
+ return {
243
+ stage: 2,
244
+ matches: [],
245
+ max_similarity: similarities[0]?.similarity || 0,
246
+ recommendation: 'DIFFERENT (no similar content found)'
247
+ };
248
+ }
249
+
250
+ const maxSim = topMatches[0].similarity;
251
+ const overallRec = getRecommendation(maxSim);
252
+
253
+ return {
254
+ stage: 2,
255
+ matches: topMatches.map(m => ({
256
+ similarity: parseFloat(m.similarity.toFixed(3)),
257
+ line: m.line,
258
+ text: m.text.substring(0, 80) + (m.text.length > 80 ? '...' : ''),
259
+ recommendation: getRecommendation(m.similarity)
260
+ })),
261
+ max_similarity: parseFloat(maxSim.toFixed(3)),
262
+ recommendation: overallRec
263
+ };
264
+ }
265
+
266
+ /**
267
+ * Clear cache
268
+ */
269
+ function clearCache() {
270
+ const cacheDir = path.join(process.cwd(), '.genie', '.cache', 'embeddings');
271
+ if (fs.existsSync(cacheDir)) {
272
+ const files = fs.readdirSync(cacheDir);
273
+ for (const file of files) {
274
+ fs.unlinkSync(path.join(cacheDir, file));
275
+ }
276
+ console.log(`Cleared ${files.length} cached embeddings`);
277
+ } else {
278
+ console.log('No cache to clear');
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Main
284
+ */
285
+ async function main() {
286
+ const args = process.argv.slice(2);
287
+
288
+ // Help flag
289
+ if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
290
+ console.log('Usage:');
291
+ console.log(' genie helper embeddings "new learning text" file.md "Section Name"');
292
+ console.log('');
293
+ console.log('Purpose: Check if new learning already exists in target section');
294
+ console.log('');
295
+ console.log('Output: Top matches with similarity scores and recommendations');
296
+ console.log(' 0.85+ = DUPLICATE (merge or skip)');
297
+ console.log(' 0.70-0.85 = RELATED (evaluate carefully)');
298
+ console.log(' <0.70 = DIFFERENT (safe to append)');
299
+ console.log('');
300
+ console.log('Commands:');
301
+ console.log(' genie helper embeddings "text" file.md "Section" # Check for duplicates');
302
+ console.log(' genie helper embeddings clear-cache # Clear cache');
303
+ console.log('');
304
+ console.log('Example:');
305
+ console.log(' genie helper embeddings \\');
306
+ console.log(' "Never rewrite entire sections" \\');
307
+ console.log(' .genie/spells/learn.md \\');
308
+ console.log(' "Grow-and-Refine Protocol"');
309
+ return;
310
+ }
311
+
312
+ // Clear cache command
313
+ if (args[0] === 'clear-cache') {
314
+ clearCache();
315
+ return;
316
+ }
317
+
318
+ // Default: compare text to section
319
+ const text = args[0];
320
+ const file = args[1] || '.genie/spells/learn.md';
321
+ const section = args[2] || 'Grow-and-Refine Protocol';
322
+
323
+ if (!text) {
324
+ console.error('Usage: genie helper embeddings "text" [file.md] ["Section Name"]');
325
+ console.error(' Defaults: file=.genie/spells/learn.md, section="Grow-and-Refine Protocol"');
326
+ process.exit(1);
327
+ }
328
+
329
+ if (!fs.existsSync(file)) {
330
+ console.error(`File not found: ${file}`);
331
+ process.exit(1);
332
+ }
333
+
334
+ const result = await compareToSection(text, file, section);
335
+ console.log(JSON.stringify(result, null, 2));
336
+ }
337
+
338
+ main().catch(err => {
339
+ console.error('ERROR:', err.message);
340
+ if (err.stack) {
341
+ console.error(err.stack);
342
+ }
343
+ process.exit(1);
344
+ });
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Find Empty Sections Helper
5
+ *
6
+ * Detect markdown headings with no content (heading followed immediately by another heading or EOF).
7
+ * These are placeholder sections that were never filled in.
8
+ *
9
+ * Usage:
10
+ * node find-empty-sections.js <file-path> # Check single file
11
+ * node find-empty-sections.js <directory> # Check all .md files recursively
12
+ *
13
+ * Output:
14
+ * <file>:<line>: Empty section "Heading Text"
15
+ */
16
+
17
+ const fs = require('fs');
18
+ const path = require('path');
19
+
20
+ /**
21
+ * Detect empty sections in file content
22
+ * Returns: [{ line, heading }]
23
+ */
24
+ function detectEmptySections(content) {
25
+ const lines = content.split('\n');
26
+ const issues = [];
27
+
28
+ for (let i = 0; i < lines.length; i++) {
29
+ const line = lines[i].trim();
30
+
31
+ // Check if this is a heading (starts with #)
32
+ if (/^#+\s+.+/.test(line)) {
33
+ const heading = line.replace(/^#+\s+/, '');
34
+ let hasContent = false;
35
+
36
+ // Look ahead for content until next heading or EOF
37
+ for (let j = i + 1; j < lines.length; j++) {
38
+ const nextLine = lines[j].trim();
39
+
40
+ // Found another heading - section is empty
41
+ if (/^#+\s+/.test(nextLine)) {
42
+ break;
43
+ }
44
+
45
+ // Found non-empty content - section has content
46
+ if (nextLine.length > 0) {
47
+ hasContent = true;
48
+ break;
49
+ }
50
+ }
51
+
52
+ // If we reached EOF or next heading without finding content, it's empty
53
+ if (!hasContent) {
54
+ issues.push({
55
+ line: i + 1,
56
+ heading,
57
+ });
58
+ }
59
+ }
60
+ }
61
+
62
+ return issues;
63
+ }
64
+
65
+ /**
66
+ * Check single file
67
+ */
68
+ function checkFile(filePath) {
69
+ try {
70
+ const content = fs.readFileSync(filePath, 'utf-8');
71
+ const issues = detectEmptySections(content);
72
+
73
+ return issues.map(issue => ({
74
+ file: filePath,
75
+ line: issue.line,
76
+ heading: issue.heading,
77
+ }));
78
+ } catch (err) {
79
+ return [{ file: filePath, error: `Failed to read file: ${err.message}` }];
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Check all .md files in directory
85
+ */
86
+ function checkDirectory(dirPath) {
87
+ const allIssues = [];
88
+
89
+ function scanDir(dir) {
90
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
91
+
92
+ entries.forEach(entry => {
93
+ const fullPath = path.join(dir, entry.name);
94
+
95
+ if (entry.isDirectory() && !entry.name.startsWith('.')) {
96
+ scanDir(fullPath);
97
+ } else if (entry.isFile() && entry.name.endsWith('.md')) {
98
+ const issues = checkFile(fullPath);
99
+ allIssues.push(...issues);
100
+ }
101
+ });
102
+ }
103
+
104
+ scanDir(dirPath);
105
+ return allIssues;
106
+ }
107
+
108
+ /**
109
+ * Main
110
+ */
111
+ function main() {
112
+ const args = process.argv.slice(2);
113
+
114
+ if (args.length === 0) {
115
+ console.error(`
116
+ Usage:
117
+ node find-empty-sections.js <file-path> # Check single file
118
+ node find-empty-sections.js <directory> # Check all .md files recursively
119
+
120
+ Output:
121
+ <file>:<line>: Empty section "Heading Text"
122
+
123
+ Exit code:
124
+ 0 = No empty sections found
125
+ 1 = Empty sections found
126
+ `);
127
+ process.exit(1);
128
+ }
129
+
130
+ const target = args[0];
131
+
132
+ if (!fs.existsSync(target)) {
133
+ console.error(`Error: Path not found: ${target}`);
134
+ process.exit(1);
135
+ }
136
+
137
+ const stat = fs.statSync(target);
138
+ const issues = stat.isDirectory()
139
+ ? checkDirectory(target)
140
+ : checkFile(target);
141
+
142
+ if (issues.length === 0) {
143
+ console.log('No empty sections found');
144
+ process.exit(0);
145
+ }
146
+
147
+ issues.forEach(issue => {
148
+ if (issue.line) {
149
+ console.log(`${issue.file}:${issue.line}: Empty section "${issue.heading}"`);
150
+ } else {
151
+ console.log(`${issue.file}: ${issue.error}`);
152
+ }
153
+ });
154
+
155
+ process.exit(1);
156
+ }
157
+
158
+ main();