( filePath: string, parentKey: string, childKey: string, newValue: string, )
| 35 | * e.g., updateJsonNestedField(path, 'dependencies', 'core', '^2.0.0') |
| 36 | */ |
| 37 | export async function updateJsonNestedField( |
| 38 | filePath: string, |
| 39 | parentKey: string, |
| 40 | childKey: string, |
| 41 | newValue: string, |
| 42 | ): Promise<void> { |
| 43 | let content = await readFile(filePath, 'utf-8'); |
| 44 | const parentEscaped = parentKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 45 | const childEscaped = childKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 46 | |
| 47 | // Find the parent object block and replace the child value within it |
| 48 | const parentPattern = new RegExp( |
| 49 | `("${parentEscaped}"\\s*:\\s*\\{)([^}]*)\\}`, |
| 50 | 's', // dotAll so . matches newlines |
| 51 | ); |
| 52 | content = content.replace(parentPattern, (match, prefix, body) => { |
| 53 | const childPattern = new RegExp(`("${childEscaped}"\\s*:\\s*)"[^"]*"`); |
| 54 | const newBody = body.replace(childPattern, `$1"${newValue}"`); |
| 55 | return `${prefix}${newBody}}`; |
| 56 | }); |
| 57 | await writeFile(filePath, content, 'utf-8'); |
| 58 | } |
| 59 | |
| 60 | export async function readText(filePath: string): Promise<string> { |
| 61 | return readFile(filePath, 'utf-8'); |
no outgoing calls
no test coverage detected
searching dependent graphs…