@graphql-codegen/cli 3.2.1 → 3.2.2-alpha-20230301201322-189cfbbc9

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/cjs/config.js CHANGED
@@ -10,12 +10,14 @@ const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
10
10
  const cosmiconfig_1 = require("cosmiconfig");
11
11
  const cosmiconfig_typescript_loader_1 = require("cosmiconfig-typescript-loader");
12
12
  const graphql_1 = require("graphql");
13
+ const child_process_1 = require("child_process");
13
14
  const string_env_interpolation_1 = require("string-env-interpolation");
14
15
  const yaml_1 = tslib_1.__importDefault(require("yaml"));
15
16
  const yargs_1 = tslib_1.__importDefault(require("yargs"));
16
17
  const graphql_config_js_1 = require("./graphql-config.js");
17
18
  const load_js_1 = require("./load.js");
18
- const { lstat } = fs_1.promises;
19
+ const file_system_js_1 = require("./utils/file-system.js");
20
+ const { lstat, rm } = fs_1.promises;
19
21
  function generateSearchPlaces(moduleName) {
20
22
  const extensions = ['json', 'yaml', 'yml', 'js', 'ts', 'config.js'];
21
23
  // gives codegen.json...
@@ -26,7 +28,8 @@ function generateSearchPlaces(moduleName) {
26
28
  }
27
29
  exports.generateSearchPlaces = generateSearchPlaces;
28
30
  function customLoader(ext) {
29
- function loader(filepath, content) {
31
+ return async function loader(filepath, content) {
32
+ var _a;
30
33
  if (typeof process !== 'undefined' && 'env' in process) {
31
34
  content = (0, string_env_interpolation_1.env)(content);
32
35
  }
@@ -47,12 +50,50 @@ function customLoader(ext) {
47
50
  return cosmiconfig_1.defaultLoaders['.js'](filepath, content);
48
51
  }
49
52
  if (ext === 'ts') {
50
- // #8437: conflict with `graphql-config` also using TypeScriptLoader(), causing a double `ts-node` register.
51
- const tsLoader = (0, cosmiconfig_typescript_loader_1.TypeScriptLoader)({ transpileOnly: true });
52
- return tsLoader(filepath, content);
53
+ try {
54
+ // #8437: conflict with `graphql-config` also using TypeScriptLoader(), causing a double `ts-node` register.
55
+ const tsLoader = (0, cosmiconfig_typescript_loader_1.TypeScriptLoader)({ transpileOnly: true });
56
+ return tsLoader(filepath, content);
57
+ }
58
+ catch (err) {
59
+ if (typeof err.stack === 'string' && err.stack.startsWith('Error [ERR_REQUIRE_ESM]:')) {
60
+ const hash = hashContent(content, 'base64url');
61
+ const tempDir = (0, file_system_js_1.getTempDir)();
62
+ // TODO
63
+ let inTempDir = [];
64
+ try {
65
+ inTempDir = await fs_1.promises.readdir(tempDir);
66
+ }
67
+ catch (err) { }
68
+ let outDir = (0, path_1.join)(tempDir, new Date().getTime() + '-' + hash);
69
+ const previousOutDir = inTempDir.find(s => s.endsWith(hash));
70
+ if (previousOutDir) {
71
+ outDir = (0, path_1.join)(tempDir, previousOutDir);
72
+ }
73
+ else {
74
+ // We're compiling the file, because ts-node doesn't work perfectly with ESM.
75
+ (0, child_process_1.execSync)(`tsc ${filepath} --module commonjs --outDir ${outDir}`, { stdio: 'pipe' });
76
+ }
77
+ const newPath = (0, path_1.join)(outDir, (0, path_1.basename)(filepath).replace(/\.ts$/, '.js'));
78
+ const config = (_a = newPath, Promise.resolve().then(() => tslib_1.__importStar(require(_a)))).then(m => {
79
+ const config = m.default;
80
+ return 'default' in config ? config.default : config;
81
+ });
82
+ // If the cache has more than 10 files, we delete the oldest one.
83
+ if (inTempDir.length > 10) {
84
+ const oldest = inTempDir.sort((a, b) => {
85
+ const aTime = Number(a.split('-')[0]);
86
+ const bTime = Number(b.split('-')[0]);
87
+ return aTime - bTime;
88
+ })[0];
89
+ await rm((0, path_1.join)(tempDir, oldest), { recursive: true, force: true });
90
+ }
91
+ return config;
92
+ }
93
+ throw err;
94
+ }
53
95
  }
54
- }
55
- return loader;
96
+ };
56
97
  }
57
98
  async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: additionalSearchPlaces, packageProp, loaders: customLoaders, }) {
58
99
  configFilePath || (configFilePath = process.cwd());
@@ -76,7 +117,18 @@ async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: add
76
117
  }
