temp.js 841 B

12345678910111213141516171819202122232425
  1. /*
  2. * Taken from renderer/app/helpers/temp.js with minor cosmetic changes.
  3. * Promisify the temp package. The temp package provides a 'track' feature
  4. * that automatically cleans up temp files at process exit, but that is not
  5. * very useful. They also provide a method to trigger cleanup, but that is not
  6. * safe for concurrent use. So, we use a disposer to unlink the file.
  7. */
  8. const BPromise = require('bluebird')
  9. const fs = BPromise.promisifyAll(require('node:fs'))
  10. const temp = BPromise.promisifyAll(require('temp'))
  11. exports.open = function (affixes) {
  12. return temp.openAsync(affixes).disposer(function (fileInfo) {
  13. fs.closeAsync(fileInfo.fd)
  14. .then(() => {
  15. return fs.unlinkAsync(fileInfo.path)
  16. })
  17. .catch(function (err) {
  18. if (err.code !== 'ENOENT') {
  19. throw err
  20. }
  21. })
  22. })
  23. }