From 17add138607a557ab4c35f456054e5ba4fae5ea6 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 29 Jan 2017 16:09:22 +0100 Subject: [PATCH] Only write to files if the change (#511) Fixes #506. --- bin/prettier.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/bin/prettier.js b/bin/prettier.js index 16ea3381..2de67aae 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -191,13 +191,17 @@ if (stdin) { } if (write) { - fs.writeFile(filename, output, "utf8", err => { - if (err) { - console.error("Unable to write file: " + filename + "\n" + err); - // Don't exit the process if one file failed - process.exitCode = 2; - } - }); + // Don't write the file if it won't change in order not to invalidate + // mtime based caches. + if (output !== input) { + fs.writeFile(filename, output, "utf8", err => { + if (err) { + console.error("Unable to write file: " + filename + "\n" + err); + // Don't exit the process if one file failed + process.exitCode = 2; + } + }); + } } else { // Don't use `console.log` here since it adds an extra newline at the end. process.stdout.write(output);