77
118
  exports.loadCodegenConfig = loadCodegenConfig;
78
119
  async function loadContext(configFilePath) {
79
- const graphqlConfig = await (0, graphql_config_js_1.findAndLoadGraphQLConfig)(configFilePath);
120
+ let graphqlConfig;
121
+ try {
122
+ graphqlConfig = await (0, graphql_config_js_1.findAndLoadGraphQLConfig)(configFilePath);
123
+ }
124
+ catch (err) {
125
+ if (typeof err.stack === 'string' && err.stack.startsWith('Error [ERR_REQUIRE_ESM]:')) {
126
+ // TODO: This needs a fix in graphql-config
127
+ }
128
+ else {
129
+ throw err;
130
+ }
131
+ }
80
132
  if (graphqlConfig) {
81
133
  return new CodegenContext({ graphqlConfig });
82
134
  }
@@ -312,8 +364,8 @@ function ensureContext(input) {
312
364
  return input instanceof CodegenContext ? input : new CodegenContext({ config: input });
313
365
  }
314
366
  exports.ensureContext = ensureContext;
315
- function hashContent(content) {
316
- return (0, crypto_1.createHash)('sha256').update(content).digest('hex');
367
+ function hashContent(content, encoding = 'hex') {
368
+ return (0, crypto_1.createHash)('sha256').update(content).digest(encoding);
317
369
  }
318
370
  function hashSchema(schema) {
319
371
  return hashContent((0, graphql_1.print)((0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema)));
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mkdirp = exports.unlinkFile = exports.readFile = exports.writeFile = void 0;
3
+ exports.getTempDir = exports.mkdirp = exports.unlinkFile = exports.readFile = exports.writeFile = void 0;
4
4
  const fs_1 = require("fs");
5
+ const os_1 = require("os");
6
+ const path_1 = require("path");
5
7
  const { writeFile: fsWriteFile, readFile: fsReadFile, mkdir } = fs_1.promises;
6
8
  function writeFile(filepath, content) {
7
9
  return fsWriteFile(filepath, content);
@@ -19,3 +21,7 @@ function mkdirp(filePath) {
19
21
  return mkdir(filePath, { recursive: true });
20
22
  }
21
23
  exports.mkdirp = mkdirp;
24
+ function getTempDir() {
25
+ return (0, path_1.join)((0, os_1.tmpdir)(), `graphql-codegen-cli`);
26
+ }
27
+ exports.getTempDir = getTempDir;
package/esm/config.js CHANGED
@@ -1,17 +1,19 @@
1
1
  import { createHash } from 'crypto';
2
2
  import { promises } from 'fs';
3
3
  import { createRequire } from 'module';
4
- import { resolve } from 'path';
4
+ import { basename, resolve, join } from 'path';
5
5
  import { createNoopProfiler, createProfiler, getCachedDocumentNodeFromSchema, } from '@graphql-codegen/plugin-helpers';
6
6
  import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
7
7
  import { TypeScriptLoader } from 'cosmiconfig-typescript-loader';
8
8
  import { print } from 'graphql';
9
+ import { execSync } from 'child_process';
9
10
  import { env } from 'string-env-interpolation';
10
11
  import yaml from 'yaml';
11
12
  import yargs from 'yargs';
12
13
  import { findAndLoadGraphQLConfig } from './graphql-config.js';
13
14
  import { defaultDocumentsLoadOptions, defaultSchemaLoadOptions, loadDocuments, loadSchema } from './load.js';
14
- const { lstat } = promises;
15
+ import { getTempDir } from './utils/file-system.js';
16
+ const { lstat, rm } = promises;
15
17
  export function generateSearchPlaces(moduleName) {
16
18
  const extensions = ['json', 'yaml', 'yml', 'js', 'ts', 'config.js'];
17
19
  // gives codegen.json...
@@ -21,7 +23,7 @@ export function generateSearchPlaces(moduleName) {
21
23
  return [...regular.concat(dot), 'package.json'];
22
24
  }
23
25
  function customLoader(ext) {
24
- function loader(filepath, content) {
26
+ return async function loader(filepath, content) {
25
27
  if (typeof process !== 'undefined' && 'env' in process) {
26
28
  content = env(content);
27
29
  }
@@ -42,12 +44,50 @@ function customLoader(ext) {
42
44
  return defaultLoaders['.js'](filepath, content);
43
45
  }
44
46
  if (ext === 'ts') {
45
- // #8437: conflict with `graphql-config` also using TypeScriptLoader(), causing a double `ts-node` register.
46
- const tsLoader = TypeScriptLoader({ transpileOnly: true });
47
- return tsLoader(filepath, content);
47
+ try {
48
+ // #8437: conflict with `graphql-config` also using TypeScriptLoader(), causing a double `ts-node` register.
49
+ const tsLoader = TypeScriptLoader({ transpileOnly: true });
50
+ return tsLoader(filepath, content);
51
+ }
52
+ catch (err) {
53
+ if (typeof err.stack === 'string' && err.stack.startsWith('Error [ERR_REQUIRE_ESM]:')) {
54
+ const hash = hashContent(content, 'base64url');
55
+ const tempDir = getTempDir();
56
+ // TODO
57
+ let inTempDir = [];
58
+ try {
59
+ inTempDir = await promises.readdir(tempDir);
60
+ }
61
+ catch (err) { }
62
+ let outDir = join(tempDir, new Date().getTime() + '-' + hash);
63
+ const previousOutDir = inTempDir.find(s => s.endsWith(hash));
64
+ if (previousOutDir) {
65
+ outDir = join(tempDir, previousOutDir);
66
+ }
67
+ else {
68
+ // We're compiling the file, because ts-node doesn't work perfectly with ESM.
69
+ execSync(`tsc ${filepath} --module commonjs --outDir ${outDir}`, { stdio: 'pipe' });
70
+ }
71
+ const newPath = join(outDir, basename(filepath).replace(/\.ts$/, '.js'));
72
+ const config = import(newPath).then(m => {
73
+ const config = m.default;
74
+ return 'default' in config ? config.default : config;
75
+ });
76
+ // If the cache has more than 10 files, we delete the oldest one.
77
+ if (inTempDir.length > 10) {
78
+ const oldest = inTempDir.sort((a, b) => {
79
+ const aTime = Number(a.split('-')[0]);
80
+ const bTime = Number(b.split('-')[0]);
81
+ return aTime - bTime;
82
+ })[0];
83
+ await rm(join(tempDir, oldest), { recursive: true, force: true });
84
+ }
85
+ return config;
86
+ }
87
+ throw err;
88
+ }
48
89
  }
49
- }
50
- return loader;
90
+ };
51
91
  }
52
92
  export async function loadCodegenConfig({ configFilePath, moduleName, searchPlaces: additionalSearchPlaces, packageProp, loaders: customLoaders, }) {
53
93
  configFilePath || (configFilePath = process.cwd());
@@ -70,7 +110,18 @@ export async function loadCodegenConfig({ configFilePath, moduleName, searchPlac
70
110
  return pathStats.isDirectory() ? cosmi.search(configFilePath) : cosmi.load(configFilePath);
71
111
  }
72
112
  export async function loadContext(configFilePath) {
73
- const graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);
113
+ let graphqlConfig;
114
+ try {
115
+ graphqlConfig = await findAndLoadGraphQLConfig(configFilePath);
116
+ }
117
+ catch (err) {
118
+ if (typeof err.stack === 'string' && err.stack.startsWith('Error [ERR_REQUIRE_ESM]:')) {
119
+ // TODO: This needs a fix in graphql-config
120
+ }
121
+ else {
122
+ throw err;
123
+ }
124
+ }
74
125
  if (graphqlConfig) {
75
126
  return new CodegenContext({ graphqlConfig });
76
127
  }
@@ -299,8 +350,8 @@ export class CodegenContext {
299
350
  export function ensureContext(input) {
300
351
  return input instanceof CodegenContext ? input : new CodegenContext({ config: input });
301
352
  }
302
- function hashContent(content) {
303
- return createHash('sha256').update(content).digest('hex');
353
+ function hashContent(content, encoding = 'hex') {
354
+ return createHash('sha256').update(content).digest(encoding);
304
355
  }
305
356
  function hashSchema(schema) {
306
357
  return hashContent(print(getCachedDocumentNodeFromSchema(schema)));
@@ -1,4 +1,6 @@
1
1
  import { promises, unlink as fsUnlink } from 'fs';
2
+ import { tmpdir } from 'os';
3
+ import { join } from 'path';
2
4
  const { writeFile: fsWriteFile, readFile: fsReadFile, mkdir } = promises;
3
5
  export function writeFile(filepath, content) {
4
6
  return fsWriteFile(filepath, content);
@@ -12,3 +14,6 @@ export function unlinkFile(filePath, cb) {
12
14
  export function mkdirp(filePath) {
13
15
  return mkdir(filePath, { recursive: true });
14
16
  }
17
+ export function getTempDir() {
18
+ return join(tmpdir(), `graphql-codegen-cli`);
19
+ }
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "3.2.1",
3
+ "version": "3.2.2-alpha-20230301201322-189cfbbc9",
4
4
  "peerDependencies": {
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 || ^16.0.0"
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 || ^16.0.0",
6
+ "typescript": ">=3.0"
6
7
  },
7
8
  "dependencies": {
8
9
  "@babel/generator": "^7.18.13",
@@ -18,6 +18,7 @@ export type YamlCliFlags = {
18
18
  emitLegacyCommonJSImports?: boolean;
19
19
  };
20
20
  export declare function generateSearchPlaces(moduleName: string): string[];
21
+ export type CodegenConfigLoader = (filepath: string, content: string) => Promise<Types.Config> | Types.Config;
21
22
  export interface LoadCodegenConfigOptions {
22
23
  /**
23
24
  * The path to the config file or directory contains the config file.
@@ -40,7 +41,7 @@ export interface LoadCodegenConfigOptions {
40
41
  /**
41
42
  * Overrides or extends the loaders for specific file extensions
42
43
  */
43
- loaders?: Record<string, (filepath: string, content: string) => Promise<Types.Config> | Types.Config>;
44
+ loaders?: Record<string, CodegenConfigLoader>;
44
45
  }
45
46
  export interface LoadCodegenConfigResult {
46
47
  filepath: string;
@@ -18,6 +18,7 @@ export type YamlCliFlags = {
18
18
  emitLegacyCommonJSImports?: boolean;
19
19
  };
20
20
  export declare function generateSearchPlaces(moduleName: string): string[];
21
+ export type CodegenConfigLoader = (filepath: string, content: string) => Promise<Types.Config> | Types.Config;
21
22
  export interface LoadCodegenConfigOptions {
22
23
  /**
23
24
  * The path to the config file or directory contains the config file.
@@ -40,7 +41,7 @@ export interface LoadCodegenConfigOptions {
40
41
  /**
41
42
  * Overrides or extends the loaders for specific file extensions
42
43
  */
43
- loaders?: Record<string, (filepath: string, content: string) => Promise<Types.Config> | Types.Config>;
44
+ loaders?: Record<string, CodegenConfigLoader>;
44
45
  }
45
46
  export interface LoadCodegenConfigResult {
46
47
  filepath: string;
@@ -2,3 +2,4 @@ export declare function writeFile(filepath: string, content: string): Promise<vo
2
2
  export declare function readFile(filepath: string): Promise<string>;
3
3
  export declare function unlinkFile(filePath: string, cb?: (err?: Error) => any): void;
4
4
  export declare function mkdirp(filePath: string): Promise<string>;
5
+ export declare function getTempDir(): string;
@@ -2,3 +2,4 @@ export declare function writeFile(filepath: string, content: string): Promise<vo
2
2
  export declare function readFile(filepath: string): Promise<string>;
3
3
  export declare function unlinkFile(filePath: string, cb?: (err?: Error) => any): void;
4
4
  export declare function mkdirp(filePath: string): Promise<string>;
5
+ export declare function getTempDir(): string;