Skip to content

Commit

Permalink
Add --sort-when-not-flat option as keys were not sorted in flat mode
Browse files Browse the repository at this point in the history
  • Loading branch information
toshitanian committed Feb 27, 2020
1 parent c997462 commit 7260b69
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cli = meow(
-f, --format json | yaml [default: json]
-d, --default-locale default locale
--flat json [default: true] | yaml [default: false]
--sort-when-not-flat keys are sorted even when flat option is false [default: false]
Example
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
Expand All @@ -25,6 +26,9 @@ const cli = meow(
flat: {
type: 'boolean'
},
'sort-when-not-flat': {
type: 'boolean'
},
output: {
type: 'string',
alias: 'o'
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Opts = {
defaultLocale: string
format?: string
flat?: boolean
sortWhenNotFlat?: boolean
[key: string]: unknown
}

Expand All @@ -72,6 +73,7 @@ const extractMessage = async (
{
format = 'json',
flat = isJson(format),
sortWhenNotFlat = false,
defaultLocale = 'en',
...opts
}: Opts = {
Expand Down Expand Up @@ -121,6 +123,8 @@ const extractMessage = async (

const fomattedLocaleMap: object = flat
? sortKeys(localeMap, { deep: true })
: sortWhenNotFlat
? sortKeys(unflatten(localeMap, { object: true }), { deep: true })
: unflatten(sortKeys(localeMap), { object: true })

const fn = isJson(format) ? writeJson : writeYaml
Expand Down

0 comments on commit 7260b69

Please sign in to comment.