@graphql-codegen/cli 2.2.1-alpha-7f84a4ff7.0 → 2.3.0-alpha-166ea3cc1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin.js CHANGED
@@ -547,6 +547,7 @@ async function loadDocuments(documentPointers, config) {
547
547
  return loadedFromToolkit;
548
548
  }
549
549
 
550
+ const { lstat } = fs.promises;
550
551
  function generateSearchPlaces(moduleName) {
551
552
  const extensions = ['json', 'yaml', 'yml', 'js', 'config.js'];
552
553
  // gives codegen.json...
@@ -579,38 +580,45 @@ function customLoader(ext) {
579
580
  }
580
581
  return loader;
581
582
  }
582
- async function loadContext(configFilePath) {
583
- const moduleName = 'codegen';
583
+ async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: additionalSearchPlaces, packageProp, loaders: customLoaders, }) {
584
+ configFilePath = configFilePath || process.cwd();
585
+ moduleName = moduleName || 'codegen';
586
+ packageProp = packageProp || moduleName;
584
587
  const cosmi = cosmiconfig.cosmiconfig(moduleName, {
585
- searchPlaces: generateSearchPlaces(moduleName),
586
- packageProp: moduleName,
588
+ searchPlaces: generateSearchPlaces(moduleName).concat(additionalSearchPlaces || []),
589
+ packageProp,
587
590
  loaders: {
588
591
  '.json': customLoader('json'),
589
592
  '.yaml': customLoader('yaml'),
590
593
  '.yml': customLoader('yaml'),
591
594
  '.js': customLoader('js'),
592
595
  noExt: customLoader('yaml'),
596
+ ...customLoaders,
593
597
  },
594
598
  });
599
+ const pathStats = await lstat(configFilePath);
600
+ return pathStats.isDirectory() ? cosmi.search(configFilePath) : cosmi.load(configFilePath);
601
+ }
602
+ async function loadContext(configFilePath) {
595
603
  const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);
596
604
  if (graphqlConfig) {
597
605
  return new CodegenContext({
598
606
  graphqlConfig,
599
607
  });
600
608
  }
601
- const result = await (configFilePath ? cosmi.load(configFilePath) : cosmi.search(process.cwd()));
609
+ const result = await loadCodegenConfig({ configFilePath });
602
610
  if (!result) {
603
611
  if (configFilePath) {
604
612
  throw new pluginHelpers.DetailedError(`Config ${configFilePath} does not exist`, `
605
613
  Config ${configFilePath} does not exist.
606
-
614
+
607
615
  $ graphql-codegen --config ${configFilePath}
608
-
616
+
609
617
  Please make sure the --config points to a correct file.
610
618
  `);
611
619
  }
612
620
  throw new pluginHelpers.DetailedError(`Unable to find Codegen config file!`, `
613
- Please make sure that you have a configuration file under the current directory!
621
+ Please make sure that you have a configuration file under the current directory!
614
622
  `);
615
623
  }
616
624
  if (result.isEmpty) {
package/config.d.ts CHANGED
@@ -10,6 +10,36 @@ export declare type YamlCliFlags = {
10
10
  silent: boolean;
11
11
  errorsOnly: boolean;
12
12
  };
13
+ export interface LoadCodegenConfigOptions {
14
+ /**
15
+ * The path to the config file or directory contains the config file.
16
+ * @default process.cwd()
17
+ */
18
+ configFilePath?: string;
19
+ /**
20
+ * The name of the config file
21
+ * @default codegen
22
+ */
23
+ moduleName?: string;
24
+ /**
25
+ * Additional search paths for the config file you want to check
26
+ */
27
+ searchPlaces?: string[];
28
+ /**
29
+ * @default codegen
30
+ */
31
+ packageProp?: string;
32
+ /**
33
+ * Overrides or extends the loaders for specific file extensions
34
+ */
35
+ loaders?: Record<string, (filepath: string, content: string) => Promise<Types.Config> | Types.Config>;
36
+ }
37
+ export interface LoadCodegenConfigResult {
38
+ filepath: string;
39
+ config: Types.Config;
40
+ isEmpty?: boolean;
41
+ }
42
+ export declare function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: additionalSearchPlaces, packageProp, loaders: customLoaders, }: LoadCodegenConfigOptions): Promise<LoadCodegenConfigResult>;
13
43
  export declare function loadContext(configFilePath?: string): Promise<CodegenContext> | never;
