Skip to content

Commit

Permalink
fix: show file path when supplied as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
markwylde committed Jul 29, 2024
1 parent 04596d3 commit db318ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
31 changes: 18 additions & 13 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const ignore = ['.terraform.lock.hcl', '.terraform', '.git', '.DS_Store', 'node_
const ignoreExtensions = ['png', 'svg', 'jpg', 'jpeg', 'bin'];

// Function to list all files recursively
function listFiles(dir, dumpContent = false, output = '', baseDir = dir) {
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) || ignoreExtensions.includes(ext)) {
return;
}
const filePath = path.join(dir, file.name);
const relativePath = path.relative(baseDir, filePath);
const relativePath = path.join(baseDir, path.relative(dir, filePath));
if (file.isDirectory()) {
output = listFiles(filePath, dumpContent, output, baseDir);
output = listFiles(filePath, dumpContent, output, relativePath);
} else {
if (dumpContent) {
output += `# ${relativePath}\n`;
Expand All @@ -45,7 +45,7 @@ const argv = minimist(process.argv.slice(2));

// Help message
const helpMessage = `
Usage: aitk [command] [directory]
Usage: aitk [command] [directory1] [directory2] ...
Commands:
cat Dump all file contents into a text file
Expand All @@ -64,24 +64,29 @@ function main() {
}

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

if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) {
console.error(`Error: "${directory}" is not a valid directory.`);
return;
if (directories.length === 0) {
directories.push(process.cwd());
}

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);
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.');
Expand Down
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.

0 comments on commit db318ba

Please sign in to comment.