@graphql-codegen/cli 2.6.0-alpha-a52c122aa.0 → 2.6.0-alpha-cb6ec4365.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
@@ -54,6 +54,7 @@ const module$1 = require('module');
54
54
  const fs = require('fs');
55
55
  const fs__default = _interopDefault(fs);
56
56
  const crypto = require('crypto');
57
+ const os = require('os');
57
58
  const Listr = _interopDefault(require('listr'));
58
59
  const isGlob = _interopDefault(require('is-glob'));
59
60
  const debounce = _interopDefault(require('debounce'));
@@ -1098,7 +1099,6 @@ async function executeCodegen(input) {
1098
1099
  pluginMap,
1099
1100
  pluginContext,
1100
1101
  profiler: context.profiler,
1101
- cache,
1102
1102
  }), `Build Generates Section: ${filename}`);
1103
1103
  }
1104
1104
  else {
@@ -1113,12 +1113,14 @@ async function executeCodegen(input) {
1113
1113
  pluginMap,
1114
1114
  pluginContext,
1115
1115
  profiler: context.profiler,
1116
- cache,
1117
1116
  },
1118
1117
  ];
1119
1118
  }
1120
1119
  const process = async (outputArgs) => {
1121
- const output = await core.codegen(outputArgs);
1120
+ const output = await core.codegen({
1121
+ ...outputArgs,
1122
+ cache,
1123
+ });
1122
1124
  result.push({
1123
1125
  filename: outputArgs.filename,
1124
1126
  content: output,
@@ -1137,8 +1139,7 @@ async function executeCodegen(input) {
1137
1139
  }), {
1138
1140
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1139
1141
  exitOnError: false,
1140
- // run 4 at once
1141
- concurrent: 4,
1142
+ concurrent: os.cpus().length,
1142
1143
  });
1143
1144
  },
1144
1145
  });
@@ -1285,15 +1286,16 @@ const createWatcher = (initalContext, onNext) => {
1285
1286
  });
1286
1287
  };
1287
1288
 
