From 2f7f91e7642a12ef646a356042fd92dd033bdb2f Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Wed, 19 May 2021 08:11:13 +1000 Subject: [PATCH] feat: only write files if the content differs This is useful in conjunction with tools like nodemon that monitor for file changes. By only writing files if the content has actually changed, unnecessary side effects (like nodemon restarting) can be avoided. Fixes #257 --- packages/cli/src/index.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index cd529587..d58425a5 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -90,13 +90,18 @@ class FileProcessor { this.config, ); if (typeDecs.length > 0) { - await fs.outputFile(decsFileName, declarationFileContents); - console.log( - `Saved ${typeDecs.length} query types to ${path.relative( - process.cwd(), - decsFileName, - )}`, - ); + const oldDeclarationFileContents = (await fs.pathExists(decsFileName)) + ? await fs.readFile(decsFileName, { encoding: 'utf-8' }) + : null; + if (oldDeclarationFileContents !== declarationFileContents) { + await fs.outputFile(decsFileName, declarationFileContents); + console.log( + `Saved ${typeDecs.length} query types to ${path.relative( + process.cwd(), + decsFileName, + )}`, + ); + } } } }