@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
42 lines (40 loc) • 1.56 kB
JavaScript
import { detectFormatCommand } from "../detectFormatCommand.mjs";
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
import { basename, dirname, join } from "node:path";
import { execSync } from "node:child_process";
import { stringifyYaml } from "@intlayer/core/utils";
//#region src/writeContentDeclaration/writeYamlFile.ts
const EXCLUDED_YAML_KEYS = new Set([
"$schema",
"id",
"filePath"
]);
const writeYamlFile = async (absoluteFilePath, dictionary, configuration) => {
const fileContent = stringifyYaml(Object.fromEntries(Object.entries(dictionary).filter(([k, v]) => !EXCLUDED_YAML_KEYS.has(k) && v !== void 0)));
await mkdir(dirname(absoluteFilePath), { recursive: true });
const tempDir = configuration.system?.tempDir;
if (tempDir) await mkdir(tempDir, { recursive: true });
const tempFileName = `${basename(absoluteFilePath)}.${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`;
const tempPath = tempDir ? join(tempDir, tempFileName) : `${absoluteFilePath}.${tempFileName}`;
try {
await writeFile(tempPath, fileContent, "utf-8");
await rename(tempPath, absoluteFilePath);
} catch (error) {
try {
await rm(tempPath, { force: true });
} catch {}
throw error;
}
const formatCommand = detectFormatCommand(configuration);
if (formatCommand) try {
execSync(formatCommand.replace("{{file}}", absoluteFilePath), {
stdio: "inherit",
cwd: configuration.system.baseDir
});
} catch (error) {
console.error(error);
}
};
//#endregion
export { writeYamlFile };
//# sourceMappingURL=writeYamlFile.mjs.map