From 986d1fa928c3b59a5c95c74cb772bad1f9a0e951 Mon Sep 17 00:00:00 2001 From: Steven Surgnier Date: Mon, 29 Jan 2018 15:41:28 -0500 Subject: [PATCH] write to a temp file and rename decrease the chance of reading from an incomplete file cc #3094 #3201 --- .../query-runner/pages-writer.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js b/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js index f11ac7f746095..c2db003cb2967 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js +++ b/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js @@ -122,16 +122,17 @@ const preferDefault = m => m && m.default || m .join(`,\n`)} }` + const writeAndMove = (file, data) => { + const destination = joinPath(program.directory, `.cache`, file) + const tmp = `${destination}.${Date.now()}` + return fs.writeFile(tmp, data) + .then(() => fs.move(tmp, destination, { overwrite: true })) + } + await Promise.all([ - fs.writeFile( - joinPath(program.directory, `.cache/pages.json`), - JSON.stringify(pagesData, null, 4) - ), - fs.writeFile(`${program.directory}/.cache/sync-requires.js`, syncRequires), - fs.writeFile( - joinPath(program.directory, `.cache/async-requires.js`), - asyncRequires - ), + writeAndMove(`pages.json`, JSON.stringify(pagesData, null, 4)), + writeAndMove(`sync-requires.js`, syncRequires), + writeAndMove(`async-requires.js`, asyncRequires), ]) return