Skip to content

Commit

Permalink
Merge pull request #1 from markwylde/ignore-multi-extensions
Browse files Browse the repository at this point in the history
feat: ignore multi file extensions
  • Loading branch information
markwylde authored Jul 31, 2024
2 parents b9d650a + 86c73e0 commit c4e4a1e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 60 deletions.
125 changes: 68 additions & 57 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,49 @@ 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'];
const ignore = ['.next', '.husky', '.terraform.lock.hcl', '.terraform', '.git', '.DS_Store', 'node_modules', 'package-lock.json', 'coverage'];
const ignoreExtensions = ['png', 'svg', 'jpg', 'jpeg', 'bin', 'stories.tsx'];

// Function to list all files recursively
function listFiles(dir, dumpContent = false, output = '', baseDir = '') {
const files = fs.readdirSync(dir, { withFileTypes: true });
files.forEach(file => {
const ext = file.name.split('.').at(-1);
if (ignore.includes(file.name || (file.name.includes('.') && ignoreExtensions.includes(ext)))) {
return;
function listFiles(dir, dumpContent = false, output = '', baseDir = '', depth = 0) {
const files = fs.readdirSync(dir, {
withFileTypes: true
});
files.forEach((file, index) => {
const fileNameParts = file.name.split('.');
const ext = fileNameParts.length > 1 ? fileNameParts.slice(-2).join('.') : fileNameParts[fileNameParts.length - 1];

if (ignore.includes(file.name) || ignoreExtensions.includes(ext)) {
return;
}

const filePath = path.join(dir, file.name);
const relativePath = path.join(baseDir, path.relative(dir, filePath));
const isLast = index === files.length - 1;
const prefix = depth === 0 ? '' : ' '.repeat(depth - 1) + (isLast ? '└─ ' : '├─ ');

if (file.isDirectory()) {
if (!dumpContent) {
output += `${prefix}${file.name}/\n`;
}
const filePath = path.join(dir, file.name);
const relativePath = path.join(baseDir, path.relative(dir, filePath));
if (file.isDirectory()) {
output = listFiles(filePath, dumpContent, output, relativePath);
output = listFiles(filePath, dumpContent, output, relativePath, depth + 1);
} else {
if (dumpContent) {
output += `# ${relativePath}\n`;
} 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";
}
output += `${prefix}${file.name}\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;
}
Expand All @@ -58,39 +69,39 @@ Options:

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

const command = argv._[0];
const directories = argv._.slice(1);
const command = argv._[0];
const directories = argv._.slice(1);

if (directories.length === 0) {
directories.push(process.cwd());
}
if (directories.length === 0) {
directories.push(process.cwd());
}

let output = '';

switch (command) {
case 'cat':
case 'ls':
directories.forEach(directory => {
if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) {
console.error(`Error: "${directory}" is not a valid directory.`);
return;
}
const baseName = path.basename(directory);
output += listFiles(directory, command === 'cat', '', baseName);
});
console.log(output);
if (command === 'cat') {
console.log('File contents dumped to output.txt');
}
break;
default:
console.log('Invalid command. Use "aitk help" for usage information.');
}
let output = '';

switch (command) {
case 'cat':
case 'ls':
directories.forEach(directory => {
if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) {
console.error(`Error: "${directory}" is not a valid directory.`);
return;
}
const baseName = path.basename(directory);
output += listFiles(directory, command === 'cat', '', baseName);
});
console.log(output);
if (command === 'cat') {
console.log('File contents dumped to output.txt');
}
break;
default:
console.log('Invalid command. Use "aitk help" for usage information.');
}
}

main();
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@markwylde/ai-toolkit",
"private": false,
"version": "1.2.0",
"version": "1.3.0",
"bin": {
"aitk": "bin/index.js"
},
Expand Down

0 comments on commit c4e4a1e

Please sign in to comment.