mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-22 23:11:10 +00:00
26 lines
640 B
JavaScript
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;
|
|
}
|