@graphql-codegen/cli 2.6.0-alpha-bbecef04a.0 → 2.6.0-alpha-64b0da579.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
@@ -53,6 +53,7 @@ const yaml = _interopDefault(require('yaml'));
53
53
  const module$1 = require('module');
54
54
  const fs = require('fs');
55
55
  const fs__default = _interopDefault(fs);
56
+ const os = require('os');
56
57
  const Listr = _interopDefault(require('listr'));
57
58
  const isGlob = _interopDefault(require('is-glob'));
58
59
  const debounce = _interopDefault(require('debounce'));
@@ -505,30 +506,20 @@ async function loadSchema(schemaPointers, config) {
505
506
 
506
507
  ${e.message || e}
507
508
  ${e.stack || ''}
508
-
509
+
509
510
  GraphQL Code Generator supports:
510
511
  - ES Modules and CommonJS exports (export as default or named export "schema")
511
512
  - Introspection JSON File
512
513
  - URL of GraphQL endpoint
513
514
  - Multiple files with type definitions (glob expression)
514
515
  - String in config file
515
-
516
+
516
517
  Try to use one of above options and run codegen again.
517
-
518
+
518
519
  `);
519
520
  }
520
521
  }
521
- const requiredLoader = async (pointers, schema) => {
522
- return Promise.all(pointers.map(async (pointer) => {
523
- const userLoader = (await makeDefaultLoader(process.cwd())(pointer.require)).default;
524
- const documents = userLoader(pointer.config || {}, schema);
525
- return documents;
526
- })).then(results => [].concat(...results));
527
- };
528
- const isRequirePointer = (pointer) => {
529
- return typeof pointer === 'object' && 'require' in pointer;
530
- };
531
- async function loadDocuments(documentPointers, config, schema) {
522
+ async function loadDocuments(documentPointers, config) {
532
523
  const loaders = [
533
524
  new codeFileLoader.CodeFileLoader({
534
525
  pluckConfig: {
@@ -547,35 +538,14 @@ async function loadDocuments(documentPointers, config, schema) {
547
538
  }
548
539
  ignore.push(path.join(process.cwd(), generatePath));
549
540
  }
550
- const loaderPointers = [];
551
- const requestPointers = [];
552
- if (Array.isArray(documentPointers)) {
553
- documentPointers.forEach(pointer => {
554
- if (isRequirePointer(pointer)) {
555
- requestPointers.push(pointer);
556
- }
557
- else {
558
- loaderPointers.push(pointer);
559
- }
560
- });
561
- }
562
- else {
563
- if (isRequirePointer(documentPointers)) {
564
- requestPointers.push(documentPointers);
565
- }
566
- else {
567
- loaderPointers.push(documentPointers);
568
- }
569
- }
570
- const loadedFromToolkit = await load.loadDocuments(loaderPointers, {
541
+ const loadedFromToolkit = await load.loadDocuments(documentPointers, {
571
542
  ...defaultDocumentsLoadOptions,
572
543
  ignore,
573
544
  loaders,
574
545
  ...config,
575
546
  ...config.config,
576
547
  });
577
- const loadedAlterantively = await requiredLoader(requestPointers, schema);
578
- return [...loadedFromToolkit, ...loadedAlterantively];
548
+ return loadedFromToolkit;
579
549
  }
580
550
 
581
551
  const { lstat } = fs.promises;
@@ -813,14 +783,14 @@ class CodegenContext {
813
783
  }
814
784
  return loadSchema(pointer, config);
815
785
  }
816
- async loadDocuments(input) {
786
+ async loadDocuments(pointer) {
817
787
  const config = this.getConfig(defaultDocumentsLoadOptions);
818
788
  if (this._graphqlConfig) {
819
789
  // TODO: pointer won't work here
820
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(input.pointer, config);
790
+ const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
821
791
  return documents;
822
792
  }
823
- return loadDocuments(input.pointer, config, input.schema);
793
+ return loadDocuments(pointer, config);
824
794
  }
825
795
  }
826
796
  function ensureContext(input) {
@@ -924,7 +894,7 @@ async function executeCodegen(input) {
924
894
  const documentsLoadingCache = createCache(async function (hash) {
925
895
  const documents = await context.loadDocuments(JSON.parse(hash));
926
896
  return {
927
- documents,
897
+ documents: documents,
928
898
  };
929
899
  });
930
900
  function wrapTask(task, source, taskName) {
@@ -1055,7 +1025,7 @@ async function executeCodegen(input) {
1055
1025
  debugLog(`[CLI] Loading Documents`);
1056
1026
  // get different cache for shared docs and output specific docs
1057
1027
  const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
1058
- const hash = JSON.stringify({ pointer: docs, schema: outputSchema });
1028
+ const hash = JSON.stringify(docs);
1059
1029
  return documentsLoadingCache.load(hash);
1060
1030
  }));
1061
1031
  const documents = [];
@@ -1139,8 +1109,7 @@ async function executeCodegen(input) {
1139
1109
  }), {
1140
1110
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1141
1111
  exitOnError: false,
1142
- // run 4 at once
1143
- concurrent: 4,
1112
+ concurrent: os.cpus().length,
1144
1113
  });
1145
1114
  },
1146
1115
  });
@@ -1287,15 +1256,16 @@ const createWatcher = (initalContext, onNext) => {
1287
1256
  });
1288
1257
  };
1289
1258
 
1290
- function writeSync(filepath, content) {
1291
- return fs.writeFileSync(filepath, content);
1259
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = fs.promises;
1260
+ function writeFile(filepath, content) {
1261
+ return fsWriteFile(filepath, content);
1292
1262
  }
1293
- function readSync(filepath) {
1294
- return fs.readFileSync(filepath, 'utf-8');
1263
+ function readFile(filepath) {
1264
+ return fsReadFile(filepath, 'utf-8');
1295
1265
  }
1296
- function fileExists(filePath) {
1266
+ async function fileExists(filePath) {
1297
1267
  try {
1298
- return fs.statSync(filePath).isFile();
1268
+ return (await fsStat(filePath)).isFile();
1299
1269
  }
1300
1270
  catch (err) {
1301
1271
  return false;
@@ -1317,7 +1287,7 @@ async function generate(input, saveToFile = true) {
1317
1287
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1318
1288
  staleFilenames.forEach(filename => {
1319
1289
  if (shouldOverwrite(config, filename)) {
1320
- unlinkFile(filename, err => {
1290
+ return unlinkFile(filename, err => {
1321
1291
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1322
1292
  if (err) {
1323
1293
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1338,7 +1308,39 @@ async function generate(input, saveToFile = true) {
1338
1308
  if (config.watch) {
1339
1309
  removeStaleFiles(config, generationResult);
1340
1310
  }
1341
- await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
1311
+ await context.profiler.run(async () => {
1312
+ await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
1313
+ await Promise.all(generationResult.map(async (result) => {
1314
+ const exists = await fileExists(result.filename);
1315
+ if (!shouldOverwrite(config, result.filename) && exists) {
1316
+ return;
1317
+ }
1318
+ const content = result.content || '';
1319
+ const currentHash = hash(content);
1320
+ let previousHash = recentOutputHash.get(result.filename);
1321
+ if (!previousHash && exists) {
1322
+ previousHash = hash(await readFile(result.filename));
1323
+ }
1324
+ if (previousHash && currentHash === previousHash) {
1325
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
1326
+ return;
1327
+ }
1328
+ if (content.length === 0) {
1329
+ return;
1330
+ }
1331
+ recentOutputHash.set(result.filename, currentHash);
1332
+ const basedir = path.dirname(result.filename);
1333
+ await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
1334
+ await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
1335
+ await mkdirp(basedir);
1336
+ const absolutePath = path.isAbsolute(result.filename)
1337
+ ? result.filename
1338
+ : path.join(input.cwd || process.cwd(), result.filename);
1339
+ await writeFile(absolutePath, result.content);
1340
+ await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1341
+ await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1342
+ }));
1343
+ }, 'Lifecycle: beforeAllFileWrite');
1342
1344
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1343
1345
  const exists = fileExists(result.filename);
1344
1346
  if (!shouldOverwrite(config, result.filename) && exists) {
@@ -1348,7 +1350,7 @@ async function generate(input, saveToFile = true) {
1348
1350
  const currentHash = hash(content);
1349
1351
  let previousHash = recentOutputHash.get(result.filename);
1350
1352
  if (!previousHash && exists) {
1351
- previousHash = hash(readSync(result.filename));
1353
+ previousHash = hash(await readFile(result.filename));
1352
1354
  }
1353
1355
  if (previousHash && currentHash === previousHash) {
1354
1356
  debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
@@ -1365,7 +1367,7 @@ async function generate(input, saveToFile = true) {
1365
1367
  const absolutePath = path.isAbsolute(result.filename)
1366
1368
  ? result.filename
1367
1369
  : path.join(input.cwd || process.cwd(), result.filename);
1368
- writeSync(absolutePath, result.content);
1370
+ await writeFile(absolutePath, result.content);
1369
1371
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1370
1372
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1371
1373
  })), 'Write files');
@@ -1380,7 +1382,7 @@ async function generate(input, saveToFile = true) {
1380
1382
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1381
1383
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1382
1384
  if (context.profilerOutput) {
1383
- writeSync(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1385
+ await writeFile(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1384
1386
  }
1385
1387
  return outputFiles;
1386
1388
  }
package/codegen.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import { Types } from '@graphql-codegen/plugin-helpers';
2
2
  import { CodegenContext } from './config';
3
- export declare const makeDefaultLoader: (from: string) => (mod: string) => Promise<any>;
4
3
  export declare function executeCodegen(input: CodegenContext | Types.Config): Promise<Types.FileOutput[]>;
package/config.d.ts CHANGED
@@ -111,9 +111,6 @@ export declare class CodegenContext {
111
111
  [key: string]: any;
112
112
  };
113
113
  loadSchema(pointer: Types.Schema): Promise<GraphQLSchema>;
114
- loadDocuments(input: {
115
- pointer: Types.OperationDocument[];
116
- schema: GraphQLSchema;
117
- }): Promise<Types.DocumentFile[]>;
114
+ loadDocuments(pointer: Types.OperationDocument[]): Promise<Types.DocumentFile[]>;
118
115
  }
119
116
  export declare function ensureContext(input: CodegenContext | Types.Config): CodegenContext;
package/index.js CHANGED
@@ -53,6 +53,7 @@ const yaml = _interopDefault(require('yaml'));
53
53
  const module$1 = require('module');
54
54
  const fs = require('fs');
55
55
  const fs__default = _interopDefault(fs);
56
+ const os = require('os');
56
57
  const Listr = _interopDefault(require('listr'));
57
58
  const child_process = require('child_process');
58
59
  const isGlob = _interopDefault(require('is-glob'));
@@ -423,30 +424,20 @@ async function loadSchema(schemaPointers, config) {
423
424
 
424
425
  ${e.message || e}
425
426
  ${e.stack || ''}
426
-
427
+
427
428
  GraphQL Code Generator supports:
428
429
  - ES Modules and CommonJS exports (export as default or named export "schema")
429
430
  - Introspection JSON File
430
431
  - URL of GraphQL endpoint
431
432
  - Multiple files with type definitions (glob expression)
432
433
  - String in config file
433
-
434
+
434
435
  Try to use one of above options and run codegen again.
435
-
436
+
436
437
  `);
