@vendure/ui-devkit 2.2.0-next.3 → 2.2.0-next.5

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.
@@ -1,12 +1,12 @@
1
- import { LanguageCode } from '@vendure/common/lib/generated-types';
2
- import { TranslationExtension } from './types';
3
- /**
4
- * Given an array of extensions, returns a map of languageCode to all files specified by the
5
- * configured globs.
6
- */
7
- export declare function getAllTranslationFiles(extensions: TranslationExtension[]): {
8
- [languageCode in LanguageCode]?: string[];
9
- };
10
- export declare function mergeExtensionTranslations(outputPath: string, translationFiles: {
11
- [languageCode in LanguageCode]?: string[];
12
- }): Promise<void>;
1
+ import { LanguageCode } from '@vendure/common/lib/generated-types';
2
+ import { TranslationExtension } from './types';
3
+ /**
4
+ * Given an array of extensions, returns a map of languageCode to all files specified by the
5
+ * configured globs.
6
+ */
7
+ export declare function getAllTranslationFiles(extensions: TranslationExtension[]): {
8
+ [languageCode in LanguageCode]?: string[];
9
+ };
10
+ export declare function mergeExtensionTranslations(outputPath: string, translationFiles: {
11
+ [languageCode in LanguageCode]?: string[];
12
+ }): Promise<void>;
@@ -1,137 +1,137 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.mergeExtensionTranslations = exports.getAllTranslationFiles = void 0;
27
- const fs = __importStar(require("fs-extra"));
28
- const glob_1 = require("glob");
29
- const path = __importStar(require("path"));
30
- const utils_1 = require("./utils");
31
- /**
32
- * Given an array of extensions, returns a map of languageCode to all files specified by the
33
- * configured globs.
34
- */
35
- function getAllTranslationFiles(extensions) {
36
- // First collect all globs by language
37
- const allTranslationsWithGlobs = {};
38
- for (const extension of extensions) {
39
- for (const [languageCode, globPattern] of Object.entries(extension.translations || {})) {
40
- const code = languageCode;
41
- if (globPattern) {
42
- if (!allTranslationsWithGlobs[code]) {
43
- allTranslationsWithGlobs[code] = [globPattern];
44
- }
45
- else {
46
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
47
- allTranslationsWithGlobs[code].push(globPattern);
48
- }
49
- }
50
- }
51
- }
52
- const allTranslationsWithFiles = {};
53
- for (const [languageCode, globs] of Object.entries(allTranslationsWithGlobs)) {
54
- const code = languageCode;
55
- allTranslationsWithFiles[code] = [];
56
- if (!globs) {
57
- continue;
58
- }
59
- for (const pattern of globs) {
60
- const files = (0, glob_1.globSync)(pattern);
61
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
62
- allTranslationsWithFiles[code].push(...files);
63
- }
64
- }
65
- return allTranslationsWithFiles;
66
- }
67
- exports.getAllTranslationFiles = getAllTranslationFiles;
68
- async function mergeExtensionTranslations(outputPath, translationFiles) {
69
- // Now merge them into the final language-speicific json files
70
- const i18nMessagesDir = path.join(outputPath, 'src/i18n-messages');
71
- for (const [languageCode, files] of Object.entries(translationFiles)) {
72
- if (!files) {
73
- continue;
74
- }
75
- const translationFile = path.join(i18nMessagesDir, `${languageCode}.json`);
76
- const translationBackupFile = path.join(i18nMessagesDir, `${languageCode}.json.bak`);
77
- if (fs.existsSync(translationBackupFile)) {
78
- // restore the original translations from the backup
79
- await fs.copy(translationBackupFile, translationFile);
80
- }
81
- let translations = {};
82
- if (fs.existsSync(translationFile)) {
83
- // create a backup of the original (unextended) translations
84
- await fs.copy(translationFile, translationBackupFile);
85
- try {
86
- translations = await fs.readJson(translationFile);
87
- }
88
- catch (e) {
89
- utils_1.logger.error(`Could not load translation file: ${translationFile}`);
90
- utils_1.logger.error(e);
91
- }
92
- }
93
- for (const file of files) {
94
- try {
95
- const contents = await fs.readJson(file);
96
- translations = mergeTranslations(translations, contents);
97
- }
98
- catch (e) {
99
- utils_1.logger.error(`Could not load translation file: ${translationFile}`);
100
- utils_1.logger.error(e);
101
- }
102
- }
103
- // write the final translation files to disk
104
- const sortedTranslations = sortTranslationKeys(translations);
105
- await fs.writeFile(translationFile, JSON.stringify(sortedTranslations, null, 2), 'utf8');
106
- }
107
- }
108
- exports.mergeExtensionTranslations = mergeExtensionTranslations;
109
- /**
110
- * Sorts the contents of the translation files so the sections & keys are alphabetical.
111
- */
112
- function sortTranslationKeys(translations) {
113
- const result = {};
114
- const sections = Object.keys(translations).sort();
115
- for (const section of sections) {
116
- const sortedTokens = Object.entries(translations[section])
117
- .sort(([keyA], [keyB]) => (keyA < keyB ? -1 : 1))
118
- .reduce((output, [key, val]) => ({ ...output, [key]: val }), {});
119
- result[section] = sortedTokens;
120
- }
121
- return result;
122
- }
123
- /**
124
- * Merges the second set of translations into the first, returning a new translations
125
- * object.
126
- */
127
- function mergeTranslations(t1, t2) {
128
- const result = { ...t1 };
129
- for (const [section, translations] of Object.entries(t2)) {
130
- result[section] = {
131
- ...t1[section],
132
- ...translations,
133
- };
134
- }
135
- return result;
136
- }
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.mergeExtensionTranslations = exports.getAllTranslationFiles = void 0;
27
+ const fs = __importStar(require("fs-extra"));
28
+ const glob_1 = require("glob");
29
+ const path = __importStar(require("path"));
30
+ const utils_1 = require("./utils");
31
+ /**
32
+ * Given an array of extensions, returns a map of languageCode to all files specified by the
33
+ * configured globs.
34
+ */
35
+ function getAllTranslationFiles(extensions) {
36
+ // First collect all globs by language
37
+ const allTranslationsWithGlobs = {};
38
+ for (const extension of extensions) {
39
+ for (const [languageCode, globPattern] of Object.entries(extension.translations || {})) {
40
+ const code = languageCode;
41
+ if (globPattern) {
42
+ if (!allTranslationsWithGlobs[code]) {
43
+ allTranslationsWithGlobs[code] = [globPattern];
44
+ }
45
+ else {
46
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
47
+ allTranslationsWithGlobs[code].push(globPattern);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ const allTranslationsWithFiles = {};
53
+ for (const [languageCode, globs] of Object.entries(allTranslationsWithGlobs)) {
54
+ const code = languageCode;
55
+ allTranslationsWithFiles[code] = [];
56
+ if (!globs) {
57
+ continue;
58
+ }
59
+ for (const pattern of globs) {
60
+ const files = (0, glob_1.globSync)(pattern);
61
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
62
+ allTranslationsWithFiles[code].push(...files);
63
+ }
64
+ }
65
+ return allTranslationsWithFiles;
66
+ }
67
+ exports.getAllTranslationFiles = getAllTranslationFiles;
68
+ async function mergeExtensionTranslations(outputPath, translationFiles) {
69
+ // Now merge them into the final language-speicific json files
70
+ const i18nMessagesDir = path.join(outputPath, 'src/i18n-messages');
71
+ for (const [languageCode, files] of Object.entries(translationFiles)) {
72
+ if (!files) {
73
+ continue;
74
+ }
75
+ const translationFile = path.join(i18nMessagesDir, `${languageCode}.json`);
76
+ const translationBackupFile = path.join(i18nMessagesDir, `${languageCode}.json.bak`);
77
+ if (fs.existsSync(translationBackupFile)) {
78
+ // restore the original translations from the backup
79
+ await fs.copy(translationBackupFile, translationFile);
80
+ }
81
+ let translations = {};
82
+ if (fs.existsSync(translationFile)) {
83
+ // create a backup of the original (unextended) translations
84
+ await fs.copy(translationFile, translationBackupFile);
85
+ try {
86
+ translations = await fs.readJson(translationFile);
87
+ }
88
+ catch (e) {
89
+ utils_1.logger.error(`Could not load translation file: ${translationFile}`);
90
+ utils_1.logger.error(e);
91
+ }
92
+ }
93
+ for (const file of files) {
94
+ try {
95
+ const contents = await fs.readJson(file);
96
+ translations = mergeTranslations(translations, contents);
97
+ }
98
+ catch (e) {
99
+ utils_1.logger.error(`Could not load translation file: ${translationFile}`);
100
+ utils_1.logger.error(e);
101
+ }
102
+ }
103
+ // write the final translation files to disk
104
+ const sortedTranslations = sortTranslationKeys(translations);
105
+ await fs.writeFile(translationFile, JSON.stringify(sortedTranslations, null, 2), 'utf8');
106
+ }
107
+ }
108
+ exports.mergeExtensionTranslations = mergeExtensionTranslations;
109
+ /**
110
+ * Sorts the contents of the translation files so the sections & keys are alphabetical.
111
+ */
112
+ function sortTranslationKeys(translations) {
113
+ const result = {};
114
+ const sections = Object.keys(translations).sort();
115
+ for (const section of sections) {
116
+ const sortedTokens = Object.entries(translations[section])
117
+ .sort(([keyA], [keyB]) => (keyA < keyB ? -1 : 1))
118
+ .reduce((output, [key, val]) => ({ ...output, [key]: val }), {});
119
+ result[section] = sortedTokens;
120
+ }
121
+ return result;
122
+ }
123
+ /**
124
+ * Merges the second set of translations into the first, returning a new translations
125
+ * object.
126
+ */
127
+ function mergeTranslations(t1, t2) {
128
+ const result = { ...t1 };
129
+ for (const [section, translations] of Object.entries(t2)) {
130
+ result[section] = {
131
+ ...t1[section],
132
+ ...translations,
133
+ };
134
+ }
135
+ return result;
136
+ }
137
137
  //# sourceMappingURL=translations.js.map