Skip to content

Commit

Permalink
Adding a tree generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gommo committed Nov 26, 2024
1 parent 071b0f1 commit 663e856
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
.ai*
ai-kb*.md
ai-kb*.*
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ This will provide detailed logging information about the file matching process,

The tool generates markdown files named `ai-kb-<section-name>.md` for each section defined in your config file. These files contain the content of the matched source files, optimized for AI consumption.

## Command Line Options

The tool supports the following command line options:

- `-v`: Enable verbose debug logging
- `--tree`: Generate a directory tree structure at the beginning of each output file

When using the `--tree` option, the generated markdown will include a visual representation of your project's directory structure before the file contents. This can be helpful for:

- Understanding the overall project organization
- Documenting the project structure
- Making it easier to navigate through the codebase

Example tree output:
```
# Directory Structure
├── src/
│ ├── components/
│ │ ├── Button.js
│ │ └── Input.js
│ ├── utils/
│ │ └── helpers.js
│ └── index.js
├── tests/
│ └── unit/
│ └── helpers.test.js
└── package.json
```

The tree structure respects the same ignore patterns defined in your `.ai-kb-config` file and the global ignore patterns.

## Customization

The tool uses sensible defaults, but you can customize its behavior:
Expand Down
72 changes: 68 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import { debug } from 'console';
import fs from 'fs-extra';
import { glob } from 'glob';
import path from 'path';
Expand Down Expand Up @@ -105,6 +106,48 @@ const GLOBAL_IGNORES = [
'ai-kb-*.md'
];


// Function to generate a tree structure of files and directories
async function generateTree(rootDir, excludePatterns = []) {
let treeContent = `# Directory Structure\n\n`;

async function buildTree(dir, prefix = '') {
const items = await fs.readdir(dir);
const sortedItems = items.sort((a, b) => {
// Sort directories first, then files
const aIsDir = fs.statSync(path.join(dir, a)).isDirectory();
const bIsDir = fs.statSync(path.join(dir, b)).isDirectory();
if (aIsDir === bIsDir) return a.localeCompare(b);
return bIsDir ? 1 : -1;
});

for (let i = 0; i < sortedItems.length; i++) {
const item = sortedItems[i];
const itemPath = path.join(dir, item);
const relativePath = path.relative(rootDir, itemPath);

// Skip if item matches exclude patterns or global ignores
const shouldExclude = [...GLOBAL_IGNORES, ...excludePatterns].some(pattern => {
return itemPath.includes(pattern.replace('/**', '').replace('**/', ''));
});
if (shouldExclude) continue;

const isLast = i === sortedItems.length - 1;
const stats = await fs.stat(itemPath);

if (stats.isDirectory()) {
treeContent += `${prefix}${isLast ? '└── ' : '├── '}${item}/\n`;
await buildTree(itemPath, `${prefix}${isLast ? ' ' : '│ '}`);
} else {
treeContent += `${prefix}${isLast ? '└── ' : '├── '}${item}\n`;
}
}
}

await buildTree(rootDir);
return treeContent;
}

// Function to read and parse the config file
function readConfig() {
const configPath = '.ai-kb-config';
Expand Down Expand Up @@ -216,21 +259,42 @@ async function generateMarkdown(files) {
return markdowns.join('\n\n');
}

// Main function
async function main() {
const shouldGenerateTree = process.argv.includes('--tree');
console.log('Should generate tree:', shouldGenerateTree);

if (shouldGenerateTree) {
try {
const rootDir = process.cwd();
const treeStructure = await generateTree(rootDir, []);

// Write tree to file instead of console
const treeFile = 'ai-kb-tree.txt';
await fs.writeFile(treeFile, treeStructure);
console.log(`Generated tree structure in ${treeFile}`);
return;
} catch (error) {
console.error('Error generating tree structure:', error);
process.exit(1);
}
}

// Regular markdown generation (only runs if --tree is not present)
const config = readConfig();
debugLog('Parsed config:', config);

for (const [section, patterns] of Object.entries(config)) {
console.log(`Processing section: ${section}`);
const includePatterns = patterns.filter(p => !p.startsWith('-'));
const excludePatterns = patterns.filter(p => p.startsWith('-')).map(p => p.slice(1));

const files = await getMatchingFiles(includePatterns, excludePatterns);
console.log(`Matching files for ${section}:`, files);

if (files.length > 0) {
// Generate regular markdown content
const markdown = await generateMarkdown(files);

const outputFile = `ai-kb-${section}.md`;
await fs.writeFile(outputFile, markdown);
console.log(`Generated ${outputFile}`);
Expand Down

0 comments on commit 663e856

Please sign in to comment.