437
438
  }
438
439
  }
439
- const requiredLoader = async (pointers, schema) => {
440
- return Promise.all(pointers.map(async (pointer) => {
441
- const userLoader = (await makeDefaultLoader(process.cwd())(pointer.require)).default;
442
- const documents = userLoader(pointer.config || {}, schema);
443
- return documents;
444
- })).then(results => [].concat(...results));
445
- };
446
- const isRequirePointer = (pointer) => {
447
- return typeof pointer === 'object' && 'require' in pointer;
448
- };
449
- async function loadDocuments(documentPointers, config, schema) {
440
+ async function loadDocuments(documentPointers, config) {
450
441
  const loaders = [
451
442
  new codeFileLoader.CodeFileLoader({
452
443
  pluckConfig: {
@@ -465,35 +456,14 @@ async function loadDocuments(documentPointers, config, schema) {
465
456
  }
466
457
  ignore.push(path.join(process.cwd(), generatePath));
467
458
  }
468
- const loaderPointers = [];
469
- const requestPointers = [];
470
- if (Array.isArray(documentPointers)) {
471
- documentPointers.forEach(pointer => {
472
- if (isRequirePointer(pointer)) {
473
- requestPointers.push(pointer);
474
- }
475
- else {
476
- loaderPointers.push(pointer);
477
- }
478
- });
479
- }
480
- else {
481
- if (isRequirePointer(documentPointers)) {
482
- requestPointers.push(documentPointers);
483
- }
484
- else {
485
- loaderPointers.push(documentPointers);
486
- }
487
- }
488
- const loadedFromToolkit = await load.loadDocuments(loaderPointers, {
459
+ const loadedFromToolkit = await load.loadDocuments(documentPointers, {
489
460
  ...defaultDocumentsLoadOptions,
490
461
  ignore,
491
462
  loaders,
492
463
  ...config,
493
464
  ...config.config,
494
465
  });
495
- const loadedAlterantively = await requiredLoader(requestPointers, schema);
496
- return [...loadedFromToolkit, ...loadedAlterantively];
466
+ return loadedFromToolkit;
497
467
  }
498
468
 
499
469
  const { lstat } = fs.promises;
@@ -731,14 +701,14 @@ class CodegenContext {
731
701
  }
732
702
  return loadSchema(pointer, config);
733
703
  }
734
- async loadDocuments(input) {
704
+ async loadDocuments(pointer) {
735
705
  const config = this.getConfig(defaultDocumentsLoadOptions);
736
706
  if (this._graphqlConfig) {
737
707
  // TODO: pointer won't work here
738
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(input.pointer, config);
708
+ const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
739
709
  return documents;
740
710
  }
741
- return loadDocuments(input.pointer, config, input.schema);
711
+ return loadDocuments(pointer, config);
742
712
  }
743
713
  }
744
714
  function ensureContext(input) {
@@ -842,7 +812,7 @@ async function executeCodegen(input) {
842
812
  const documentsLoadingCache = createCache(async function (hash) {
843
813
  const documents = await context.loadDocuments(JSON.parse(hash));
844
814
  return {
845
- documents,
815
+ documents: documents,
846
816
  };
847
817
  });
848
818
  function wrapTask(task, source, taskName) {
@@ -973,7 +943,7 @@ async function executeCodegen(input) {
973
943
  debugLog(`[CLI] Loading Documents`);
974
944
  // get different cache for shared docs and output specific docs
975
945
  const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
976
- const hash = JSON.stringify({ pointer: docs, schema: outputSchema });
946
+ const hash = JSON.stringify(docs);
977
947
  return documentsLoadingCache.load(hash);
978
948
  }));
979
949
  const documents = [];
@@ -1057,8 +1027,7 @@ async function executeCodegen(input) {
1057
1027
  }), {
1058
1028
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1059
1029
  exitOnError: false,
1060
- // run 4 at once
1061
- concurrent: 4,
1030
+ concurrent: os.cpus().length,
1062
1031
  });
1063
1032
  },
1064
1033
  });
@@ -1288,15 +1257,16 @@ const createWatcher = (initalContext, onNext) => {
1288
1257
  });
