Created
June 19, 2015 22:18
-
-
Save adamreisnz/b6b2e9b0b99a125d9442 to your computer and use it in GitHub Desktop.
Node module to get path to an empty temporary directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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