Skip to content

Commit

Permalink
write to a temp file and rename
Browse files Browse the repository at this point in the history
decrease the chance of reading from an incomplete file

cc gatsbyjs#3094 gatsbyjs#3201
  • Loading branch information
stevensurgnier committed Jan 29, 2018
1 parent a03310a commit 986d1fa
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/gatsby/src/internal-plugins/query-runner/pages-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 986d1fa

Please sign in to comment.