MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / writeIfChanged

Function writeIfChanged

packages/studio-server/src/routes/files.ts:159–180  ·  view source on GitHub ↗

Write `next` to `absPath` only if it differs from `original`, returning a standardized change response.

(
  c: RouteContext,
  projectDir: string,
  filePath: string,
  absPath: string,
  original: string,
  next: string,
)

Source from the content-addressed store, hash-verified

157
158/** Write `next` to `absPath` only if it differs from `original`, returning a standardized change response. */
159function writeIfChanged(
160 c: RouteContext,
161 projectDir: string,
162 filePath: string,
163 absPath: string,
164 original: string,
165 next: string,
166): Response {
167 if (next === original) {
168 return c.json({ ok: true, changed: false, content: original, path: filePath });
169 }
170 const backup = snapshotBeforeWrite(projectDir, absPath);
171 if (backup.error) console.warn(`Failed to create backup for ${filePath}: ${backup.error}`);
172 writeFileSync(absPath, next, "utf-8");
173 return c.json({
174 ok: true,
175 changed: true,
176 content: next,
177 path: filePath,
178 backupPath: backupPathForResponse(projectDir, backup.backupPath),
179 });
180}
181
182function rejectUnsafeMutationValues(
183 c: RouteContext,

Callers 1

registerFileRoutesFunction · 0.85

Calls 3

snapshotBeforeWriteFunction · 0.85
backupPathForResponseFunction · 0.85
warnMethod · 0.80

Tested by

no test coverage detected