function _createTmpDir(options, callback) { var args = _parseArguments(options, callback), opts = args[0], cb = args[1]; // gets a temporary filename _getTmpName(opts, function _tmpNameCreated(err, name) { if (err) return cb(err); // create the directory fs.mkdir(name, opts.mode || DIR_MODE, function _dirCreated(err) { if (err) return cb(err); cb(null, name, _prepareTmpDirRemoveCallback(name, opts)); }); }); }
...
Simple temporary directory creation, it will be removed on process exit.
If the directory still contains items on process exit, then it won't be removed.
```javascript
var tmp = require('tmp');
tmp.dir(function _tempDirCreated(err, path, cleanupCallback) {
if (err) throw err;
console.log("Dir: ", path);
// Manual cleanup
cleanupCallback();
});
...
function _createTmpDirSync(options) { var args = _parseArguments(options), opts = args[0]; var name = _getTmpNameSync(opts); fs.mkdirSync(name, opts.mode || DIR_MODE); return { name : name, removeCallback : _prepareTmpDirRemoveCallback(name, opts) }; }
...
### Synchronous directory creation
A synchronous version of the above.
```javascript
var tmp = require('tmp');
var tmpobj = tmp.dirSync();
console.log("Dir: ", tmpobj.name);
// Manual cleanup
tmpobj.removeCallback();
```
Note that this might throw an exception if either the maximum limit of retries
for creating a temporary name fails, or, in case that you do not have the permission
...
function _createTmpFile(options, callback) { var args = _parseArguments(options, callback), opts = args[0], cb = args[1]; opts.postfix = (_isUndefined(opts.postfix)) ? '.tmp' : opts.postfix; // gets a temporary filename _getTmpName(opts, function _tmpNameCreated(err, name) { if (err) return cb(err); // create and open the file fs.open(name, CREATE_FLAGS, opts.mode || FILE_MODE, function _fileCreated(err, fd) { if (err) return cb(err); if (opts.discardDescriptor) { return fs.close(fd, function _discardCallback(err) { if (err) { // Low probability, and the file exists, so this could be // ignored. If it isn't we certainly need to unlink the // file, and if that fails too its error is more // important. try { fs.unlinkSync(name); } catch (e) { err = e; } return cb(err); } cb(null, name, undefined, _prepareTmpFileRemoveCallback(name, -1, opts)); }); } if (opts.detachDescriptor) { return cb(null, name, fd, _prepareTmpFileRemoveCallback(name, -1, opts)); } cb(null, name, fd, _prepareTmpFileRemoveCallback(name, fd, opts)); }); }); }
...
### Asynchronous file creation
Simple temporary file creation, the file will be closed and unlinked on process exit.
```javascript
var tmp = require('tmp');
tmp.file(function _tempFileCreated(err, path, fd, cleanupCallback) {
if (err) throw err;
console.log("File: ", path);
console.log("Filedescriptor: ", fd);
// If we don't need the file anymore we could manually call the cleanupCallback
// But that is not necessary if we didn't pass the keep option because the library
...
function _createTmpFileSync(options) { var args = _parseArguments(options), opts = args[0]; opts.postfix = opts.postfix || '.tmp'; var name = _getTmpNameSync(opts); var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE); return { name : name, fd : fd, removeCallback : _prepareTmpFileRemoveCallback(name, fd, opts) }; }
...
### Synchronous file creation
A synchronous version of the above.
```javascript
var tmp = require('tmp');
var tmpobj = tmp.fileSync();
console.log("File: ", tmpobj.name);
console.log("Filedescriptor: ", tmpobj.fd);
// If we don't need the file anymore we could manually call the removeCallback
// But that is not necessary if we didn't pass the keep option because the library
// will clean after itself.
tmpobj.removeCallback();
...
function _setGracefulCleanup() { _gracefulCleanup = true; }
...
One may want to cleanup the temporary files even when an uncaught exception
occurs. To enforce this, you can call the `setGracefulCleanup()` method:
```javascript
var tmp = require('tmp');
tmp.setGracefulCleanup();
```
## Options
All options are optional :)
* `mode`: the file mode to create with, it fallbacks to `0600` on file creation and `0700` on directory creation
...
function _getTmpName(options, callback) { var args = _parseArguments(options, callback), opts = args[0], cb = args[1], tries = opts.tries || DEFAULT_TRIES; if (isNaN(tries) || tries < 0) return cb(new Error('Invalid tries')); if (opts.template && !opts.template.match(TEMPLATE_PATTERN)) return cb(new Error('Invalid template provided')); (function _getUniqueName() { var name = _generateTmpName(opts); // check whether the path exists then retry if needed fs.stat(name, function (err) { if (!err) { if (tries-- > 0) return _getUniqueName(); return cb(new Error('Could not get a unique tmp filename, max tries reached ' + name)); } cb(null, name); }); }()); }
...
It is possible with this library to generate a unique filename in the specified
directory.
```javascript
var tmp = require('tmp');
tmp.tmpName(function _tempNameGenerated(err, path) {
if (err) throw err;
console.log("Created temporary filename: ", path);
});
```
### Synchronous filename generation
...
function _getTmpNameSync(options) { var args = _parseArguments(options), opts = args[0], tries = opts.tries || DEFAULT_TRIES; if (isNaN(tries) || tries < 0) throw new Error('Invalid tries'); if (opts.template && !opts.template.match(TEMPLATE_PATTERN)) throw new Error('Invalid template provided'); do { var name = _generateTmpName(opts); try { fs.statSync(name); } catch (e) { return name; } } while (tries-- > 0); throw new Error('Could not get a unique tmp filename, max tries reached'); }
...
### Synchronous filename generation
A synchronous version of the above.
```javascript
var tmp = require('tmp');
var name = tmp.tmpNameSync();
console.log("Created temporary filename: ", name);
```
## Advanced usage
### Asynchronous file creation
...