11import path from "path"
22import fs from "fs"
33import chalk from "chalk"
4- import { cosmiconfigSync } from "cosmiconfig"
4+ import { Loader , cosmiconfigSync } from "cosmiconfig"
55import { multipleValidOptions , validate } from "jest-validate"
6+ import get from "lodash.get"
67
78export type CatalogFormat = "lingui" | "minimal" | "po" | "csv"
89
@@ -27,7 +28,7 @@ type DefaultLocaleObject = {
2728}
2829export type FallbackLocales = LocaleObject | DefaultLocaleObject | false
2930
30- type ModuleSource = [ string , string ?] ;
31+ type ModuleSource = [ string , string ?]
3132
3233export type LinguiConfig = {
3334 catalogs : CatalogConfig [ ]
@@ -42,7 +43,7 @@ export type LinguiConfig = {
4243 orderBy : OrderBy
4344 pseudoLocale : string
4445 rootDir : string
45- runtimeConfigModule : ModuleSource | { [ symbolName : string ] : ModuleSource } ,
46+ runtimeConfigModule : ModuleSource | { [ symbolName : string ] : ModuleSource }
4647 sourceLocale : string
4748}
4849
@@ -92,7 +93,23 @@ export function getConfig({
9293 skipValidation ?: boolean
9394} = { } ) : LinguiConfig {
9495 const defaultRootDir = cwd || process . cwd ( )
95- const configExplorer = cosmiconfigSync ( "lingui" )
96+ const moduleName = "lingui"
97+ const configExplorer = cosmiconfigSync ( moduleName , {
98+ searchPlaces : [
99+ "package.json" ,
100+ `.${ moduleName } rc` ,
101+ `.${ moduleName } rc.json` ,
102+ `.${ moduleName } rc.yaml` ,
103+ `.${ moduleName } rc.yml` ,
104+ `.${ moduleName } rc.ts` ,
105+ `.${ moduleName } rc.js` ,
106+ `${ moduleName } .config.ts` ,
107+ `${ moduleName } .config.js` ,
108+ ] ,
109+ loaders : {
110+ ".ts" : TypeScriptLoader ,
111+ } ,
112+ } )
96113
97114 const result = configExists ( configPath )
98115 ? configExplorer . load ( configPath )
@@ -141,7 +158,7 @@ const exampleConfig = {
141158}
142159
143160const deprecatedConfig = {
144- fallbackLocale : ( config : LinguiConfig & DeprecatedFallbackLanguage ) =>
161+ fallbackLocale : ( config : LinguiConfig & DeprecatedFallbackLanguage ) =>
145162 ` Option ${ chalk . bold ( "fallbackLocale" ) } was replaced by ${ chalk . bold (
146163 "fallbackLocales"
147164 ) }
@@ -151,9 +168,7 @@ const deprecatedConfig = {
151168 @lingui/cli now treats your current configuration as:
152169 {
153170 ${ chalk . bold ( '"fallbackLocales"' ) } : {
154- default: ${ chalk . bold (
155- `"${ config . fallbackLocale } "`
156- ) }
171+ default: ${ chalk . bold ( `"${ config . fallbackLocale } "` ) }
157172 }
158173 }
159174
@@ -276,17 +291,18 @@ export function fallbackLanguageMigration(
276291) : LinguiConfig {
277292 const { fallbackLocale, fallbackLocales } = config
278293
279- if ( fallbackLocales === false ) return {
280- ...config ,
281- fallbackLocales : null ,
282- }
294+ if ( fallbackLocales === false )
295+ return {
296+ ...config ,
297+ fallbackLocales : null ,
298+ }
283299
284300 config . locales . forEach ( ( locale ) => {
285301 const fl = getCldrParentLocale ( locale . toLowerCase ( ) )
286302 if ( fl && ! config . fallbackLocales [ locale ] ) {
287303 config . fallbackLocales = {
288304 ...config . fallbackLocales ,
289- [ locale ] : fl
305+ [ locale ] : fl ,
290306 }
291307 }
292308 } )
@@ -475,7 +491,7 @@ function getCldrParentLocale(sourceLocale: string) {
475491 "yue-hans" : "yue" ,
476492 "zh-hant" : "zh" ,
477493 "zh-hant-hk" : "zh" ,
478- "zh-hant-mo" : "zh-hant-hk"
494+ "zh-hant-mo" : "zh-hant-hk" ,
479495 } [ sourceLocale ]
480496}
481497
@@ -525,3 +541,21 @@ export function catalogMigration(
525541
526542const pipe = ( ...functions : Array < Function > ) => ( args : any ) : any =>
527543 functions . reduce ( ( arg , fn ) => fn ( arg ) , args )
544+
545+ // copied from @EndemolShineGroup /cosmiconfig-typescript-loader
546+ // to support sync operations
547+ // if this PR gets merged and published, this piece of code can be thrown away
548+ // https://github.com/EndemolShineGroup/cosmiconfig-typescript-loader/pull/132
549+ function TypeScriptLoader ( filePath : string ) {
550+ try {
551+ require ( "ts-node/register" )
552+ const result = require ( filePath )
553+
554+ return get ( result , "default" , result )
555+ } catch ( error ) {
556+ // Replace with logger class OR throw a more specific error
557+ throw require ( "@endemolshinegroup/cosmiconfig-typescript-loader/dist/Errors/TypeScriptCompileError" ) . fromError (
558+ error
559+ )
560+ }
561+ }
0 commit comments