Skip to content

Instantly share code, notes, and snippets.

@adamreisnz
Created June 19, 2015 22:18
Show Gist options
  • Save adamreisnz/b6b2e9b0b99a125d9442 to your computer and use it in GitHub Desktop.
Save adamreisnz/b6b2e9b0b99a125d9442 to your computer and use it in GitHub Desktop.
Node module to get path to an empty temporary directory
/**
* Module dependencies
*/
var fs = require('fs');
var os = require('os');
var del = require('del');
/**
* Get an temporary directory with given name
* and make sure it exists and is empty
*/
module.exports = function(name) {
//Prepare temporary directory
var tmpDir = os.tmpDir() + name;
if (fs.existsSync(tmpDir)) {
del.sync(tmpDir, {
force: true
});
}
//Create it
fs.mkdirSync(tmpDir);
if (!fs.existsSync(tmpDir)) {
return '';
}
//Return it
return tmpDir;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment