Skip to content

Commit

Permalink
[export] Clean up temporary files on failure (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Jun 4, 2019
1 parent 1f5708e commit 4d032fb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/@sanity/export/src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function exportDataset(opts) {

const prefix = `${opts.dataset}-export-${slugDate}`
const tmpDir = path.join(os.tmpdir(), prefix)
const cleanup = () =>
fse.remove(tmpDir).catch(err => {
debug(`Error while cleaning up temporary files: ${err.message}`)
})

const assetHandler = new AssetHandler({
client: options.client,
tmpDir,
Expand All @@ -50,9 +55,10 @@ function exportDataset(opts) {
}

return new Promise(async (resolve, reject) => {
miss.finished(archive, archiveErr => {
miss.finished(archive, async archiveErr => {
if (archiveErr) {
debug('Archiving errored! %s', archiveErr.stack)
await cleanup()
reject(archiveErr)
return
}
Expand Down Expand Up @@ -139,6 +145,7 @@ function exportDataset(opts) {
clearInterval(progressInterval)
} catch (assetErr) {
clearInterval(progressInterval)
await cleanup()
reject(assetErr)
return
}
Expand All @@ -165,7 +172,7 @@ function exportDataset(opts) {

async function onComplete(err) {
onProgress({step: 'Clearing temporary files...'})
await fse.remove(tmpDir)
await cleanup()

if (!err) {
resolve()
Expand Down

0 comments on commit 4d032fb

Please sign in to comment.