@intlayer/sync-json-plugin 8.12.5-canary.0 → 9.0.0-canary.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/loadJSON.cjs +30 -4
- package/dist/cjs/loadJSON.cjs.map +1 -1
- package/dist/cjs/syncJSON.cjs +59 -8
- package/dist/cjs/syncJSON.cjs.map +1 -1
- package/dist/esm/loadJSON.mjs +30 -4
- package/dist/esm/loadJSON.mjs.map +1 -1
- package/dist/esm/syncJSON.mjs +59 -8
- package/dist/esm/syncJSON.mjs.map +1 -1
- package/dist/types/loadJSON.d.ts +13 -0
- package/dist/types/loadJSON.d.ts.map +1 -1
- package/dist/types/syncJSON.d.ts +24 -0
- package/dist/types/syncJSON.d.ts.map +1 -1
- package/package.json +7 -7
package/dist/cjs/loadJSON.cjs
CHANGED
|
@@ -3,6 +3,7 @@ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
|
3
3
|
const require_syncJSON = require('./syncJSON.cjs');
|
|
4
4
|
let node_path = require("node:path");
|
|
5
5
|
let _intlayer_config_file = require("@intlayer/config/file");
|
|
6
|
+
let _intlayer_config_logger = require("@intlayer/config/logger");
|
|
6
7
|
let _intlayer_config_utils = require("@intlayer/config/utils");
|
|
7
8
|
let fast_glob = require("fast-glob");
|
|
8
9
|
fast_glob = require_runtime.__toESM(fast_glob);
|
|
@@ -65,25 +66,50 @@ const loadJSON = (options) => {
|
|
|
65
66
|
return {
|
|
66
67
|
name: "load-json",
|
|
67
68
|
loadDictionaries: async ({ configuration }) => {
|
|
69
|
+
const appLogger = (0, _intlayer_config_logger.getAppLogger)(configuration);
|
|
68
70
|
const dictionariesMap = await loadMessagePathMap(options.source, configuration);
|
|
71
|
+
if (dictionariesMap.length === 0) appLogger(`No dictionaries found at locations matching source pattern: ${(0, _intlayer_config_logger.colorizePath)(await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
72
|
+
key: "{{key}}",
|
|
73
|
+
locale: "{{locale}}"
|
|
74
|
+
}))}`, { level: "warn" });
|
|
75
|
+
const hasKeyPlaceholder = (await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
76
|
+
key: "{{key}}",
|
|
77
|
+
locale: "{{locale}}"
|
|
78
|
+
})).includes("{{key}}");
|
|
79
|
+
const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;
|
|
69
80
|
const dictionaries = [];
|
|
70
81
|
for (const { path, key, locale: entryLocale } of dictionariesMap) {
|
|
71
82
|
const json = await (0, _intlayer_config_file.loadExternalFile)(path, { logError: false }) ?? {};
|
|
72
83
|
const filePath = (0, node_path.relative)(configuration.system.baseDir, path);
|
|
73
84
|
const entryUsedLocale = locale ?? entryLocale;
|
|
74
|
-
const
|
|
85
|
+
const filled = entryUsedLocale !== configuration.internationalization.defaultLocale ? true : void 0;
|
|
86
|
+
if (shouldSplitByKeys) {
|
|
87
|
+
for (const [namespaceKey, namespaceContent] of Object.entries(json)) dictionaries.push({
|
|
88
|
+
key: namespaceKey,
|
|
89
|
+
locale: entryUsedLocale,
|
|
90
|
+
fill: filePath,
|
|
91
|
+
format,
|
|
92
|
+
localId: `${namespaceKey}::${location}::${filePath}`,
|
|
93
|
+
location,
|
|
94
|
+
filled,
|
|
95
|
+
content: namespaceContent,
|
|
96
|
+
filePath,
|
|
97
|
+
priority
|
|
98
|
+
});
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
dictionaries.push({
|
|
75
102
|
key,
|
|
76
103
|
locale: entryUsedLocale,
|
|
77
104
|
fill: filePath,
|
|
78
105
|
format,
|
|
79
106
|
localId: `${key}::${location}::${filePath}`,
|
|
80
107
|
location,
|
|
81
|
-
filled
|
|
108
|
+
filled,
|
|
82
109
|
content: json,
|
|
83
110
|
filePath,
|
|
84
111
|
priority
|
|
85
|
-
};
|
|
86
|
-
dictionaries.push(dictionary);
|
|
112
|
+
});
|
|
87
113
|
}
|
|
88
114
|
return dictionaries;
|
|
89
115
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadJSON.cjs","names":["extractKeyAndLocaleFromPath","sourcePattern"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n // extractKeyAndLocaleFromPath requires at least one named capture group\n // ({{__LOCALE__}} or {{__KEY__}}) in the mask to return a non-null result.\n // When the mask is fully concrete (e.g. `messages_ICU/en.json` — the source\n // has {{locale}} but no {{key}}), no groups exist and it returns null.\n // In that case, fall back directly to the loop locale and key = 'index'.\n const hasLocaleInMask = maskPatternLocale.includes('{{__LOCALE__}}');\n const hasKeyInMask = maskPatternLocale.includes('{{__KEY__}}');\n\n let key: string;\n let extractedLocale: Locale;\n\n if (hasLocaleInMask || hasKeyInMask) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n key = extraction.key;\n extractedLocale = extraction.locale;\n } else {\n // Mask has no placeholders — the file was found via a concrete locale\n // glob. Attribute it directly to the current loop locale.\n key = 'index';\n extractedLocale = locale;\n }\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absolutePath;\n }\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const sourcePattern = source as FilePathPattern;\n const messages: MessagesRecord = await listMessages(\n sourcePattern,\n configuration\n );\n\n // Always include all discovered locales — loadJSON is read-only and should\n // ingest every locale file that exists, just like syncJSON does.\n const entries = Object.entries(messages) as [\n Locale,\n Record<Dictionary['key'], FilePath>,\n ][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionary content.\n *\n * @example\n * ```ts\n * loadJSON({\n * format: 'icu',\n * })\n * ```\n */\n format?: DictionaryFormat;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale, format } = {\n location: 'plugin',\n priority: 0,\n ...options,\n } as const;\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key, locale: entryLocale } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // use ?? {} to guarantee a plain object regardless.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n // Use the per-entry locale discovered from the file path. If a fixed\n // locale override was provided, use it only as a fallback.\n const entryUsedLocale = (locale ?? entryLocale) as Locale;\n\n const dictionary: Dictionary = {\n key,\n locale: entryUsedLocale,\n fill: filePath,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n entryUsedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;;;;;AAwBA,MAAM,eAAe,OACnB,QACA,kBAC4B;CAC5B,MAAM,EAAE,QAAQ,yBAAyB;CAEzC,MAAM,EAAE,YAAY;CACpB,MAAM,EAAE,YAAY;CAEpB,MAAM,SAAyB,EAAE;AAEjC,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,oBAAoB,uDAA2B,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;EAEnC,MAAM,oBAAoB,uDAA2B,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;AAEnC,MAAI,CAAC,qBAAqB,CAAC,kBACzB;EAOF,MAAM,QAAQ,6BAJgB,kBAAkB,WAAW,KAAK,GAC5D,kBAAkB,MAAM,EAAE,GAC1B,mBAE0C,EAC5C,KAAK,SACN,CAAC;AAEF,OAAK,MAAM,QAAQ,OAAO;GAMxB,MAAM,kBAAkB,kBAAkB,SAAS,iBAAiB;GACpE,MAAM,eAAe,kBAAkB,SAAS,cAAc;GAE9D,IAAI;GACJ,IAAI;AAEJ,OAAI,mBAAmB,cAAc;IACnC,MAAM,aAAaA,6CACjB,MACA,mBACA,SACA,OACD;AAED,QAAI,CAAC,WACH;AAGF,UAAM,WAAW;AACjB,sBAAkB,WAAW;UACxB;AAGL,UAAM;AACN,sBAAkB;;GAGpB,MAAM,yCAA0B,KAAK,GAAG,8BAAe,SAAS,KAAK;GAErE,MAAM,aAAa;AACnB,OAAI,CAAC,OAAO,YACV,QAAO,cAAc,EAAE;AAGzB,UAAO,YAAY,OAA4B;;;AAMnD,QAAO;;AAKT,MAAM,qBAAqB,OACzB,QACA,kBACG;CAEH,MAAM,WAA2B,MAAM,aACrCC,QACA,cACD;AAwBD,QApBgB,OAAO,QAAQ,SAKqB,CAAC,SAClD,CAAC,QAAQ,gBACR,OAAO,QAAQ,WAAW,CAAC,KAAK,CAAC,KAAK,UAAU;AAK9C,SAAO;GACL,gCAL8B,KAAK,GACjC,8BACQ,cAAc,OAAO,SAAS,KAAK;GAI7C;GACA;GACD;GACD,CAGoB;;AA+E5B,MAAa,YAAY,YAA2C;CAClE,MAAM,EAAE,UAAU,UAAU,QAAQ,WAAW;EAC7C,UAAU;EACV,UAAU;EACV,GAAG;EACJ;AAED,QAAO;EACL,MAAM;EAEN,kBAAkB,OAAO,EAAE,oBAAoB;GAC7C,MAAM,kBAAmC,MAAM,mBAC7C,QAAQ,QACR,cACD;GAED,MAAM,eAA6B,EAAE;AAErC,QAAK,MAAM,EAAE,MAAM,KAAK,QAAQ,iBAAiB,iBAAiB;IAGhE,MAAM,OACH,kDAAuB,MAAM,EAAE,UAAU,OAAO,CAAC,IAAK,EAAE;IAE3D,MAAM,mCAAoB,cAAc,OAAO,SAAS,KAAK;IAI7D,MAAM,kBAAmB,UAAU;IAEnC,MAAM,aAAyB;KAC7B;KACA,QAAQ;KACR,MAAM;KACN;KACA,SAAS,GAAG,IAAI,IAAI,SAAS,IAAI;KACvB;KACV,QACE,oBAAoB,cAAc,qBAAqB,gBACnD,OACA;KACN,SAAS;KACT;KACA;KACD;AAED,iBAAa,KAAK,WAAW;;AAG/B,UAAO;;EAEV"}
|
|
1
|
+
{"version":3,"file":"loadJSON.cjs","names":["extractKeyAndLocaleFromPath","sourcePattern"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { colorizePath, getAppLogger } from '@intlayer/config/logger';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n // extractKeyAndLocaleFromPath requires at least one named capture group\n // ({{__LOCALE__}} or {{__KEY__}}) in the mask to return a non-null result.\n // When the mask is fully concrete (e.g. `messages_ICU/en.json` — the source\n // has {{locale}} but no {{key}}), no groups exist and it returns null.\n // In that case, fall back directly to the loop locale and key = 'index'.\n const hasLocaleInMask = maskPatternLocale.includes('{{__LOCALE__}}');\n const hasKeyInMask = maskPatternLocale.includes('{{__KEY__}}');\n\n let key: string;\n let extractedLocale: Locale;\n\n if (hasLocaleInMask || hasKeyInMask) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n key = extraction.key;\n extractedLocale = extraction.locale;\n } else {\n // Mask has no placeholders — the file was found via a concrete locale\n // glob. Attribute it directly to the current loop locale.\n key = 'index';\n extractedLocale = locale;\n }\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absolutePath;\n }\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const sourcePattern = source as FilePathPattern;\n const messages: MessagesRecord = await listMessages(\n sourcePattern,\n configuration\n );\n\n // Always include all discovered locales — loadJSON is read-only and should\n // ingest every locale file that exists, just like syncJSON does.\n const entries = Object.entries(messages) as [\n Locale,\n Record<Dictionary['key'], FilePath>,\n ][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionary content.\n *\n * @example\n * ```ts\n * loadJSON({\n * format: 'icu',\n * })\n * ```\n */\n format?: DictionaryFormat;\n\n /**\n * Whether each top-level key of the JSON file should become its own\n * dictionary (keyed by that top-level key) instead of a single dictionary\n * holding the whole file.\n *\n * This matches the namespace model of libraries such as `next-intl` /\n * `react-intl`, where a single `messages/{locale}.json` file groups several\n * namespaces by its first-level keys.\n *\n * When omitted, it is auto-detected: the file is split when the `source`\n * pattern has no `{{key}}` segment, and kept as a single dictionary otherwise.\n */\n splitKeys?: boolean;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale, format } = {\n location: 'plugin',\n priority: 0,\n ...options,\n } as const;\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const appLogger = getAppLogger(configuration);\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n if (dictionariesMap.length === 0) {\n const pattern = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n appLogger(\n `No dictionaries found at locations matching source pattern: ${colorizePath(pattern)}`,\n { level: 'warn' }\n );\n }\n\n // When the source pattern has no `{{key}}` segment, a single file holds\n // every namespace as a first-level key (the next-intl / react-intl model).\n // In that case each top-level key becomes its own dictionary.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const hasKeyPlaceholder = patternMarker.includes('{{key}}');\n const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key, locale: entryLocale } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // use ?? {} to guarantee a plain object regardless.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n // Use the per-entry locale discovered from the file path. If a fixed\n // locale override was provided, use it only as a fallback.\n const entryUsedLocale = (locale ?? entryLocale) as Locale;\n\n const filled =\n entryUsedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined;\n\n if (shouldSplitByKeys) {\n for (const [namespaceKey, namespaceContent] of Object.entries(json)) {\n dictionaries.push({\n key: namespaceKey,\n locale: entryUsedLocale,\n fill: filePath,\n format,\n localId:\n `${namespaceKey}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: namespaceContent as JSONContent,\n filePath,\n priority,\n });\n }\n continue;\n }\n\n dictionaries.push({\n key,\n locale: entryUsedLocale,\n fill: filePath,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: json,\n filePath,\n priority,\n });\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;;;;;;AAyBA,MAAM,eAAe,OACnB,QACA,kBAC4B;CAC5B,MAAM,EAAE,QAAQ,yBAAyB;CAEzC,MAAM,EAAE,YAAY;CACpB,MAAM,EAAE,YAAY;CAEpB,MAAM,SAAyB,EAAE;AAEjC,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,oBAAoB,uDAA2B,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;EAEnC,MAAM,oBAAoB,uDAA2B,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;AAEnC,MAAI,CAAC,qBAAqB,CAAC,kBACzB;EAOF,MAAM,QAAQ,6BAJgB,kBAAkB,WAAW,KAAK,GA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BAAoB,uDAJuB,QAAQ,QAAQ;IAC/D,KAAK;IACL,QAAQ;IACT,CAAkC,EACK,SAAS,UAAU;GAC3D,MAAM,oBAAoB,QAAQ,aAAa,CAAC;GAEhD,MAAM,eAA6B,EAAE;AAErC,QAAK,MAAM,EAAE,MAAM,KAAK,QAAQ,iBAAiB,iBAAiB;IAGhE,MAAM,OACH,kDAAuB,MAAM,EAAE,UAAU,OAAO,CAAC,IAAK,EAAE;IAE3D,MAAM,mCAAoB,cAAc,OAAO,SAAS,KAAK;IAI7D,MAAM,kBAAmB,UAAU;IAEnC,MAAM,SACJ,oBAAoB,cAAc,qBAAqB,gBACnD,OACA;AAEN,QAAI,mBAAmB;AACrB,UAAK,MAAM,CAAC,cAAc,qBAAqB,OAAO,QAAQ,KAAK,CACjE,cAAa,KAAK;MAChB,KAAK;MACL,QAAQ;MACR,MAAM;MACN;MACA,SACE,GAAG,aAAa,IAAI,SAAS,IAAI;MACzB;MACV;MACA,SAAS;MACT;MACA;MACD,CAAC;AAEJ;;AAGF,iBAAa,KAAK;KAChB;KACA,QAAQ;KACR,MAAM;KACN;KACA,SAAS,GAAG,IAAI,IAAI,SAAS,IAAI;KACvB;KACV;KACA,SAAS;KACT;KACA;KACD,CAAC;;AAGJ,UAAO;;EAEV"}
|
package/dist/cjs/syncJSON.cjs
CHANGED
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
let node_path = require("node:path");
|
|
4
4
|
let _intlayer_config_file = require("@intlayer/config/file");
|
|
5
|
+
let _intlayer_config_logger = require("@intlayer/config/logger");
|
|
5
6
|
let _intlayer_config_utils = require("@intlayer/config/utils");
|
|
6
7
|
let fast_glob = require("fast-glob");
|
|
7
8
|
fast_glob = require_runtime.__toESM(fast_glob);
|
|
@@ -95,18 +96,26 @@ const loadMessagePathMap = async (source, configuration) => {
|
|
|
95
96
|
}));
|
|
96
97
|
};
|
|
97
98
|
const syncJSON = async (options) => {
|
|
99
|
+
const patternMarker = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
100
|
+
key: "{{key}}",
|
|
101
|
+
locale: "{{locale}}"
|
|
102
|
+
});
|
|
98
103
|
const { location, priority, format } = {
|
|
99
|
-
location: `sync-json::${
|
|
100
|
-
key: "{{key}}",
|
|
101
|
-
locale: "{{locale}}"
|
|
102
|
-
})}`,
|
|
104
|
+
location: `sync-json::${patternMarker}`,
|
|
103
105
|
priority: 0,
|
|
104
106
|
...options
|
|
105
107
|
};
|
|
108
|
+
const hasKeyPlaceholder = patternMarker.includes("{{key}}");
|
|
109
|
+
const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;
|
|
106
110
|
return {
|
|
107
111
|
name: "sync-json",
|
|
108
112
|
loadDictionaries: async ({ configuration }) => {
|
|
113
|
+
const appLogger = (0, _intlayer_config_logger.getAppLogger)(configuration);
|
|
109
114
|
const dictionariesMap = await loadMessagePathMap(options.source, configuration);
|
|
115
|
+
if (dictionariesMap.length === 0) appLogger(`[sync-json] No dictionaries found at locations matching source pattern: ${(0, _intlayer_config_logger.colorizePath)(await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
116
|
+
key: "{{key}}",
|
|
117
|
+
locale: "{{locale}}"
|
|
118
|
+
}))}`, { level: "warn" });
|
|
110
119
|
let fill = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
111
120
|
key: "{{key}}",
|
|
112
121
|
locale: "{{locale}}"
|
|
@@ -116,25 +125,41 @@ const syncJSON = async (options) => {
|
|
|
116
125
|
for (const { locale, path, key } of dictionariesMap) {
|
|
117
126
|
const json = await (0, _intlayer_config_file.loadExternalFile)(path, { logError: false }) ?? {};
|
|
118
127
|
const filePath = (0, node_path.relative)(configuration.system.baseDir, path);
|
|
119
|
-
const
|
|
128
|
+
const filled = locale !== configuration.internationalization.defaultLocale ? true : void 0;
|
|
129
|
+
if (shouldSplitByKeys) {
|
|
130
|
+
for (const [namespaceKey, namespaceContent] of Object.entries(json)) dictionaries.push({
|
|
131
|
+
key: namespaceKey,
|
|
132
|
+
locale,
|
|
133
|
+
fill,
|
|
134
|
+
format,
|
|
135
|
+
localId: `${namespaceKey}::${location}::${filePath}`,
|
|
136
|
+
location,
|
|
137
|
+
filled,
|
|
138
|
+
content: namespaceContent,
|
|
139
|
+
filePath,
|
|
140
|
+
priority
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
dictionaries.push({
|
|
120
145
|
key,
|
|
121
146
|
locale,
|
|
122
147
|
fill,
|
|
123
148
|
format,
|
|
124
149
|
localId: `${key}::${location}::${filePath}`,
|
|
125
150
|
location,
|
|
126
|
-
filled
|
|
151
|
+
filled,
|
|
127
152
|
content: json,
|
|
128
153
|
filePath,
|
|
129
154
|
priority
|
|
130
|
-
};
|
|
131
|
-
dictionaries.push(dictionary);
|
|
155
|
+
});
|
|
132
156
|
}
|
|
133
157
|
return dictionaries;
|
|
134
158
|
},
|
|
135
159
|
formatOutput: async ({ dictionary, configuration }) => {
|
|
136
160
|
const { formatDictionaryOutput } = await import("@intlayer/chokidar/build");
|
|
137
161
|
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
162
|
+
if (shouldSplitByKeys) return dictionary;
|
|
138
163
|
const builderPath = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
139
164
|
key: dictionary.key,
|
|
140
165
|
locale: dictionary.locale
|
|
@@ -147,6 +172,32 @@ const syncJSON = async (options) => {
|
|
|
147
172
|
const { parallelize } = await import("@intlayer/chokidar/utils");
|
|
148
173
|
const { formatDictionaryOutput } = await import("@intlayer/chokidar/build");
|
|
149
174
|
const { locales } = configuration.internationalization;
|
|
175
|
+
if (shouldSplitByKeys) {
|
|
176
|
+
const mergedByLocale = {};
|
|
177
|
+
const filePathByLocale = {};
|
|
178
|
+
for (const [key, entry] of Object.entries(dictionaries.mergedDictionaries)) {
|
|
179
|
+
const dictionary = entry.dictionary;
|
|
180
|
+
if (dictionary.location !== location) continue;
|
|
181
|
+
for (const locale of locales) {
|
|
182
|
+
const formattedOutput = formatDictionaryOutput(getPerLocaleDictionary(dictionary, locale), format);
|
|
183
|
+
const content = JSON.parse(JSON.stringify(formattedOutput.content));
|
|
184
|
+
if (typeof content === "undefined" || typeof content === "object" && content !== null && Object.keys(content).length === 0) continue;
|
|
185
|
+
mergedByLocale[locale] ??= {};
|
|
186
|
+
mergedByLocale[locale][key] = content;
|
|
187
|
+
filePathByLocale[locale] = await (0, _intlayer_config_utils.parseFilePathPattern)(options.source, {
|
|
188
|
+
key,
|
|
189
|
+
locale
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
await parallelize(Object.keys(mergedByLocale), async (locale) => {
|
|
194
|
+
const builderPath = filePathByLocale[locale];
|
|
195
|
+
if (!builderPath) return;
|
|
196
|
+
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(builderPath), { recursive: true });
|
|
197
|
+
await (0, node_fs_promises.writeFile)(builderPath, `${JSON.stringify(mergedByLocale[locale], null, 2)}\n`, "utf-8");
|
|
198
|
+
});
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
150
201
|
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
151
202
|
key,
|
|
152
203
|
dictionary: dictionary.dictionary,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncJSON.cjs","names":[],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n DictionaryLocation,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n): { key: string; locale: Locale } | null => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n // fast-glob strips leading \"./\" from returned paths; normalize both sides\n const normalize = (path: string) =>\n path.startsWith('./') ? path.slice(2) : path;\n\n const normalizedFilePath = normalize(filePath);\n const normalizedMask = normalize(maskPattern);\n\n const localesAlternation = locales.join('|');\n\n // Escape special regex chars, then convert glob wildcards to regex equivalents.\n // Must replace ** before * to avoid double-replacing.\n let regexStr = `^${escapeRegex(normalizedMask)}$`;\n regexStr = regexStr.replace(/\\\\\\*\\\\\\*/g, '.*'); // ** → match any path segments\n regexStr = regexStr.replace(/\\\\\\*/g, '[^/]*'); // * → match within a single segment\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (normalizedMask.includes(keyPlaceholder)) {\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>.+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n const match = maskRegex.exec(normalizedFilePath);\n\n if (!match?.groups) {\n return null;\n }\n\n let locale = match.groups.locale as Locale | undefined;\n let key = (match.groups.key as string | undefined) ?? 'index';\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n const { key, locale: extractedLocale } = extraction;\n\n // Generate what the path SHOULD be for this key/locale using the current builder\n const expectedPath = await parseFilePathPattern(source, {\n key,\n locale: extractedLocale,\n } as any as FilePathPatternContext);\n\n // Resolve both to absolute paths to ensure safe comparison\n const absoluteFoundPath = isAbsolute(file)\n ? file\n : resolve(baseDir, file);\n const absoluteExpectedPath = isAbsolute(expectedPath)\n ? expectedPath\n : resolve(baseDir, expectedPath);\n\n // If the file found doesn't exactly match the file expected, it belongs to another plugin/structure\n if (absoluteFoundPath !== absoluteExpectedPath) {\n continue;\n }\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absoluteFoundPath;\n }\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n const maskWithKey = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale: locales[0],\n } as any as FilePathPatternContext);\n\n const hasKeyInMask = maskWithKey.includes('{{__KEY__}}');\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = await parseFilePathPattern(source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = await listMessages(\n source as FilePathPattern,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * // Example usage:\n * const config = {\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: DictionaryLocation | (string & {});\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionaries created by the plugin.\n *\n * Default: 'intlayer'\n *\n * The format of the dictionaries created by the plugin.\n */\n format?: DictionaryFormat;\n};\n\nexport const syncJSON = async (\n options: SyncJSONPluginOptions\n): Promise<Plugin> => {\n // Generate a unique default location based on the source pattern.\n // This ensures that if you have multiple plugins, they don't share the same 'plugin' ID.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const defaultLocation = `sync-json::${patternMarker}`;\n\n const { location, priority, format } = {\n location: defaultLocation,\n priority: 0,\n ...options,\n };\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n let fill: string = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n if (fill) {\n fill = relative(\n configuration.system.baseDir,\n resolve(configuration.system.baseDir, fill)\n );\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // the try/catch does not help here — use ?? {} to guarantee a plain object.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale,\n fill,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n\n formatOutput: async ({ dictionary, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n const builderPath = await parseFilePathPattern(options.source, {\n key: dictionary.key,\n locale: dictionary.locale,\n } as FilePathPatternContext);\n\n // Verification to ensure we are formatting the correct file\n if (\n resolve(configuration.system.baseDir, builderPath) !==\n resolve(configuration.system.baseDir, dictionary.filePath)\n ) {\n return dictionary;\n }\n\n const formattedOutput = formatDictionaryOutput(dictionary, format);\n\n return formattedOutput.content;\n },\n\n afterBuild: async ({ dictionaries, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { getPerLocaleDictionary } = await import('@intlayer/core/plugins');\n const { parallelize } = await import('@intlayer/chokidar/utils');\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n const { locales } = configuration.internationalization;\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n // We get all dictionaries, but we need to filter them\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) {\n return;\n }\n\n const builderPath = await parseFilePathPattern(options.source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n\n const localizedDictionary = getPerLocaleDictionary(dictionary, locale);\n\n const formattedOutput = formatDictionaryOutput(\n localizedDictionary,\n format\n );\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n return;\n }\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(content, null, 2);\n\n await writeFile(builder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WAAW,OAAQ,QAAO;GAEvD,MAAM,cAAc,uDAA2B,QAAQ,QAAQ;IAC7D,KAAK,WAAW;IAChB,QAAQ,WAAW;IACpB,CAA2B;AAG5B,8BACU,cAAc,OAAO,SAAS,YAAY,4BAC1C,cAAc,OAAO,SAAS,WAAW,SAAS,CAE1D,QAAO;AAKT,UAFwB,uBAAuB,YAAY,OAErC,CAAC;;EAGzB,YAAY,OAAO,EAAE,cAAc,oBAAoB;GAErD,MAAM,EAAE,2BAA2B,MAAM,OAAO;GAChD,MAAM,EAAE,gBAAgB,MAAM,OAAO;GACrC,MAAM,EAAE,2BAA2B,MAAM,OACvC;GAGF,MAAM,EAAE,YAAY,cAAc;AAmBlC,SAAM,YAV2B,OAAO,QACtC,aAAa,mBACd,CAAC,SAAS,CAAC,KAAK,gBACf,QAAQ,KAAK,YAAY;IACvB;IACA,YAAY,WAAW;IACvB;IACD,EAAE,CAGuB,EAAE,OAAO,EAAE,KAAK,YAAY,aAAa;AAEnE,QAAI,WAAW,aAAa,SAC1B;IAGF,MAAM,cAAc,uDAA2B,QAAQ,QAAQ;KAC7D;KACA;KACD,CAAkC;IAInC,MAAM,kBAAkB,uBAFI,uBAAuB,YAAY,OAG1C,EACnB,OACD;IAED,MAAM,UAAU,KAAK,MAAM,KAAK,UAAU,gBAAgB,QAAQ,CAAC;AAEnE,QACE,OAAO,YAAY,eAClB,OAAO,YAAY,YAClB,OAAO,KAAK,QAAmC,CAAC,WAAW,EAE7D;AAGF,6DAAoB,YAAY,EAAE,EAAE,WAAW,MAAM,CAAC;AAItD,0CAAgB,aAAa,GAFP,KAAK,UAAU,SAAS,MAAM,EAEP,CAAC,KAAK,QAAQ;KAC3D;;EAEL"}
|
|
1
|
+
{"version":3,"file":"syncJSON.cjs","names":[],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { colorizePath, getAppLogger } from '@intlayer/config/logger';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n DictionaryLocation,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n): { key: string; locale: Locale } | null => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n // fast-glob strips leading \"./\" from returned paths; normalize both sides\n const normalize = (path: string) =>\n path.startsWith('./') ? path.slice(2) : path;\n\n const normalizedFilePath = normalize(filePath);\n const normalizedMask = normalize(maskPattern);\n\n const localesAlternation = locales.join('|');\n\n // Escape special regex chars, then convert glob wildcards to regex equivalents.\n // Must replace ** before * to avoid double-replacing.\n let regexStr = `^${escapeRegex(normalizedMask)}$`;\n regexStr = regexStr.replace(/\\\\\\*\\\\\\*/g, '.*'); // ** → match any path segments\n regexStr = regexStr.replace(/\\\\\\*/g, '[^/]*'); // * → match within a single segment\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (normalizedMask.includes(keyPlaceholder)) {\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>.+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n const match = maskRegex.exec(normalizedFilePath);\n\n if (!match?.groups) {\n return null;\n }\n\n let locale = match.groups.locale as Locale | undefined;\n let key = (match.groups.key as string | undefined) ?? 'index';\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n const { key, locale: extractedLocale } = extraction;\n\n // Generate what the path SHOULD be for this key/locale using the current builder\n const expectedPath = await parseFilePathPattern(source, {\n key,\n locale: extractedLocale,\n } as any as FilePathPatternContext);\n\n // Resolve both to absolute paths to ensure safe comparison\n const absoluteFoundPath = isAbsolute(file)\n ? file\n : resolve(baseDir, file);\n const absoluteExpectedPath = isAbsolute(expectedPath)\n ? expectedPath\n : resolve(baseDir, expectedPath);\n\n // If the file found doesn't exactly match the file expected, it belongs to another plugin/structure\n if (absoluteFoundPath !== absoluteExpectedPath) {\n continue;\n }\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absoluteFoundPath;\n }\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n const maskWithKey = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale: locales[0],\n } as any as FilePathPatternContext);\n\n const hasKeyInMask = maskWithKey.includes('{{__KEY__}}');\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = await parseFilePathPattern(source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = await listMessages(\n source as FilePathPattern,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * // Example usage:\n * const config = {\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: DictionaryLocation | (string & {});\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionaries created by the plugin.\n *\n * Default: 'intlayer'\n *\n * The format of the dictionaries created by the plugin.\n */\n format?: DictionaryFormat;\n\n /**\n * Whether each top-level key of the JSON file should become its own\n * dictionary (keyed by that top-level key) instead of a single dictionary\n * holding the whole file.\n *\n * This matches the namespace model of libraries such as `next-intl` /\n * `react-intl`, where a single `messages/{locale}.json` file groups several\n * namespaces by its first-level keys and each namespace is addressed\n * independently (e.g. `useTranslations('Hero')` → dictionary `Hero`).\n *\n * When omitted, it is auto-detected: the file is split when the `source`\n * pattern has no `{{key}}` segment (i.e. one file holds every namespace),\n * and kept as a single dictionary otherwise (one file per key).\n *\n * @example\n * ```ts\n * // messages/en.json → dictionaries: Hero, Nav, About, …\n * syncJSON({\n * source: ({ locale }) => `./messages/${locale}.json`,\n * splitKeys: true,\n * })\n * ```\n */\n splitKeys?: boolean;\n};\n\nexport const syncJSON = async (\n options: SyncJSONPluginOptions\n): Promise<Plugin> => {\n // Generate a unique default location based on the source pattern.\n // This ensures that if you have multiple plugins, they don't share the same 'plugin' ID.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const defaultLocation = `sync-json::${patternMarker}`;\n\n const { location, priority, format } = {\n location: defaultLocation,\n priority: 0,\n ...options,\n };\n\n // When the source pattern has no `{{key}}` segment, a single file holds every\n // namespace as a first-level key (the next-intl / react-intl model). In that\n // case each top-level key becomes its own dictionary. Can be forced via the\n // `splitKeys` option.\n const hasKeyPlaceholder = patternMarker.includes('{{key}}');\n const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const appLogger = getAppLogger(configuration);\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n if (dictionariesMap.length === 0) {\n const pattern = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n appLogger(\n `[sync-json] No dictionaries found at locations matching source pattern: ${colorizePath(pattern)}`,\n { level: 'warn' }\n );\n }\n\n let fill: string = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n if (fill) {\n fill = relative(\n configuration.system.baseDir,\n resolve(configuration.system.baseDir, fill)\n );\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // the try/catch does not help here — use ?? {} to guarantee a plain object.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n const filled =\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined;\n\n // One file groups several namespaces by its first-level keys: emit one\n // dictionary per top-level key (e.g. `Hero`, `Nav`, …).\n if (shouldSplitByKeys) {\n for (const [namespaceKey, namespaceContent] of Object.entries(json)) {\n dictionaries.push({\n key: namespaceKey,\n locale,\n fill,\n format,\n localId:\n `${namespaceKey}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: namespaceContent as JSONContent,\n filePath,\n priority,\n });\n }\n continue;\n }\n\n dictionaries.push({\n key,\n locale,\n fill,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: json,\n filePath,\n priority,\n });\n }\n\n return dictionaries;\n },\n\n formatOutput: async ({ dictionary, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n // In split mode several namespaces share the same file; the file is\n // re-assembled in `afterBuild`. Skip here to avoid overwriting the whole\n // file with a single namespace.\n if (shouldSplitByKeys) return dictionary;\n\n const builderPath = await parseFilePathPattern(options.source, {\n key: dictionary.key,\n locale: dictionary.locale,\n } as FilePathPatternContext);\n\n // Verification to ensure we are formatting the correct file\n if (\n resolve(configuration.system.baseDir, builderPath) !==\n resolve(configuration.system.baseDir, dictionary.filePath)\n ) {\n return dictionary;\n }\n\n const formattedOutput = formatDictionaryOutput(dictionary, format);\n\n return formattedOutput.content;\n },\n\n afterBuild: async ({ dictionaries, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { getPerLocaleDictionary } = await import('@intlayer/core/plugins');\n const { parallelize } = await import('@intlayer/chokidar/utils');\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n const { locales } = configuration.internationalization;\n\n // Split mode: every namespace dictionary writes back into the same\n // per-locale file. Re-assemble them under their top-level key and write\n // each file once, instead of one file per key (which would overwrite).\n if (shouldSplitByKeys) {\n const mergedByLocale: Record<string, Record<string, unknown>> = {};\n const filePathByLocale: Record<string, string> = {};\n\n for (const [key, entry] of Object.entries(\n dictionaries.mergedDictionaries\n )) {\n const dictionary = entry.dictionary as Dictionary;\n\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) continue;\n\n for (const locale of locales) {\n const localizedDictionary = getPerLocaleDictionary(\n dictionary,\n locale\n );\n\n const formattedOutput = formatDictionaryOutput(\n localizedDictionary,\n format\n );\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n content !== null &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n continue;\n }\n\n mergedByLocale[locale] ??= {};\n mergedByLocale[locale][key] = content;\n filePathByLocale[locale] = await parseFilePathPattern(\n options.source,\n { key, locale } as any as FilePathPatternContext\n );\n }\n }\n\n await parallelize(Object.keys(mergedByLocale), async (locale) => {\n const builderPath = filePathByLocale[locale];\n\n if (!builderPath) return;\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(mergedByLocale[locale], null, 2);\n\n await writeFile(builderPath, `${stringContent}\\n`, 'utf-8');\n });\n\n return;\n }\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n // We get all dictionaries, but we need to filter them\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) {\n return;\n }\n\n const builderPath = await parseFilePathPattern(options.source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n\n const localizedDictionary = getPerLocaleDictionary(dictionary, locale);\n\n const formattedOutput = formatDictionaryOutput(\n localizedDictionary,\n format\n );\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n return;\n }\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(content, null, 2);\n\n await writeFile(builderPath, `${stringContent}\\n`, 'utf-8');\n });\n },\n };\n};\n"],"mappings":";;;;;;;;;;;AA0BA,MAAM,eAAe,QAAgB,IAAI,QAAQ,uBAAuB,OAAO;AAE/E,MAAa,+BACX,UACA,aACA,SACA,kBAC2C;CAC3C,MAAM,iBAAiB;CACvB,MAAM,oBAAoB;CAG1B,MAA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B,uBAAiB,UAAU,uDACzB,QAAQ,QACR;OAAE;OAAK;OAAQ,CAChB;;;AAIL,UAAM,YAAY,OAAO,KAAK,eAAe,EAAE,OAAO,WAAW;KAC/D,MAAM,cAAc,iBAAiB;AAErC,SAAI,CAAC,YAAa;AAElB,8DAAoB,YAAY,EAAE,EAAE,WAAW,MAAM,CAAC;AAItD,2CAAgB,aAAa,GAFP,KAAK,UAAU,eAAe,SAAS,MAAM,EAEtB,CAAC,KAAK,QAAQ;MAC3D;AAEF;;AAoBF,SAAM,YAV2B,OAAO,QACtC,aAAa,mBACd,CAAC,SAAS,CAAC,KAAK,gBACf,QAAQ,KAAK,YAAY;IACvB;IACA,YAAY,WAAW;IACvB;IACD,EAAE,CAGuB,EAAE,OAAO,EAAE,KAAK,YAAY,aAAa;AAEnE,QAAI,WAAW,aAAa,SAC1B;IAGF,MAAM,cAAc,uDAA2B,QAAQ,QAAQ;KAC7D;KACA;KACD,CAAkC;IAInC,MAAM,kBAAkB,uBAFI,uBAAuB,YAAY,OAG1C,EACnB,OACD;IAED,MAAM,UAAU,KAAK,MAAM,KAAK,UAAU,gBAAgB,QAAQ,CAAC;AAEnE,QACE,OAAO,YAAY,eAClB,OAAO,YAAY,YAClB,OAAO,KAAK,QAAmC,CAAC,WAAW,EAE7D;AAGF,6DAAoB,YAAY,EAAE,EAAE,WAAW,MAAM,CAAC;AAItD,0CAAgB,aAAa,GAFP,KAAK,UAAU,SAAS,MAAM,EAEP,CAAC,KAAK,QAAQ;KAC3D;;EAEL"}
|
package/dist/esm/loadJSON.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { extractKeyAndLocaleFromPath } from "./syncJSON.mjs";
|
|
2
2
|
import { isAbsolute, relative, resolve } from "node:path";
|
|
3
3
|
import { loadExternalFile } from "@intlayer/config/file";
|
|
4
|
+
import { colorizePath, getAppLogger } from "@intlayer/config/logger";
|
|
4
5
|
import { parseFilePathPattern } from "@intlayer/config/utils";
|
|
5
6
|
import fg from "fast-glob";
|
|
6
7
|
|
|
@@ -62,25 +63,50 @@ const loadJSON = (options) => {
|
|
|
62
63
|
return {
|
|
63
64
|
name: "load-json",
|
|
64
65
|
loadDictionaries: async ({ configuration }) => {
|
|
66
|
+
const appLogger = getAppLogger(configuration);
|
|
65
67
|
const dictionariesMap = await loadMessagePathMap(options.source, configuration);
|
|
68
|
+
if (dictionariesMap.length === 0) appLogger(`No dictionaries found at locations matching source pattern: ${colorizePath(await parseFilePathPattern(options.source, {
|
|
69
|
+
key: "{{key}}",
|
|
70
|
+
locale: "{{locale}}"
|
|
71
|
+
}))}`, { level: "warn" });
|
|
72
|
+
const hasKeyPlaceholder = (await parseFilePathPattern(options.source, {
|
|
73
|
+
key: "{{key}}",
|
|
74
|
+
locale: "{{locale}}"
|
|
75
|
+
})).includes("{{key}}");
|
|
76
|
+
const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;
|
|
66
77
|
const dictionaries = [];
|
|
67
78
|
for (const { path, key, locale: entryLocale } of dictionariesMap) {
|
|
68
79
|
const json = await loadExternalFile(path, { logError: false }) ?? {};
|
|
69
80
|
const filePath = relative(configuration.system.baseDir, path);
|
|
70
81
|
const entryUsedLocale = locale ?? entryLocale;
|
|
71
|
-
const
|
|
82
|
+
const filled = entryUsedLocale !== configuration.internationalization.defaultLocale ? true : void 0;
|
|
83
|
+
if (shouldSplitByKeys) {
|
|
84
|
+
for (const [namespaceKey, namespaceContent] of Object.entries(json)) dictionaries.push({
|
|
85
|
+
key: namespaceKey,
|
|
86
|
+
locale: entryUsedLocale,
|
|
87
|
+
fill: filePath,
|
|
88
|
+
format,
|
|
89
|
+
localId: `${namespaceKey}::${location}::${filePath}`,
|
|
90
|
+
location,
|
|
91
|
+
filled,
|
|
92
|
+
content: namespaceContent,
|
|
93
|
+
filePath,
|
|
94
|
+
priority
|
|
95
|
+
});
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
dictionaries.push({
|
|
72
99
|
key,
|
|
73
100
|
locale: entryUsedLocale,
|
|
74
101
|
fill: filePath,
|
|
75
102
|
format,
|
|
76
103
|
localId: `${key}::${location}::${filePath}`,
|
|
77
104
|
location,
|
|
78
|
-
filled
|
|
105
|
+
filled,
|
|
79
106
|
content: json,
|
|
80
107
|
filePath,
|
|
81
108
|
priority
|
|
82
|
-
};
|
|
83
|
-
dictionaries.push(dictionary);
|
|
109
|
+
});
|
|
84
110
|
}
|
|
85
111
|
return dictionaries;
|
|
86
112
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadJSON.mjs","names":["sourcePattern"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n // extractKeyAndLocaleFromPath requires at least one named capture group\n // ({{__LOCALE__}} or {{__KEY__}}) in the mask to return a non-null result.\n // When the mask is fully concrete (e.g. `messages_ICU/en.json` — the source\n // has {{locale}} but no {{key}}), no groups exist and it returns null.\n // In that case, fall back directly to the loop locale and key = 'index'.\n const hasLocaleInMask = maskPatternLocale.includes('{{__LOCALE__}}');\n const hasKeyInMask = maskPatternLocale.includes('{{__KEY__}}');\n\n let key: string;\n let extractedLocale: Locale;\n\n if (hasLocaleInMask || hasKeyInMask) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n key = extraction.key;\n extractedLocale = extraction.locale;\n } else {\n // Mask has no placeholders — the file was found via a concrete locale\n // glob. Attribute it directly to the current loop locale.\n key = 'index';\n extractedLocale = locale;\n }\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absolutePath;\n }\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const sourcePattern = source as FilePathPattern;\n const messages: MessagesRecord = await listMessages(\n sourcePattern,\n configuration\n );\n\n // Always include all discovered locales — loadJSON is read-only and should\n // ingest every locale file that exists, just like syncJSON does.\n const entries = Object.entries(messages) as [\n Locale,\n Record<Dictionary['key'], FilePath>,\n ][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionary content.\n *\n * @example\n * ```ts\n * loadJSON({\n * format: 'icu',\n * })\n * ```\n */\n format?: DictionaryFormat;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale, format } = {\n location: 'plugin',\n priority: 0,\n ...options,\n } as const;\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key, locale: entryLocale } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // use ?? {} to guarantee a plain object regardless.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n // Use the per-entry locale discovered from the file path. If a fixed\n // locale override was provided, use it only as a fallback.\n const entryUsedLocale = (locale ?? entryLocale) as Locale;\n\n const dictionary: Dictionary = {\n key,\n locale: entryUsedLocale,\n fill: filePath,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n entryUsedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;;AAwBA,MAAM,eAAe,OACnB,QACA,kBAC4B;CAC5B,MAAM,EAAE,QAAQ,yBAAyB;CAEzC,MAAM,EAAE,YAAY;CACpB,MAAM,EAAE,YAAY;CAEpB,MAAM,SAAyB,EAAE;AAEjC,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,oBAAoB,MAAM,qBAAqB,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;EAEnC,MAAM,oBAAoB,MAAM,qBAAqB,QAAQ;GAC3D,KAAK;GACL;GACD,CAAkC;AAEnC,MAAI,CAAC,qBAAqB,CAAC,kBACzB;EAOF,MAAM,QAAQ,MAAM,GAJU,kBAAkB,WAAW,KAAK,GAC5D,kBAAkB,MAAM,EAAE,GAC1B,mBAE0C,EAC5C,KAAK,SACN,CAAC;AAEF,OAAK,MAAM,QAAQ,OAAO;GAMxB,MAAM,kBAAkB,kBAAkB,SAAS,iBAAiB;GACpE,MAAM,eAAe,kBAAkB,SAAS,cAAc;GAE9D,IAAI;GACJ,IAAI;AAEJ,OAAI,mBAAmB,cAAc;IACnC,MAAM,aAAa,4BACjB,MACA,mBACA,SACA,OACD;AAED,QAAI,CAAC,WACH;AAGF,UA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,"file":"loadJSON.mjs","names":["sourcePattern"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { colorizePath, getAppLogger } from '@intlayer/config/logger';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n // extractKeyAndLocaleFromPath requires at least one named capture group\n // ({{__LOCALE__}} or {{__KEY__}}) in the mask to return a non-null result.\n // When the mask is fully concrete (e.g. `messages_ICU/en.json` — the source\n // has {{locale}} but no {{key}}), no groups exist and it returns null.\n // In that case, fall back directly to the loop locale and key = 'index'.\n const hasLocaleInMask = maskPatternLocale.includes('{{__LOCALE__}}');\n const hasKeyInMask = maskPatternLocale.includes('{{__KEY__}}');\n\n let key: string;\n let extractedLocale: Locale;\n\n if (hasLocaleInMask || hasKeyInMask) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n key = extraction.key;\n extractedLocale = extraction.locale;\n } else {\n // Mask has no placeholders — the file was found via a concrete locale\n // glob. Attribute it directly to the current loop locale.\n key = 'index';\n extractedLocale = locale;\n }\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absolutePath;\n }\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const sourcePattern = source as FilePathPattern;\n const messages: MessagesRecord = await listMessages(\n sourcePattern,\n configuration\n );\n\n // Always include all discovered locales — loadJSON is read-only and should\n // ingest every locale file that exists, just like syncJSON does.\n const entries = Object.entries(messages) as [\n Locale,\n Record<Dictionary['key'], FilePath>,\n ][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionary content.\n *\n * @example\n * ```ts\n * loadJSON({\n * format: 'icu',\n * })\n * ```\n */\n format?: DictionaryFormat;\n\n /**\n * Whether each top-level key of the JSON file should become its own\n * dictionary (keyed by that top-level key) instead of a single dictionary\n * holding the whole file.\n *\n * This matches the namespace model of libraries such as `next-intl` /\n * `react-intl`, where a single `messages/{locale}.json` file groups several\n * namespaces by its first-level keys.\n *\n * When omitted, it is auto-detected: the file is split when the `source`\n * pattern has no `{{key}}` segment, and kept as a single dictionary otherwise.\n */\n splitKeys?: boolean;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale, format } = {\n location: 'plugin',\n priority: 0,\n ...options,\n } as const;\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const appLogger = getAppLogger(configuration);\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n if (dictionariesMap.length === 0) {\n const pattern = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n appLogger(\n `No dictionaries found at locations matching source pattern: ${colorizePath(pattern)}`,\n { level: 'warn' }\n );\n }\n\n // When the source pattern has no `{{key}}` segment, a single file holds\n // every namespace as a first-level key (the next-intl / react-intl model).\n // In that case each top-level key becomes its own dictionary.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const hasKeyPlaceholder = patternMarker.includes('{{key}}');\n const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key, locale: entryLocale } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // use ?? {} to guarantee a plain object regardless.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n // Use the per-entry locale discovered from the file path. If a fixed\n // locale override was provided, use it only as a fallback.\n const entryUsedLocale = (locale ?? entryLocale) as Locale;\n\n const filled =\n entryUsedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined;\n\n if (shouldSplitByKeys) {\n for (const [namespaceKey, namespaceContent] of Object.entries(json)) {\n dictionaries.push({\n key: namespaceKey,\n locale: entryUsedLocale,\n fill: filePath,\n format,\n localId:\n `${namespaceKey}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: namespaceContent as JSONContent,\n filePath,\n priority,\n });\n }\n continue;\n }\n\n dictionaries.push({\n key,\n locale: entryUsedLocale,\n fill: filePath,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: json,\n filePath,\n priority,\n });\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;;;AAyBA,MAAM,eAAe,OACnB,QACA,kBAC4B;CAC5B,MAAM,EAAE,QAAQ,yBAAyB;CAEzC,MAAM,EAAE,YAAY;CACpB,MAAM,EAAE,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relative, resolve } from "node:path";
|
|
2
2
|
import { loadExternalFile } from "@intlayer/config/file";
|
|
3
|
+
import { colorizePath, getAppLogger } from "@intlayer/config/logger";
|
|
3
4
|
import { parseFilePathPattern } from "@intlayer/config/utils";
|
|
4
5
|
import fg from "fast-glob";
|
|
5
6
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
@@ -92,18 +93,26 @@ const loadMessagePathMap = async (source, configuration) => {
|
|
|
92
93
|
}));
|
|
93
94
|
};
|
|
94
95
|
const syncJSON = async (options) => {
|
|
96
|
+
const patternMarker = await parseFilePathPattern(options.source, {
|
|
97
|
+
key: "{{key}}",
|
|
98
|
+
locale: "{{locale}}"
|
|
99
|
+
});
|
|
95
100
|
const { location, priority, format } = {
|
|
96
|
-
location: `sync-json::${
|
|
97
|
-
key: "{{key}}",
|
|
98
|
-
locale: "{{locale}}"
|
|
99
|
-
})}`,
|
|
101
|
+
location: `sync-json::${patternMarker}`,
|
|
100
102
|
priority: 0,
|
|
101
103
|
...options
|
|
102
104
|
};
|
|
105
|
+
const hasKeyPlaceholder = patternMarker.includes("{{key}}");
|
|
106
|
+
const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;
|
|
103
107
|
return {
|
|
104
108
|
name: "sync-json",
|
|
105
109
|
loadDictionaries: async ({ configuration }) => {
|
|
110
|
+
const appLogger = getAppLogger(configuration);
|
|
106
111
|
const dictionariesMap = await loadMessagePathMap(options.source, configuration);
|
|
112
|
+
if (dictionariesMap.length === 0) appLogger(`[sync-json] No dictionaries found at locations matching source pattern: ${colorizePath(await parseFilePathPattern(options.source, {
|
|
113
|
+
key: "{{key}}",
|
|
114
|
+
locale: "{{locale}}"
|
|
115
|
+
}))}`, { level: "warn" });
|
|
107
116
|
let fill = await parseFilePathPattern(options.source, {
|
|
108
117
|
key: "{{key}}",
|
|
109
118
|
locale: "{{locale}}"
|
|
@@ -113,25 +122,41 @@ const syncJSON = async (options) => {
|
|
|
113
122
|
for (const { locale, path, key } of dictionariesMap) {
|
|
114
123
|
const json = await loadExternalFile(path, { logError: false }) ?? {};
|
|
115
124
|
const filePath = relative(configuration.system.baseDir, path);
|
|
116
|
-
const
|
|
125
|
+
const filled = locale !== configuration.internationalization.defaultLocale ? true : void 0;
|
|
126
|
+
if (shouldSplitByKeys) {
|
|
127
|
+
for (const [namespaceKey, namespaceContent] of Object.entries(json)) dictionaries.push({
|
|
128
|
+
key: namespaceKey,
|
|
129
|
+
locale,
|
|
130
|
+
fill,
|
|
131
|
+
format,
|
|
132
|
+
localId: `${namespaceKey}::${location}::${filePath}`,
|
|
133
|
+
location,
|
|
134
|
+
filled,
|
|
135
|
+
content: namespaceContent,
|
|
136
|
+
filePath,
|
|
137
|
+
priority
|
|
138
|
+
});
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
dictionaries.push({
|
|
117
142
|
key,
|
|
118
143
|
locale,
|
|
119
144
|
fill,
|
|
120
145
|
format,
|
|
121
146
|
localId: `${key}::${location}::${filePath}`,
|
|
122
147
|
location,
|
|
123
|
-
filled
|
|
148
|
+
filled,
|
|
124
149
|
content: json,
|
|
125
150
|
filePath,
|
|
126
151
|
priority
|
|
127
|
-
};
|
|
128
|
-
dictionaries.push(dictionary);
|
|
152
|
+
});
|
|
129
153
|
}
|
|
130
154
|
return dictionaries;
|
|
131
155
|
},
|
|
132
156
|
formatOutput: async ({ dictionary, configuration }) => {
|
|
133
157
|
const { formatDictionaryOutput } = await import("@intlayer/chokidar/build");
|
|
134
158
|
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
159
|
+
if (shouldSplitByKeys) return dictionary;
|
|
135
160
|
const builderPath = await parseFilePathPattern(options.source, {
|
|
136
161
|
key: dictionary.key,
|
|
137
162
|
locale: dictionary.locale
|
|
@@ -144,6 +169,32 @@ const syncJSON = async (options) => {
|
|
|
144
169
|
const { parallelize } = await import("@intlayer/chokidar/utils");
|
|
145
170
|
const { formatDictionaryOutput } = await import("@intlayer/chokidar/build");
|
|
146
171
|
const { locales } = configuration.internationalization;
|
|
172
|
+
if (shouldSplitByKeys) {
|
|
173
|
+
const mergedByLocale = {};
|
|
174
|
+
const filePathByLocale = {};
|
|
175
|
+
for (const [key, entry] of Object.entries(dictionaries.mergedDictionaries)) {
|
|
176
|
+
const dictionary = entry.dictionary;
|
|
177
|
+
if (dictionary.location !== location) continue;
|
|
178
|
+
for (const locale of locales) {
|
|
179
|
+
const formattedOutput = formatDictionaryOutput(getPerLocaleDictionary(dictionary, locale), format);
|
|
180
|
+
const content = JSON.parse(JSON.stringify(formattedOutput.content));
|
|
181
|
+
if (typeof content === "undefined" || typeof content === "object" && content !== null && Object.keys(content).length === 0) continue;
|
|
182
|
+
mergedByLocale[locale] ??= {};
|
|
183
|
+
mergedByLocale[locale][key] = content;
|
|
184
|
+
filePathByLocale[locale] = await parseFilePathPattern(options.source, {
|
|
185
|
+
key,
|
|
186
|
+
locale
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
await parallelize(Object.keys(mergedByLocale), async (locale) => {
|
|
191
|
+
const builderPath = filePathByLocale[locale];
|
|
192
|
+
if (!builderPath) return;
|
|
193
|
+
await mkdir(dirname(builderPath), { recursive: true });
|
|
194
|
+
await writeFile(builderPath, `${JSON.stringify(mergedByLocale[locale], null, 2)}\n`, "utf-8");
|
|
195
|
+
});
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
147
198
|
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
148
199
|
key,
|
|
149
200
|
dictionary: dictionary.dictionary,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncJSON.mjs","names":[],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n DictionaryLocation,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n): { key: string; locale: Locale } | null => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n // fast-glob strips leading \"./\" from returned paths; normalize both sides\n const normalize = (path: string) =>\n path.startsWith('./') ? path.slice(2) : path;\n\n const normalizedFilePath = normalize(filePath);\n const normalizedMask = normalize(maskPattern);\n\n const localesAlternation = locales.join('|');\n\n // Escape special regex chars, then convert glob wildcards to regex equivalents.\n // Must replace ** before * to avoid double-replacing.\n let regexStr = `^${escapeRegex(normalizedMask)}$`;\n regexStr = regexStr.replace(/\\\\\\*\\\\\\*/g, '.*'); // ** → match any path segments\n regexStr = regexStr.replace(/\\\\\\*/g, '[^/]*'); // * → match within a single segment\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (normalizedMask.includes(keyPlaceholder)) {\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>.+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n const match = maskRegex.exec(normalizedFilePath);\n\n if (!match?.groups) {\n return null;\n }\n\n let locale = match.groups.locale as Locale | undefined;\n let key = (match.groups.key as string | undefined) ?? 'index';\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n const { key, locale: extractedLocale } = extraction;\n\n // Generate what the path SHOULD be for this key/locale using the current builder\n const expectedPath = await parseFilePathPattern(source, {\n key,\n locale: extractedLocale,\n } as any as FilePathPatternContext);\n\n // Resolve both to absolute paths to ensure safe comparison\n const absoluteFoundPath = isAbsolute(file)\n ? file\n : resolve(baseDir, file);\n const absoluteExpectedPath = isAbsolute(expectedPath)\n ? expectedPath\n : resolve(baseDir, expectedPath);\n\n // If the file found doesn't exactly match the file expected, it belongs to another plugin/structure\n if (absoluteFoundPath !== absoluteExpectedPath) {\n continue;\n }\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absoluteFoundPath;\n }\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n const maskWithKey = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale: locales[0],\n } as any as FilePathPatternContext);\n\n const hasKeyInMask = maskWithKey.includes('{{__KEY__}}');\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = await parseFilePathPattern(source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = await listMessages(\n source as FilePathPattern,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * // Example usage:\n * const config = {\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: DictionaryLocation | (string & {});\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionaries created by the plugin.\n *\n * Default: 'intlayer'\n *\n * The format of the dictionaries created by the plugin.\n */\n format?: DictionaryFormat;\n};\n\nexport const syncJSON = async (\n options: SyncJSONPluginOptions\n): Promise<Plugin> => {\n // Generate a unique default location based on the source pattern.\n // This ensures that if you have multiple plugins, they don't share the same 'plugin' ID.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const defaultLocation = `sync-json::${patternMarker}`;\n\n const { location, priority, format } = {\n location: defaultLocation,\n priority: 0,\n ...options,\n };\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n let fill: string = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n if (fill) {\n fill = relative(\n configuration.system.baseDir,\n resolve(configuration.system.baseDir, fill)\n );\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // the try/catch does not help here — use ?? {} to guarantee a plain object.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale,\n fill,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n\n formatOutput: async ({ dictionary, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n const builderPath = await parseFilePathPattern(options.source, {\n key: dictionary.key,\n locale: dictionary.locale,\n } as FilePathPatternContext);\n\n // Verification to ensure we are formatting the correct file\n if (\n resolve(configuration.system.baseDir, builderPath) !==\n resolve(configuration.system.baseDir, dictionary.filePath)\n ) {\n return dictionary;\n }\n\n const formattedOutput = formatDictionaryOutput(dictionary, format);\n\n return formattedOutput.content;\n },\n\n afterBuild: async ({ dictionaries, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { getPerLocaleDictionary } = await import('@intlayer/core/plugins');\n const { parallelize } = await import('@intlayer/chokidar/utils');\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n const { locales } = configuration.internationalization;\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n // We get all dictionaries, but we need to filter them\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) {\n return;\n }\n\n const builderPath = await parseFilePathPattern(options.source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n\n const localizedDictionary = getPerLocaleDictionary(dictionary, locale);\n\n const formattedOutput = formatDictionaryOutput(\n localizedDictionary,\n format\n );\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n return;\n }\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(content, null, 2);\n\n await writeFile(builderPath, `${stringContent}\\n`, 'utf-8');\n });\n },\n };\n};\n"],"mappings":";;;;;;;AAyBA,MAAM,eAAe,QAAgB,IAAI,QAAQ,uBAAuB,OAAO;AAE/E,MAAa,+BACX,UACA,aACA,SACA,kBAC2C;CAC3C,MAAM,iBAAiB;CACvB,MAAM,oBAAoB;CAG1B,MAAM,aAAa,SACjB,KAAK,WAAW,KAAK,GAAG,KAAK,MAAM,EAAE,GAAG;CAE1C,MAAM,qBAAqB,UAAU,SAAS;CAC9C,MAAM,iBAAiB,UAAU,YAAY;CAE7C,MAAM,qBAAqB,QAAQ,KAAK,IAAI;CAI5C,IAAI,WAAW,IAAI,YAAY,eAAe,CAAC;AAC/C,YAAW,SAAS,QAAQ,aAAa,KAAK;AAC9C,YAAW,SAAS,QAAQ,SAAS,QAAQ;AAE7C,YAAW,SAAS,QAClB,YAAY,kBAAkB,EAC9B,aAAa,mBAAmB,GACjC;AAED,KAAI,eAAe,SAAS,eAAe,CACzC,YAAW,SAAS,QAAQ,YAAY,eAAe,EAAE,aAAa;CAIxE,MAAM,QAAQ,IADQ,OAAO,SACN,CAAC,KAAK,mBAAmB;AAEhD,KAAI,CAAC,OAAO,OACV,QAAO;CAGT,IAAI,SAAS,MAAM,OAAO;CAC1B,IAAI,MAAO,MAAM,OAAO,OAA8B;AAEtD,KAAI,OAAO,QAAQ,YACjB,OAAM;AAGR,KAAI,OAAO,WAAW,YACpB,UAAS;AAGX,QAAO;EACL;EACA;EACD;;AAGH,MAAM,eAAe,OACnB,QACA,kBAC4B;CAC5B,MAAM,EAAE,QAAQ,yBAAyB;CAEzC,MAAM,EAAE,YAAY;CACpB,MAAM,EAAE,YAAY;CAEpB,MAAM,SAAyB,EAAE;AAEjC,MAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,oBAAoB,MAAM,qBAAqB,QAAQ;GAC3D,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U,SAAS,MAAM,EAEP,CAAC,KAAK,QAAQ;KAC3D;;EAEL"}
|
|
1
|
+
{"version":3,"file":"syncJSON.mjs","names":[],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, relative, resolve } from 'node:path';\nimport { loadExternalFile } from '@intlayer/config/file';\nimport { colorizePath, getAppLogger } from '@intlayer/config/logger';\nimport { parseFilePathPattern } from '@intlayer/config/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type {\n Dictionary,\n DictionaryFormat,\n DictionaryLocation,\n LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type {\n FilePathPattern,\n FilePathPatternContext,\n} from '@intlayer/types/filePathPattern';\nimport type { Plugin } from '@intlayer/types/plugin';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype FilePath = string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n): { key: string; locale: Locale } | null => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n // fast-glob strips leading \"./\" from returned paths; normalize both sides\n const normalize = (path: string) =>\n path.startsWith('./') ? path.slice(2) : path;\n\n const normalizedFilePath = normalize(filePath);\n const normalizedMask = normalize(maskPattern);\n\n const localesAlternation = locales.join('|');\n\n // Escape special regex chars, then convert glob wildcards to regex equivalents.\n // Must replace ** before * to avoid double-replacing.\n let regexStr = `^${escapeRegex(normalizedMask)}$`;\n regexStr = regexStr.replace(/\\\\\\*\\\\\\*/g, '.*'); // ** → match any path segments\n regexStr = regexStr.replace(/\\\\\\*/g, '[^/]*'); // * → match within a single segment\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (normalizedMask.includes(keyPlaceholder)) {\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>.+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n const match = maskRegex.exec(normalizedFilePath);\n\n if (!match?.groups) {\n return null;\n }\n\n let locale = match.groups.locale as Locale | undefined;\n let key = (match.groups.key as string | undefined) ?? 'index';\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = async (\n source: FilePathPattern,\n configuration: IntlayerConfig\n): Promise<MessagesRecord> => {\n const { system, internationalization } = configuration;\n\n const { baseDir } = system;\n const { locales } = internationalization;\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const locale of locales) {\n const globPatternLocale = await parseFilePathPattern(source, {\n key: '**',\n locale,\n } as any as FilePathPatternContext);\n\n const maskPatternLocale = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale,\n } as any as FilePathPatternContext);\n\n if (!globPatternLocale || !maskPatternLocale) {\n continue;\n }\n\n const normalizedGlobPattern = globPatternLocale.startsWith('./')\n ? globPatternLocale.slice(2)\n : globPatternLocale;\n\n const files = await fg(normalizedGlobPattern, {\n cwd: baseDir,\n });\n\n for (const file of files) {\n const extraction = extractKeyAndLocaleFromPath(\n file,\n maskPatternLocale,\n locales,\n locale\n );\n\n if (!extraction) {\n continue;\n }\n\n const { key, locale: extractedLocale } = extraction;\n\n // Generate what the path SHOULD be for this key/locale using the current builder\n const expectedPath = await parseFilePathPattern(source, {\n key,\n locale: extractedLocale,\n } as any as FilePathPatternContext);\n\n // Resolve both to absolute paths to ensure safe comparison\n const absoluteFoundPath = isAbsolute(file)\n ? file\n : resolve(baseDir, file);\n const absoluteExpectedPath = isAbsolute(expectedPath)\n ? expectedPath\n : resolve(baseDir, expectedPath);\n\n // If the file found doesn't exactly match the file expected, it belongs to another plugin/structure\n if (absoluteFoundPath !== absoluteExpectedPath) {\n continue;\n }\n\n const usedLocale = extractedLocale as Locale;\n if (!result[usedLocale]) {\n result[usedLocale] = {};\n }\n\n result[usedLocale][key as Dictionary['key']] = absoluteFoundPath;\n }\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n const maskWithKey = await parseFilePathPattern(source, {\n key: '{{__KEY__}}',\n locale: locales[0],\n } as any as FilePathPatternContext);\n\n const hasKeyInMask = maskWithKey.includes('{{__KEY__}}');\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = await parseFilePathPattern(source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = async (\n source: MessagesRecord | FilePathPattern,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = await listMessages(\n source as FilePathPattern,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.system.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: FilePathPattern;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * // Example usage:\n * const config = {\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: DictionaryLocation | (string & {});\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n\n /**\n * The format of the dictionaries created by the plugin.\n *\n * Default: 'intlayer'\n *\n * The format of the dictionaries created by the plugin.\n */\n format?: DictionaryFormat;\n\n /**\n * Whether each top-level key of the JSON file should become its own\n * dictionary (keyed by that top-level key) instead of a single dictionary\n * holding the whole file.\n *\n * This matches the namespace model of libraries such as `next-intl` /\n * `react-intl`, where a single `messages/{locale}.json` file groups several\n * namespaces by its first-level keys and each namespace is addressed\n * independently (e.g. `useTranslations('Hero')` → dictionary `Hero`).\n *\n * When omitted, it is auto-detected: the file is split when the `source`\n * pattern has no `{{key}}` segment (i.e. one file holds every namespace),\n * and kept as a single dictionary otherwise (one file per key).\n *\n * @example\n * ```ts\n * // messages/en.json → dictionaries: Hero, Nav, About, …\n * syncJSON({\n * source: ({ locale }) => `./messages/${locale}.json`,\n * splitKeys: true,\n * })\n * ```\n */\n splitKeys?: boolean;\n};\n\nexport const syncJSON = async (\n options: SyncJSONPluginOptions\n): Promise<Plugin> => {\n // Generate a unique default location based on the source pattern.\n // This ensures that if you have multiple plugins, they don't share the same 'plugin' ID.\n const patternMarker = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n const defaultLocation = `sync-json::${patternMarker}`;\n\n const { location, priority, format } = {\n location: defaultLocation,\n priority: 0,\n ...options,\n };\n\n // When the source pattern has no `{{key}}` segment, a single file holds every\n // namespace as a first-level key (the next-intl / react-intl model). In that\n // case each top-level key becomes its own dictionary. Can be forced via the\n // `splitKeys` option.\n const hasKeyPlaceholder = patternMarker.includes('{{key}}');\n const shouldSplitByKeys = options.splitKeys ?? !hasKeyPlaceholder;\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const appLogger = getAppLogger(configuration);\n const dictionariesMap: DictionariesMap = await loadMessagePathMap(\n options.source,\n configuration\n );\n\n if (dictionariesMap.length === 0) {\n const pattern = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n appLogger(\n `[sync-json] No dictionaries found at locations matching source pattern: ${colorizePath(pattern)}`,\n { level: 'warn' }\n );\n }\n\n let fill: string = await parseFilePathPattern(options.source, {\n key: '{{key}}',\n locale: '{{locale}}',\n } as any as FilePathPatternContext);\n\n if (fill) {\n fill = relative(\n configuration.system.baseDir,\n resolve(configuration.system.baseDir, fill)\n );\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n // loadExternalFile swallows errors and returns undefined for missing files;\n // the try/catch does not help here — use ?? {} to guarantee a plain object.\n const json: JSONContent =\n (await loadExternalFile(path, { logError: false })) ?? {};\n\n const filePath = relative(configuration.system.baseDir, path);\n\n const filled =\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined;\n\n // One file groups several namespaces by its first-level keys: emit one\n // dictionary per top-level key (e.g. `Hero`, `Nav`, …).\n if (shouldSplitByKeys) {\n for (const [namespaceKey, namespaceContent] of Object.entries(json)) {\n dictionaries.push({\n key: namespaceKey,\n locale,\n fill,\n format,\n localId:\n `${namespaceKey}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: namespaceContent as JSONContent,\n filePath,\n priority,\n });\n }\n continue;\n }\n\n dictionaries.push({\n key,\n locale,\n fill,\n format,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled,\n content: json,\n filePath,\n priority,\n });\n }\n\n return dictionaries;\n },\n\n formatOutput: async ({ dictionary, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n // In split mode several namespaces share the same file; the file is\n // re-assembled in `afterBuild`. Skip here to avoid overwriting the whole\n // file with a single namespace.\n if (shouldSplitByKeys) return dictionary;\n\n const builderPath = await parseFilePathPattern(options.source, {\n key: dictionary.key,\n locale: dictionary.locale,\n } as FilePathPatternContext);\n\n // Verification to ensure we are formatting the correct file\n if (\n resolve(configuration.system.baseDir, builderPath) !==\n resolve(configuration.system.baseDir, dictionary.filePath)\n ) {\n return dictionary;\n }\n\n const formattedOutput = formatDictionaryOutput(dictionary, format);\n\n return formattedOutput.content;\n },\n\n afterBuild: async ({ dictionaries, configuration }) => {\n // Lazy import intlayer modules to avoid circular dependencies\n const { getPerLocaleDictionary } = await import('@intlayer/core/plugins');\n const { parallelize } = await import('@intlayer/chokidar/utils');\n const { formatDictionaryOutput } = await import(\n '@intlayer/chokidar/build'\n );\n\n const { locales } = configuration.internationalization;\n\n // Split mode: every namespace dictionary writes back into the same\n // per-locale file. Re-assemble them under their top-level key and write\n // each file once, instead of one file per key (which would overwrite).\n if (shouldSplitByKeys) {\n const mergedByLocale: Record<string, Record<string, unknown>> = {};\n const filePathByLocale: Record<string, string> = {};\n\n for (const [key, entry] of Object.entries(\n dictionaries.mergedDictionaries\n )) {\n const dictionary = entry.dictionary as Dictionary;\n\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) continue;\n\n for (const locale of locales) {\n const localizedDictionary = getPerLocaleDictionary(\n dictionary,\n locale\n );\n\n const formattedOutput = formatDictionaryOutput(\n localizedDictionary,\n format\n );\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n content !== null &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n continue;\n }\n\n mergedByLocale[locale] ??= {};\n mergedByLocale[locale][key] = content;\n filePathByLocale[locale] = await parseFilePathPattern(\n options.source,\n { key, locale } as any as FilePathPatternContext\n );\n }\n }\n\n await parallelize(Object.keys(mergedByLocale), async (locale) => {\n const builderPath = filePathByLocale[locale];\n\n if (!builderPath) return;\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(mergedByLocale[locale], null, 2);\n\n await writeFile(builderPath, `${stringContent}\\n`, 'utf-8');\n });\n\n return;\n }\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n // We get all dictionaries, but we need to filter them\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n // Only process dictionaries that belong to THIS plugin instance.\n if (dictionary.location !== location) {\n return;\n }\n\n const builderPath = await parseFilePathPattern(options.source, {\n key,\n locale,\n } as any as FilePathPatternContext);\n\n const localizedDictionary = getPerLocaleDictionary(dictionary, locale);\n\n const formattedOutput = formatDictionaryOutput(\n localizedDictionary,\n format\n );\n\n const content = JSON.parse(JSON.stringify(formattedOutput.content));\n\n if (\n typeof content === 'undefined' ||\n (typeof content === 'object' &&\n Object.keys(content as Record<string, unknown>).length === 0)\n ) {\n return;\n }\n\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(content, null,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E,EAAE,WAAW,MAAM,CAAC;AAItD,UAAM,UAAU,aAAa,GAFP,KAAK,UAAU,SAAS,MAAM,EAEP,CAAC,KAAK,QAAQ;KAC3D;;EAEL"}
|
package/dist/types/loadJSON.d.ts
CHANGED
|
@@ -74,6 +74,19 @@ type LoadJSONPluginOptions = {
|
|
|
74
74
|
* ```
|
|
75
75
|
*/
|
|
76
76
|
format?: DictionaryFormat;
|
|
77
|
+
/**
|
|
78
|
+
* Whether each top-level key of the JSON file should become its own
|
|
79
|
+
* dictionary (keyed by that top-level key) instead of a single dictionary
|
|
80
|
+
* holding the whole file.
|
|
81
|
+
*
|
|
82
|
+
* This matches the namespace model of libraries such as `next-intl` /
|
|
83
|
+
* `react-intl`, where a single `messages/{locale}.json` file groups several
|
|
84
|
+
* namespaces by its first-level keys.
|
|
85
|
+
*
|
|
86
|
+
* When omitted, it is auto-detected: the file is split when the `source`
|
|
87
|
+
* pattern has no `{{key}}` segment, and kept as a single dictionary otherwise.
|
|
88
|
+
*/
|
|
89
|
+
splitKeys?: boolean;
|
|
77
90
|
};
|
|
78
91
|
declare const loadJSON: (options: LoadJSONPluginOptions) => Plugin;
|
|
79
92
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loadJSON.d.ts","names":[],"sources":["../../src/loadJSON.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"loadJSON.d.ts","names":[],"sources":["../../src/loadJSON.ts"],"mappings":";;;;;;KAiJK,qBAAA;;AAlIgD;;;;;;;;;;EA8InD,MAAA,EAAQ,eAAA;EAeC;;;;;;;;AA+DX;;;;;EA/DE,MAAA,GAAS,MAAA;EA+D+C;;;;;;;;;;;;;;;;;;;;;;EAvCxD,QAAA;;;;;;;;;EAUA,QAAA;;;;;;;;;;;EAYA,MAAA,GAAS,gBAAA;;;;;;;;;;;;;EAcT,SAAA;AAAA;AAAA,cAGW,QAAA,GAAY,OAAA,EAAS,qBAAA,KAAwB,MAAA"}
|
package/dist/types/syncJSON.d.ts
CHANGED
|
@@ -60,6 +60,30 @@ type SyncJSONPluginOptions = {
|
|
|
60
60
|
* The format of the dictionaries created by the plugin.
|
|
61
61
|
*/
|
|
62
62
|
format?: DictionaryFormat;
|
|
63
|
+
/**
|
|
64
|
+
* Whether each top-level key of the JSON file should become its own
|
|
65
|
+
* dictionary (keyed by that top-level key) instead of a single dictionary
|
|
66
|
+
* holding the whole file.
|
|
67
|
+
*
|
|
68
|
+
* This matches the namespace model of libraries such as `next-intl` /
|
|
69
|
+
* `react-intl`, where a single `messages/{locale}.json` file groups several
|
|
70
|
+
* namespaces by its first-level keys and each namespace is addressed
|
|
71
|
+
* independently (e.g. `useTranslations('Hero')` → dictionary `Hero`).
|
|
72
|
+
*
|
|
73
|
+
* When omitted, it is auto-detected: the file is split when the `source`
|
|
74
|
+
* pattern has no `{{key}}` segment (i.e. one file holds every namespace),
|
|
75
|
+
* and kept as a single dictionary otherwise (one file per key).
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* // messages/en.json → dictionaries: Hero, Nav, About, …
|
|
80
|
+
* syncJSON({
|
|
81
|
+
* source: ({ locale }) => `./messages/${locale}.json`,
|
|
82
|
+
* splitKeys: true,
|
|
83
|
+
* })
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
splitKeys?: boolean;
|
|
63
87
|
};
|
|
64
88
|
declare const syncJSON: (options: SyncJSONPluginOptions) => Promise<Plugin>;
|
|
65
89
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncJSON.d.ts","names":[],"sources":["../../src/syncJSON.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"syncJSON.d.ts","names":[],"sources":["../../src/syncJSON.ts"],"mappings":";;;;;;cA4Ba,2BAAA,GACX,QAAA,UACA,WAAA,UACA,OAAA,EAAS,MAAA,IACT,aAAA,EAAe,MAAA;EACZ,GAAA;EAAa,MAAA,EAAQ,MAAA;AAAA;AAAA,KA0MrB,qBAAA;EA5MM;;;;;;;;;;EAuNT,MAAA,EAAQ,eAAA;EArNL;;;;;AAkDH;;;;;;;;;;;;;;;;;EA2LA,QAAA,GAAW,kBAAA;EA+CA;;;;;;;;EArCX,QAAA;EAsCA;;;;;;;EA7BA,MAAA,GAAS,gBAAA;;;;;;;;;;;;;;;;;;;;;;;;EAyBT,SAAA;AAAA;AAAA,cAGW,QAAA,GACX,OAAA,EAAS,qBAAA,KACR,OAAA,CAAQ,MAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/sync-json-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0-canary.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A plugin for Intlayer that syncs JSON files to dictionaries.",
|
|
6
6
|
"keywords": [
|
|
@@ -71,21 +71,21 @@
|
|
|
71
71
|
"typecheck": "tsc --noEmit --project tsconfig.types.json"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@intlayer/chokidar": "
|
|
75
|
-
"@intlayer/config": "
|
|
76
|
-
"@intlayer/core": "
|
|
77
|
-
"@intlayer/types": "
|
|
74
|
+
"@intlayer/chokidar": "9.0.0-canary.3",
|
|
75
|
+
"@intlayer/config": "9.0.0-canary.3",
|
|
76
|
+
"@intlayer/core": "9.0.0-canary.3",
|
|
77
|
+
"@intlayer/types": "9.0.0-canary.3",
|
|
78
78
|
"fast-glob": "3.3.3"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@types/node": "25.9.
|
|
81
|
+
"@types/node": "25.9.3",
|
|
82
82
|
"@utils/ts-config": "1.0.4",
|
|
83
83
|
"@utils/ts-config-types": "1.0.4",
|
|
84
84
|
"@utils/tsdown-config": "1.0.4",
|
|
85
85
|
"rimraf": "6.1.3",
|
|
86
86
|
"tsdown": "0.21.10",
|
|
87
87
|
"typescript": "6.0.3",
|
|
88
|
-
"vitest": "4.1.
|
|
88
|
+
"vitest": "4.1.9"
|
|
89
89
|
},
|
|
90
90
|
"engines": {
|
|
91
91
|
"node": ">=14.18"
|