Files
openclaw/scripts/lib/generated-text-asset.mjs
2026-07-18 06:42:27 +01:00

26 lines
640 B
JavaScript

import fs from "node:fs/promises";
import path from "node:path";
/**
* Writes a generated text asset only when its contents changed.
*/
export async function writeGeneratedTextAsset(filePath, contents, params = {}) {
const fsImpl = params.fs ?? fs;
let currentContents = null;
try {
currentContents = await fsImpl.readFile(filePath, "utf8");
} catch (error) {
if (error?.code !== "ENOENT") {
throw error;
}
}
if (currentContents === contents) {
return false;
}
await fsImpl.mkdir(path.dirname(filePath), { recursive: true });
await fsImpl.writeFile(filePath, contents, "utf8");
return true;
}