Skip to content

Commit 50c1d32

Browse files
authored
feat(cli): export loadCodegenConfig to load codegen configuration files (#6917)
1 parent d823f08 commit 50c1d32

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

.changeset/calm-cherries-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/cli': minor
3+
---
4+
5+
feat(cli): export loadCodegenConfig to load codegen configuration files

babel.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module.exports = {
2-
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
2+
presets: [
3+
['@babel/preset-env', { targets: { node: process.versions.node.split('.')[0] } }],
4+
'@babel/preset-typescript',
5+
],
36
};

packages/graphql-codegen-cli/src/config.ts

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { loadSchema, loadDocuments, defaultSchemaLoadOptions, defaultDocumentsLo
99
import { GraphQLSchema } from 'graphql';
1010
import yaml from 'yaml';
1111
import { createRequire } from 'module';
12+
import { promises } from 'fs';
13+
14+
const { lstat } = promises;
1215

1316
export type YamlCliFlags = {
1417
config: string;
@@ -58,20 +61,64 @@ function customLoader(ext: 'json' | 'yaml' | 'js') {
5861
return loader;
5962
}
6063

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;
63105
const cosmi = cosmiconfig(moduleName, {
64-
searchPlaces: generateSearchPlaces(moduleName),
65-
packageProp: moduleName,
106+
searchPlaces: generateSearchPlaces(moduleName).concat(additionalSearchPlaces || []),
107+
packageProp,
66108
loaders: {
67109
'.json': customLoader('json'),
68110
'.yaml': customLoader('yaml'),
69111
'.yml': customLoader('yaml'),
70112
'.js': customLoader('js'),
71113
noExt: customLoader('yaml'),
114+
...customLoaders,
72115
},
73116
});
117+
const pathStats = await lstat(configFilePath);
118+
return pathStats.isDirectory() ? cosmi.search(configFilePath) : cosmi.load(configFilePath);
119+
}
74120

121+
export async function loadContext(configFilePath?: string): Promise<CodegenContext> | never {
75122
const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);
76123

77124
if (graphqlConfig) {
@@ -80,17 +127,17 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
80127
});
81128
}
82129

83-
const result = await (configFilePath ? cosmi.load(configFilePath) : cosmi.search(process.cwd()));
130+
const result = await loadCodegenConfig({ configFilePath });
84131

85132
if (!result) {
86133
if (configFilePath) {
87134
throw new DetailedError(
88135
`Config ${configFilePath} does not exist`,
89136
`
90137
Config ${configFilePath} does not exist.
91-
138+
92139
$ graphql-codegen --config ${configFilePath}
93-
140+
94141
Please make sure the --config points to a correct file.
95142
`
96143
);
@@ -99,7 +146,7 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
99146
throw new DetailedError(
100147
`Unable to find Codegen config file!`,
101148
`
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!
103150
`
104151
);
105152
}

0 commit comments

Comments
 (0)