temp_dir.js 594 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict'
  2. const path = require('path')
  3. const fs = require('graceful-fs')
  4. const rimraf = require('rimraf')
  5. const log = require('./logger').create('temp-dir')
  6. const TEMP_DIR = require('os').tmpdir()
  7. module.exports = {
  8. getPath (suffix) {
  9. return path.normalize(TEMP_DIR + suffix)
  10. },
  11. create (path) {
  12. log.debug(`Creating temp dir at ${path}`)
  13. try {
  14. fs.mkdirSync(path)
  15. } catch (e) {
  16. log.warn(`Failed to create a temp dir at ${path}`)
  17. }
  18. return path
  19. },
  20. remove (path, done) {
  21. log.debug(`Cleaning temp dir ${path}`)
  22. rimraf(path, done)
  23. }
  24. }