@@ -9,6 +9,9 @@ import { loadSchema, loadDocuments, defaultSchemaLoadOptions, defaultDocumentsLo
9
9
import { GraphQLSchema } from 'graphql' ;
10
10
import yaml from 'yaml' ;
11
11
import { createRequire } from 'module' ;
12
+ import { promises } from 'fs' ;
13
+
14
+ const { lstat } = promises ;
12
15
13
16
export type YamlCliFlags = {
14
17
config : string ;
@@ -58,20 +61,64 @@ function customLoader(ext: 'json' | 'yaml' | 'js') {
58
61
return loader ;
59
62
}
60
63
61
- export async function loadContext ( configFilePath ?: string ) : Promise < CodegenContext > | never {
62
- const moduleName = 'codegen' ;
64
+ export interface LoadCodegenConfigOptions {
65
+ /**
66
+ * The path to the config file or directory contains the config file.
67
+ * @default process.cwd()
68
+ */
69
+ configFilePath ?: string ;
70
+ /**
71
+ * The name of the config file
72
+ * @default codegen
73
+ */
74
+ moduleName ?: string ;
75
+ /**
76
+ * Additional search paths for the config file you want to check
77
+ */
78
+ searchPlaces ?: string [ ] ;
79
+ /**
80
+ * @default codegen
81
+ */
82
+ packageProp ?: string ;
83
+ /**
84
+ * Overrides or extends the loaders for specific file extensions
85
+ */
86
+ loaders ?: Record < string , ( filepath : string , content : string ) => Promise < Types . Config > | Types . Config > ;
87
+ }
88
+
89
+ export interface LoadCodegenConfigResult {
90
+ filepath : string ;
91
+ config : Types . Config ;
92
+ isEmpty ?: boolean ;
93
+ }
94
+
95
+ export async function loadCodegenConfig ( {
96
+ configFilePath,
97
+ moduleName,
98
+ searchPlaces : additionalSearchPlaces ,
99
+ packageProp,
100
+ loaders : customLoaders ,
101
+ } : LoadCodegenConfigOptions ) : Promise < LoadCodegenConfigResult > {
102
+ configFilePath = configFilePath || process . cwd ( ) ;
103
+ moduleName = moduleName || 'codegen' ;
104
+ packageProp = packageProp || moduleName ;
63
105
const cosmi = cosmiconfig ( moduleName , {
64
- searchPlaces : generateSearchPlaces ( moduleName ) ,
65
- packageProp : moduleName ,
106
+ searchPlaces : generateSearchPlaces ( moduleName ) . concat ( additionalSearchPlaces || [ ] ) ,
107
+ packageProp,
66
108
loaders : {
67
109
'.json' : customLoader ( 'json' ) ,
68
110
'.yaml' : customLoader ( 'yaml' ) ,
69
111
'.yml' : customLoader ( 'yaml' ) ,
70
112
'.js' : customLoader ( 'js' ) ,
71
113
noExt : customLoader ( 'yaml' ) ,
114
+ ...customLoaders ,
72
115
} ,
73
116
} ) ;
117
+ const pathStats = await lstat ( configFilePath ) ;
118
+ return pathStats . isDirectory ( ) ? cosmi . search ( configFilePath ) : cosmi . load ( configFilePath ) ;
119
+ }
74
120
121
+ export async function loadContext ( configFilePath ?: string ) : Promise < CodegenContext > | never {
75
122
const graphqlConfig = await findAndLoadGraphQLConfig ( configFilePath ) ;
76
123
77
124
if ( graphqlConfig ) {
@@ -80,17 +127,17 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
80
127
} ) ;
81
128
}
82
129
83
- const result = await ( configFilePath ? cosmi . load ( configFilePath ) : cosmi . search ( process . cwd ( ) ) ) ;
130
+ const result = await loadCodegenConfig ( { configFilePath } ) ;
84
131
85
132
if ( ! result ) {
86
133
if ( configFilePath ) {
87
134
throw new DetailedError (
88
135
`Config ${ configFilePath } does not exist` ,
89
136
`
90
137
Config ${ configFilePath } does not exist.
91
-
138
+
92
139
$ graphql-codegen --config ${ configFilePath }
93
-
140
+
94
141
Please make sure the --config points to a correct file.
95
142
`
96
143
) ;
@@ -99,7 +146,7 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
99
146
throw new DetailedError (
100
147
`Unable to find Codegen config file!` ,
101
148
`
102
- Please make sure that you have a configuration file under the current directory!
149
+ Please make sure that you have a configuration file under the current directory!
103
150
`
104
151
) ;
105
152
}
0 commit comments