Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add inline --ignore option #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,45 @@ describe("AI Digest CLI", () => {
await fs.rm(tempDir, { recursive: true, force: true });
}
}, 15000);

it("should respect the --ignore flag", async () => {
// Create a temporary directory
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ai-digest-ignore-flag-test-'));

try {
// Create some test files in the temporary directory
await fs.writeFile(path.join(tempDir, 'include.txt'), 'This file should be included');
await fs.writeFile(path.join(tempDir, 'exclude.js'), 'This file should be excluded');
await fs.writeFile(path.join(tempDir, 'also_exclude.py'), 'This file should also be excluded');

// Run the CLI with the --ignore flag
const { stdout } = await runCLI(`--input ${tempDir} --ignore '*.js' --ignore '*.py' --show-output-files`);

// Check if the output contains only the files we want to include
expect(stdout).toContain('include.txt');
expect(stdout).not.toContain('exclude.js');
expect(stdout).not.toContain('also_exclude.py');

// Check if the ignore patterns are mentioned
expect(stdout).toContain('Ignore patterns from command line:');
expect(stdout).toContain(' - *.js');
expect(stdout).toContain(' - *.py');

// Read the generated codebase.md file
const codebasePath = path.resolve(process.cwd(), "codebase.md");
const content = await fs.readFile(codebasePath, 'utf-8');

// Verify the content of codebase.md
expect(content).toContain('# include.txt');
expect(content).toContain('This file should be included');
expect(content).not.toContain('# exclude.js');
expect(content).not.toContain('This file should be excluded');
expect(content).not.toContain('# also_exclude.py');
expect(content).not.toContain('This file should also be excluded');

} finally {
// Clean up: remove the temporary directory and its contents
await fs.rm(tempDir, { recursive: true, force: true });
}
})
});
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ function displayIncludedFiles(includedFiles: string[]): void {
});
}

async function aggregateFiles(inputDir: string, outputFile: string, useDefaultIgnores: boolean, removeWhitespaceFlag: boolean, showOutputFiles: boolean, ignoreFile: string): Promise<void> {
async function aggregateFiles(inputDir: string, outputFile: string, useDefaultIgnores: boolean, removeWhitespaceFlag: boolean, showOutputFiles: boolean, ignoreFile: string, inlineIgnorePatterns: string[]): Promise<void> {
try {
const userIgnorePatterns = await readIgnoreFile(inputDir, ignoreFile);
const defaultIgnore = useDefaultIgnores ? ignore().add(DEFAULT_IGNORES) : ignore();
const customIgnore = createIgnoreFilter(userIgnorePatterns, ignoreFile);
const customIgnore = createIgnoreFilter(userIgnorePatterns, ignoreFile, inlineIgnorePatterns);

if (useDefaultIgnores) {
console.log(formatLog('Using default ignore patterns.', '🚫'));
Expand Down Expand Up @@ -158,6 +158,10 @@ async function aggregateFiles(inputDir: string, outputFile: string, useDefaultIg
}
}

function collect(value: string, previous: string[]) {
return previous.concat(value);
}

program
.version('1.0.0')
.description('Aggregate files into a single Markdown file')
Expand All @@ -167,10 +171,11 @@ program
.option('--whitespace-removal', 'Enable whitespace removal')
.option('--show-output-files', 'Display a list of files included in the output')
.option('--ignore-file <file>', 'Custom ignore file name', '.aidigestignore')
.option("--ignore <pattern>", 'Add ignore pattern (can be used multiple times). If using *, wrap in single quotes so the shell does not expand it.', collect, [])
.action(async (options) => {
const inputDir = path.resolve(options.input);
const outputFile = path.isAbsolute(options.output) ? options.output : path.join(process.cwd(), options.output);
await aggregateFiles(inputDir, outputFile, options.defaultIgnores, options.whitespaceRemoval, options.showOutputFiles, options.ignoreFile);
await aggregateFiles(inputDir, outputFile, options.defaultIgnores, options.whitespaceRemoval, options.showOutputFiles, options.ignoreFile, options.ignore);
});

program.parse(process.argv);
20 changes: 15 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,24 @@ export function escapeTripleBackticks(content: string): string {
return content.replace(/\`\`\`/g, "\\`\\`\\`");
}

export function createIgnoreFilter(ignorePatterns: string[], ignoreFile: string): Ignore {
const ig = require("ignore")().add(ignorePatterns);
if (ignorePatterns.length > 0) {
export function createIgnoreFilter(fileIgnorePatterns: string[], ignoreFile: string, inlineIgnorePatterns: string[]): Ignore {
const allIgnorePatterns = [...fileIgnorePatterns, ...inlineIgnorePatterns];
const ig = require("ignore")().add(allIgnorePatterns);

if (fileIgnorePatterns.length > 0) {
console.log(`Ignore patterns from ${ignoreFile}:`);
ignorePatterns.forEach((pattern) => {
fileIgnorePatterns.forEach((pattern) => {
console.log(` - ${pattern}`);
});
}
if (inlineIgnorePatterns.length > 0) {
console.log("Ignore patterns from command line:");
inlineIgnorePatterns.forEach((pattern) => {
console.log(` - ${pattern}`);
});
} else {
}

if (allIgnorePatterns.length === 0) {
console.log("No custom ignore patterns found.");
}
return ig;
Expand Down