diff --git a/src/MarkdownGenerator.ts b/src/MarkdownGenerator.ts index 48ccda4..398854b 100644 --- a/src/MarkdownGenerator.ts +++ b/src/MarkdownGenerator.ts @@ -11,7 +11,7 @@ import { glob } from 'glob'; import { isPreset, type PresetPrompt, prompts } from './prompts.ts'; -interface MarkdownGeneratorOptions { +export interface MarkdownGeneratorOptions { dir?: string; outputFilePath?: string; fileTypeExclusions?: Set; @@ -48,6 +48,7 @@ export class MarkdownGenerator { options.fileTypeExclusions || fileTypeExclusions, ); this.fileExclusions = options.fileExclusions || [...fileExclusions]; + // @ts-ignore - options.customPatterns signature is valid this.tokenCleaner = new TokenCleaner(options.customPatterns, options.customSecretPatterns); this.verbose = options.verbose !== undefined ? options.verbose : true; this.initialized = false; @@ -232,7 +233,7 @@ export class MarkdownGenerator { if (this.verbose) { console.log('File not found, creating a new \'todo\' file.'); } - await writeFile(todoPath, todoPrompt); // Create an empty 'todo' file + await writeFile(todoPath, ''); // Create an empty 'todo' file return await this.getTodo(); // Await the recursive call } if (this.verbose) { @@ -281,7 +282,7 @@ export class MarkdownGenerator { console.log({ total_tokens: totalTokens }); } return { success: true, tokenCount: llama3Tokenizer.encode(markdown).length }; - } catch (error) { + } catch (error: any) { if (this.verbose) { console.error('Error writing markdown document:', error); } diff --git a/src/cli.ts b/src/cli.ts index 4446bfb..8d519b4 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,8 +1,35 @@ #!/usr/bin/env node -console.log('RUNNING TOKENIZER'); -import { MarkdownGenerator } from './MarkdownGenerator.js'; +import type { PresetPrompt } from './prompts'; -const generator = new MarkdownGenerator(); +console.log('RUNNING TOKENIZER'); +import { MarkdownGenerator, type MarkdownGeneratorOptions } from './MarkdownGenerator'; + +const args = process.argv.slice(2); +const options: { prompt?: PresetPrompt; } & MarkdownGeneratorOptions = { + +}; +type ValidArg = keyof MarkdownGeneratorOptions; + +for (let i = 0; i < args.length; i++) { + if (args[i] === '--help') { + console.log(`${Object.keys(options).map(item => "--" + item).join(', ')}`); + } + if (args[i] === '--prompt') { + options["todoPrompt"] = args[i + 1] + i++; + } + const arg = args[i].replace(/^--/, ''); + if (arg as any satisfies ValidArg) { + // @ts-ignore - arg can't be used to index options + options[arg] = args[i + 1] + i++; + } else { + console.log(`Invalid argument specified: ${arg}`); + console.log(`Possible arguments: ${Object.keys(options).map(item => "--" + item).join(', ')}`); + } +} + +const generator = new MarkdownGenerator(options); generator .createMarkdownDocument() .then((result: { success: boolean }) => { diff --git a/src/prompts.ts b/src/prompts.ts index 234ce43..8097938 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -1,14 +1,21 @@ const taskConditionsStandard_FixErrors = ` -TASK: Fix these errors. -CONDITIONS: Output labeled and fully fixed files only, no diffs. -STANDARD: Respond with the files, no examples or excessive explanations. +## Task +1. Fix these errors. +## Conditions +2. Output labeled and fully fixed files only, no diffs. +## Standard +3. Respond with the files, no examples or excessive explanations. ~~~console - +clean up your errors and put them here ~~~ `; + + + + export const customPrompts: Record = { "tcs:fix:errors": taskConditionsStandard_FixErrors, }; diff --git a/test/cli.test.ts b/test/cli.test.ts new file mode 100644 index 0000000..a2062ed --- /dev/null +++ b/test/cli.test.ts @@ -0,0 +1,100 @@ +// test/cli.test.ts +import { describe, it, expect, beforeEach, afterEach, spyOn, mock } from 'bun:test'; +import { MarkdownGenerator, type MarkdownGeneratorOptions } from '../src/MarkdownGenerator'; +import type { PresetPrompt } from '../src/prompts'; + +// Function to process CLI arguments similar to cli.ts +function processArgs(args: string[]): { prompt?: PresetPrompt; } & MarkdownGeneratorOptions { + const options: { prompt?: PresetPrompt; } & MarkdownGeneratorOptions = {}; + + for (let i = 0; i < args.length; i++) { + if (args[i] === '--help') { + console.log(`${Object.keys(options).map(item => "--" + item).join(', ')}`); + continue; + } + if (args[i] === '--prompt') { + options["todoPrompt"] = args[i + 1] as PresetPrompt; + i++; + continue; + } + + const arg = args[i].replace(/^--/, ''); + if (['dir', 'outputFilePath', 'verbose', 'todoPrompt', 'fileTypeExclusions', 'fileExclusions', 'customPatterns', 'customSecretPatterns'].includes(arg)) { + // @ts-ignore - dynamic property access + options[arg] = args[i + 1]; + i++; + } else { + console.log(`Invalid argument specified: ${arg}`); + console.log(`Possible arguments: ${Object.keys(options).map(item => "--" + item).join(', ')}`); + } + } + + return options; +} + +describe('CLI', () => { + describe('argument handling', () => { + it('should process --prompt argument correctly', () => { + // Set up test arguments + const args = ['--prompt', 'tcs:fix:errors']; + + // Process arguments + const options = processArgs(args); + + // Verify options + expect(options).toEqual(expect.objectContaining({ + todoPrompt: 'tcs:fix:errors' + })); + }); + + it('should process other valid arguments correctly', () => { + // Set up test arguments + const args = ['--dir', './src', '--outputFilePath', './custom.md', '--verbose', 'false']; + + // Process arguments + const options = processArgs(args); + + // Verify options + expect(options).toEqual(expect.objectContaining({ + dir: './src', + outputFilePath: './custom.md', + verbose: 'false' + })); + }); + + it('should handle invalid arguments', () => { + // Mock console.log to capture output + const consoleLogSpy = spyOn(console, 'log'); + + // Set up test arguments + const args = ['--invalidArg', 'value']; + + // Process arguments + processArgs(args); + + // Verify error message was logged + expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid argument specified: invalidArg')); + + // Restore original function + consoleLogSpy.mockRestore(); + }); + + it('should display help when --help argument is provided', () => { + // Mock console.log to capture output + const consoleLogSpy = spyOn(console, 'log'); + + // Set up test arguments + const args = ['--help']; + + // Process arguments + processArgs(args); + + // Verify help message was logged + // At this point, the options object is empty, so we just check that console.log was called + expect(consoleLogSpy).toHaveBeenCalled(); + + // Restore original function + consoleLogSpy.mockRestore(); + }); + }); +});