ignore file is created if it does not exist

This commit is contained in:
geoffsee
2025-03-06 08:26:19 -05:00
committed by Geoff Seemueller
parent 4e8bf7184f
commit 019b6e48c9
4 changed files with 47 additions and 1 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -1,6 +1,6 @@
{
"name": "toak",
"version": "3.1.1",
"version": "3.1.2",
"type": "module",
"license": "AGPL-3.0-or-later",
"repository": "https://github.com/seemueller-io/toak.git",

View File

@@ -238,6 +238,23 @@ export class MarkdownGenerator {
}
}
async getRootIgnore(): Promise<string> {
const rootIgnorePath = path.join(this.dir, '.toak-ignore');
try {
return await readFile(rootIgnorePath, 'utf-8');
} catch (error: any) {
if (error.code === 'ENOENT') {
// File does not exist
if (this.verbose) {
console.log('File not found, creating a root \'.toak-ignore\' file.');
}
await writeFile(rootIgnorePath, 'todo\nprompt.md'); // Create an empty 'todo' file
return await this.getRootIgnore(); // Await the recursive call
}
throw error;
}
}
/**
* Creates a complete markdown document combining code documentation and todos.
* @async
@@ -251,6 +268,7 @@ export class MarkdownGenerator {
try {
const codeMarkdown = await this.generateMarkdown();
const todos = await this.getTodo();
const _ = await this.getRootIgnore();
const markdown = codeMarkdown + `\n---\n\n${todos}\n`;
await writeFile(this.outputFilePath, markdown);
if (this.verbose) {

View File

@@ -353,6 +353,34 @@ const a = 1;
});
});
describe('getRootIgnore', () => {
it('should create root ignore file if it does not exist', async () => {
const rootIgnorePath = path.join('.', '.toak-ignore');
// First call to readFile throws ENOENT, second call resolves to empty string
const readFileSpy = spyOn(fs, 'readFile')
.mockImplementationOnce(() => {
const error: any = new Error('File not found');
error.code = 'ENOENT';
return Promise.reject(error);
})
.mockResolvedValueOnce('');
// Spy on fs.writeFile
const writeFileSpy = spyOn(fs, 'writeFile').mockResolvedValue(undefined);
const rootIgnore = await markdownGenerator.getRootIgnore();
expect(readFileSpy).toHaveBeenCalledWith(rootIgnorePath, 'utf-8');
expect(writeFileSpy).toHaveBeenCalledWith(rootIgnorePath, '');
expect(rootIgnore).toBe('');
// Restore the original implementations
readFileSpy.mockRestore();
writeFileSpy.mockRestore();
});
});
describe('createMarkdownDocument', () => {
it('should create markdown document successfully', async () => {
const mockContent = '# Project Files\n\n## test.txt\n~~~\ntest\n~~~\n\n';