Skip to content

Commit 0bcd628

Browse files
Avoid writing to output files when no changes (#6550)
* fix(cli): avoid write same output when no changes * generalize outputFile This function will check a cache, it will only write the file if: - The modified timestamps changed since last time we wrote something. This is useful to know if something changed by another tool or manually without diffing the full file. - The contents changed. * further simplify checks Turns out that reading files and comparing them is fairly fast and there is no huge benefit over only using the Stats of the file and keeping track of that information. Thanks @kentcdodds! Co-authored-by: Robin Malfait <[email protected]>
1 parent 722232c commit 0bcd628

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/cli.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ function formatNodes(root) {
4141
}
4242
}
4343

44+
async function outputFile(file, contents) {
45+
if (fs.existsSync(file) && (await fs.promises.readFile(file, 'utf8')) === contents) {
46+
return // Skip writing the file
47+
}
48+
49+
// Write the file
50+
await fs.promises.writeFile(file, contents, 'utf8')
51+
}
52+
4453
function help({ message, usage, commands, options }) {
4554
let indent = 2
4655

@@ -534,6 +543,7 @@ async function build() {
534543
if (!output) {
535544
return process.stdout.write(result.css)
536545
}
546+
537547
return Promise.all(
538548
[
539549
fs.promises.writeFile(output, result.css, () => true),
@@ -664,10 +674,10 @@ async function build() {
664674
return process.stdout.write(result.css)
665675
}
666676

667-
await Promise.all(
677+
return Promise.all(
668678
[
669-
fs.promises.writeFile(output, result.css, () => true),
670-
result.map && fs.writeFile(output + '.map', result.map.toString(), () => true),
679+
outputFile(output, result.css),
680+
result.map && outputFile(output + '.map', result.map.toString()),
671681
].filter(Boolean)
672682
)
673683
})

0 commit comments

Comments
 (0)