@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
39 lines • 1.44 kB
JavaScript
import { getConfiguration, normalizePath } from "@intlayer/config";
import { basename, extname, relative } from "path";
import { getFileHash } from "../../utils/getFileHash.mjs";
const generateDictionaryListContent = (dictionaries, format = "esm", configuration = getConfiguration()) => {
const { mainDir } = configuration.content;
let content = "";
const dictionariesRef = dictionaries.map((dictionaryPath) => ({
relativePath: normalizePath(relative(mainDir, dictionaryPath)),
id: basename(dictionaryPath, extname(dictionaryPath)),
// Get the base name as the dictionary id
hash: `_${getFileHash(dictionaryPath)}`
// Get the hash of the dictionary to avoid conflicts
}));
dictionariesRef.forEach((dictionary) => {
if (format === "esm")
content += `import ${dictionary.hash} from '${dictionary.relativePath}' with { type: 'json' };
`;
if (format === "cjs")
content += `const ${dictionary.hash} = require('${dictionary.relativePath}');
`;
});
content += "\n";
const formattedDictionaryMap = dictionariesRef.map((dictionary) => ` "${dictionary.id}": ${dictionary.hash}`).join(",\n");
if (format === "esm")
content += `export default {
${formattedDictionaryMap}
};
`;
if (format === "cjs")
content += `module.exports = {
${formattedDictionaryMap}
};
`;
return content;
};
export {
generateDictionaryListContent
};
//# sourceMappingURL=generateDictionaryListContent.mjs.map