@graphql-codegen/cli 2.5.0 → 2.6.0-alpha-3f811f314.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,9 +53,11 @@ 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'));
60
+ const promises = require('fs/promises');
59
61
  const mkdirp = _interopDefault(require('mkdirp'));
60
62
  const crypto = require('crypto');
61
63
  const inquirer = _interopDefault(require('inquirer'));
@@ -1108,8 +1110,7 @@ async function executeCodegen(input) {
1108
1110
  }), {
1109
1111
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1110
1112
  exitOnError: false,
1111
- // run 4 at once
1112
- concurrent: 4,
1113
+ concurrent: os.cpus().length,
1113
1114
  });
1114
1115
  },
1115
1116
  });
@@ -1256,15 +1257,15 @@ const createWatcher = (initalContext, onNext) => {
1256
1257
  });
1257
1258
  };
1258
1259
 
1259
- function writeSync(filepath, content) {
1260
- return fs.writeFileSync(filepath, content);
1260
+ function writeFile(filepath, content) {
1261
+ return promises.writeFile(filepath, content);
1261
1262
  }
1262
- function readSync(filepath) {
1263
- return fs.readFileSync(filepath, 'utf-8');
1263
+ function readFile(filepath) {
1264
+ return promises.readFile(filepath, 'utf-8');
1264
1265
  }
1265
- function fileExists(filePath) {
1266
+ async function fileExists(filePath) {
1266
1267
  try {
1267
- return fs.statSync(filePath).isFile();
1268
+ return (await promises.stat(filePath)).isFile();
1268
1269
  }
1269
1270
  catch (err) {
1270
1271
  return false;
@@ -1286,7 +1287,7 @@ async function generate(input, saveToFile = true) {
1286
1287
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1287
1288
  staleFilenames.forEach(filename => {
1288
1289
  if (shouldOverwrite(config, filename)) {
1289
- unlinkFile(filename, err => {
1290
+ return unlinkFile(filename, err => {
1290
1291
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1291
1292
  if (err) {
1292
1293
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1307,7 +1308,39 @@ async function generate(input, saveToFile = true) {
1307
1308
  if (config.watch) {
1308
1309
  removeStaleFiles(config, generationResult);
1309
1310
  }
1310
- 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');
1311
1344
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1312
1345
  const exists = fileExists(result.filename);
1313
1346
  if (!shouldOverwrite(config, result.filename) && exists) {
@@ -1317,7 +1350,7 @@ async function generate(input, saveToFile = true) {
1317
1350
  const currentHash = hash(content);
1318
1351
  let previousHash = recentOutputHash.get(result.filename);
1319
1352
  if (!previousHash && exists) {
1320
- previousHash = hash(readSync(result.filename));
1353
+ previousHash = hash(await readFile(result.filename));
1321
1354
  }
1322
1355
  if (previousHash && currentHash === previousHash) {
1323
1356
  debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
@@ -1334,7 +1367,7 @@ async function generate(input, saveToFile = true) {
1334
1367
  const absolutePath = path.isAbsolute(result.filename)
1335
1368
  ? result.filename
1336
1369
  : path.join(input.cwd || process.cwd(), result.filename);
1337
- writeSync(absolutePath, result.content);
1370
+ await writeFile(absolutePath, result.content);
1338
1371
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1339
1372
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1340
1373
  })), 'Write files');
@@ -1349,7 +1382,7 @@ async function generate(input, saveToFile = true) {
1349
1382
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1350
1383
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1351
1384
  if (context.profilerOutput) {
1352
- 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()));
1353
1386
  }
1354
1387
  return outputFiles;
1355
1388
  }
package/index.js CHANGED
@@ -53,10 +53,12 @@ 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'));
59
60
  const debounce = _interopDefault(require('debounce'));
61
+ const promises = require('fs/promises');
60
62
  const mkdirp = _interopDefault(require('mkdirp'));
61
63
  const crypto = require('crypto');
62
64
  const inquirer = _interopDefault(require('inquirer'));
@@ -1026,8 +1028,7 @@ async function executeCodegen(input) {
1026
1028
  }), {
1027
1029
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1028
1030
  exitOnError: false,
1029
- // run 4 at once
1030
- concurrent: 4,
1031
+ concurrent: os.cpus().length,
1031
1032
  });
1032
1033
  },
1033
1034
  });
@@ -1257,15 +1258,15 @@ const createWatcher = (initalContext, onNext) => {
1257
1258
  });
1258
1259
  };
1259
1260
 
1260
- function writeSync(filepath, content) {
1261
- return fs.writeFileSync(filepath, content);
1261
+ function writeFile(filepath, content) {
1262
+ return promises.writeFile(filepath, content);
1262
1263
  }
1263
- function readSync(filepath) {
1264
- return fs.readFileSync(filepath, 'utf-8');
1264
+ function readFile(filepath) {
1265
+ return promises.readFile(filepath, 'utf-8');
1265
1266
  }
1266
- function fileExists(filePath) {
1267
+ async function fileExists(filePath) {
1267
1268
  try {
1268
- return fs.statSync(filePath).isFile();
1269
+ return (await promises.stat(filePath)).isFile();
1269
1270
  }
1270
1271
  catch (err) {
1271
1272
  return false;
@@ -1287,7 +1288,7 @@ async function generate(input, saveToFile = true) {
1287
1288
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1288
1289
  staleFilenames.forEach(filename => {
1289
1290
  if (shouldOverwrite(config, filename)) {
1290
- unlinkFile(filename, err => {
1291
+ return unlinkFile(filename, err => {
1291
1292
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1292
1293
  if (err) {
1293
1294
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1308,7 +1309,39 @@ async function generate(input, saveToFile = true) {
1308
1309
  if (config.watch) {
1309
1310
  removeStaleFiles(config, generationResult);
1310
1311
  }
1311
- 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');
1312
1345
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1313
1346
  const exists = fileExists(result.filename);
1314
1347
  if (!shouldOverwrite(config, result.filename) && exists) {
@@ -1318,7 +1351,7 @@ async function generate(input, saveToFile = true) {
1318
1351
  const currentHash = hash(content);
1319
1352
  let previousHash = recentOutputHash.get(result.filename);
1320
1353
  if (!previousHash && exists) {
1321
- previousHash = hash(readSync(result.filename));
1354
+ previousHash = hash(await readFile(result.filename));
1322
1355
  }
1323
1356
  if (previousHash && currentHash === previousHash) {
1324
1357
  debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
@@ -1335,7 +1368,7 @@ async function generate(input, saveToFile = true) {
1335
1368
  const absolutePath = path.isAbsolute(result.filename)
1336
1369
  ? result.filename
1337
1370
  : path.join(input.cwd || process.cwd(), result.filename);
1338
- writeSync(absolutePath, result.content);
1371
+ await writeFile(absolutePath, result.content);
1339
1372
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1340
1373
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1341
1374
  })), 'Write files');
@@ -1350,7 +1383,7 @@ async function generate(input, saveToFile = true) {
1350
1383
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1351
1384
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1352
1385
  if (context.profilerOutput) {
1353
- 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()));
1354
1387
  }
1355
1388
  return outputFiles;
1356
1389
  }
package/index.mjs CHANGED
@@ -25,11 +25,13 @@ 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';
32
33
  import debounce from 'debounce';
34
+ import { writeFile as writeFile$1, readFile as readFile$1, stat } from 'fs/promises';
33
35
  import mkdirp from 'mkdirp';
34
36
  import { createHash } from 'crypto';
35
37
  import inquirer from 'inquirer';
@@ -999,8 +1001,7 @@ async function executeCodegen(input) {
999
1001
  }), {
1000
1002
  // it doesn't stop when one of tasks failed, to finish at least some of outputs
1001
1003
  exitOnError: false,
1002
- // run 4 at once
1003
- concurrent: 4,
1004
+ concurrent: cpus().length,
1004
1005
  });
1005
1006
  },
1006
1007
  });
@@ -1230,15 +1231,15 @@ const createWatcher = (initalContext, onNext) => {
1230
1231
  });
1231
1232
  };
1232
1233
 
1233
- function writeSync(filepath, content) {
1234
- return writeFileSync(filepath, content);
1234
+ function writeFile(filepath, content) {
1235
+ return writeFile$1(filepath, content);
1235
1236
  }
1236
- function readSync(filepath) {
1237
- return readFileSync(filepath, 'utf-8');
1237
+ function readFile(filepath) {
1238
+ return readFile$1(filepath, 'utf-8');
1238
1239
  }
1239
- function fileExists(filePath) {
1240
+ async function fileExists(filePath) {
1240
1241
  try {
1241
- return statSync(filePath).isFile();
1242
+ return (await stat(filePath)).isFile();
1242
1243
  }
1243
1244
  catch (err) {
1244
1245
  return false;
@@ -1260,7 +1261,7 @@ async function generate(input, saveToFile = true) {
1260
1261
  const staleFilenames = previouslyGeneratedFilenames.filter(f => !filenames.includes(f));
1261
1262
  staleFilenames.forEach(filename => {
1262
1263
  if (shouldOverwrite(config, filename)) {
1263
- unlinkFile(filename, err => {
1264
+ return unlinkFile(filename, err => {
1264
1265
  const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
1265
1266
  if (err) {
1266
1267
  debugLog(`Cannot remove stale file: ${prettyFilename}\n${err}`);
@@ -1281,7 +1282,39 @@ async function generate(input, saveToFile = true) {
1281
1282
  if (config.watch) {
1282
1283
  removeStaleFiles(config, generationResult);
1283
1284
  }
1284
- 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');
1285
1318
  await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
1286
1319
  const exists = fileExists(result.filename);
1287
1320
  if (!shouldOverwrite(config, result.filename) && exists) {
@@ -1291,7 +1324,7 @@ async function generate(input, saveToFile = true) {
1291
1324
  const currentHash = hash(content);
1292
1325
  let previousHash = recentOutputHash.get(result.filename);
1293
1326
  if (!previousHash && exists) {
1294
- previousHash = hash(readSync(result.filename));
1327
+ previousHash = hash(await readFile(result.filename));
1295
1328
  }
1296
1329
  if (previousHash && currentHash === previousHash) {
1297
1330
  debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
@@ -1308,7 +1341,7 @@ async function generate(input, saveToFile = true) {
1308
1341
  const absolutePath = isAbsolute(result.filename)
1309
1342
  ? result.filename
1310
1343
  : join(input.cwd || process.cwd(), result.filename);
1311
- writeSync(absolutePath, result.content);
1344
+ await writeFile(absolutePath, result.content);
1312
1345
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
1313
1346
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
1314
1347
  })), 'Write files');
@@ -1323,7 +1356,7 @@ async function generate(input, saveToFile = true) {
1323
1356
  await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
1324
1357
  await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
1325
1358
  if (context.profilerOutput) {
1326
- writeSync(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1359
+ await writeFile(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
1327
1360
  }
1328
1361
  return outputFiles;
1329
1362
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.5.0",
3
+ "version": "2.6.0-alpha-3f811f314.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
  },
@@ -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;