@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
94 lines (92 loc) • 4.93 kB
JavaScript
import { createDictionaryEntryPoint } from "./createDictionaryEntryPoint/createDictionaryEntryPoint.mjs";
import { readDictionariesFromDisk } from "./utils/readDictionariesFromDisk.mjs";
import { writeJsonIfChanged } from "./writeJsonIfChanged.mjs";
import { readFile, rm } from "node:fs/promises";
import { join, normalize, relative } from "node:path";
import { normalizePath } from "@intlayer/config/client";
import { colorizeKey, colorizePath, getAppLogger } from "@intlayer/config/logger";
import fg from "fast-glob";
//#region src/cleanRemovedContentDeclaration.ts
const cleanRemovedContentDeclaration = async (filePath, keysToKeep, configuration) => {
const appLogger = getAppLogger(configuration);
const unmergedDictionaries = readDictionariesFromDisk(configuration.system.unmergedDictionariesDir);
const baseDir = configuration.system.baseDir;
const relativeFilePath = relative(baseDir, filePath);
const uniqueUnmergedDictionaries = Object.values(unmergedDictionaries).flat().filter((dictionary) => dictionary.filePath === relativeFilePath && !keysToKeep.includes(dictionary.key)).filter((dictionary, index, self) => index === self.findIndex((t) => t.key === dictionary.key));
const changedDictionariesLocalIds = [];
const filesToRemove = [];
const excludeKeys = [];
await Promise.all(uniqueUnmergedDictionaries.map(async (dictionary) => {
const unmergedFilePath = normalize(join(configuration.system.unmergedDictionariesDir, `${dictionary.key}.json`));
try {
const jsonContent = await readFile(unmergedFilePath, "utf8");
const parsedContent = JSON.parse(jsonContent);
if (parsedContent.length === 1) {
if (parsedContent[0].filePath === relativeFilePath) {
appLogger(`Removing outdated dictionary ${colorizeKey(dictionary.key)}`, { isVerbose: true });
filesToRemove.push(unmergedFilePath);
excludeKeys.push(dictionary.key);
}
} else {
await writeJsonIfChanged(unmergedFilePath, parsedContent.filter((content) => content.filePath !== relativeFilePath));
changedDictionariesLocalIds.push(dictionary.localId);
}
} catch (error) {
if (error.code === "ENOENT") {
if (!excludeKeys.includes(dictionary.key)) excludeKeys.push(dictionary.key);
}
}
}));
const dictionaries = readDictionariesFromDisk(configuration.system.dictionariesDir);
const uniqueMergedDictionaries = (Object.values(dictionaries)?.filter((dictionary) => !keysToKeep.includes(dictionary.key) && dictionary.localIds?.length === 1 && dictionary.localIds[0].endsWith(`::local::${relativeFilePath}`))).filter((dictionary, index, self) => index === self.findIndex((t) => t.key === dictionary.key));
await Promise.all(uniqueMergedDictionaries.map(async (dictionary) => {
const mergedFilePath = normalize(join(configuration.system.dictionariesDir, `${dictionary.key}.json`));
try {
const fileContent = await readFile(mergedFilePath, "utf8");
const parsedContent = JSON.parse(fileContent);
if (parsedContent.localIds?.length === 1) {
if (parsedContent.localIds[0].endsWith(`::local::${relativeFilePath}`)) {
appLogger(`Removing outdated unmerged dictionary ${colorizeKey(dictionary.key)}`, { isVerbose: true });
filesToRemove.push(mergedFilePath);
const typesFilePath = normalize(join(configuration.system.typesDir, `${dictionary.key}.ts`));
filesToRemove.push(typesFilePath);
const dynamicFiles = await fg(normalizePath(join(configuration.system.dynamicDictionariesDir, `${dictionary.key}.*`)), { absolute: true });
filesToRemove.push(...dynamicFiles);
if (!excludeKeys.includes(dictionary.key)) excludeKeys.push(dictionary.key);
}
} else {
const localIds = parsedContent.localIds?.filter((localeId) => !localeId.endsWith(`::local::${relativeFilePath}`));
await writeJsonIfChanged(mergedFilePath, {
...parsedContent,
localIds
});
}
} catch (error) {
if (error.code === "ENOENT") {
if (!excludeKeys.includes(dictionary.key)) excludeKeys.push(dictionary.key);
const typesFilePath = normalize(join(configuration.system.typesDir, `${dictionary.key}.ts`));
filesToRemove.push(typesFilePath);
}
}
}));
if (filesToRemove.length > 0 || excludeKeys.length > 0) {
await createDictionaryEntryPoint(configuration, { excludeKeys });
if (filesToRemove.length > 0) setTimeout(async () => await Promise.all(filesToRemove.map(async (path) => {
const relativePath = relative(baseDir, path);
try {
await rm(path, { force: true });
appLogger(`Deleted artifact: ${colorizePath(relativePath)}`, { isVerbose: true });
} catch {
appLogger(`Error while removing file ${colorizePath(relativePath)}`, { isVerbose: true });
}
})), 3e3);
}
return {
changedDictionariesLocalIds,
excludeKeys,
hasRebuilt: filesToRemove.length > 0 || excludeKeys.length > 0
};
};
//#endregion
export { cleanRemovedContentDeclaration };
//# sourceMappingURL=cleanRemovedContentDeclaration.mjs.map