gtx-cli 2.5.6 → 2.5.7

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # gtx-cli
2
2
 
3
+ ## 2.5.7
4
+
5
+ ### Patch Changes
6
+
7
+ - [#819](https://github.com/generaltranslation/gt/pull/819) [`50338d2`](https://github.com/generaltranslation/gt/commit/50338d2192e2882a4192273a7bbf12d39939c209) Thanks [@fernando-aviles](https://github.com/fernando-aviles)! - Adding sorting by locale order
8
+
3
9
  ## 2.5.6
4
10
 
5
11
  ### Patch Changes
@@ -83,7 +83,7 @@ export async function downloadFileBatch(fileTracker, files, options, forceDownlo
83
83
  translatedContent: file.data,
84
84
  targetLocale: locale,
85
85
  },
86
- ], options.defaultLocale)[0];
86
+ ], options.defaultLocale, options.locales)[0];
87
87
  }
88
88
  }
89
89
  }
@@ -2,7 +2,7 @@ import { AdditionalOptions, SourceObjectOptions } from '../../types/index.js';
2
2
  export declare function mergeJson(originalContent: string, inputPath: string, options: AdditionalOptions, targets: {
3
3
  translatedContent: string;
4
4
  targetLocale: string;
5
- }[], defaultLocale: string): string[];
5
+ }[], defaultLocale: string, localeOrder?: string[]): string[];
6
6
  /**
7
7
  * Apply transformations to the sourceItem in-place
8
8
  * @param sourceItem - The source item to apply transformations to
@@ -1,10 +1,10 @@
1
1
  import JSONPointer from 'jsonpointer';
2
2
  import { exit, logError, logWarning } from '../../console/logging.js';
3
- import { findMatchingItemArray, findMatchingItemObject, generateSourceObjectPointers, getSourceObjectOptionsArray, validateJsonSchema, } from './utils.js';
3
+ import { findMatchingItemArray, findMatchingItemObject, generateSourceObjectPointers, getIdentifyingLocaleProperty, getSourceObjectOptionsArray, validateJsonSchema, } from './utils.js';
4
4
  import { JSONPath } from 'jsonpath-plus';
5
5
  import { getLocaleProperties } from 'generaltranslation';
6
6
  import { replaceLocalePlaceholders } from '../utils.js';
7
- export function mergeJson(originalContent, inputPath, options, targets, defaultLocale) {
7
+ export function mergeJson(originalContent, inputPath, options, targets, defaultLocale, localeOrder = []) {
8
8
  const jsonSchema = validateJsonSchema(options, inputPath);
9
9
  if (!jsonSchema) {
10
10
  return targets.map((target) => target.translatedContent);
@@ -136,7 +136,7 @@ export function mergeJson(originalContent, inputPath, options, targets, defaultL
136
136
  const filteredSourceObjectValue = sourceObjectValue.filter((_, index) => !indiciesToRemove.has(index));
137
137
  // 10. Add all items to the original JSON
138
138
  filteredSourceObjectValue.push(...itemsToAdd);
139
- JSONPointer.set(mergedJson, sourceObjectPointer, filteredSourceObjectValue);
139
+ JSONPointer.set(mergedJson, sourceObjectPointer, sortByLocaleOrder(filteredSourceObjectValue, sourceObjectOptions, localeOrder, sourceObjectPointer, defaultLocale));
140
140
  }
141
141
  else {
142
142
  // Validate type
@@ -205,6 +205,53 @@ export function mergeJson(originalContent, inputPath, options, targets, defaultL
205
205
  }
206
206
  return [JSON.stringify(mergedJson, null, 2)];
207
207
  }
208
+ function sortByLocaleOrder(items, sourceObjectOptions, localeOrder, sourceObjectPointer, defaultLocale) {
209
+ if (sourceObjectOptions.experimentalSort !== 'locales' ||
210
+ !localeOrder.length ||
211
+ !sourceObjectOptions.key) {
212
+ return items;
213
+ }
214
+ const orderedLocaleList = [
215
+ defaultLocale,
216
+ ...localeOrder.filter((locale) => locale !== defaultLocale),
217
+ ];
218
+ const localeOrderValues = orderedLocaleList.map((locale) => getIdentifyingLocaleProperty(locale, sourceObjectPointer, sourceObjectOptions));
219
+ const itemsWithLocale = items.map((item) => {
220
+ let localeValue;
221
+ try {
222
+ const values = JSONPath({
223
+ json: item,
224
+ path: sourceObjectOptions.key,
225
+ resultType: 'value',
226
+ flatten: true,
227
+ wrap: true,
228
+ });
229
+ const value = values?.[0];
230
+ if (typeof value === 'string') {
231
+ localeValue = value;
232
+ }
233
+ }
234
+ catch {
235
+ /* empty */
236
+ }
237
+ return { item, localeValue };
238
+ });
239
+ const orderedItems = [];
240
+ const remainingItems = [...itemsWithLocale];
241
+ for (const localeValue of localeOrderValues) {
242
+ for (let i = 0; i < remainingItems.length;) {
243
+ const entry = remainingItems[i];
244
+ if (entry.localeValue === localeValue) {
245
+ orderedItems.push(entry.item);
246
+ remainingItems.splice(i, 1);
247
+ continue;
248
+ }
249
+ i += 1;
250
+ }
251
+ }
252
+ remainingItems.forEach((entry) => orderedItems.push(entry.item));
253
+ return orderedItems;
254
+ }
208
255
  /**
209
256
  * Apply transformations to the sourceItem in-place
210
257
  * @param sourceItem - The source item to apply transformations to
@@ -187,6 +187,7 @@ export type SourceObjectOptions = {
187
187
  key?: string;
188
188
  localeProperty?: string;
189
189
  transform?: TransformOptions;
190
+ experimentalSort?: 'locales';
190
191
  };
191
192
  export type TransformOptions = {
192
193
  [transformPath: string]: TransformOption;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "2.5.6",
3
+ "version": "2.5.7",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [