wrapRenameStream = (writeable, tmp, filename) => {
  let errorEmitted = false
  writeable.on('error', () => errorEmitted = true)

  const cleanup = () => {
    return access(tmp)
      .then(() => {
        return unlink(tmp).then(() => {
          log.trace(`Write failed. Remove temporary file ${tmp}`)
        })
      }, () => {}) // tmp does not exist
  }

  const renameFile = (err, cb) => {
    if (err || errorEmitted) {
      return cleanup().then(() => cb(err))
    }

    rename(tmp, filename).then(cb, cb)
  }

  const stream = new Transform({
    transform(chunk, enc, cb) {
      if (errorEmitted) {
        return cb()
      }
      writeable.write(chunk, enc, (err) => {
        if (err) {
          return cleanup().then(() => cb(err))
        }
        cb()
      })
    },
    flush(cb) {
      const end = err => {
        renameFile(err, cb)
      }

      writeable.once('error', end)
      writeable.once('finish', end)
      writeable.end()
    }
  })

  return stream
}
@param {Writeable} writeable
@param {string} tmp
@param {string} filename
@returns {Transform} Stream