@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
81 lines (79 loc) • 2.88 kB
JavaScript
import { kebabCaseToCamelCase } from "@intlayer/config/utils";
//#region src/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.ts
const getContentDeclarationFileTemplate = async (key, format, fileParams = {}, noMetadata) => {
const camelCaseKey = kebabCaseToCamelCase(key);
const name = camelCaseKey.charAt(0).toLowerCase() + camelCaseKey.slice(1);
const fileParamsString = Object.entries(fileParams).filter(([, value]) => value !== void 0).map(([paramKey, value]) => {
return `\n ${/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(paramKey) ? paramKey : JSON.stringify(paramKey)}: ${typeof value === "object" || typeof value === "string" ? JSON.stringify(value) : value},`;
}).join("");
let content;
if (noMetadata) content = "{}";
else if (format === "json" || format === "jsonc" || format === "json5") content = [
"{",
" \"$schema\": \"https://intlayer.org/schema.json\",",
` "key": "${key}",${fileParamsString}`,
" \"content\": {",
" }",
"}"
].join("\n");
else content = [
"{",
` key: '${key}',${fileParamsString}`,
" content: {",
" },",
"}"
].join("\n");
const jsdoc = `/** @type {import('intlayer').Dictionary${noMetadata ? "['content']" : ""}} **/`;
const satisfiesType = noMetadata ? "Dictionary['content']" : "Dictionary";
switch (format) {
case "yaml": return `${[
`key: ${key}`,
...fileParams.locale ? [`locale: ${fileParams.locale}`] : [],
...fileParams.title ? [`title: ${JSON.stringify(fileParams.title)}`] : [],
...fileParams.description ? [`description: ${JSON.stringify(fileParams.description)}`] : [],
...fileParams.tags?.length ? ["tags:", ...fileParams.tags.map((tag) => ` - ${JSON.stringify(tag)}`)] : [],
"content: {}"
].join("\n")}\n`;
case "md": return [
"---",
...[
`key: ${key}`,
...fileParams.locale ? [`locale: ${fileParams.locale}`] : [],
...fileParams.title ? [`title: ${JSON.stringify(fileParams.title)}`] : [],
...fileParams.description ? [`description: ${JSON.stringify(fileParams.description)}`] : [],
...fileParams.tags?.length ? ["tags:", ...fileParams.tags.map((t) => ` - ${JSON.stringify(t)}`)] : []
],
"---",
"",
""
].join("\n");
case "ts": return [
"import { type Dictionary } from 'intlayer';",
"",
`const ${name}Content = ${content} satisfies ${satisfiesType};`,
"",
`export default ${name}Content;`,
""
].join("\n");
case "cjs": return [
jsdoc,
`const ${name}Content = ${content};`,
"",
`module.exports = ${name}Content;`,
""
].join("\n");
case "json":
case "jsonc":
case "json5": return [content, ""].join("\n");
default: return [
jsdoc,
`const ${name}Content = ${content};`,
"",
`export default ${name}Content;`,
""
].join("\n");
}
};
//#endregion
export { getContentDeclarationFileTemplate };
//# sourceMappingURL=getContentDeclarationFileTemplate.mjs.map