@vocab/core 1.6.4 → 1.6.5-fix-messageformat-import-20250923004826
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/chunk-nOFOJqeH.js +30 -0
- package/dist/icu-handler-9mldObYa.js +29 -0
- package/dist/icu-handler-9mldObYa.js.map +1 -0
- package/dist/icu-handler-COUleaeZ.mjs +22 -0
- package/dist/icu-handler-COUleaeZ.mjs.map +1 -0
- package/dist/icu-handler.d.mts +7 -0
- package/dist/icu-handler.d.ts +7 -0
- package/dist/icu-handler.js +3 -0
- package/dist/icu-handler.mjs +3 -0
- package/dist/index.d.mts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +679 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +660 -0
- package/dist/index.mjs.map +1 -0
- package/dist/runtime.d.mts +8 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +13 -0
- package/dist/runtime.js.map +1 -0
- package/dist/runtime.mjs +12 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/translation-file-BEIJ1-3N.js +34 -0
- package/dist/translation-file-BEIJ1-3N.js.map +1 -0
- package/dist/translation-file-CAOIsAU9.d.mts +7 -0
- package/dist/translation-file-DQOiWtfV.d.ts +7 -0
- package/dist/translation-file-Dn19n2oY.mjs +28 -0
- package/dist/translation-file-Dn19n2oY.mjs.map +1 -0
- package/dist/translation-file.d.mts +3 -0
- package/dist/translation-file.d.ts +3 -0
- package/dist/translation-file.js +3 -0
- package/dist/translation-file.mjs +3 -0
- package/dist/types-DFxEF4pq.d.mts +146 -0
- package/dist/types-Di9uIscO.d.ts +146 -0
- package/package.json +15 -25
- package/README.md +0 -827
- package/dist/declarations/src/compile.d.ts +0 -6
- package/dist/declarations/src/config.d.ts +0 -4
- package/dist/declarations/src/icu-handler.d.ts +0 -2
- package/dist/declarations/src/index.d.ts +0 -6
- package/dist/declarations/src/load-translations.d.ts +0 -32
- package/dist/declarations/src/runtime.d.ts +0 -3
- package/dist/declarations/src/translation-file.d.ts +0 -2
- package/dist/declarations/src/types.d.ts +0 -143
- package/dist/declarations/src/utils.d.ts +0 -26
- package/dist/declarations/src/validate/index.d.ts +0 -3
- package/dist/vocab-core.cjs.d.ts +0 -2
- package/dist/vocab-core.cjs.dev.js +0 -893
- package/dist/vocab-core.cjs.js +0 -7
- package/dist/vocab-core.cjs.prod.js +0 -893
- package/dist/vocab-core.esm.js +0 -866
- package/icu-handler/dist/vocab-core-icu-handler.cjs.d.ts +0 -2
- package/icu-handler/dist/vocab-core-icu-handler.cjs.dev.js +0 -31
- package/icu-handler/dist/vocab-core-icu-handler.cjs.js +0 -7
- package/icu-handler/dist/vocab-core-icu-handler.cjs.prod.js +0 -31
- package/icu-handler/dist/vocab-core-icu-handler.esm.js +0 -23
- package/icu-handler/package.json +0 -4
- package/runtime/dist/vocab-core-runtime.cjs.d.ts +0 -2
- package/runtime/dist/vocab-core-runtime.cjs.dev.js +0 -15
- package/runtime/dist/vocab-core-runtime.cjs.js +0 -7
- package/runtime/dist/vocab-core-runtime.cjs.prod.js +0 -15
- package/runtime/dist/vocab-core-runtime.esm.js +0 -10
- package/runtime/package.json +0 -4
- package/translation-file/dist/vocab-core-translation-file.cjs.d.ts +0 -2
- package/translation-file/dist/vocab-core-translation-file.cjs.dev.js +0 -35
- package/translation-file/dist/vocab-core-translation-file.cjs.js +0 -7
- package/translation-file/dist/vocab-core-translation-file.cjs.prod.js +0 -35
- package/translation-file/dist/vocab-core-translation-file.esm.js +0 -31
- package/translation-file/package.json +0 -4
package/dist/vocab-core.esm.js
DELETED
|
@@ -1,866 +0,0 @@
|
|
|
1
|
-
import { existsSync, promises } from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { TYPE, parse, isSelectElement, isPluralElement, isTagElement, isArgumentElement, isNumberElement, isDateElement, isTimeElement } from '@formatjs/icu-messageformat-parser';
|
|
4
|
-
import prettier from 'prettier';
|
|
5
|
-
import chokidar from 'chokidar';
|
|
6
|
-
import pc from 'picocolors';
|
|
7
|
-
import debug from 'debug';
|
|
8
|
-
import glob from 'fast-glob';
|
|
9
|
-
import IntlMessageFormat from 'intl-messageformat';
|
|
10
|
-
import { printAST } from '@formatjs/icu-messageformat-parser/printer';
|
|
11
|
-
import findUp from 'find-up';
|
|
12
|
-
import Validator from 'fastest-validator';
|
|
13
|
-
|
|
14
|
-
const trace = debug(`vocab:core`);
|
|
15
|
-
|
|
16
|
-
const defaultTranslationDirSuffix = '.vocab';
|
|
17
|
-
const devTranslationFileName = 'translations.json';
|
|
18
|
-
const globAnyPathWithOptionalPrefix = '**/?(*)';
|
|
19
|
-
function isDevLanguageFile(filePath) {
|
|
20
|
-
return filePath.endsWith(`/${devTranslationFileName}`) || filePath === devTranslationFileName;
|
|
21
|
-
}
|
|
22
|
-
function isAltLanguageFile(filePath) {
|
|
23
|
-
return filePath.endsWith('.translations.json');
|
|
24
|
-
}
|
|
25
|
-
function isTranslationDirectory(filePath, {
|
|
26
|
-
translationsDirectorySuffix = defaultTranslationDirSuffix
|
|
27
|
-
}) {
|
|
28
|
-
return filePath.endsWith(translationsDirectorySuffix);
|
|
29
|
-
}
|
|
30
|
-
function getTranslationFolderGlob({
|
|
31
|
-
translationsDirectorySuffix = defaultTranslationDirSuffix
|
|
32
|
-
}) {
|
|
33
|
-
const result = `${globAnyPathWithOptionalPrefix}${translationsDirectorySuffix}`;
|
|
34
|
-
trace('getTranslationFolderGlob', result);
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
function getDevTranslationFileGlob({
|
|
38
|
-
translationsDirectorySuffix = defaultTranslationDirSuffix
|
|
39
|
-
}) {
|
|
40
|
-
const result = `${globAnyPathWithOptionalPrefix}${translationsDirectorySuffix}/${devTranslationFileName}`;
|
|
41
|
-
trace('getDevTranslationFileGlob', result);
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
function getAltTranslationFileGlob(config) {
|
|
45
|
-
const altLanguages = getAltLanguages(config);
|
|
46
|
-
const langMatch = altLanguages.length === 1 ? altLanguages[0] : `{${altLanguages.join(',')}}`;
|
|
47
|
-
const {
|
|
48
|
-
translationsDirectorySuffix = defaultTranslationDirSuffix
|
|
49
|
-
} = config;
|
|
50
|
-
const result = `**/*${translationsDirectorySuffix}/${langMatch}.translations.json`;
|
|
51
|
-
trace('getAltTranslationFileGlob', result);
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
function getAltLanguages({
|
|
55
|
-
devLanguage,
|
|
56
|
-
languages
|
|
57
|
-
}) {
|
|
58
|
-
return languages.map(v => v.name).filter(lang => lang !== devLanguage);
|
|
59
|
-
}
|
|
60
|
-
function getDevLanguageFileFromTsFile(tsFilePath) {
|
|
61
|
-
const directory = path.dirname(tsFilePath);
|
|
62
|
-
const result = path.normalize(path.join(directory, devTranslationFileName));
|
|
63
|
-
trace(`Returning dev language path ${result} for path ${tsFilePath}`);
|
|
64
|
-
return result;
|
|
65
|
-
}
|
|
66
|
-
function getDevLanguageFileFromAltLanguageFile(altLanguageFilePath) {
|
|
67
|
-
const directory = path.dirname(altLanguageFilePath);
|
|
68
|
-
const result = path.normalize(path.join(directory, devTranslationFileName));
|
|
69
|
-
trace(`Returning dev language path ${result} for path ${altLanguageFilePath}`);
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
function getTSFileFromDevLanguageFile(devLanguageFilePath) {
|
|
73
|
-
const directory = path.dirname(devLanguageFilePath);
|
|
74
|
-
const result = path.normalize(path.join(directory, 'index.ts'));
|
|
75
|
-
trace(`Returning TS path ${result} for path ${devLanguageFilePath}`);
|
|
76
|
-
return result;
|
|
77
|
-
}
|
|
78
|
-
function getAltLanguageFilePath(devLanguageFilePath, language) {
|
|
79
|
-
const directory = path.dirname(devLanguageFilePath);
|
|
80
|
-
const result = path.normalize(path.join(directory, `${language}.translations.json`));
|
|
81
|
-
trace(`Returning alt language path ${result} for path ${devLanguageFilePath}`);
|
|
82
|
-
return path.normalize(result);
|
|
83
|
-
}
|
|
84
|
-
function mapValues(obj, func) {
|
|
85
|
-
const newObj = {};
|
|
86
|
-
const keys = Object.keys(obj);
|
|
87
|
-
for (const key of keys) {
|
|
88
|
-
newObj[key] = func(obj[key]);
|
|
89
|
-
}
|
|
90
|
-
return newObj;
|
|
91
|
-
}
|
|
92
|
-
function getTranslationMessages(translations) {
|
|
93
|
-
return mapValues(translations, v => v.message);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function generateLanguageFromTranslations({
|
|
97
|
-
baseTranslations,
|
|
98
|
-
generator
|
|
99
|
-
}) {
|
|
100
|
-
if (!generator.transformElement && !generator.transformMessage) {
|
|
101
|
-
return baseTranslations;
|
|
102
|
-
}
|
|
103
|
-
const translationKeys = Object.keys(baseTranslations);
|
|
104
|
-
const generatedTranslations = {};
|
|
105
|
-
for (const translationKey of translationKeys) {
|
|
106
|
-
const translation = baseTranslations[translationKey];
|
|
107
|
-
let transformedMessage = translation.message;
|
|
108
|
-
if (generator.transformElement) {
|
|
109
|
-
const messageAst = new IntlMessageFormat(translation.message).getAst();
|
|
110
|
-
const transformedAst = messageAst.map(transformMessageFormatElement(generator.transformElement));
|
|
111
|
-
transformedMessage = printAST(transformedAst);
|
|
112
|
-
}
|
|
113
|
-
if (generator.transformMessage) {
|
|
114
|
-
transformedMessage = generator.transformMessage(transformedMessage);
|
|
115
|
-
}
|
|
116
|
-
generatedTranslations[translationKey] = {
|
|
117
|
-
message: transformedMessage
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return generatedTranslations;
|
|
121
|
-
}
|
|
122
|
-
function transformMessageFormatElement(transformElement) {
|
|
123
|
-
return messageFormatElement => {
|
|
124
|
-
const transformedMessageFormatElement = {
|
|
125
|
-
...messageFormatElement
|
|
126
|
-
};
|
|
127
|
-
switch (transformedMessageFormatElement.type) {
|
|
128
|
-
case TYPE.literal:
|
|
129
|
-
const transformedValue = transformElement(transformedMessageFormatElement.value);
|
|
130
|
-
transformedMessageFormatElement.value = transformedValue;
|
|
131
|
-
break;
|
|
132
|
-
case TYPE.select:
|
|
133
|
-
case TYPE.plural:
|
|
134
|
-
const transformedOptions = {
|
|
135
|
-
...transformedMessageFormatElement.options
|
|
136
|
-
};
|
|
137
|
-
for (const key of Object.keys(transformedOptions)) {
|
|
138
|
-
transformedOptions[key].value = transformedOptions[key].value.map(transformMessageFormatElement(transformElement));
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
case TYPE.tag:
|
|
142
|
-
const transformedChildren = transformedMessageFormatElement.children.map(transformMessageFormatElement(transformElement));
|
|
143
|
-
transformedMessageFormatElement.children = transformedChildren;
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
return transformedMessageFormatElement;
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function getUniqueKey(key, namespace) {
|
|
151
|
-
return `${key}.${namespace}`;
|
|
152
|
-
}
|
|
153
|
-
function mergeWithDevLanguageTranslation({
|
|
154
|
-
translation,
|
|
155
|
-
devTranslation
|
|
156
|
-
}) {
|
|
157
|
-
// Only use keys from the dev translation
|
|
158
|
-
const keys = Object.keys(devTranslation);
|
|
159
|
-
const newLanguage = {};
|
|
160
|
-
for (const key of keys) {
|
|
161
|
-
if (translation[key]) {
|
|
162
|
-
newLanguage[key] = {
|
|
163
|
-
message: translation[key].message,
|
|
164
|
-
description: devTranslation[key].description
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return newLanguage;
|
|
169
|
-
}
|
|
170
|
-
function getLanguageFallbacks({
|
|
171
|
-
languages
|
|
172
|
-
}) {
|
|
173
|
-
const languageFallbackMap = new Map();
|
|
174
|
-
for (const lang of languages) {
|
|
175
|
-
if (lang.extends) {
|
|
176
|
-
languageFallbackMap.set(lang.name, lang.extends);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return languageFallbackMap;
|
|
180
|
-
}
|
|
181
|
-
function getLanguageHierarchy({
|
|
182
|
-
languages
|
|
183
|
-
}) {
|
|
184
|
-
const hierarchyMap = new Map();
|
|
185
|
-
const fallbacks = getLanguageFallbacks({
|
|
186
|
-
languages
|
|
187
|
-
});
|
|
188
|
-
for (const lang of languages) {
|
|
189
|
-
const langHierarchy = [];
|
|
190
|
-
let currLang = lang.extends;
|
|
191
|
-
while (currLang) {
|
|
192
|
-
langHierarchy.push(currLang);
|
|
193
|
-
currLang = fallbacks.get(currLang);
|
|
194
|
-
}
|
|
195
|
-
hierarchyMap.set(lang.name, langHierarchy);
|
|
196
|
-
}
|
|
197
|
-
return hierarchyMap;
|
|
198
|
-
}
|
|
199
|
-
function getFallbackLanguageOrder({
|
|
200
|
-
languages,
|
|
201
|
-
languageName,
|
|
202
|
-
devLanguage,
|
|
203
|
-
fallbacks
|
|
204
|
-
}) {
|
|
205
|
-
const languageHierarchy = getLanguageHierarchy({
|
|
206
|
-
languages
|
|
207
|
-
}).get(languageName);
|
|
208
|
-
if (!languageHierarchy) {
|
|
209
|
-
throw new Error(`Missing language hierarchy for ${languageName}`);
|
|
210
|
-
}
|
|
211
|
-
const fallbackLanguageOrder = [languageName];
|
|
212
|
-
if (fallbacks !== 'none') {
|
|
213
|
-
fallbackLanguageOrder.unshift(...languageHierarchy.reverse());
|
|
214
|
-
if (fallbacks === 'all' && fallbackLanguageOrder[0] !== devLanguage) {
|
|
215
|
-
fallbackLanguageOrder.unshift(devLanguage);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return fallbackLanguageOrder;
|
|
219
|
-
}
|
|
220
|
-
function getNamespaceByFilePath(relativePath, {
|
|
221
|
-
translationsDirectorySuffix = defaultTranslationDirSuffix
|
|
222
|
-
}) {
|
|
223
|
-
let namespace = path.dirname(relativePath).replace(/^src\//, '').replace(/\//g, '_');
|
|
224
|
-
if (namespace.endsWith(translationsDirectorySuffix)) {
|
|
225
|
-
namespace = namespace.slice(0, -translationsDirectorySuffix.length);
|
|
226
|
-
}
|
|
227
|
-
return namespace;
|
|
228
|
-
}
|
|
229
|
-
function printValidationError(...params) {
|
|
230
|
-
// eslint-disable-next-line no-console
|
|
231
|
-
console.error(pc.red('Error loading translation:'), ...params);
|
|
232
|
-
}
|
|
233
|
-
function getTranslationsFromFile(translationFileContents, {
|
|
234
|
-
isAltLanguage,
|
|
235
|
-
filePath,
|
|
236
|
-
withTags
|
|
237
|
-
}) {
|
|
238
|
-
if (!translationFileContents || typeof translationFileContents !== 'object') {
|
|
239
|
-
throw new Error(`Unable to read translation file ${filePath}. Translations must be an object.`);
|
|
240
|
-
}
|
|
241
|
-
const {
|
|
242
|
-
$namespace,
|
|
243
|
-
_meta,
|
|
244
|
-
...keys
|
|
245
|
-
} = translationFileContents;
|
|
246
|
-
if (isAltLanguage && $namespace) {
|
|
247
|
-
printValidationError(`Found $namespace in alt language file in ${filePath}. $namespace is only used in the dev language and will be ignored.`);
|
|
248
|
-
}
|
|
249
|
-
if (!isAltLanguage && $namespace && typeof $namespace !== 'string') {
|
|
250
|
-
printValidationError(`Found non-string $namespace in language file in ${filePath}. $namespace must be a string.`);
|
|
251
|
-
}
|
|
252
|
-
if (isAltLanguage && _meta !== null && _meta !== void 0 && _meta.tags) {
|
|
253
|
-
printValidationError(`Found _meta.tags in alt language file in ${filePath}. _meta.tags is only used in the dev language and will be ignored.`);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Never return tags if we're fetching translations for an alt language
|
|
257
|
-
const includeTags = !isAltLanguage && withTags;
|
|
258
|
-
const validKeys = {};
|
|
259
|
-
for (const [translationKey, {
|
|
260
|
-
tags,
|
|
261
|
-
...translation
|
|
262
|
-
}] of Object.entries(keys)) {
|
|
263
|
-
if (typeof translation === 'string') {
|
|
264
|
-
printValidationError(`Found string for a translation "${translationKey}" in ${filePath}. Translation must be an object of the format {message: string}.`);
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
if (!translation) {
|
|
268
|
-
printValidationError(`Found empty translation "${translationKey}" in ${filePath}. Translation must be an object of the format {message: string}.`);
|
|
269
|
-
continue;
|
|
270
|
-
}
|
|
271
|
-
if (!translation.message || typeof translation.message !== 'string') {
|
|
272
|
-
printValidationError(`No message found for translation "${translationKey}" in ${filePath}. Translation must be an object of the format {message: string}.`);
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
validKeys[translationKey] = {
|
|
276
|
-
...translation,
|
|
277
|
-
tags: includeTags ? tags : undefined
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
const metadata = {
|
|
281
|
-
tags: includeTags ? _meta === null || _meta === void 0 ? void 0 : _meta.tags : undefined
|
|
282
|
-
};
|
|
283
|
-
return {
|
|
284
|
-
$namespace,
|
|
285
|
-
keys: validKeys,
|
|
286
|
-
metadata
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
function loadAltLanguageFile({
|
|
290
|
-
filePath,
|
|
291
|
-
languageName,
|
|
292
|
-
devTranslation,
|
|
293
|
-
fallbacks
|
|
294
|
-
}, {
|
|
295
|
-
devLanguage,
|
|
296
|
-
languages
|
|
297
|
-
}) {
|
|
298
|
-
const altLanguageTranslation = {};
|
|
299
|
-
const fallbackLanguageOrder = getFallbackLanguageOrder({
|
|
300
|
-
languages,
|
|
301
|
-
languageName,
|
|
302
|
-
devLanguage,
|
|
303
|
-
fallbacks
|
|
304
|
-
});
|
|
305
|
-
trace(`Loading alt language file with precedence: ${fallbackLanguageOrder.slice().reverse().join(' -> ')}`);
|
|
306
|
-
for (const fallbackLanguage of fallbackLanguageOrder) {
|
|
307
|
-
if (fallbackLanguage !== devLanguage) {
|
|
308
|
-
try {
|
|
309
|
-
const altFilePath = getAltLanguageFilePath(filePath, fallbackLanguage);
|
|
310
|
-
delete require.cache[altFilePath];
|
|
311
|
-
|
|
312
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
313
|
-
const translationFile = require(altFilePath);
|
|
314
|
-
const {
|
|
315
|
-
keys: fallbackLanguageTranslation
|
|
316
|
-
} = getTranslationsFromFile(translationFile, {
|
|
317
|
-
filePath: altFilePath,
|
|
318
|
-
isAltLanguage: true
|
|
319
|
-
});
|
|
320
|
-
Object.assign(altLanguageTranslation, mergeWithDevLanguageTranslation({
|
|
321
|
-
translation: fallbackLanguageTranslation,
|
|
322
|
-
devTranslation
|
|
323
|
-
}));
|
|
324
|
-
} catch {
|
|
325
|
-
trace(`Missing alt language file ${getAltLanguageFilePath(filePath, fallbackLanguage)}
|
|
326
|
-
`);
|
|
327
|
-
}
|
|
328
|
-
} else {
|
|
329
|
-
Object.assign(altLanguageTranslation, devTranslation);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
return altLanguageTranslation;
|
|
333
|
-
}
|
|
334
|
-
function stripTagsFromTranslations(translations) {
|
|
335
|
-
return Object.fromEntries(Object.entries(translations).map(([key, {
|
|
336
|
-
tags,
|
|
337
|
-
...rest
|
|
338
|
-
}]) => [key, rest]));
|
|
339
|
-
}
|
|
340
|
-
function loadTranslation({
|
|
341
|
-
filePath,
|
|
342
|
-
fallbacks,
|
|
343
|
-
withTags
|
|
344
|
-
}, userConfig) {
|
|
345
|
-
trace(`Loading translation file in "${fallbacks}" fallback mode: "${filePath}"`);
|
|
346
|
-
const languageSet = {};
|
|
347
|
-
delete require.cache[filePath];
|
|
348
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
349
|
-
const translationContent = require(filePath);
|
|
350
|
-
const relativePath = path.relative(userConfig.projectRoot || process.cwd(), filePath);
|
|
351
|
-
const {
|
|
352
|
-
$namespace,
|
|
353
|
-
keys: devTranslation,
|
|
354
|
-
metadata
|
|
355
|
-
} = getTranslationsFromFile(translationContent, {
|
|
356
|
-
filePath,
|
|
357
|
-
isAltLanguage: false,
|
|
358
|
-
withTags
|
|
359
|
-
});
|
|
360
|
-
const namespace = typeof $namespace === 'string' ? $namespace : getNamespaceByFilePath(relativePath, userConfig);
|
|
361
|
-
trace(`Found file ${filePath}. Using namespace ${namespace}`);
|
|
362
|
-
languageSet[userConfig.devLanguage] = devTranslation;
|
|
363
|
-
const devTranslationNoTags = withTags ? stripTagsFromTranslations(devTranslation) : devTranslation;
|
|
364
|
-
const altLanguages = getAltLanguages(userConfig);
|
|
365
|
-
for (const languageName of altLanguages) {
|
|
366
|
-
languageSet[languageName] = loadAltLanguageFile({
|
|
367
|
-
filePath,
|
|
368
|
-
languageName,
|
|
369
|
-
devTranslation: devTranslationNoTags,
|
|
370
|
-
fallbacks
|
|
371
|
-
}, userConfig);
|
|
372
|
-
}
|
|
373
|
-
for (const generatedLanguage of userConfig.generatedLanguages || []) {
|
|
374
|
-
const {
|
|
375
|
-
name: generatedLanguageName,
|
|
376
|
-
generator
|
|
377
|
-
} = generatedLanguage;
|
|
378
|
-
const baseLanguage = generatedLanguage.extends || userConfig.devLanguage;
|
|
379
|
-
const baseTranslations = languageSet[baseLanguage];
|
|
380
|
-
languageSet[generatedLanguageName] = generateLanguageFromTranslations({
|
|
381
|
-
baseTranslations,
|
|
382
|
-
generator
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
return {
|
|
386
|
-
filePath,
|
|
387
|
-
keys: Object.keys(devTranslation),
|
|
388
|
-
namespace,
|
|
389
|
-
relativePath,
|
|
390
|
-
languages: languageSet,
|
|
391
|
-
metadata
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
async function loadAllTranslations({
|
|
395
|
-
fallbacks,
|
|
396
|
-
includeNodeModules,
|
|
397
|
-
withTags
|
|
398
|
-
}, config) {
|
|
399
|
-
const {
|
|
400
|
-
projectRoot,
|
|
401
|
-
ignore = []
|
|
402
|
-
} = config;
|
|
403
|
-
const translationFiles = await glob(getDevTranslationFileGlob(config), {
|
|
404
|
-
ignore: includeNodeModules ? ignore : [...ignore, '**/node_modules/**'],
|
|
405
|
-
absolute: true,
|
|
406
|
-
cwd: projectRoot
|
|
407
|
-
});
|
|
408
|
-
trace(`Found ${translationFiles.length} translation files`);
|
|
409
|
-
const loadedTranslations = [];
|
|
410
|
-
const keys = new Set();
|
|
411
|
-
for (const translationFile of translationFiles) {
|
|
412
|
-
const loadedTranslation = loadTranslation({
|
|
413
|
-
filePath: translationFile,
|
|
414
|
-
fallbacks,
|
|
415
|
-
withTags
|
|
416
|
-
}, config);
|
|
417
|
-
loadedTranslations.push(loadedTranslation);
|
|
418
|
-
for (const key of loadedTranslation.keys) {
|
|
419
|
-
const uniqueKey = getUniqueKey(key, loadedTranslation.namespace);
|
|
420
|
-
if (keys.has(uniqueKey)) {
|
|
421
|
-
trace(`Duplicate keys found`);
|
|
422
|
-
throw new Error(`Duplicate keys found. Key with namespace ${loadedTranslation.namespace} and key ${key} was found multiple times.`);
|
|
423
|
-
}
|
|
424
|
-
keys.add(uniqueKey);
|
|
425
|
-
const globalKey = loadedTranslation.languages[config.devLanguage][key].globalKey;
|
|
426
|
-
if (globalKey) {
|
|
427
|
-
if (keys.has(globalKey)) {
|
|
428
|
-
throw new Error(`Duplicate keys found. Key with global key ${globalKey} and key ${key} was found multiple times`);
|
|
429
|
-
}
|
|
430
|
-
keys.add(globalKey);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
return loadedTranslations;
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
function extractHasTags(ast) {
|
|
438
|
-
return ast.some(element => {
|
|
439
|
-
if (isSelectElement(element) || isPluralElement(element)) {
|
|
440
|
-
const children = Object.values(element.options).map(o => o.value);
|
|
441
|
-
return children.some(child => extractHasTags(child));
|
|
442
|
-
}
|
|
443
|
-
return isTagElement(element);
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
function extractParamTypes(ast, currentParams) {
|
|
447
|
-
let params = {
|
|
448
|
-
...currentParams
|
|
449
|
-
};
|
|
450
|
-
let vocabTypesImports = new Set();
|
|
451
|
-
for (const element of ast) {
|
|
452
|
-
if (isArgumentElement(element)) {
|
|
453
|
-
if (!(element.value in params)) {
|
|
454
|
-
// Preserve existing types for parameters that we've already parsed
|
|
455
|
-
// This applies to self-referential parameters, for example `{foo, plural, 1 {{foo} thing} other {{foo} things}}`
|
|
456
|
-
params[element.value] = 'string';
|
|
457
|
-
}
|
|
458
|
-
} else if (isNumberElement(element)) {
|
|
459
|
-
params[element.value] = 'number';
|
|
460
|
-
} else if (isPluralElement(element)) {
|
|
461
|
-
params[element.value] = 'number';
|
|
462
|
-
const children = Object.values(element.options).map(o => o.value);
|
|
463
|
-
for (const child of children) {
|
|
464
|
-
const [newParams, subImports] = extractParamTypes(child, params);
|
|
465
|
-
vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
|
|
466
|
-
params = newParams;
|
|
467
|
-
}
|
|
468
|
-
} else if (isDateElement(element) || isTimeElement(element)) {
|
|
469
|
-
params[element.value] = 'Date | number';
|
|
470
|
-
} else if (isTagElement(element)) {
|
|
471
|
-
params[element.value] = 'FormatXMLElementFn<T>';
|
|
472
|
-
vocabTypesImports.add('FormatXMLElementFn');
|
|
473
|
-
const [newParams, subImports] = extractParamTypes(element.children, params);
|
|
474
|
-
vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
|
|
475
|
-
params = newParams;
|
|
476
|
-
} else if (isSelectElement(element)) {
|
|
477
|
-
const options = Object.keys(element.options);
|
|
478
|
-
|
|
479
|
-
// `other` will always be an option as the parser enforces this by default
|
|
480
|
-
const nonOtherOptions = options.filter(o => o !== 'other');
|
|
481
|
-
const nonOtherOptionsUnion = nonOtherOptions.map(o => `'${o}'`).join(' | ');
|
|
482
|
-
params[element.value] = `StringWithSuggestions<${nonOtherOptionsUnion}>`;
|
|
483
|
-
vocabTypesImports.add('StringWithSuggestions');
|
|
484
|
-
const children = Object.values(element.options).map(o => o.value);
|
|
485
|
-
for (const child of children) {
|
|
486
|
-
const [newParams, subImports] = extractParamTypes(child, params);
|
|
487
|
-
vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
|
|
488
|
-
params = newParams;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
return [params, vocabTypesImports];
|
|
493
|
-
}
|
|
494
|
-
function serialiseObjectToType(v) {
|
|
495
|
-
let result = '';
|
|
496
|
-
for (const [key, value] of Object.entries(v)) {
|
|
497
|
-
result += `${JSON.stringify(key)}: ${value && typeof value === 'object' ? serialiseObjectToType(value) : value},`;
|
|
498
|
-
}
|
|
499
|
-
return `{ ${result} }`;
|
|
500
|
-
}
|
|
501
|
-
const serializeTypeImports = (imports, moduleName) => {
|
|
502
|
-
if (imports.size === 0) {
|
|
503
|
-
return '';
|
|
504
|
-
}
|
|
505
|
-
const importNames = Array.from(imports);
|
|
506
|
-
return `import type { ${importNames.join(', ')} } from '${moduleName}';`;
|
|
507
|
-
};
|
|
508
|
-
function serialiseTranslationRuntime(value, imports, loadedTranslation) {
|
|
509
|
-
trace('Serialising translations:', loadedTranslation);
|
|
510
|
-
const translationsType = {};
|
|
511
|
-
for (const [key, {
|
|
512
|
-
params,
|
|
513
|
-
message,
|
|
514
|
-
hasTags
|
|
515
|
-
}] of value.entries()) {
|
|
516
|
-
let translationFunctionString = `() => ${message}`;
|
|
517
|
-
if (Object.keys(params).length > 0) {
|
|
518
|
-
const formatGeneric = hasTags ? '<T = string>' : '';
|
|
519
|
-
const formatReturn = hasTags && imports.has('FormatXMLElementFn') ? 'ReturnType<FormatXMLElementFn<T>>' : 'string';
|
|
520
|
-
translationFunctionString = `${formatGeneric}(values: ${serialiseObjectToType(params)}) => ${formatReturn}`;
|
|
521
|
-
}
|
|
522
|
-
translationsType[key] = translationFunctionString;
|
|
523
|
-
}
|
|
524
|
-
const languagesUnionAsString = Object.keys(loadedTranslation.languages).map(l => `'${l}'`).join(' | ');
|
|
525
|
-
const languageEntries = Object.entries(loadedTranslation.languages).map(([languageName, translations]) => `${JSON.stringify(languageName)}: createLanguage(${JSON.stringify(getTranslationMessages(translations))})`).join(',');
|
|
526
|
-
return /* ts */`
|
|
527
|
-
// This file is automatically generated by Vocab.
|
|
528
|
-
// To make changes update translation.json files directly.
|
|
529
|
-
|
|
530
|
-
${serializeTypeImports(imports, '@vocab/core')}
|
|
531
|
-
import { createLanguage, createTranslationFile } from '@vocab/core/runtime';
|
|
532
|
-
|
|
533
|
-
const translations = createTranslationFile<
|
|
534
|
-
${languagesUnionAsString},
|
|
535
|
-
${serialiseObjectToType(translationsType)}
|
|
536
|
-
>({
|
|
537
|
-
${languageEntries}
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
export default translations;
|
|
541
|
-
`;
|
|
542
|
-
}
|
|
543
|
-
async function generateRuntime(loadedTranslation) {
|
|
544
|
-
const {
|
|
545
|
-
languages: loadedLanguages,
|
|
546
|
-
filePath
|
|
547
|
-
} = loadedTranslation;
|
|
548
|
-
trace('Generating types for', filePath);
|
|
549
|
-
const translationTypes = new Map();
|
|
550
|
-
let imports = new Set();
|
|
551
|
-
for (const key of loadedTranslation.keys) {
|
|
552
|
-
let params = {};
|
|
553
|
-
const messages = new Set();
|
|
554
|
-
let hasTags = false;
|
|
555
|
-
for (const translatedLanguage of Object.values(loadedLanguages)) {
|
|
556
|
-
if (translatedLanguage[key]) {
|
|
557
|
-
const ast = parse(translatedLanguage[key].message);
|
|
558
|
-
hasTags = hasTags || extractHasTags(ast);
|
|
559
|
-
const [parsedParams, vocabTypesImports] = extractParamTypes(ast, params);
|
|
560
|
-
imports = new Set([...imports, ...vocabTypesImports]);
|
|
561
|
-
params = parsedParams;
|
|
562
|
-
messages.add(JSON.stringify(translatedLanguage[key].message));
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
translationTypes.set(key, {
|
|
566
|
-
params,
|
|
567
|
-
hasTags,
|
|
568
|
-
message: Array.from(messages).join(' | ')
|
|
569
|
-
});
|
|
570
|
-
}
|
|
571
|
-
const prettierConfig = await prettier.resolveConfig(filePath);
|
|
572
|
-
const serializedTranslationType = serialiseTranslationRuntime(translationTypes, imports, loadedTranslation);
|
|
573
|
-
const declaration = await prettier.format(serializedTranslationType, {
|
|
574
|
-
...prettierConfig,
|
|
575
|
-
parser: 'typescript'
|
|
576
|
-
});
|
|
577
|
-
const outputFilePath = getTSFileFromDevLanguageFile(filePath);
|
|
578
|
-
trace(`Writing translation types to ${outputFilePath}`);
|
|
579
|
-
await writeIfChanged(outputFilePath, declaration);
|
|
580
|
-
}
|
|
581
|
-
function watch(config) {
|
|
582
|
-
const cwd = config.projectRoot || process.cwd();
|
|
583
|
-
const watcher = chokidar.watch([getDevTranslationFileGlob(config), getAltTranslationFileGlob(config), getTranslationFolderGlob(config)], {
|
|
584
|
-
cwd,
|
|
585
|
-
ignored: config.ignore ? [...config.ignore, '**/node_modules/**'] : ['**/node_modules/**'],
|
|
586
|
-
ignoreInitial: true
|
|
587
|
-
});
|
|
588
|
-
const onTranslationChange = async relativePath => {
|
|
589
|
-
trace(`Detected change for file ${relativePath}`);
|
|
590
|
-
let targetFile;
|
|
591
|
-
if (isDevLanguageFile(relativePath)) {
|
|
592
|
-
targetFile = path.resolve(cwd, relativePath);
|
|
593
|
-
} else if (isAltLanguageFile(relativePath)) {
|
|
594
|
-
targetFile = getDevLanguageFileFromAltLanguageFile(path.resolve(cwd, relativePath));
|
|
595
|
-
}
|
|
596
|
-
if (targetFile) {
|
|
597
|
-
try {
|
|
598
|
-
const loadedTranslation = loadTranslation({
|
|
599
|
-
filePath: targetFile,
|
|
600
|
-
fallbacks: 'all'
|
|
601
|
-
}, config);
|
|
602
|
-
await generateRuntime(loadedTranslation);
|
|
603
|
-
} catch (e) {
|
|
604
|
-
// eslint-disable-next-line no-console
|
|
605
|
-
console.log('Failed to generate types for', relativePath);
|
|
606
|
-
// eslint-disable-next-line no-console
|
|
607
|
-
console.error(e);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
};
|
|
611
|
-
const onNewDirectory = async relativePath => {
|
|
612
|
-
trace('Detected new directory', relativePath);
|
|
613
|
-
if (!isTranslationDirectory(relativePath, config)) {
|
|
614
|
-
trace('Ignoring non-translation directory:', relativePath);
|
|
615
|
-
return;
|
|
616
|
-
}
|
|
617
|
-
const newFilePath = path.join(relativePath, devTranslationFileName);
|
|
618
|
-
if (!existsSync(newFilePath)) {
|
|
619
|
-
await promises.writeFile(newFilePath, JSON.stringify({}, null, 2));
|
|
620
|
-
trace('Created new empty translation file:', newFilePath);
|
|
621
|
-
} else {
|
|
622
|
-
trace(`New directory already contains translation file. Skipping creation. Existing file ${newFilePath}`);
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
watcher.on('addDir', onNewDirectory);
|
|
626
|
-
watcher.on('add', onTranslationChange).on('change', onTranslationChange);
|
|
627
|
-
return () => watcher.close();
|
|
628
|
-
}
|
|
629
|
-
async function compile({
|
|
630
|
-
watch: shouldWatch = false
|
|
631
|
-
} = {}, config) {
|
|
632
|
-
const translations = await loadAllTranslations({
|
|
633
|
-
fallbacks: 'all',
|
|
634
|
-
includeNodeModules: false
|
|
635
|
-
}, config);
|
|
636
|
-
for (const loadedTranslation of translations) {
|
|
637
|
-
await generateRuntime(loadedTranslation);
|
|
638
|
-
}
|
|
639
|
-
if (shouldWatch) {
|
|
640
|
-
trace('Listening for changes to files...');
|
|
641
|
-
return watch(config);
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
async function writeIfChanged(filepath, contents) {
|
|
645
|
-
let hasChanged = true;
|
|
646
|
-
try {
|
|
647
|
-
const existingContents = await promises.readFile(filepath, {
|
|
648
|
-
encoding: 'utf-8'
|
|
649
|
-
});
|
|
650
|
-
hasChanged = existingContents !== contents;
|
|
651
|
-
} catch {
|
|
652
|
-
// ignore error, likely a file doesn't exist error so we want to write anyway
|
|
653
|
-
}
|
|
654
|
-
if (hasChanged) {
|
|
655
|
-
await promises.writeFile(filepath, contents, {
|
|
656
|
-
encoding: 'utf-8'
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
/* eslint-disable no-console */
|
|
662
|
-
function findMissingKeys(loadedTranslation, devLanguageName, altLanguages) {
|
|
663
|
-
const devLanguage = loadedTranslation.languages[devLanguageName];
|
|
664
|
-
if (!devLanguage) {
|
|
665
|
-
throw new Error(`Failed to load dev language: ${loadedTranslation.filePath}`);
|
|
666
|
-
}
|
|
667
|
-
const result = {};
|
|
668
|
-
let valid = true;
|
|
669
|
-
const requiredKeys = Object.keys(devLanguage);
|
|
670
|
-
if (requiredKeys.length > 0) {
|
|
671
|
-
for (const altLanguageName of altLanguages) {
|
|
672
|
-
var _loadedTranslation$la;
|
|
673
|
-
const altLanguage = (_loadedTranslation$la = loadedTranslation.languages[altLanguageName]) !== null && _loadedTranslation$la !== void 0 ? _loadedTranslation$la : {};
|
|
674
|
-
for (const key of requiredKeys) {
|
|
675
|
-
var _altLanguage$key;
|
|
676
|
-
if (typeof ((_altLanguage$key = altLanguage[key]) === null || _altLanguage$key === void 0 ? void 0 : _altLanguage$key.message) !== 'string') {
|
|
677
|
-
if (!result[altLanguageName]) {
|
|
678
|
-
result[altLanguageName] = [];
|
|
679
|
-
}
|
|
680
|
-
result[altLanguageName].push(key);
|
|
681
|
-
valid = false;
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
return [valid, result];
|
|
687
|
-
}
|
|
688
|
-
async function validate(config) {
|
|
689
|
-
const allTranslations = await loadAllTranslations({
|
|
690
|
-
fallbacks: 'valid',
|
|
691
|
-
includeNodeModules: true
|
|
692
|
-
}, config);
|
|
693
|
-
let valid = true;
|
|
694
|
-
for (const loadedTranslation of allTranslations) {
|
|
695
|
-
const [translationValid, result] = findMissingKeys(loadedTranslation, config.devLanguage, getAltLanguages(config));
|
|
696
|
-
if (!translationValid) {
|
|
697
|
-
valid = false;
|
|
698
|
-
console.log(pc.red(`Incomplete translations: "${pc.bold(loadedTranslation.relativePath)}"`));
|
|
699
|
-
for (const lang of Object.keys(result)) {
|
|
700
|
-
const missingKeys = result[lang];
|
|
701
|
-
console.log(pc.yellow(lang), '->', missingKeys.map(v => `"${v}"`).join(', '));
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
return valid;
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
class ValidationError extends Error {
|
|
709
|
-
constructor(code, message) {
|
|
710
|
-
super(`Invalid vocab.config.js: ${code} - ${message}`);
|
|
711
|
-
this.code = code;
|
|
712
|
-
this.rawMessage = message;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
const boldCyan = s => pc.bold(pc.cyan(s));
|
|
717
|
-
const validator = new Validator();
|
|
718
|
-
const schema = {
|
|
719
|
-
$$strict: true,
|
|
720
|
-
devLanguage: {
|
|
721
|
-
type: 'string'
|
|
722
|
-
},
|
|
723
|
-
languages: {
|
|
724
|
-
type: 'array',
|
|
725
|
-
items: {
|
|
726
|
-
type: 'object',
|
|
727
|
-
props: {
|
|
728
|
-
name: {
|
|
729
|
-
type: 'string'
|
|
730
|
-
},
|
|
731
|
-
extends: {
|
|
732
|
-
type: 'string',
|
|
733
|
-
optional: true
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
},
|
|
738
|
-
generatedLanguages: {
|
|
739
|
-
type: 'array',
|
|
740
|
-
items: {
|
|
741
|
-
type: 'object',
|
|
742
|
-
props: {
|
|
743
|
-
name: {
|
|
744
|
-
type: 'string'
|
|
745
|
-
},
|
|
746
|
-
extends: {
|
|
747
|
-
type: 'string',
|
|
748
|
-
optional: true
|
|
749
|
-
},
|
|
750
|
-
generator: {
|
|
751
|
-
type: 'object',
|
|
752
|
-
props: {
|
|
753
|
-
transformElement: {
|
|
754
|
-
type: 'function',
|
|
755
|
-
optional: true
|
|
756
|
-
},
|
|
757
|
-
transformMessage: {
|
|
758
|
-
type: 'function',
|
|
759
|
-
optional: true
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
},
|
|
765
|
-
optional: true
|
|
766
|
-
},
|
|
767
|
-
translationsDirectorySuffix: {
|
|
768
|
-
type: 'string',
|
|
769
|
-
optional: true
|
|
770
|
-
},
|
|
771
|
-
projectRoot: {
|
|
772
|
-
type: 'string',
|
|
773
|
-
optional: true
|
|
774
|
-
},
|
|
775
|
-
ignore: {
|
|
776
|
-
type: 'array',
|
|
777
|
-
items: 'string',
|
|
778
|
-
optional: true
|
|
779
|
-
}
|
|
780
|
-
};
|
|
781
|
-
const checkConfigFile = validator.compile(schema);
|
|
782
|
-
const splitMap = (message, callback) => message.split(' ,').map(v => callback(v)).join(' ,');
|
|
783
|
-
function validateConfig(c) {
|
|
784
|
-
trace('Validating configuration file');
|
|
785
|
-
// Note: checkConfigFile mutates the config file by applying defaults
|
|
786
|
-
const isValid = checkConfigFile(c);
|
|
787
|
-
if (isValid !== true) {
|
|
788
|
-
throw new ValidationError('InvalidStructure', (Array.isArray(isValid) ? isValid : []).map(v => {
|
|
789
|
-
if (v.type === 'objectStrict') {
|
|
790
|
-
return `Invalid key(s) ${splitMap(v.actual, m => `"${pc.cyan(m)}"`)}. Expected one of ${splitMap(v.expected, pc.green)}`;
|
|
791
|
-
}
|
|
792
|
-
if (v.field) {
|
|
793
|
-
var _v$message;
|
|
794
|
-
return (_v$message = v.message) === null || _v$message === void 0 ? void 0 : _v$message.replace(v.field, pc.cyan(v.field));
|
|
795
|
-
}
|
|
796
|
-
return v.message;
|
|
797
|
-
}).join(' \n'));
|
|
798
|
-
}
|
|
799
|
-
const languageStrings = c.languages.map(v => v.name);
|
|
800
|
-
|
|
801
|
-
// Dev Language should exist in languages
|
|
802
|
-
if (!languageStrings.includes(c.devLanguage)) {
|
|
803
|
-
throw new ValidationError('InvalidDevLanguage', `The dev language "${boldCyan(c.devLanguage)}" was not found in languages ${languageStrings.join(', ')}.`);
|
|
804
|
-
}
|
|
805
|
-
const foundLanguages = [];
|
|
806
|
-
for (const lang of c.languages) {
|
|
807
|
-
// Languages must only exist once
|
|
808
|
-
if (foundLanguages.includes(lang.name)) {
|
|
809
|
-
throw new ValidationError('DuplicateLanguage', `The language "${boldCyan(lang.name)}" was defined multiple times.`);
|
|
810
|
-
}
|
|
811
|
-
foundLanguages.push(lang.name);
|
|
812
|
-
|
|
813
|
-
// Any extends must be in languages
|
|
814
|
-
if (lang.extends && !languageStrings.includes(lang.extends)) {
|
|
815
|
-
throw new ValidationError('InvalidExtends', `The language "${boldCyan(lang.name)}"'s extends of ${boldCyan(lang.extends)} was not found in languages ${languageStrings.join(', ')}.`);
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
const foundGeneratedLanguages = [];
|
|
819
|
-
for (const generatedLang of c.generatedLanguages || []) {
|
|
820
|
-
// Generated languages must only exist once
|
|
821
|
-
if (foundGeneratedLanguages.includes(generatedLang.name)) {
|
|
822
|
-
throw new ValidationError('DuplicateGeneratedLanguage', `The generated language "${boldCyan(generatedLang.name)}" was defined multiple times.`);
|
|
823
|
-
}
|
|
824
|
-
foundGeneratedLanguages.push(generatedLang.name);
|
|
825
|
-
|
|
826
|
-
// Generated language names must not conflict with language names
|
|
827
|
-
if (languageStrings.includes(generatedLang.name)) {
|
|
828
|
-
throw new ValidationError('InvalidGeneratedLanguage', `The generated language "${boldCyan(generatedLang.name)}" is already defined as a language.`);
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
// Any extends must be in languages
|
|
832
|
-
if (generatedLang.extends && !languageStrings.includes(generatedLang.extends)) {
|
|
833
|
-
throw new ValidationError('InvalidExtends', `The generated language "${boldCyan(generatedLang.name)}"'s extends of ${boldCyan(generatedLang.extends)} was not found in languages ${languageStrings.join(', ')}.`);
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
trace('Configuration file is valid');
|
|
837
|
-
return true;
|
|
838
|
-
}
|
|
839
|
-
function createConfig(configFilePath) {
|
|
840
|
-
const cwd = path.dirname(configFilePath);
|
|
841
|
-
return {
|
|
842
|
-
projectRoot: cwd,
|
|
843
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
844
|
-
...require(configFilePath)
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
async function resolveConfig(customConfigFilePath) {
|
|
848
|
-
const configFilePath = customConfigFilePath ? path.resolve(customConfigFilePath) : await findUp(['vocab.config.js', 'vocab.config.cjs']);
|
|
849
|
-
if (configFilePath) {
|
|
850
|
-
trace(`Resolved configuration file to ${configFilePath}`);
|
|
851
|
-
return createConfig(configFilePath);
|
|
852
|
-
}
|
|
853
|
-
trace('No configuration file found');
|
|
854
|
-
return null;
|
|
855
|
-
}
|
|
856
|
-
function resolveConfigSync(customConfigFilePath) {
|
|
857
|
-
const configFilePath = customConfigFilePath ? path.resolve(customConfigFilePath) : findUp.sync(['vocab.config.js', 'vocab.config.cjs']);
|
|
858
|
-
if (configFilePath) {
|
|
859
|
-
trace(`Resolved configuration file to ${configFilePath}`);
|
|
860
|
-
return createConfig(configFilePath);
|
|
861
|
-
}
|
|
862
|
-
trace('No configuration file found');
|
|
863
|
-
return null;
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
export { compile, getAltLanguageFilePath, getAltLanguages, getDevLanguageFileFromTsFile, getUniqueKey, loadAllTranslations, loadTranslation, resolveConfig, resolveConfigSync, validate, validateConfig, watch };
|