1
1
import { cosmiconfig , cosmiconfigSync , Loader , defaultLoaders } from 'cosmiconfig' ;
2
- import { TypeScriptLoader } from 'cosmiconfig-typescript-loader' ;
3
2
import { loadToml } from 'cosmiconfig-toml-loader' ;
4
3
import { env } from 'string-env-interpolation' ;
5
4
@@ -9,49 +8,47 @@ export interface ConfigSearchResult {
9
8
isEmpty ?: boolean ;
10
9
}
11
10
12
- const legacySearchPlaces = [ '.graphqlconfig' , '.graphqlconfig.json' , '.graphqlconfig.yaml' , '.graphqlconfig.yml' ] ;
11
+ const legacySearchPlaces = [
12
+ '.graphqlconfig' ,
13
+ '.graphqlconfig.json' ,
14
+ '.graphqlconfig.yaml' ,
15
+ '.graphqlconfig.yml' ,
16
+ ] as const ;
13
17
14
- export function isLegacyConfig ( filepath : string ) : boolean {
15
- filepath = filepath . toLowerCase ( ) ;
16
-
17
- return legacySearchPlaces . some ( ( name ) => filepath . endsWith ( name ) ) ;
18
+ export function isLegacyConfig ( filePath : string ) : boolean {
19
+ filePath = filePath . toLowerCase ( ) ;
20
+ return legacySearchPlaces . some ( ( name ) => filePath . endsWith ( name ) ) ;
18
21
}
19
22
20
23
function transformContent ( content : string ) : string {
21
24
return env ( content ) ;
22
25
}
23
26
24
- const createCustomLoader = ( loader : Loader ) : Loader => {
25
- return ( filepath , content ) => {
26
- return loader ( filepath , transformContent ( content ) ) ;
27
- } ;
28
- } ;
27
+ function createCustomLoader ( loader : Loader ) : Loader {
28
+ return ( filePath , content ) => loader ( filePath , transformContent ( content ) ) ;
29
+ }
29
30
30
- export function createCosmiConfig (
31
- moduleName : string ,
32
- {
33
- legacy,
34
- } : {
35
- legacy : boolean ;
36
- } ,
37
- ) {
38
- const options = prepareCosmiconfig ( moduleName , {
39
- legacy,
40
- } ) ;
31
+ export function createCosmiConfig ( moduleName : string , legacy : boolean ) {
32
+ const options = prepareCosmiconfig ( moduleName , legacy ) ;
41
33
42
34
return cosmiconfig ( moduleName , options ) ;
43
35
}
44
36
45
- export function createCosmiConfigSync ( moduleName : string , { legacy } : { legacy : boolean } ) {
46
- const options = prepareCosmiconfig ( moduleName , { legacy } ) ;
37
+ export function createCosmiConfigSync ( moduleName : string , legacy : boolean ) {
38
+ const options = prepareCosmiconfig ( moduleName , legacy ) ;
47
39
48
40
return cosmiconfigSync ( moduleName , options ) ;
49
41
}
50
42
51
- function prepareCosmiconfig ( moduleName : string , { legacy } : { legacy : boolean } ) {
43
+ const loadTypeScript : Loader = ( ...args ) => {
44
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
45
+ const { TypeScriptLoader } = require ( 'cosmiconfig-typescript-loader' ) ;
46
+
47
+ return TypeScriptLoader ( { transpileOnly : true } ) ( ...args ) ;
48
+ } ;
49
+
50
+ function prepareCosmiconfig ( moduleName : string , legacy : boolean ) {
52
51
const loadYaml = createCustomLoader ( defaultLoaders [ '.yaml' ] ) ;
53
- const loadTomlCustom = createCustomLoader ( loadToml ) ;
54
- const loadJson = createCustomLoader ( defaultLoaders [ '.json' ] ) ;
55
52
56
53
const searchPlaces = [
57
54
'#.config.ts' ,
@@ -81,12 +78,12 @@ function prepareCosmiconfig(moduleName: string, { legacy }: { legacy: boolean })
81
78
return {
82
79
searchPlaces : searchPlaces . map ( ( place ) => place . replace ( '#' , moduleName ) ) ,
83
80
loaders : {
84
- '.ts' : TypeScriptLoader ( { transpileOnly : true } ) ,
81
+ '.ts' : loadTypeScript ,
85
82
'.js' : defaultLoaders [ '.js' ] ,
86
- '.json' : loadJson ,
83
+ '.json' : createCustomLoader ( defaultLoaders [ '.json' ] ) ,
87
84
'.yaml' : loadYaml ,
88
85
'.yml' : loadYaml ,
89
- '.toml' : loadTomlCustom ,
86
+ '.toml' : createCustomLoader ( loadToml ) ,
90
87
noExt : loadYaml ,
91
88
} ,
92
89
} ;
0 commit comments