|
1 | 1 | import { existsSync, statSync } from 'node:fs'; |
2 | 2 | import { join, relative, resolve } from 'node:path'; |
3 | 3 | import { glob } from 'glob'; |
4 | | -import { FileUtils } from '../utils/file-utils'; |
| 4 | +import { FileUtils } from '../utils/file-utils.js'; |
5 | 5 |
|
6 | 6 | // Test PR creation with trailing spaces on main branch |
7 | 7 |
|
@@ -99,6 +99,7 @@ interface IndexCliOptions { |
99 | 99 | template?: string; |
100 | 100 | dryRun?: boolean; |
101 | 101 | verbose?: boolean; |
| 102 | + json?: boolean; |
102 | 103 | maxDepth?: number; |
103 | 104 | noTraverseUp?: boolean; |
104 | 105 | boundary?: string; |
@@ -145,7 +146,65 @@ export async function indexCommand( |
145 | 146 | ...(cliOptions.boundary && { boundary: cliOptions.boundary }), |
146 | 147 | }; |
147 | 148 |
|
148 | | - return generateIndexFiles(options, directory || '.'); |
| 149 | + if (cliOptions.json) { |
| 150 | + return generateIndexFilesJson(options, directory || '.'); |
| 151 | + } else { |
| 152 | + return generateIndexFiles(options, directory || '.'); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** Generate index files for markdown documentation (JSON output) */ |
| 157 | +async function generateIndexFilesJson(options: IndexOptions, directory: string): Promise<void> { |
| 158 | + const targetDir = resolve(directory); |
| 159 | + |
| 160 | + if (!existsSync(targetDir)) { |
| 161 | + throw new Error(`Directory not found: ${targetDir}`); |
| 162 | + } |
| 163 | + |
| 164 | + if (!statSync(targetDir).isDirectory()) { |
| 165 | + throw new Error(`Path is not a directory: ${targetDir}`); |
| 166 | + } |
| 167 | + |
| 168 | + try { |
| 169 | + // Discover markdown files |
| 170 | + const files = await discoverMarkdownFiles(targetDir, options); |
| 171 | + |
| 172 | + // Organize files based on strategy |
| 173 | + const organizedFiles = organizeFiles(files, options); |
| 174 | + |
| 175 | + // Convert to JSON output |
| 176 | + const jsonOutput = { |
| 177 | + directory: targetDir, |
| 178 | + options: { |
| 179 | + type: options.type, |
| 180 | + strategy: options.strategy, |
| 181 | + location: options.location, |
| 182 | + }, |
| 183 | + totalFiles: files.length, |
| 184 | + organizedFiles: Object.fromEntries( |
| 185 | + Array.from(organizedFiles.entries()).map(([key, groupFiles]) => [ |
| 186 | + key, |
| 187 | + groupFiles.map(file => ({ |
| 188 | + path: file.path, |
| 189 | + relativePath: file.relativePath, |
| 190 | + title: file.metadata.title || file.relativePath, |
| 191 | + })) |
| 192 | + ]) |
| 193 | + ), |
| 194 | + files: files.map(file => ({ |
| 195 | + path: file.path, |
| 196 | + relativePath: file.relativePath, |
| 197 | + title: file.metadata.title || file.relativePath, |
| 198 | + })) |
| 199 | + }; |
| 200 | + |
| 201 | + console.log(JSON.stringify(jsonOutput, null, 2)); |
| 202 | + } catch (error) { |
| 203 | + if (error instanceof Error) { |
| 204 | + throw new Error(`Failed to generate index: ${error.message}`); |
| 205 | + } |
| 206 | + throw error; |
| 207 | + } |
149 | 208 | } |
150 | 209 |
|
151 | 210 | /** Generate index files for markdown documentation */ |
|
0 commit comments