Skip to content

Commit

Permalink
feat: Support lingui config in multiple formats (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredyC authored Jan 13, 2021
1 parent 249c486 commit 8631111
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/conf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"@babel/runtime": "^7.11.2",
"@endemolshinegroup/cosmiconfig-typescript-loader": "^3.0.2",
"chalk": "^4.1.0",
"cosmiconfig": "^7.0.0",
"jest-validate": "^26.5.2"
Expand Down
62 changes: 48 additions & 14 deletions packages/conf/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "path"
import fs from "fs"
import chalk from "chalk"
import { cosmiconfigSync } from "cosmiconfig"
import { Loader, cosmiconfigSync } from "cosmiconfig"
import { multipleValidOptions, validate } from "jest-validate"
import get from "lodash.get"

export type CatalogFormat = "lingui" | "minimal" | "po" | "csv"

Expand All @@ -27,7 +28,7 @@ type DefaultLocaleObject = {
}
export type FallbackLocales = LocaleObject | DefaultLocaleObject | false

type ModuleSource = [string, string?];
type ModuleSource = [string, string?]

export type LinguiConfig = {
catalogs: CatalogConfig[]
Expand All @@ -42,7 +43,7 @@ export type LinguiConfig = {
orderBy: OrderBy
pseudoLocale: string
rootDir: string
runtimeConfigModule: ModuleSource | {[symbolName: string]: ModuleSource},
runtimeConfigModule: ModuleSource | { [symbolName: string]: ModuleSource }
sourceLocale: string
}

Expand Down Expand Up @@ -92,7 +93,23 @@ export function getConfig({
skipValidation?: boolean
} = {}): LinguiConfig {
const defaultRootDir = cwd || process.cwd()
const configExplorer = cosmiconfigSync("lingui")
const moduleName = "lingui"
const configExplorer = cosmiconfigSync(moduleName, {
searchPlaces: [
"package.json",
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.js`,
`${moduleName}.config.ts`,
`${moduleName}.config.js`,
],
loaders: {
".ts": TypeScriptLoader,
},
})

const result = configExists(configPath)
? configExplorer.load(configPath)
Expand Down Expand Up @@ -141,7 +158,7 @@ const exampleConfig = {
}

const deprecatedConfig = {
fallbackLocale: (config: LinguiConfig & DeprecatedFallbackLanguage) =>
fallbackLocale: (config: LinguiConfig & DeprecatedFallbackLanguage) =>
` Option ${chalk.bold("fallbackLocale")} was replaced by ${chalk.bold(
"fallbackLocales"
)}
Expand All @@ -151,9 +168,7 @@ const deprecatedConfig = {
@lingui/cli now treats your current configuration as:
{
${chalk.bold('"fallbackLocales"')}: {
default: ${chalk.bold(
`"${config.fallbackLocale}"`
)}
default: ${chalk.bold(`"${config.fallbackLocale}"`)}
}
}
Expand Down Expand Up @@ -276,17 +291,18 @@ export function fallbackLanguageMigration(
): LinguiConfig {
const { fallbackLocale, fallbackLocales } = config

if (fallbackLocales === false) return {
...config,
fallbackLocales: null,
}
if (fallbackLocales === false)
return {
...config,
fallbackLocales: null,
}

config.locales.forEach((locale) => {
const fl = getCldrParentLocale(locale.toLowerCase())
if (fl && !config.fallbackLocales[locale]) {
config.fallbackLocales = {
...config.fallbackLocales,
[locale]: fl
[locale]: fl,
}
}
})
Expand Down Expand Up @@ -475,7 +491,7 @@ function getCldrParentLocale(sourceLocale: string) {
"yue-hans": "yue",
"zh-hant": "zh",
"zh-hant-hk": "zh",
"zh-hant-mo": "zh-hant-hk"
"zh-hant-mo": "zh-hant-hk",
}[sourceLocale]
}

Expand Down Expand Up @@ -525,3 +541,21 @@ export function catalogMigration(

const pipe = (...functions: Array<Function>) => (args: any): any =>
functions.reduce((arg, fn) => fn(arg), args)

// copied from @EndemolShineGroup/cosmiconfig-typescript-loader
// to support sync operations
// if this PR gets merged and published, this piece of code can be thrown away
// https://github.com/EndemolShineGroup/cosmiconfig-typescript-loader/pull/132
function TypeScriptLoader(filePath: string) {
try {
require("ts-node/register")
const result = require(filePath)

return get(result, "default", result)
} catch (error) {
// Replace with logger class OR throw a more specific error
throw require("@endemolshinegroup/cosmiconfig-typescript-loader/dist/Errors/TypeScriptCompileError").fromError(
error
)
}
}
48 changes: 45 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,16 @@
exec-sh "^0.3.2"
minimist "^1.2.0"

"@endemolshinegroup/cosmiconfig-typescript-loader@^3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@endemolshinegroup/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-3.0.2.tgz#eea4635828dde372838b0909693ebd9aafeec22d"
integrity sha512-QRVtqJuS1mcT56oHpVegkKBlgtWjXw/gHNWO3eL9oyB5Sc7HBoc2OLG/nYpVfT/Jejvo3NUrD0Udk7XgoyDKkA==
dependencies:
lodash.get "^4"
make-error "^1"
ts-node "^9"
tslib "^2"

"@eslint/eslintrc@^0.1.3":
version "0.1.3"
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-0.1.3.tgz#7d1a2b2358552cc04834c0979bd4275362e37085"
Expand Down Expand Up @@ -3386,6 +3396,11 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0"
readable-stream "^2.0.6"

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
Expand Down Expand Up @@ -4758,6 +4773,11 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-env@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.2.tgz#bd5ed31339a93a3418ac4f3ca9ca3403082ae5f9"
Expand Down Expand Up @@ -8244,7 +8264,7 @@ lodash.foreach@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.foreach/-/lodash.foreach-4.5.0.tgz#1a6a35eace401280c7f06dddec35165ab27e3e53"
integrity sha1-Gmo16s5AEoDH8G3d7DUWWrJ+PlM=

lodash.get@^4.4.2:
lodash.get@^4, lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=
Expand Down Expand Up @@ -8484,7 +8504,7 @@ make-dir@^3.0.0, make-dir@^3.0.2:
dependencies:
semver "^6.0.0"

make-error@1.x:
make-error@1.x, make-error@^1, make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
Expand Down Expand Up @@ -11448,7 +11468,7 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"

source-map-support@^0.5.11, source-map-support@^0.5.19, source-map-support@^0.5.6, source-map-support@^0.5.7, source-map-support@~0.5.12:
source-map-support@^0.5.11, source-map-support@^0.5.17, source-map-support@^0.5.19, source-map-support@^0.5.6, source-map-support@^0.5.7, source-map-support@~0.5.12:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
Expand Down Expand Up @@ -12302,6 +12322,18 @@ ts-jest@^26.4.1:
semver "7.x"
yargs-parser "20.x"

ts-node@^9:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
dependencies:
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"

ts-toolbelt@^6.3.3:
version "6.15.5"
resolved "https://registry.yarnpkg.com/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz#cb3b43ed725cb63644782c64fbcad7d8f28c0a83"
Expand Down Expand Up @@ -12332,6 +12364,11 @@ tslib@^1, tslib@^1.10.0, tslib@^1.11.1, tslib@^1.11.2, tslib@^1.13.0, tslib@^1.8
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
integrity sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==

tslib@^2:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"
integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==

tsutils@^3.17.1:
version "3.17.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.17.1.tgz#ed719917f11ca0dee586272b2ac49e015a2dd759"
Expand Down Expand Up @@ -13154,3 +13191,8 @@ yargs@^7.1.0:
which-module "^1.0.0"
y18n "^3.2.1"
yargs-parser "5.0.0-security.0"

yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

1 comment on commit 8631111

@vercel
Copy link

@vercel vercel bot commented on 8631111 Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.