1289
1258
  };
1290
1259
 
1291
- function writeSync(filepath, content) {
1292
- return fs.writeFileSync(filepath, content);
1260
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = fs.promises;
1261
+ function writeFile(filepath, content) {
1262
+ return fsWriteFile(filepath, content);
1293
1263
  }
1294
- function readSync(filepath) {
1295
- return fs.readFileSync(filepath, 'utf-8');
1264
+ function readFile(filepath) {
1265
+ return fsReadFile(filepath, 'utf-8');
1296
1266
  }
1297
- function fileExists(filePath) {
1267
+ async function fileExists(filePath) {
1298
1268
  try {
1299
- return fs.statSync(filePath).isFile();
1269
+ return (await fsStat(filePath)).isFile();
1300
1270
  }
1301
1271
  catch (err) {
1302
1272
  return false;
@@ -1318,7 +1288,7 @@ async function generate(input, saveToFile = true) {
1318
1288
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1319
1289
  staleFilenames.forEach(filename => {
1320
1290
  if (shouldOverwrite(config, filename)) {
1321
- unlinkFile(filename, err => {
1291
+ return unlinkFile(filename, err => {
1322
1292
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1323
1293
  if (err) {
1324
1294
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1339,7 +1309,39 @@ async function generate(input, saveToFile = true) {
1339
1309
  if (config.watch) {
1340
1310
  removeStaleFiles(config, generationResult);
1341
1311
  }
1342
- await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
1312
+ await context.profiler.run(async () => {
1313
+ await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
1314
+ await Promise.all(generationResult.map(async (result) => {
1315
+ const exists = await fileExists(result.filename);
1316
+ if (!shouldOverwrite(config, result.filename) && exists) {
1317
+ return;
1318
+ }
1319
+ const content = result.content || '';
1320
+ const currentHash = hash(content);
1321
+ let previousHash = recentOutputHash.get(result.filename);
1322
+ if (!previousHash && exists) {
1323
+ previousHash = hash(await readFile(result.filename));
1324
+ }
1325
+ if (previousHash && currentHash === previousHash) {
1326
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
1327
+ return;
1328
+ }
1329
+ if (content.length === 0) {
1330
+ return;
1331
+ }
1332
+ recentOutputHash.set(result.filename, currentHash);
1333
+ const basedir = path.dirname(result.filename);
1334
+ await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
1335
+ await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
1336
+ await mkdirp(basedir);
1337
+ const absolutePath = path.isAbsolute(result.filename)
1338
+ ? result.filename
1339
+ : path.join(input.cwd || process.cwd(), result.filename);
1340
+ await writeFile(absolutePath, result.content);
1341
+ await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1342
+ await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1343
+ }));
1344
+ }, 'Lifecycle: beforeAllFileWrite');
1343
1345
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1344
1346
  const exists = fileExists(result.filename);
1345
1347
  if (!shouldOverwrite(config, result.filename) && exists) {
@@ -1349,7 +1351,7 @@ async function generate(input, saveToFile = true) {
1349
1351
  const currentHash = hash(content);
1350
1352
  let previousHash = recentOutputHash.get(result.filename);
1351
1353
  if (!previousHash && exists) {
1352
- previousHash = hash(readSync(result.filename));
1354
+ previousHash = hash(await readFile(result.filename));
1353
1355
  }
1354
1356
  if (previousHash && currentHash === previousHash) {
1355
1357
  debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
@@ -1366,7 +1368,7 @@ async function generate(input, saveToFile = true) {
1366
1368
  const absolutePath = path.isAbsolute(result.filename)
1367
1369
  ? result.filename
1368
1370
  : path.join(input.cwd || process.cwd(), result.filename);
1369
- writeSync(absolutePath, result.content);
1371
+ await writeFile(absolutePath, result.content);
1370
1372
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1371
1373
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1372
1374
  })), 'Write files');
@@ -1381,7 +1383,7 @@ async function generate(input, saveToFile = true) {
1381
1383
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1382
1384
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1383
1385
  if (context.profilerOutput) {
1384
- writeSync(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1386
+ await writeFile(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1385
1387
  }
1386
1388
  return outputFiles;
1387
1389
  }
package/index.mjs CHANGED
@@ -25,7 +25,8 @@ import { JsonFileLoader } from '@graphql-tools/json-file-loader';
25
25
  import { UrlLoader } from '@graphql-tools/url-loader';
26
26
  import yaml from 'yaml';
27
27
  import { createRequire } from 'module';
28
- import fs, { promises, writeFileSync, readFileSync, statSync, unlink } from 'fs';
28
+ import fs, { promises, unlink, writeFileSync, readFileSync } from 'fs';
29
+ import { cpus } from 'os';
29
30
  import Listr from 'listr';
30
31
  import { exec } from 'child_process';
31
32
  import isGlob from 'is-glob';
@@ -396,30 +397,20 @@ async function loadSchema(schemaPointers, config) {
396
397
 
397
398
  ${e.message || e}
398
399
  ${e.stack || ''}
399
-
400
+
400
401
  GraphQL Code Generator supports:
401
402
  - ES Modules and CommonJS exports (export as default or named export "schema")
402
403
  - Introspection JSON File
403
404
  - URL of GraphQL endpoint
404
405
  - Multiple files with type definitions (glob expression)
405
406
  - String in config file
406
-
407
+
407
408
  Try to use one of above options and run codegen again.
408
-
409
+
409
410
  `);
410
411
  }
411
412
  }
412
- const requiredLoader = async (pointers, schema) => {
413
- return Promise.all(pointers.map(async (pointer) => {
414
- const userLoader = (await makeDefaultLoader(process.cwd())(pointer.require)).default;
415
- const documents = userLoader(pointer.config || {}, schema);
416
- return documents;
417
- })).then(results => [].concat(...results));
418
- };
419
- const isRequirePointer = (pointer) => {
420
- return typeof pointer === 'object' && 'require' in pointer;
421
- };
422
- async function loadDocuments(documentPointers, config, schema) {
413
+ async function loadDocuments(documentPointers, config) {
423
414
  const loaders = [
424
415
  new CodeFileLoader({
425
416
  pluckConfig: {
@@ -438,35 +429,14 @@ async function loadDocuments(documentPointers, config, schema) {
438
429
  }
439
430
  ignore.push(join(process.cwd(), generatePath));
440
431
  }
441
- const loaderPointers = [];
442
- const requestPointers = [];
443
- if (Array.isArray(documentPointers)) {
444
- documentPointers.forEach(pointer => {
445
- if (isRequirePointer(pointer)) {
446
- requestPointers.push(pointer);
447
- }
448
- else {
449
- loaderPointers.push(pointer);
450
- }
451
- });
452
- }
453
- else {
454
- if (isRequirePointer(documentPointers)) {
455
- requestPointers.push(documentPointers);
456
- }
457
- else {
458
- loaderPointers.push(documentPointers);
459
- }
460
- }
461
- const loadedFromToolkit = await loadDocuments$1(loaderPointers, {
432
+ const loadedFromToolkit = await loadDocuments$1(documentPointers, {
462
433
  ...defaultDocumentsLoadOptions,
463
434
  ignore,
464
435
  loaders,
465
436
  ...config,
466
437
  ...config.config,
467
438
  });
468
- const loadedAlterantively = await requiredLoader(requestPointers, schema);
469
- return [...loadedFromToolkit, ...loadedAlterantively];
439
+ return loadedFromToolkit;
470
440
  }
471
441
 
472
442
  const { lstat } = promises;
@@ -704,14 +674,14 @@ class CodegenContext {
704
674
  }
705
675
  return loadSchema(pointer, config);
706
676
  }
707
- async loadDocuments(input) {
677
+ async loadDocuments(pointer) {
708
678
  const config = this.getConfig(defaultDocumentsLoadOptions);
709
679
  if (this._graphqlConfig) {
710
680
  // TODO: pointer won't work here
711
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(input.pointer, config);
681
+ const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
712
682
  return documents;
713
683
  }
714
- return loadDocuments(input.pointer, config, input.schema);
684
+ return loadDocuments(pointer, config);
715
685
  }
716
686
  }
717
687
  function ensureContext(input) {
@@ -815,7 +785,7 @@ async function executeCodegen(input) {
815
785
  const documentsLoadingCache = createCache(async function (hash) {
816
786
  const documents = await context.loadDocuments(JSON.parse(hash));
817
787
  return {
818
- documents,
788
+ documents: documents,
819
789
  };
820
790
  });
821
791
  function wrapTask(task, source, taskName) {
@@ -946,7 +916,7 @@ async function executeCodegen(input) {
946
916
  debugLog(`[CLI] Loading Documents`);
947
917
  // get different cache for shared docs and output specific docs
948
918
  const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
949
- const hash = JSON.stringify({ pointer: docs, schema: outputSchema });
919
+ const hash = JSON.stringify(docs);
950
920
  return documentsLoadingCache.load(hash);
951
921
  }));
952
922
  const documents = [];
@@ -1030,8 +1000,7 @@ async function executeCodegen(input) {
1030
1000
  }), {
1031
1001
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1032
1002
  exitOnError: false,
1033
- // run 4 at once
1034
- concurrent: 4,
1003
+ concurrent: cpus().length,
1035
1004
  });
1036
1005
  },
1037
1006
  });
@@ -1261,15 +1230,16 @@ const createWatcher = (initalContext, onNext) => {
1261
1230
  });
1262
1231
  };
1263
1232
 
1264
- function writeSync(filepath, content) {
1265
- return writeFileSync(filepath, content);
1233
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = promises;
1234
+ function writeFile(filepath, content) {
1235
+ return fsWriteFile(filepath, content);
1266
1236
  }
1267
- function readSync(filepath) {
1268
- return readFileSync(filepath, 'utf-8');
1237
+ function readFile(filepath) {
1238
+ return fsReadFile(filepath, 'utf-8');
1269
1239
  }
1270
- function fileExists(filePath) {
1240
+ async function fileExists(filePath) {
1271
1241
  try {
1272
- return statSync(filePath).isFile();
1242
+ return (await fsStat(filePath)).isFile();
1273
1243
  }
1274
1244
  catch (err) {
1275
1245
  return false;
@@ -1291,7 +1261,7 @@ async function generate(input, saveToFile = true) {
1291
1261
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1292
1262
  staleFilenames.forEach(filename => {
1293
1263
  if (shouldOverwrite(config, filename)) {
1294
- unlinkFile(filename, err => {
1264
+ return unlinkFile(filename, err => {
1295
1265
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1296
1266
  if (err) {
1297
1267
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1312,7 +1282,39 @@ async function generate(input, saveToFile = true) {
1312
1282
  if (config.watch) {
1313
1283
  removeStaleFiles(config, generationResult);
1314
1284
  }
1315
- await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
1285
+ await context.profiler.run(async () => {
1286
+ await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
1287
+ await Promise.all(generationResult.map(async (result) => {
1288
+ const exists = await fileExists(result.filename);
1289
+ if (!shouldOverwrite(config, result.filename) && exists) {
1290
+ return;
1291
+ }
1292
+ const content = result.content || '';
1293
+ const currentHash = hash(content);
1294
+ let previousHash = recentOutputHash.get(result.filename);
1295
+ if (!previousHash && exists) {
1296
+ previousHash = hash(await readFile(result.filename));
1297
+ }
1298
+ if (previousHash && currentHash === previousHash) {
1299
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
1300
+ return;
1301
+ }
1302
+ if (content.length === 0) {
1303
+ return;
1304
+ }
1305
+ recentOutputHash.set(result.filename, currentHash);
1306
+ const basedir = dirname(result.filename);
1307
+ await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
1308
+ await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
1309
+ await mkdirp(basedir);
1310
+ const absolutePath = isAbsolute(result.filename)
1311
+ ? result.filename
1312
+ : join(input.cwd || process.cwd(), result.filename);
1313
+ await writeFile(absolutePath, result.content);
1314
+ await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1315
+ await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1316
+ }));
1317
+ }, 'Lifecycle: beforeAllFileWrite');
1316
1318
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1317
1319
  const exists = fileExists(result.filename);
1318
1320
  if (!shouldOverwrite(config, result.filename) && exists) {
@@ -1322,7 +1324,7 @@ async function generate(input, saveToFile = true) {
1322
1324
  const currentHash = hash(content);
1323
1325
  let previousHash = recentOutputHash.get(result.filename);
1324
1326
  if (!previousHash && exists) {
1325
- previousHash = hash(readSync(result.filename));
1327
+ previousHash = hash(await readFile(result.filename));
1326
1328
  }
1327
1329
  if (previousHash && currentHash === previousHash) {
1328
1330
  debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
@@ -1339,7 +1341,7 @@ async function generate(input, saveToFile = true) {
1339
1341
  const absolutePath = isAbsolute(result.filename)
1340
1342
  ? result.filename
1341
1343
  : join(input.cwd || process.cwd(), result.filename);
1342
- writeSync(absolutePath, result.content);
1344
+ await writeFile(absolutePath, result.content);
1343
1345
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1344
1346
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1345
1347
  })), 'Write files');
@@ -1354,7 +1356,7 @@ async function generate(input, saveToFile = true) {
1354
1356
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1355
1357
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1356
1358
  if (context.profilerOutput) {
1357
- writeSync(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1359
+ await writeFile(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1358
1360
  }
1359
1361
  return outputFiles;
1360
1362
  }
package/load.d.ts CHANGED
@@ -12,4 +12,4 @@ export declare const defaultDocumentsLoadOptions: {
12
12
  skipGraphQLImport: boolean;
13
13
  };
14
14
  export declare function loadSchema(schemaPointers: UnnormalizedTypeDefPointer, config: Types.Config): Promise<GraphQLSchema>;
15
- export declare function loadDocuments(documentPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], config: Types.Config, schema: GraphQLSchema): Promise<Types.DocumentFile[]>;
15
+ export declare function loadDocuments(documentPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], config: Types.Config): Promise<Types.DocumentFile[]>;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.6.0-alpha-bbecef04a.0",
3
+ "version": "2.6.0-alpha-64b0da579.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 || ^16.0.0"
6
6
  },
7
7
  "dependencies": {
8
- "@graphql-codegen/core": "2.5.1-alpha-bbecef04a.0",
9
- "@graphql-codegen/plugin-helpers": "2.4.1-alpha-bbecef04a.0",
8
+ "@graphql-codegen/core": "2.5.0",
9
+ "@graphql-codegen/plugin-helpers": "^2.4.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",
@@ -1,4 +1,4 @@
1
- export declare function writeSync(filepath: string, content: string): void;
2
- export declare function readSync(filepath: string): string;
3
- export declare function fileExists(filePath: string): boolean;
1
+ export declare function writeFile(filepath: string, content: string): Promise<void>;
2
+ export declare function readFile(filepath: string): Promise<string>;
3
+ export declare function fileExists(filePath: string): Promise<boolean>;
4
4
  export declare function unlinkFile(filePath: string, cb?: (err?: Error) => any): void;