1288
- function writeSync(filepath, content) {
1289
- return fs.writeFileSync(filepath, content);
1289
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = fs.promises;
1290
+ function writeFile(filepath, content) {
1291
+ return fsWriteFile(filepath, content);
1290
1292
  }
1291
- function readSync(filepath) {
1292
- return fs.readFileSync(filepath, 'utf-8');
1293
+ function readFile(filepath) {
1294
+ return fsReadFile(filepath, 'utf-8');
1293
1295
  }
1294
- function fileExists(filePath) {
1296
+ async function fileExists(filePath) {
1295
1297
  try {
1296
- return fs.statSync(filePath).isFile();
1298
+ return (await fsStat(filePath)).isFile();
1297
1299
  }
1298
1300
  catch (err) {
1299
1301
  return false;
@@ -1315,7 +1317,7 @@ async function generate(input, saveToFile = true) {
1315
1317
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1316
1318
  staleFilenames.forEach(filename => {
1317
1319
  if (shouldOverwrite(config, filename)) {
1318
- unlinkFile(filename, err => {
1320
+ return unlinkFile(filename, err => {
1319
1321
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1320
1322
  if (err) {
1321
1323
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1336,9 +1338,11 @@ async function generate(input, saveToFile = true) {
1336
1338
  if (config.watch) {
1337
1339
  removeStaleFiles(config, generationResult);
1338
1340
  }
1339
- await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
1341
+ await context.profiler.run(async () => {
1342
+ await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
1343
+ }, 'Lifecycle: beforeAllFileWrite');
1340
1344
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1341
- const exists = fileExists(result.filename);
1345
+ const exists = await fileExists(result.filename);
1342
1346
  if (!shouldOverwrite(config, result.filename) && exists) {
1343
1347
  return;
1344
1348
  }
@@ -1346,10 +1350,10 @@ async function generate(input, saveToFile = true) {
1346
1350
  const currentHash = hash(content);
1347
1351
  let previousHash = recentOutputHash.get(result.filename);
1348
1352
  if (!previousHash && exists) {
1349
- previousHash = hash(readSync(result.filename));
1353
+ previousHash = hash(await readFile(result.filename));
1350
1354
  }
1351
1355
  if (previousHash && currentHash === previousHash) {
1352
- debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
1356
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
1353
1357
  return;
1354
1358
  }
1355
1359
  if (content.length === 0) {
@@ -1359,11 +1363,11 @@ async function generate(input, saveToFile = true) {
1359
1363
  const basedir = path.dirname(result.filename);
1360
1364
  await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
1361
1365
  await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
1362
- mkdirp.sync(basedir);
1366
+ await mkdirp(basedir);
1363
1367
  const absolutePath = path.isAbsolute(result.filename)
1364
1368
  ? result.filename
1365
1369
  : path.join(input.cwd || process.cwd(), result.filename);
1366
- writeSync(absolutePath, result.content);
1370
+ await writeFile(absolutePath, result.content);
1367
1371
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1368
1372
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1369
1373
  })), 'Write files');
@@ -1378,7 +1382,7 @@ async function generate(input, saveToFile = true) {
1378
1382
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1379
1383
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1380
1384
  if (context.profilerOutput) {
1381
- 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()));
1382
1386
  }
1383
1387
  return outputFiles;
1384
1388
  }
package/index.js CHANGED
@@ -54,6 +54,7 @@ const module$1 = require('module');
54
54
  const fs = require('fs');
55
55
  const fs__default = _interopDefault(fs);
56
56
  const crypto = require('crypto');
57
+ const os = require('os');
57
58
  const Listr = _interopDefault(require('listr'));
58
59
  const child_process = require('child_process');
59
60
  const isGlob = _interopDefault(require('is-glob'));
@@ -1016,7 +1017,6 @@ async function executeCodegen(input) {
1016
1017
  pluginMap,
1017
1018
  pluginContext,
1018
1019
  profiler: context.profiler,
1019
- cache,
1020
1020
  }), `Build Generates Section: ${filename}`);
1021
1021
  }
1022
1022
  else {
@@ -1031,12 +1031,14 @@ async function executeCodegen(input) {
1031
1031
  pluginMap,
1032
1032
  pluginContext,
1033
1033
  profiler: context.profiler,
1034
- cache,
1035
1034
  },
1036
1035
  ];
1037
1036
  }
1038
1037
  const process = async (outputArgs) => {
1039
- const output = await core.codegen(outputArgs);
1038
+ const output = await core.codegen({
1039
+ ...outputArgs,
1040
+ cache,
1041
+ });
1040
1042
  result.push({
1041
1043
  filename: outputArgs.filename,
1042
1044
  content: output,
@@ -1055,8 +1057,7 @@ async function executeCodegen(input) {
1055
1057
  }), {
1056
1058
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1057
1059
  exitOnError: false,
1058
- // run 4 at once
1059
- concurrent: 4,
1060
+ concurrent: os.cpus().length,
1060
1061
  });
1061
1062
  },
1062
1063
  });
@@ -1286,15 +1287,16 @@ const createWatcher = (initalContext, onNext) => {
1286
1287
  });
1287
1288
  };
1288
1289
 
1289
- function writeSync(filepath, content) {
1290
- return fs.writeFileSync(filepath, content);
1290
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = fs.promises;
1291
+ function writeFile(filepath, content) {
1292
+ return fsWriteFile(filepath, content);
1291
1293
  }
1292
- function readSync(filepath) {
1293
- return fs.readFileSync(filepath, 'utf-8');
1294
+ function readFile(filepath) {
1295
+ return fsReadFile(filepath, 'utf-8');
1294
1296
  }
1295
- function fileExists(filePath) {
1297
+ async function fileExists(filePath) {
1296
1298
  try {
1297
- return fs.statSync(filePath).isFile();
1299
+ return (await fsStat(filePath)).isFile();
1298
1300
  }
1299
1301
  catch (err) {
1300
1302
  return false;
@@ -1316,7 +1318,7 @@ async function generate(input, saveToFile = true) {
1316
1318
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1317
1319
  staleFilenames.forEach(filename => {
1318
1320
  if (shouldOverwrite(config, filename)) {
1319
- unlinkFile(filename, err => {
1321
+ return unlinkFile(filename, err => {
1320
1322
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1321
1323
  if (err) {
1322
1324
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1337,9 +1339,11 @@ async function generate(input, saveToFile = true) {
1337
1339
  if (config.watch) {
1338
1340
  removeStaleFiles(config, generationResult);
1339
1341
  }
1340
- await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
1342
+ await context.profiler.run(async () => {
1343
+ await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
1344
+ }, 'Lifecycle: beforeAllFileWrite');
1341
1345
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1342
- const exists = fileExists(result.filename);
1346
+ const exists = await fileExists(result.filename);
1343
1347
  if (!shouldOverwrite(config, result.filename) && exists) {
1344
1348
  return;
1345
1349
  }
@@ -1347,10 +1351,10 @@ async function generate(input, saveToFile = true) {
1347
1351
  const currentHash = hash(content);
1348
1352
  let previousHash = recentOutputHash.get(result.filename);
1349
1353
  if (!previousHash && exists) {
1350
- previousHash = hash(readSync(result.filename));
1354
+ previousHash = hash(await readFile(result.filename));
1351
1355
  }
1352
1356
  if (previousHash && currentHash === previousHash) {
1353
- debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
1357
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
1354
1358
  return;
1355
1359
  }
1356
1360
  if (content.length === 0) {
@@ -1360,11 +1364,11 @@ async function generate(input, saveToFile = true) {
1360
1364
  const basedir = path.dirname(result.filename);
1361
1365
  await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
1362
1366
  await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
1363
- mkdirp.sync(basedir);
1367
+ await mkdirp(basedir);
1364
1368
  const absolutePath = path.isAbsolute(result.filename)
1365
1369
  ? result.filename
1366
1370
  : path.join(input.cwd || process.cwd(), result.filename);
1367
- writeSync(absolutePath, result.content);
1371
+ await writeFile(absolutePath, result.content);
1368
1372
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1369
1373
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1370
1374
  })), 'Write files');
@@ -1379,7 +1383,7 @@ async function generate(input, saveToFile = true) {
1379
1383
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1380
1384
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1381
1385
  if (context.profilerOutput) {
1382
- 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()));
1383
1387
  }
1384
1388
  return outputFiles;
1385
1389
  }
package/index.mjs CHANGED
@@ -25,8 +25,9 @@ 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
29
  import { createHash } from 'crypto';
30
+ import { cpus } from 'os';
30
31
  import Listr from 'listr';
31
32
  import { exec } from 'child_process';
32
33
  import isGlob from 'is-glob';
@@ -989,7 +990,6 @@ async function executeCodegen(input) {
989
990
  pluginMap,
990
991
  pluginContext,
991
992
  profiler: context.profiler,
992
- cache,
993
993
  }), `Build Generates Section: ${filename}`);
994
994
  }
995
995
  else {
@@ -1004,12 +1004,14 @@ async function executeCodegen(input) {
1004
1004
  pluginMap,
1005
1005
  pluginContext,
1006
1006
  profiler: context.profiler,
1007
- cache,
1008
1007
  },
1009
1008
  ];
1010
1009
  }
1011
1010
  const process = async (outputArgs) => {
1012
- const output = await codegen(outputArgs);
1011
+ const output = await codegen({
1012
+ ...outputArgs,
1013
+ cache,
1014
+ });
1013
1015
  result.push({
1014
1016
  filename: outputArgs.filename,
1015
1017
  content: output,
@@ -1028,8 +1030,7 @@ async function executeCodegen(input) {
1028
1030
  }), {
1029
1031
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1030
1032
  exitOnError: false,
1031
- // run 4 at once
1032
- concurrent: 4,
1033
+ concurrent: cpus().length,
1033
1034
  });
1034
1035
  },
1035
1036
  });
@@ -1259,15 +1260,16 @@ const createWatcher = (initalContext, onNext) => {
1259
1260
  });
1260
1261
  };
1261
1262
 
1262
- function writeSync(filepath, content) {
1263
- return writeFileSync(filepath, content);
1263
+ const { writeFile: fsWriteFile, readFile: fsReadFile, stat: fsStat } = promises;
1264
+ function writeFile(filepath, content) {
1265
+ return fsWriteFile(filepath, content);
1264
1266
  }
1265
- function readSync(filepath) {
1266
- return readFileSync(filepath, 'utf-8');
1267
+ function readFile(filepath) {
1268
+ return fsReadFile(filepath, 'utf-8');
1267
1269
  }
1268
- function fileExists(filePath) {
1270
+ async function fileExists(filePath) {
1269
1271
  try {
1270
- return statSync(filePath).isFile();
1272
+ return (await fsStat(filePath)).isFile();
1271
1273
  }
1272
1274
  catch (err) {
1273
1275
  return false;
@@ -1289,7 +1291,7 @@ async function generate(input, saveToFile = true) {
1289
1291
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1290
1292
  staleFilenames.forEach(filename => {
1291
1293
  if (shouldOverwrite(config, filename)) {
1292
- unlinkFile(filename, err => {
1294
+ return unlinkFile(filename, err => {
1293
1295
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1294
1296
  if (err) {
1295
1297
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1310,9 +1312,11 @@ async function generate(input, saveToFile = true) {
1310
1312
  if (config.watch) {
1311
1313
  removeStaleFiles(config, generationResult);
1312
1314
  }
1313
- await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
1315
+ await context.profiler.run(async () => {
1316
+ await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
1317
+ }, 'Lifecycle: beforeAllFileWrite');
1314
1318
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1315
- const exists = fileExists(result.filename);
1319
+ const exists = await fileExists(result.filename);
1316
1320
  if (!shouldOverwrite(config, result.filename) && exists) {
1317
1321
  return;
1318
1322
  }
@@ -1320,10 +1324,10 @@ async function generate(input, saveToFile = true) {
1320
1324
  const currentHash = hash(content);
1321
1325
  let previousHash = recentOutputHash.get(result.filename);
1322
1326
  if (!previousHash && exists) {
1323
- previousHash = hash(readSync(result.filename));
1327
+ previousHash = hash(await readFile(result.filename));
1324
1328
  }
1325
1329
  if (previousHash && currentHash === previousHash) {
1326
- debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
1330
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
1327
1331
  return;
1328
1332
  }
1329
1333
  if (content.length === 0) {
@@ -1333,11 +1337,11 @@ async function generate(input, saveToFile = true) {
1333
1337
  const basedir = dirname(result.filename);
1334
1338
  await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
1335
1339
  await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
1336
- mkdirp.sync(basedir);
1340
+ await mkdirp(basedir);
1337
1341
  const absolutePath = isAbsolute(result.filename)
1338
1342
  ? result.filename
1339
1343
  : join(input.cwd || process.cwd(), result.filename);
1340
- writeSync(absolutePath, result.content);
1344
+ await writeFile(absolutePath, result.content);
1341
1345
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1342
1346
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1343
1347
  })), 'Write files');
@@ -1352,7 +1356,7 @@ async function generate(input, saveToFile = true) {
1352
1356
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1353
1357
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1354
1358
  if (context.profilerOutput) {
1355
- writeSync(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1359
+ await writeFile(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1356
1360
  }
1357
1361
  return outputFiles;
1358
1362
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.6.0-alpha-a52c122aa.0",
3
+ "version": "2.6.0-alpha-cb6ec4365.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.6.0-alpha-a52c122aa.0",
9
- "@graphql-codegen/plugin-helpers": "2.5.0-alpha-a52c122aa.0",
8
+ "@graphql-codegen/core": "2.6.0-alpha-cb6ec4365.0",
9
+ "@graphql-codegen/plugin-helpers": "2.5.0-alpha-cb6ec4365.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;