Repository: lingui/js-lingui
Pull request #565 (2019-09-09 16:14:52)

Add orderBy option (origin or messageId)
This is a proposal to add a config option `orderBy` to expose a new order by origin feature. 

Context #562


now[bot]: 
This pull request is [automatically deployed](https://zeit.co/docs/v2/integrations/now-for-github?utm_source=automated&utm_medium=github&utm_campaign=now_bot) with [Now](https://zeit.co/now?utm_source=automated&utm_medium=github&utm_campaign=now_bot).
To access deployments, click Details below or on the icon next to each push.
	
tricoder42: Ups, I got a little bit confused by the second PR. What's the difference between this and #563? I think I merged the wrong one...
skovhus: This is for version 3, the Next branch. The other one was for the version 2, master. 
codecov[bot]: # [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=h1) Report
> Merging [#565](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=desc) into [next](https://codecov.io/gh/lingui/js-lingui/commit/4cd2c1dfd8681f37a8604d1e8fe08a282f30dd85?src=pr&el=desc) will **decrease** coverage by `0.1%`.
> The diff coverage is `68.75%`.

[![Impacted file tree graph](https://codecov.io/gh/lingui/js-lingui/pull/565/graphs/tree.svg?width=650&token=CuJ1VD5BY5&height=150&src=pr)](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##             next     #565      +/-   ##
==========================================
- Coverage   85.08%   84.97%   -0.11%     
==========================================
  Files          38       38              
  Lines        1153     1158       +5     
  Branches      302      303       +1     
==========================================
+ Hits          981      984       +3     
- Misses        101      102       +1     
- Partials       71       72       +1
```


| [Impacted Files](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/conf/src/index.ts](https://codecov.io/gh/lingui/js-lingui/pull/565/diff?src=pr&el=tree#diff-cGFja2FnZXMvY29uZi9zcmMvaW5kZXgudHM=) | `88.13% <ø> (ø)` | :arrow_up: |
| [packages/cli/src/api/catalog.ts](https://codecov.io/gh/lingui/js-lingui/pull/565/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2xpL3NyYy9hcGkvY2F0YWxvZy50cw==) | `80.92% <68.75%> (-0.66%)` | :arrow_down: |
| [packages/core/src/i18n.ts](https://codecov.io/gh/lingui/js-lingui/pull/565/diff?src=pr&el=tree#diff-cGFja2FnZXMvY29yZS9zcmMvaTE4bi50cw==) | `67.85% <0%> (-3.85%)` | :arrow_down: |
| [packages/core/src/eventEmitter.ts](https://codecov.io/gh/lingui/js-lingui/pull/565/diff?src=pr&el=tree#diff-cGFja2FnZXMvY29yZS9zcmMvZXZlbnRFbWl0dGVyLnRz) | `100% <0%> (ø)` | :arrow_up: |
| [packages/jest-mocks/index.ts](https://codecov.io/gh/lingui/js-lingui/pull/565/diff?src=pr&el=tree#diff-cGFja2FnZXMvamVzdC1tb2Nrcy9pbmRleC50cw==) | `95.83% <0%> (+8.33%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=footer). Last update [4cd2c1d...2d6c862](https://codecov.io/gh/lingui/js-lingui/pull/565?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

tricoder42: Merde, I've just merged `master` into `next` and ported sorting feature before I realized there's another PR for it :) Anyway, `orderBy` is a better name than `sorting`, so I'm gonna keep this version.

Thank you for the hard work! 🥇 

Patch:
diff --git a/docs/ref/conf.rst b/docs/ref/conf.rst
index 7aa8eebd..ef920199 100644
--- a/docs/ref/conf.rst
+++ b/docs/ref/conf.rst
@@ -195,6 +195,23 @@ Default: ``<rootDir>/locale``
 
 Directory where message catalogs should be saved.
 
+orderBy
+---------
+
+Default: ``messageId``
+
+Sorting order for catalog files. Possible values are:
+
+messageId
+^^^^^^
+
+Order by the message ID.
+
+origin
+^^^^^^^
+
+Order by the file origin of the message.
+
 runtimeConfigModule
 -------------------
 
diff --git a/packages/cli/src/api/catalog.ts b/packages/cli/src/api/catalog.ts
index 94054c0e..6bd8f4f9 100644
--- a/packages/cli/src/api/catalog.ts
+++ b/packages/cli/src/api/catalog.ts
@@ -6,7 +6,7 @@ import chalk from "chalk"
 import glob from "glob"
 import minimatch from "minimatch"
 
-import { LinguiConfig } from "@lingui/conf"
+import { LinguiConfig, OrderBy } from "@lingui/conf"
 import getFormat from "./formats"
 import extract from "./extractors"
 import { prettyOrigin, removeDirectory } from "./utils"
@@ -14,8 +14,7 @@ import {
   AllCatalogsType,
   ExtractedCatalogType,
   ExtractedMessageType,
-  MessageType,
-  Sorting
+  MessageType
 } from "./types"
 import { CliExtractOptions } from "../lingui-extract"
 
@@ -23,8 +22,8 @@ const NAME = "{name}"
 const LOCALE = "{locale}"
 
 export interface MakeOptions extends CliExtractOptions {
-  sorting: Sorting
   projectType?: string
+  orderBy?: OrderBy
 }
 
 export interface MergeOptions {
@@ -77,7 +76,7 @@ export class Catalog {
         // Clean obsolete messages
         options.clean ? cleanObsolete : R.identity,
         // Sort messages
-        order(options.sorting)
+        order(options.orderBy)
       )
     ) as unknown) as (catalog: AllCatalogsType) => AllCatalogsType
     this.writeAll(cleanAndSort(catalogs))
@@ -463,7 +462,7 @@ export const cleanObsolete = R.filter(
 )
 
 export function order(
-  by: Sorting
+  by: OrderBy
 ): (catalog: ExtractedCatalogType) => ExtractedCatalogType {
   return {
     messageId: orderByMessageId,
diff --git a/packages/cli/src/api/types.ts b/packages/cli/src/api/types.ts
index 9c41849e..bd72e5fc 100644
--- a/packages/cli/src/api/types.ts
+++ b/packages/cli/src/api/types.ts
@@ -15,6 +15,8 @@ export interface MessageType extends ExtractedMessageType {
   translation: string
 }
 
+export type OrderByType = "messageId" | "origin"
+
 export type CatalogType = {
   [msgId: string]: MessageType
 }
@@ -63,5 +65,3 @@ export type CatalogApi = {
     options: getTranslationOptions
   ): Maybe<string>
 }
-
-export type Sorting = "messageId" | "origin"
diff --git a/packages/cli/src/lingui-extract.ts b/packages/cli/src/lingui-extract.ts
index ca7a6314..d1deb7a5 100644
--- a/packages/cli/src/lingui-extract.ts
+++ b/packages/cli/src/lingui-extract.ts
@@ -37,7 +37,7 @@ export default function command(
   catalogs.forEach(catalog => {
     catalog.make({
       ...options,
-      sorting: config.sorting,
+      orderBy: config.orderBy,
       projectType: detect()
       // const pseudoLocale = config.pseudoLocale
       // if (pseudoLocale) {
diff --git a/packages/conf/src/index.ts b/packages/conf/src/index.ts
index 57c59041..e47043cc 100644
--- a/packages/conf/src/index.ts
+++ b/packages/conf/src/index.ts
@@ -5,6 +5,7 @@ import cosmiconfig from "cosmiconfig"
 import { validate } from "jest-validate"
 
 export type CatalogFormat = "po" | "minimal" | "lingui"
+export type OrderBy = "messageId" | "origin"
 
 type CatalogConfig = {
   name?: string
@@ -20,10 +21,10 @@ export type LinguiConfig = {
   fallbackLocale: string
   format: CatalogFormat
   locales: Array<string>
+  orderBy: OrderBy
   pseudoLocale: string
   rootDir: string
   runtimeConfigModule: [string, string?]
-  sorting: string
   sourceLocale: string
 }
 
@@ -40,10 +41,10 @@ export const defaultConfig: LinguiConfig = {
   fallbackLocale: "",
   format: "po",
   locales: [],
+  orderBy: "messageId",
   pseudoLocale: "",
   rootDir: ".",
   runtimeConfigModule: ["@lingui/core", "i18n"],
-  sorting: "messageId",
   sourceLocale: ""
 }
 
@@ -107,14 +108,14 @@ const deprecatedConfig = {
     ` Option ${chalk.bold("fallbackLanguage")} was replaced by ${chalk.bold(
       "fallbackLocale"
     )}
-    
+
     @lingui/cli now treats your current configuration as:
     {
       ${chalk.bold('"fallbackLocale"')}: ${chalk.bold(
       `"${config.fallbackLanguage}"`
     )}
     }
-    
+
     Please update your configuration.
     `,
   localeDir: (config: LinguiConfig & DeprecatedLocaleDir) =>
@@ -123,9 +124,9 @@ const deprecatedConfig = {
     )} is deprecated. Configure source paths using ${chalk.bold(
       "catalogs"
     )} instead.
-    
+
     @lingui/cli now treats your current configuration as:
-    
+
     {
       ${chalk.bold('"catalogs"')}: ${JSON.stringify(
       catalogMigration(config).catalogs,
@@ -133,7 +134,7 @@ const deprecatedConfig = {
       2
     )}
     }
-    
+
     Please update your configuration.
     `,
   srcPathDirs: (config: LinguiConfig & DeprecatedLocaleDir) =>
@@ -142,9 +143,9 @@ const deprecatedConfig = {
     )} is deprecated. Configure source paths using ${chalk.bold(
       "catalogs"
     )} instead.
-    
+
     @lingui/cli now treats your current configuration as:
-    
+
     {
       ${chalk.bold('"catalogs"')}: ${JSON.stringify(
       catalogMigration(config).catalogs,
@@ -152,7 +153,7 @@ const deprecatedConfig = {
       2
     )}
     }
-    
+
     Please update your configuration.
     `,
   srcPathIgnorePatterns: (config: LinguiConfig & DeprecatedLocaleDir) =>
@@ -161,9 +162,9 @@ const deprecatedConfig = {
     )} is deprecated. Configure excluded source paths using ${chalk.bold(
       "catalogs"
     )} instead.
-    
+
     @lingui/cli now treats your current configuration as:
-    
+
     {
       ${chalk.bold('"catalogs"')}: ${JSON.stringify(
       catalogMigration(config).catalogs,
@@ -171,7 +172,7 @@ const deprecatedConfig = {
       2
     )}
     }
-    
+
     Please update your configuration.
     `
 }


Test patch:
diff --git a/packages/cli/src/api/catalog.test.ts b/packages/cli/src/api/catalog.test.ts
index 20602c3e..86784c75 100644
--- a/packages/cli/src/api/catalog.test.ts
+++ b/packages/cli/src/api/catalog.test.ts
@@ -8,7 +8,6 @@ import {
   getCatalogs,
   getCatalogForFile,
   Catalog,
-  orderByMessageId,
   cleanObsolete,
   MergeOptions,
   order
@@ -22,7 +21,7 @@ const defaultMakeOptions: MakeOptions = {
   clean: false,
   overwrite: false,
   prevFormat: null,
-  sorting: "messageId"
+  orderBy: "messageId"
 }
 
 const defaultMergeOptions: MergeOptions = {
diff --git a/packages/conf/src/__snapshots__/index.test.ts.snap b/packages/conf/src/__snapshots__/index.test.ts.snap
index 9de8cb82..e47f20e5 100644
--- a/packages/conf/src/__snapshots__/index.test.ts.snap
+++ b/packages/conf/src/__snapshots__/index.test.ts.snap
@@ -4,9 +4,9 @@ exports[`@lingui/conf catalogMigration should show deprecation warning 1`] = `
 ● Deprecation Warning:
 
  Option localeDir is deprecated. Configure source paths using catalogs instead.
-    
+
     @lingui/cli now treats your current configuration as:
-    
+
     {
       "catalogs": [
   {
@@ -20,7 +20,7 @@ exports[`@lingui/conf catalogMigration should show deprecation warning 1`] = `
   }
 ]
     }
-    
+
     Please update your configuration.
     
 
@@ -28,9 +28,9 @@ Documentation: https://lingui.js.org/ref/conf.html
 ● Deprecation Warning:
 
  Option srcPathDirs is deprecated. Configure source paths using catalogs instead.
-    
+
     @lingui/cli now treats your current configuration as:
-    
+
     {
       "catalogs": [
   {
@@ -44,7 +44,7 @@ Documentation: https://lingui.js.org/ref/conf.html
   }
 ]
     }
-    
+
     Please update your configuration.
     
 
@@ -52,9 +52,9 @@ Documentation: https://lingui.js.org/ref/conf.html
 ● Deprecation Warning:
 
  Option srcPathIgnorePatterns is deprecated. Configure excluded source paths using catalogs instead.
-    
+
     @lingui/cli now treats your current configuration as:
-    
+
     {
       "catalogs": [
   {
@@ -68,7 +68,7 @@ Documentation: https://lingui.js.org/ref/conf.html
   }
 ]
     }
-    
+
     Please update your configuration.
     
 
@@ -98,13 +98,13 @@ Object {
   locales: Array [
     en-gb,
   ],
+  orderBy: messageId,
   pseudoLocale: ,
   rootDir: .,
   runtimeConfigModule: Array [
     @lingui/core,
     i18n,
   ],
-  sorting: messageId,
   sourceLocale: ,
 }
 `;