Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
markwylde committed Jul 29, 2024
0 parents commit 8b434e6
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# AI Toolkit (aitk)

AI Toolkit is a command-line utility for listing and dumping file contents in a directory structure, designed to assist with AI-related tasks.

## Installation

To install the AI Toolkit globally, run:

```bash
npm install -g @markwylde/ai-toolkit
```

## Usage

```bash
aitk [command] [directory]
```

### Commands

- `cat`: Dump all file contents into a text file
- `ls`: Show a recursive directory tree of all files
- `help`: Show the help message

### Options

- `--help`: Show help message

## Examples

1. List all files in the current directory:
```bash
aitk ls
```

2. Dump contents of all files in a specific directory:
```bash
aitk cat
```

3. Show help message:
```bash
aitk help
```

## Features

- Recursively lists files in a directory
- Ignores specified files and directories (e.g., `.git`, `node_modules`)
- Ignores files with specific extensions (e.g., `png`, `svg`, `jpg`)
- Dumps file contents with file paths as headers
91 changes: 91 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import minimist from 'minimist';

const ignore = ['.terraform.lock.hcl', '.terraform', '.git', '.DS_Store', 'node_modules', 'package-lock.json', 'coverage'];
const ignoreExtensions = ['png', 'svg', 'jpg', 'jpeg', 'bin'];

// Function to list all files recursively
function listFiles(dir, dumpContent = false, output = '', baseDir = dir) {
const files = fs.readdirSync(dir, { withFileTypes: true });
files.forEach(file => {
const ext = file.name.split('.').at(-1);
if (ignore.includes(file.name) || ignoreExtensions.includes(ext)) {
return;
}
const filePath = path.join(dir, file.name);
const relativePath = path.relative(baseDir, filePath);
if (file.isDirectory()) {
output = listFiles(filePath, dumpContent, output, baseDir);
} else {
if (dumpContent) {
output += `# ${relativePath}\n`;
} else {
output += `- ${relativePath}\n`;
}
if (dumpContent) {
output += "```\n";
try {
const content = fs.readFileSync(filePath, 'utf8');
output += content + "\n";
} catch (err) {
output += `Error reading file: ${err.message}\n`;
}
output += "```\n\n";
}
}
});
return output;
}

// Parse command line arguments
const argv = minimist(process.argv.slice(2));

// Help message
const helpMessage = `
Usage: aitk [command] [directory]
Commands:
cat Dump all file contents into a text file
ls Show a recursive directory tree of all files
help Show this help message
Options:
--help Show help message
`;

// Main function
function main() {
if (argv.help || argv._.includes('help') || argv._.length === 0) {
console.log(helpMessage);
return;
}

const command = argv._[0];
const directory = argv._[1] || process.cwd();

if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) {
console.error(`Error: "${directory}" is not a valid directory.`);
return;
}

let output = '';

switch (command) {
case 'cat':
output = listFiles(directory, true);
console.log(output);
console.log('File contents dumped to output.txt');
break;
case 'ls':
output = listFiles(directory, false);
console.log(output);
break;
default:
console.log('Invalid command. Use "aitk help" for usage information.');
}
}

main();
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "ai-toolkit",
"version": "1.0.0",
"bin": {
"aitk": "bin/index.js"
},
"type": "module",
"main": "bin/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"files": [
"bin"
],
"keywords": [],
"author": {
"email": "[email protected]",
"name": "Mark Wylde",
"url": "https://markwylde.com"
},
"license": "MIT",
"description": "A set of utilities for working with ai tools",
"dependencies": {
"minimist": "^1.2.8"
}
}

0 comments on commit 8b434e6

Please sign in to comment.