14
44
  export declare function buildOptions(): {
15
45
  c: {
package/index.js CHANGED
@@ -465,6 +465,7 @@ async function loadDocuments(documentPointers, config) {
465
465
  return loadedFromToolkit;
466
466
  }
467
467
 
468
+ const { lstat } = fs.promises;
468
469
  function generateSearchPlaces(moduleName) {
469
470
  const extensions = ['json', 'yaml', 'yml', 'js', 'config.js'];
470
471
  // gives codegen.json...
@@ -497,38 +498,45 @@ function customLoader(ext) {
497
498
  }
498
499
  return loader;
499
500
  }
500
- async function loadContext(configFilePath) {
501
- const moduleName = 'codegen';
501
+ async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: additionalSearchPlaces, packageProp, loaders: customLoaders, }) {
502
+ configFilePath = configFilePath || process.cwd();
503
+ moduleName = moduleName || 'codegen';
504
+ packageProp = packageProp || moduleName;
502
505
  const cosmi = cosmiconfig.cosmiconfig(moduleName, {
503
- searchPlaces: generateSearchPlaces(moduleName),
504
- packageProp: moduleName,
506
+ searchPlaces: generateSearchPlaces(moduleName).concat(additionalSearchPlaces || []),
507
+ packageProp,
505
508
  loaders: {
506
509
  '.json': customLoader('json'),
507
510
  '.yaml': customLoader('yaml'),
508
511
  '.yml': customLoader('yaml'),
509
512
  '.js': customLoader('js'),
510
513
  noExt: customLoader('yaml'),
514
+ ...customLoaders,
511
515
  },
512
516
  });
517
+ const pathStats = await lstat(configFilePath);
518
+ return pathStats.isDirectory() ? cosmi.search(configFilePath) : cosmi.load(configFilePath);
519
+ }
520
+ async function loadContext(configFilePath) {
513
521
  const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);
514
522
  if (graphqlConfig) {
515
523
  return new CodegenContext({
516
524
  graphqlConfig,
517
525
  });
518
526
  }
519
- const result = await (configFilePath ? cosmi.load(configFilePath) : cosmi.search(process.cwd()));
527
+ const result = await loadCodegenConfig({ configFilePath });
520
528
  if (!result) {
521
529
  if (configFilePath) {
522
530
  throw new pluginHelpers.DetailedError(`Config ${configFilePath} does not exist`, `
523
531
  Config ${configFilePath} does not exist.
524
-
532
+
525
533
  $ graphql-codegen --config ${configFilePath}
526
-
534
+
527
535
  Please make sure the --config points to a correct file.
528
536
  `);
529
537
  }
530
538
  throw new pluginHelpers.DetailedError(`Unable to find Codegen config file!`, `
531
- Please make sure that you have a configuration file under the current directory!
539
+ Please make sure that you have a configuration file under the current directory!
532
540
  `);
533
541
  }
534
542
  if (result.isEmpty) {
@@ -1779,6 +1787,7 @@ exports.executeCodegen = executeCodegen;
1779
1787
  exports.findAndLoadGraphQLConfig = findAndLoadGraphQLConfig;
1780
1788
  exports.generate = generate;
1781
1789
  exports.init = init;
1790
+ exports.loadCodegenConfig = loadCodegenConfig;
1782
1791
  exports.loadContext = loadContext;
1783
1792
  exports.parseArgv = parseArgv;
1784
1793
  exports.runCli = runCli;
package/index.mjs CHANGED
@@ -24,7 +24,7 @@ import { JsonFileLoader } from '@graphql-tools/json-file-loader';
24
24
  import { UrlLoader } from '@graphql-tools/url-loader';
25
25
  import yaml from 'yaml';
26
26
  import { createRequire } from 'module';
27
- import fs, { readFileSync, writeFileSync, statSync, unlink } from 'fs';
27
+ import fs, { promises, readFileSync, writeFileSync, statSync, unlink } from 'fs';
28
28
  import Listr from 'listr';
29
29
  import { exec } from 'child_process';
30
30
  import isGlob from 'is-glob';
@@ -438,6 +438,7 @@ async function loadDocuments(documentPointers, config) {
438
438
  return loadedFromToolkit;
439
439
  }
440
440
 
441
+ const { lstat } = promises;
441
442
  function generateSearchPlaces(moduleName) {
442
443
  const extensions = ['json', 'yaml', 'yml', 'js', 'config.js'];
443
444
  // gives codegen.json...
@@ -470,38 +471,45 @@ function customLoader(ext) {
470
471
  }
471
472
  return loader;
472
473
  }
473
- async function loadContext(configFilePath) {
474
- const moduleName = 'codegen';
474
+ async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: additionalSearchPlaces, packageProp, loaders: customLoaders, }) {
475
+ configFilePath = configFilePath || process.cwd();
476
+ moduleName = moduleName || 'codegen';
477
+ packageProp = packageProp || moduleName;
475
478
  const cosmi = cosmiconfig(moduleName, {
476
- searchPlaces: generateSearchPlaces(moduleName),
477
- packageProp: moduleName,
479
+ searchPlaces: generateSearchPlaces(moduleName).concat(additionalSearchPlaces || []),
480
+ packageProp,
478
481
  loaders: {
479
482
  '.json': customLoader('json'),
480
483
  '.yaml': customLoader('yaml'),
481
484
  '.yml': customLoader('yaml'),
482
485
  '.js': customLoader('js'),
483
486
  noExt: customLoader('yaml'),
487
+ ...customLoaders,
484
488
  },
485
489
  });
490
+ const pathStats = await lstat(configFilePath);
491
+ return pathStats.isDirectory() ? cosmi.search(configFilePath) : cosmi.load(configFilePath);
492
+ }
493
+ async function loadContext(configFilePath) {
486
494
  const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);
487
495
  if (graphqlConfig) {
488
496
  return new CodegenContext({
489
497
  graphqlConfig,
490
498
  });
491
499
  }
492
- const result = await (configFilePath ? cosmi.load(configFilePath) : cosmi.search(process.cwd()));
500
+ const result = await loadCodegenConfig({ configFilePath });
493
501
  if (!result) {
494
502
  if (configFilePath) {
495
503
  throw new DetailedError(`Config ${configFilePath} does not exist`, `
496
504
  Config ${configFilePath} does not exist.
497
-
505
+
498
506
  $ graphql-codegen --config ${configFilePath}
499
-
507
+
500
508
  Please make sure the --config points to a correct file.
501
509
  `);
502
510
  }
503
511
  throw new DetailedError(`Unable to find Codegen config file!`, `
504
- Please make sure that you have a configuration file under the current directory!
512
+ Please make sure that you have a configuration file under the current directory!
505
513
  `);
506
514
  }
507
515
  if (result.isEmpty) {
@@ -1741,4 +1749,4 @@ async function ensureGraphQlPackage() {
1741
1749
  }
1742
1750
  }
1743
1751
 
1744
- export { CodegenContext, CodegenExtension, buildOptions, cliError, createContext, ensureContext, ensureGraphQlPackage, executeCodegen, findAndLoadGraphQLConfig, generate, init, loadContext, parseArgv, runCli, updateContextWithCliFlags };
1752
+ export { CodegenContext, CodegenExtension, buildOptions, cliError, createContext, ensureContext, ensureGraphQlPackage, executeCodegen, findAndLoadGraphQLConfig, generate, init, loadCodegenConfig, loadContext, parseArgv, runCli, updateContextWithCliFlags };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.2.1-alpha-7f84a4ff7.0",
3
+ "version": "2.3.0-alpha-166ea3cc1.0",
4
4
  "peerDependencies": {
5
5
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0"
6
6
  },
7
7
  "dependencies": {
8
- "@graphql-codegen/core": "2.2.0-alpha-7f84a4ff7.0",
9
- "@graphql-codegen/plugin-helpers": "2.2.0-alpha-7f84a4ff7.0",
8
+ "@graphql-codegen/core": "2.2.0",
9
+ "@graphql-codegen/plugin-helpers": "^2.2.0",
10
10
  "@graphql-tools/apollo-engine-loader": "^7.0.5",
11
11
  "@graphql-tools/code-file-loader": "^7.0.6",
12
12
  "@graphql-tools/git-loader": "^7.0.5",