|
1 | | -import * as fs from 'fs/promises'; |
| 1 | +import * as fsPromises from 'fs/promises'; |
| 2 | +import * as fs from 'fs'; |
2 | 3 | import * as path from 'path'; |
3 | 4 | import { Tool } from '../../core/types.js'; |
4 | 5 | import { z } from 'zod'; |
@@ -47,32 +48,33 @@ export const updateFileTool: Tool<Parameters, ReturnType> = { |
47 | 48 | const absolutePath = path.resolve(path.normalize(filePath)); |
48 | 49 | logger.verbose(`Updating file: ${absolutePath}`); |
49 | 50 |
|
50 | | - await fs.mkdir(path.dirname(absolutePath), { recursive: true }); |
| 51 | + await fsPromises.mkdir(path.dirname(absolutePath), { recursive: true }); |
51 | 52 |
|
52 | 53 | if (operation.command === 'update') { |
53 | | - const content = await fs.readFile(absolutePath, 'utf8'); |
| 54 | + const content = await fsPromises.readFile(absolutePath, 'utf8'); |
54 | 55 | const occurrences = content.split(operation.oldStr).length - 1; |
55 | 56 | if (occurrences !== 1) { |
56 | 57 | throw new Error( |
57 | 58 | `Found ${occurrences} occurrences of oldStr, expected exactly 1`, |
58 | 59 | ); |
59 | 60 | } |
60 | | - await fs.writeFile( |
| 61 | + await fsPromises.writeFile( |
61 | 62 | absolutePath, |
62 | 63 | content.replace(operation.oldStr, operation.newStr), |
63 | 64 | 'utf8', |
64 | 65 | ); |
65 | 66 | } else if (operation.command === 'append') { |
66 | | - await fs.appendFile(absolutePath, operation.content, 'utf8'); |
| 67 | + await fsPromises.appendFile(absolutePath, operation.content, 'utf8'); |
67 | 68 | } else { |
68 | | - await fs.writeFile(absolutePath, operation.content, 'utf8'); |
| 69 | + await fsPromises.writeFile(absolutePath, operation.content, 'utf8'); |
69 | 70 | } |
70 | 71 |
|
71 | 72 | logger.verbose(`Operation complete: ${operation.command}`); |
72 | 73 | return { path: filePath, operation: operation.command }; |
73 | 74 | }, |
74 | 75 | logParameters: (input, { logger }) => { |
75 | | - logger.info(`Modifying "${input.path}", ${input.description}`); |
| 76 | + const isFile = fs.existsSync(input.path); |
| 77 | + logger.info(`${isFile ? 'Modifying' : 'Creating' } "${input.path}", ${input.description}`); |
76 | 78 | }, |
77 | 79 | logReturns: () => {}, |
78 | 80 | }; |
0 commit comments