-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheditFileByLines.js
More file actions
57 lines (48 loc) · 1.84 KB
/
editFileByLines.js
File metadata and controls
57 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// src/tools/editFileByLines.js
import fs from 'fs/promises';
import { existsSync } from 'fs';
import path from 'path';
editFileByLines.spec = {
name: editFileByLines.name,
description:
'Replaces a specific range of lines within a file. Will replace inclusively everything from the start line to end line.',
parameters: {
type: 'object',
properties: {
filepath: {
type: 'string',
description: 'The relative path to the file where the replacement should occur.',
},
range: {
type: 'string',
pattern: '^d+-d+$',
description: 'The range of line numbers to replace, formatted as "start-end".',
},
replacement: {
type: 'string',
description: 'The text content (discluding line numbers) that should replace the target lines.',
},
},
required: ['filepath', 'range', 'replacement'],
},
};
export default async function editFileByLines({ filepath, range, replacement }) {
console.log('Editing by lines', filepath);
const fullPath = path.resolve(filepath);
console.log('Replacing with: ', replacement);
const exists = existsSync(fullPath);
if (!exists) {
throw new Error(`File not found at ${fullPath}`);
}
const fileContents = await fs.readFile(fullPath, 'utf8');
const [start, end] = range.split('-').map(Number);
const lines = fileContents.split('\n');
const toInsert = replacement.split('\n');
let updatedFileContents = [...lines.slice(0, start - 1), ...toInsert, ...lines.slice(end)].join('\n');
await fs.writeFile(fullPath, updatedFileContents, 'utf8');
return {
success: true,
previousContent: fileContents,
updatedContent: updatedFileContents,
};
}