Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/graphql-codegen-cli/src/generate-and-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { executeCodegen } from './codegen.js';
import { CodegenContext, ensureContext } from './config.js';
import { lifecycleHooks } from './hooks.js';
import { debugLog } from './utils/debugging.js';
import { mkdirp, readFile, unlinkFile, writeFile } from './utils/file-system.js';
import { mkdirp, readFile, unlinkFile, writeFile, writeFileSync } from './utils/file-system.js';
import { createWatcher } from './utils/watcher.js';

const hash = (content: string): string => createHash('sha1').update(content).digest('base64');
Expand Down Expand Up @@ -128,6 +128,13 @@ export async function generate(
return generationResult;
}

// Register on exit listener to write profiler output
process.on('exit', () => {
if (context.profilerOutput) {
writeFileSync(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
}
});

// watch mode
if (config.watch) {
return createWatcher(context, writeOutput).runningWatcher;
Expand All @@ -138,10 +145,6 @@ export async function generate(
await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');

if (context.profilerOutput) {
await writeFile(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
}

return outputFiles;
}

Expand Down
6 changes: 5 additions & 1 deletion packages/graphql-codegen-cli/src/utils/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises, unlink as fsUnlink } from 'fs';
import { promises, unlink as fsUnlink, writeFileSync as fsWriteFileSync } from 'fs';

const { access: fsAccess, writeFile: fsWriteFile, readFile: fsReadFile, mkdir } = promises;

Expand All @@ -10,6 +10,10 @@ export function writeFile(filepath: string, content: string) {
return fsWriteFile(filepath, content);
}

export function writeFileSync(filepath: string, content: string) {
return fsWriteFileSync(filepath, content);
}

export function readFile(filepath: string) {
return fsReadFile(filepath, 'utf-8');
}
Expand Down