UNPKG

@intlayer/config

Version:

Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.

1 lines 2.68 kB
{"version":3,"file":"parseFilePathPattern.cjs","names":[],"sources":["../../../src/utils/parseFilePathPattern.ts"],"sourcesContent":["import type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\n\n/**\n * Parses a FilePathPattern configuration string or function to a final string path.\n * Resolves context variables like `{{locale}}`, `{{key}}`, `{{fileName}}`, `{{extension}}`.\n */\nexport const parseFilePathPattern = async (\n pattern: FilePathPattern,\n context: FilePathPatternContext\n): Promise<string> => {\n if (typeof pattern === 'function') {\n return await (pattern as any)(context);\n }\n\n if (typeof pattern === 'object' && pattern !== null && context.locale) {\n const localePattern = (pattern as any)[context.locale];\n\n if (localePattern) {\n if (typeof localePattern === 'function') {\n return await localePattern(context);\n }\n return parseStringPattern(localePattern, context);\n }\n }\n\n if (typeof pattern === 'string') {\n return parseStringPattern(pattern, context);\n }\n\n return '';\n};\n\nexport const parseStringPattern = (\n pattern: string,\n context: FilePathPatternContext\n): string =>\n pattern\n .replaceAll(/\\{\\{\\s*locale\\s*\\}\\}/g, context.locale ?? '')\n .replaceAll(/\\{\\{\\s*key\\s*\\}\\}/g, context.key ?? '')\n .replaceAll(/\\{\\{\\s*fileName\\s*\\}\\}/g, context.fileName ?? '')\n .replaceAll(/\\{\\{\\s*extension\\s*\\}\\}/g, context.extension ?? '')\n .replaceAll(\n /\\{\\{\\s*componentFileName\\s*\\}\\}/g,\n context.componentFileName ?? ''\n )\n .replaceAll(\n /\\{\\{\\s*componentExtension\\s*\\}\\}/g,\n context.componentExtension ?? ''\n )\n .replaceAll(/\\{\\{\\s*format\\s*\\}\\}/g, context.format ?? '')\n .replaceAll(/\\{\\{\\s*componentFormat\\s*\\}\\}/g, context.componentFormat ?? '')\n .replaceAll(\n /\\{\\{\\s*componentDirPath\\s*\\}\\}/g,\n context.componentDirPath ?? ''\n );\n"],"mappings":";;;;;;;AASA,MAAa,uBAAuB,OAClC,SACA,YACoB;AACpB,KAAI,OAAO,YAAY,WACrB,QAAO,MAAO,QAAgB,QAAQ;AAGxC,KAAI,OAAO,YAAY,YAAY,YAAY,QAAQ,QAAQ,QAAQ;EACrE,MAAM,gBAAiB,QAAgB,QAAQ;AAE/C,MAAI,eAAe;AACjB,OAAI,OAAO,kBAAkB,WAC3B,QAAO,MAAM,cAAc,QAAQ;AAErC,UAAO,mBAAmB,eAAe,QAAQ;;;AAIrD,KAAI,OAAO,YAAY,SACrB,QAAO,mBAAmB,SAAS,QAAQ;AAG7C,QAAO;;AAGT,MAAa,sBACX,SACA,YAEA,QACG,WAAW,yBAAyB,QAAQ,UAAU,GAAG,CACzD,WAAW,sBAAsB,QAAQ,OAAO,GAAG,CACnD,WAAW,2BAA2B,QAAQ,YAAY,GAAG,CAC7D,WAAW,4BAA4B,QAAQ,aAAa,GAAG,CAC/D,WACC,oCACA,QAAQ,qBAAqB,GAC9B,CACA,WACC,qCACA,QAAQ,sBAAsB,GAC/B,CACA,WAAW,yBAAyB,QAAQ,UAAU,GAAG,CACzD,WAAW,kCAAkC,QAAQ,mBAAmB,GAAG,CAC3E,WACC,mCACA,QAAQ,oBAAoB,GAC7B"}