From 50c1d32471dbd6b1111eec81630c6b3f70cf8385 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Sun, 14 Nov 2021 08:29:04 -0500 Subject: [PATCH] feat(cli): export loadCodegenConfig to load codegen configuration files (#6917) --- .changeset/calm-cherries-visit.md | 5 ++ babel.config.js | 5 +- packages/graphql-codegen-cli/src/config.ts | 63 +++++++++++++++++++--- 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 .changeset/calm-cherries-visit.md diff --git a/.changeset/calm-cherries-visit.md b/.changeset/calm-cherries-visit.md new file mode 100644 index 00000000000..874da586661 --- /dev/null +++ b/.changeset/calm-cherries-visit.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/cli': minor +--- + +feat(cli): export loadCodegenConfig to load codegen configuration files diff --git a/babel.config.js b/babel.config.js index e6ffbd417e7..3e4899f4a74 100644 --- a/babel.config.js +++ b/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', + ], }; diff --git a/packages/graphql-codegen-cli/src/config.ts b/packages/graphql-codegen-cli/src/config.ts index 8e9e53e0947..d701ae8c215 100644 --- a/packages/graphql-codegen-cli/src/config.ts +++ b/packages/graphql-codegen-cli/src/config.ts @@ -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; @@ -58,20 +61,64 @@ function customLoader(ext: 'json' | 'yaml' | 'js') { return loader; } -export async function loadContext(configFilePath?: string): Promise | 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 Promise | 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 { + 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 | never { const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath); if (graphqlConfig) { @@ -80,7 +127,7 @@ export async function loadContext(configFilePath?: string): Promise