MCPcopy Create free account
hub / github.com/dmno-dev/bumpy / updateJsonNestedField

Function updateJsonNestedField

packages/bumpy/src/utils/fs.ts:37–58  ·  view source on GitHub ↗
(
  filePath: string,
  parentKey: string,
  childKey: string,
  newValue: string,
)

Source from the content-addressed store, hash-verified

35 * e.g., updateJsonNestedField(path, 'dependencies', 'core', '^2.0.0')
36 */
37export 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
60export async function readText(filePath: string): Promise<string> {
61 return readFile(filePath, 'utf-8');

Callers 3

resolveProtocolsInPlaceFunction · 0.90
applyReleasePlanFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…