Skip to content

Commit

Permalink
feat(cli): export loadCodegenConfig to load codegen configuration fil…
Browse files Browse the repository at this point in the history
…es (#6917)
  • Loading branch information
ardatan committed Nov 14, 2021
1 parent d823f08 commit 50c1d32
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-cherries-visit.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': minor
---

feat(cli): export loadCodegenConfig to load codegen configuration files
5 changes: 4 additions & 1 deletion babel.config.js
@@ -1,3 +1,6 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
presets: [
['@babel/preset-env', { targets: { node: process.versions.node.split('.')[0] } }],
'@babel/preset-typescript',
],
};
63 changes: 55 additions & 8 deletions packages/graphql-codegen-cli/src/config.ts
Expand Up @@ -9,6 +9,9 @@ import { loadSchema, loadDocuments, defaultSchemaLoadOptions, defaultDocumentsLo
import { GraphQLSchema } from 'graphql';
import yaml from 'yaml';
import { createRequire } from 'module';
import { promises } from 'fs';

const { lstat } = promises;

export type YamlCliFlags = {
config: string;
Expand Down Expand Up @@ -58,20 +61,64 @@ function customLoader(ext: 'json' | 'yaml' | 'js') {
return loader;
}

export async function loadContext(configFilePath?: string): Promise<CodegenContext> | never {
const moduleName = 'codegen';
export interface LoadCodegenConfigOptions {
/**
* The path to the config file or directory contains the config file.
* @default process.cwd()
*/
configFilePath?: string;
/**
* The name of the config file
* @default codegen
*/
moduleName?: string;
/**
* Additional search paths for the config file you want to check
*/
searchPlaces?: string[];
/**
* @default codegen
*/
packageProp?: string;
/**
* Overrides or extends the loaders for specific file extensions
*/
loaders?: Record<string, (filepath: string, content: string) => Promise<Types.Config> | Types.Config>;
}

export interface LoadCodegenConfigResult {
filepath: string;
config: Types.Config;
isEmpty?: boolean;
}

export async function loadCodegenConfig({
configFilePath,
moduleName,
searchPlaces: additionalSearchPlaces,
packageProp,
loaders: customLoaders,
}: LoadCodegenConfigOptions): Promise<LoadCodegenConfigResult> {
configFilePath = configFilePath || process.cwd();
moduleName = moduleName || 'codegen';
packageProp = packageProp || moduleName;
const cosmi = cosmiconfig(moduleName, {
searchPlaces: generateSearchPlaces(moduleName),
packageProp: moduleName,
searchPlaces: generateSearchPlaces(moduleName).concat(additionalSearchPlaces || []),
packageProp,
loaders: {
'.json': customLoader('json'),
'.yaml': customLoader('yaml'),
'.yml': customLoader('yaml'),
'.js': customLoader('js'),
noExt: customLoader('yaml'),
...customLoaders,
},
});
const pathStats = await lstat(configFilePath);
return pathStats.isDirectory() ? cosmi.search(configFilePath) : cosmi.load(configFilePath);
}

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

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

const result = await (configFilePath ? cosmi.load(configFilePath) : cosmi.search(process.cwd()));
const result = await loadCodegenConfig({ configFilePath });

if (!result) {
if (configFilePath) {
throw new DetailedError(
`Config ${configFilePath} does not exist`,
`
Config ${configFilePath} does not exist.
$ graphql-codegen --config ${configFilePath}
Please make sure the --config points to a correct file.
`
);
Expand All @@ -99,7 +146,7 @@ export async function loadContext(configFilePath?: string): Promise<CodegenConte
throw new DetailedError(
`Unable to find Codegen config file!`,
`
Please make sure that you have a configuration file under the current directory!
Please make sure that you have a configuration file under the current directory!
`
);
}
Expand Down

0 comments on commit 50c1d32

Please sign in to comment.