Comparing version 0.0.6 to 0.0.7
@@ -21,4 +21,8 @@ var | ||
var _removeObjects = []; | ||
var | ||
randomChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz", | ||
randomCharsLength = randomChars.length, | ||
_removeObjects = []; | ||
function _isUndefined(obj) { | ||
@@ -40,17 +44,37 @@ return obj === void 0; | ||
*/ | ||
function _getTmpName(dir, prefix, postfix, maxTries, cb) { | ||
var tries = maxTries || 3; | ||
function _getTmpName(opts, cb) { | ||
var | ||
template = opts.template, | ||
templateDefined = !_isUndefined(template), | ||
tries = opts.tries || 3; | ||
if (tries < 0) return cb(new Error('Invalid tries')); | ||
if (tries < 0) | ||
return cb(new Error('Invalid tries')); | ||
if (templateDefined && !template.match(/XXXXXX/)) | ||
return cb(new Error('Invalid template provided')); | ||
function _getName() { | ||
var name = | ||
[ | ||
(_isUndefined(prefix)) ? 'tmp-' : prefix, | ||
// prefix and postfix | ||
if (!templateDefined) { | ||
var name = [ | ||
(_isUndefined(opts.prefix)) ? 'tmp-' : opts.prefix, | ||
process.pid, | ||
(Math.random() * 0x1000000000).toString(36), | ||
postfix | ||
opts.postfix | ||
].join(''); | ||
return path.join(dir || _TMP, name); | ||
return path.join(opts.dir || _TMP, name); | ||
} | ||
// mkstemps like template | ||
var chars = []; | ||
for (var i = 0; i < 6; i++) { | ||
chars.push( | ||
randomChars.substr(Math.floor(Math.random() * randomCharsLength), 1)); | ||
} | ||
return template.replace(/XXXXXX/, chars.join('')); | ||
} | ||
@@ -79,21 +103,20 @@ | ||
opts = args[0], | ||
cb = args[1], | ||
cb = args[1]; | ||
filePostFix = (_isUndefined(opts.postfix)) ? '.tmp' : opts.postfix; | ||
opts.postfix = (_isUndefined(opts.postfix)) ? '.tmp' : opts.postfix; | ||
// gets the temporary filename | ||
_getTmpName(opts.dir, opts.prefix, filePostFix, opts.tries, | ||
function _tmpNameCreated(err, name) { | ||
_getTmpName(opts, function _tmpNameCreated(err, name) { | ||
if (err) return cb(err); | ||
// create and open the file | ||
fs.open(name, 'w+', opts.mode || 0600, function _fileCreated(err, fd) { | ||
if (err) return cb(err); | ||
fs.open(name, 'w+', opts.mode || 0600, function _fileCreated(err, fd) { | ||
if (err) return cb(err); | ||
if (_isUndefined(opts.unlink) || opts.unlink) | ||
_removeObjects.push([ fs.unlinkSync, name ]); | ||
if (_isUndefined(opts.unlink) || opts.unlink) | ||
_removeObjects.push([ fs.unlinkSync, name ]); | ||
cb(null, name, fd); | ||
}); | ||
} | ||
); | ||
cb(null, name, fd); | ||
}); | ||
}); | ||
} | ||
@@ -108,21 +131,18 @@ | ||
opts = args[0], | ||
cb = args[1], | ||
cb = args[1]; | ||
dirPostfix = (_isUndefined(opts.postfix)) ? '' : opts.postfix; | ||
// gets the temporary filename | ||
_getTmpName(opts, function _tmpNameCreated(err, name) { | ||
if (err) return cb(err); | ||
// gets the temporary filename | ||
_getTmpName(opts.dir, opts.prefix, dirPostfix, opts.tries, | ||
function _tmpNameCreated(err, name) { | ||
// create the directory | ||
fs.mkdir(name, opts.mode || 0700, function _dirCreated(err) { | ||
if (err) return cb(err); | ||
fs.mkdir(name, opts.mode || 0700, function _dirCreated(err) { | ||
if (err) return cb(err); | ||
if (_isUndefined(opts.unlink) || opts.unlink) | ||
_removeObjects.push([ fs.rmdirSync, name ]); | ||
if (_isUndefined(opts.unlink) || opts.unlink) | ||
_removeObjects.push([ fs.rmdirSync, name ]); | ||
cb(null, name); | ||
}); | ||
} | ||
); | ||
cb(null, name); | ||
}); | ||
}); | ||
} | ||
@@ -129,0 +149,0 @@ |
{ | ||
"name": "tmp", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Temporary file and directory creator", | ||
@@ -5,0 +5,0 @@ "author": "KARASZI István <github@spam.raszi.hu> (http://raszi.hu/)", |
@@ -25,6 +25,6 @@ # Tmp | ||
tmp.file(function _tempFileCreated(err, path, fd) { | ||
if (err) throw err; | ||
if (err) throw err; | ||
console.log("File: ", path); | ||
console.log("Filedescriptor: ", fd); | ||
console.log("File: ", path); | ||
console.log("Filedescriptor: ", fd); | ||
}); | ||
@@ -37,5 +37,5 @@ | ||
tmp.dir(function _tempDirCreated(err, path) { | ||
if (err) throw err; | ||
if (err) throw err; | ||
console.log("Dir: ", path); | ||
console.log("Dir: ", path); | ||
}); | ||
@@ -50,6 +50,6 @@ | ||
tmp.file({ mode: 0644, prefix: 'prefix-', postfix: '.txt' }, function _tempFileCreated(err, path, fd) { | ||
if (err) throw err; | ||
if (err) throw err; | ||
console.log("File: ", path); | ||
console.log("Filedescriptor: ", fd); | ||
console.log("File: ", path); | ||
console.log("Filedescriptor: ", fd); | ||
}); | ||
@@ -59,9 +59,17 @@ | ||
tmp.dir({ mode: 0750, prefix: 'myTmpDir_' }, function _tempDirCreated(err, path) { | ||
if (err) throw err; | ||
tmp.dir({ mode: 0750, prefix: 'myTmpDir_' }, function _tempDirCreated(err, path) { | ||
if (err) throw err; | ||
console.log("Dir: ", path); | ||
}); | ||
console.log("Dir: ", path); | ||
}); | ||
### mkstemps like | ||
tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) { | ||
iff (err) throw err; | ||
console.log("Dir: ", path); | ||
}); | ||
## Options | ||
@@ -74,2 +82,3 @@ | ||
* `postfix`: the optional postfix, fallbacks to `.tmp` on file creation | ||
* `template`: [mkstemps][2] like filename template, no default | ||
* `dir`: the optional temporary directory, fallbacks to system default (guesses from environment) | ||
@@ -79,1 +88,2 @@ * `tries`: how many times should the function tries to get a unique filename before giving up, default `3` | ||
[1]: https://github.com/bruce/node-temp | ||
[2]: http://www.kernel.org/doc/man-pages/online/pages/man3/mkstemp.3.html |
@@ -53,2 +53,12 @@ var | ||
'when using template': { | ||
topic: function () { | ||
tmp.dir({ template: tmp.tmpdir.concat('clike-XXXXXX-postfix') }, this.callback); | ||
}, | ||
'should be a file': _testDir(040700), | ||
'should have the provided prefix': Test.testPrefix('clike-'), | ||
'should have the provided postfix': Test.testPostfix('-postfix') | ||
}, | ||
'when using multiple options': { | ||
@@ -55,0 +65,0 @@ topic: function () { |
@@ -58,2 +58,12 @@ var | ||
'when using template': { | ||
topic: function () { | ||
tmp.file({ template: tmp.tmpdir.concat('clike-XXXXXX-postfix') }, this.callback); | ||
}, | ||
'should be a file': _testFile(0100600), | ||
'should have the provided prefix': Test.testPrefix('clike-'), | ||
'should have the provided postfix': Test.testPostfix('-postfix') | ||
}, | ||
'when using multiple options': { | ||
@@ -60,0 +70,0 @@ topic: function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12555
293
84