@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
41 lines (39 loc) • 1.83 kB
JavaScript
import { getPathHash } from "../utils/getPathHash.mjs";
import { basename, extname, relative } from "node:path";
import { getConfiguration } from "@intlayer/config/node";
import { normalizePath } from "@intlayer/config/utils";
//#region src/createDictionaryEntryPoint/generateDictionaryListContent.ts
/**
* This function generates the content of the dictionary list file
*/
const generateDictionaryListContent = (dictionaries, functionName, importType, format = "esm", configuration = getConfiguration()) => {
const { mainDir } = configuration.system;
let content = "";
const dictionariesRef = dictionaries.map((dictionaryPath) => ({
relativePath: normalizePath(relative(mainDir, dictionaryPath)),
id: basename(dictionaryPath, extname(dictionaryPath)),
hash: `_${getPathHash(dictionaryPath)}`
}));
dictionariesRef.forEach((dictionary) => {
if (format === "esm") content += `import ${dictionary.hash} from '${dictionary.relativePath}'${importType === "json" ? " with { type: 'json' }" : ""};\n`;
if (format === "cjs") content += `const ${dictionary.hash} = require('${dictionary.relativePath}');\n`;
});
content += "\n";
const formattedDictionaryMap = dictionariesRef.map((dictionary) => ` "${dictionary.id}": ${dictionary.hash}`).join(",\n");
content += `const dictionaries = {\n${formattedDictionaryMap}\n};\n`;
content += `const ${functionName} = () => dictionaries;\n`;
if (format === "esm") {
content += `\n`;
content += `export { ${functionName} };\n`;
content += `export default dictionaries;\n`;
}
if (format === "cjs") {
content += `\n`;
content += `module.exports.${functionName} = ${functionName};\n`;
content += `module.exports = dictionaries;\n`;
}
return content;
};
//#endregion
export { generateDictionaryListContent };
//# sourceMappingURL=generateDictionaryListContent.mjs.map