diff --git a/bun.lockb b/bun.lockb index 8704886..9f2f5a1 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 6c62079..e63fcb1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/MarkdownGenerator.ts b/src/MarkdownGenerator.ts index 18ab72e..ac69005 100644 --- a/src/MarkdownGenerator.ts +++ b/src/MarkdownGenerator.ts @@ -238,6 +238,23 @@ export class MarkdownGenerator { } } + async getRootIgnore(): Promise { + 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) { diff --git a/test/core.test.ts b/test/core.test.ts index 4199317..1544bc5 100644 --- a/test/core.test.ts +++ b/test/core.test.ts @@ -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';