mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-16 20:40:45 +00:00
36 lines
791 B
JavaScript
36 lines
791 B
JavaScript
import fs from "node:fs";
|
|
import { dirname } from "node:path";
|
|
|
|
export function writeTextFileIfChanged(filePath, contents) {
|
|
const next = String(contents);
|
|
try {
|
|
const current = fs.readFileSync(filePath, "utf8");
|
|
if (current === next) {
|
|
return false;
|
|
}
|
|
} catch {
|
|
// Write the file when it does not exist or cannot be read.
|
|
}
|
|
fs.mkdirSync(dirname(filePath), { recursive: true });
|
|
fs.writeFileSync(filePath, next, "utf8");
|
|
return true;
|
|
}
|
|
|
|
export function removeFileIfExists(filePath) {
|
|
try {
|
|
fs.rmSync(filePath, { force: true });
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function removePathIfExists(filePath) {
|
|
try {
|
|
fs.rmSync(filePath, { recursive: true, force: true });
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|