raszi / node-tmp

Temporary file and directory creator for node.js
MIT License
736 stars 92 forks source link

API feature request: tmp.forceCleanup([...names]) #172

Closed Streemo closed 6 years ago

Streemo commented 6 years ago

Apparently, node-tmp doesn't properly cleanup in some situations. It would be nice to have a method on tmp like this:

tmp.forceCleanup = (names, cb) => {
  if (!names) return tmp._cleanupAllCreatedFilesAndDirs(cb);
  // run cleanup jobs in parallel, calls cb when all are done
  parallel(names.map(name => done => tmp._cleanupFileOrDir(name, done)), cb)
}

usage:

const tmp = require("tmp")

process.on("SIGINT", () => tmp.forceCleanup())

// my code ...

what i'll probably do currently (a hack...):

const tmp = require("tmp")
const fs = require("fs")
const { join } = require("path")

// keep track of all created crap, or if you use a single parent dir:
// delete all stuff under parent dir that starts with tmp-

process.on("SIGINT", () => {
  fs.readdir(parentDir, (err, files) => {
    //handle err
    for (let file of files){
      if (file.slice(0,4) === "tmp-"){
        // brevity (no err handling/file checking)
        fs.unlink(join(parentDir, file))
      }
    }
  })
})
silkentrance commented 6 years ago

@Streemo which version of node and tmp are you using, which OS?

Normally, this should be handled automatically when you call tmp.setGracefulCleanup(), see https://github.com/raszi/node-tmp#graceful-cleanup

Otherwise, and this is for debugging, it will keep the temporary files on process exit.

Streemo commented 6 years ago

node: 10.1.0 npm: 6.0.1 tmp: 0.0.33 os: MacOS 10.13.2 (high sierra)

The problem was that (even if setGracefulCleanup was called), CTRL+C would not actually remove the tmp folders. The folders would be removed on process' natural exit, though. I think setGracefulCleanup should try to remove all the junk created on any kind of exit (whether CTRL+C or natural exit, etc. but that's just my preference. Maybe a distinction between fullCleanup/forceCleanup and naturalCleanup for debugging purposes?

silkentrance commented 6 years ago

@Streemo so this is basically a dupe of #121. Closing. Feel free to try out the PR for #121: #159.