ignore file is created if it does not exist
This commit is contained in:

committed by
Geoff Seemueller

parent
4e8bf7184f
commit
019b6e48c9
@@ -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",
|
||||
|
@@ -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) {
|
||||
|
@@ -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';
|
||||
|
Reference in New Issue
Block a user