Comparing version 0.2.1 to 0.3.0
@@ -0,1 +1,6 @@ | ||
# 0.3.1 / 2013-04-06 | ||
- changed to create random name again if file exists | ||
- deleted mode parameter | ||
# 0.2.1 / 2013-02-29 | ||
@@ -2,0 +7,0 @@ |
@@ -8,42 +8,94 @@ // mktemp Copyright(c) 2013 sasa+1 | ||
function createFile(path, callback) { | ||
var filename = mkrand(path); | ||
function createUniqueName(template, callback) { | ||
var filename = mkrand(template); | ||
fs.writeFile(filename, '', function(err) { | ||
callback(err, filename); | ||
fs.exists(filename, function(exists) { | ||
if (exists) { | ||
createUniqueName(template, callback); | ||
} else { | ||
callback(null, filename); | ||
} | ||
}); | ||
} | ||
function createFileSync(path) { | ||
var filename = mkrand(path); | ||
function createUniqueNameSync(template) { | ||
var filename; | ||
fs.writeFileSync(filename, ''); | ||
do { | ||
filename = mkrand(template); | ||
} while (fs.existsSync(filename)); | ||
return filename; | ||
} | ||
function createDir(path, mode, callback) { | ||
var dirname = mkrand(path); | ||
function createFile(template, callback) { | ||
createUniqueName(template, function(err, filename) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
if (typeof mode === 'function' && callback === void 0) { | ||
callback = mode; | ||
mode = 511; /*=0777*/ | ||
fs.open(filename, 'ax+', 384 /*=0600*/, function(err, fd) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
fs.close(fd, function(err) { | ||
callback(err, filename); | ||
}); | ||
}); | ||
}); | ||
} | ||
function createFileSync(template) { | ||
var filename = createUniqueNameSync(template); | ||
try { | ||
fs.closeSync(fs.openSync(filename, 'ax+', 384 /*=0600*/)); | ||
} catch (e) { | ||
throw e; | ||
} | ||
fs.mkdir(dirname, mode, function(err) { | ||
callback(err, dirname); | ||
return filename; | ||
} | ||
function createDir(template, callback) { | ||
createUniqueName(template, function(err, dirname) { | ||
if (err) { | ||
callbacke(err); | ||
return; | ||
} | ||
fs.mkdir(dirname, 448 /*=0700*/, function(err) { | ||
callback(err, dirname); | ||
}); | ||
}); | ||
} | ||
function createDirSync(path, mode) { | ||
var dirname = mkrand(path); | ||
function createDirSync(template) { | ||
var dirname = createUniqueNameSync(template); | ||
fs.mkdirSync(dirname, mode || 511 /*=0777*/); | ||
try { | ||
fs.mkdirSync(dirname, 448 /*=0700*/); | ||
} catch (e) { | ||
throw e; | ||
} | ||
return dirname; | ||
} | ||
module.exports = { | ||
createFile: createFile, | ||
createFileSync: createFileSync, | ||
createDir: createDir, | ||
createDirSync: createDirSync | ||
}; | ||
module.exports = | ||
(process.env.NODE_ENV === 'test') ? { | ||
createUniqueName: createUniqueName, | ||
createUniqueNameSync: createUniqueNameSync, | ||
createFile: createFile, | ||
createFileSync: createFileSync, | ||
createDir: createDir, | ||
createDirSync: createDirSync | ||
} : { | ||
createFile: createFile, | ||
createFileSync: createFileSync, | ||
createDir: createDir, | ||
createDirSync: createDirSync | ||
}; |
{ | ||
"name": "mktemp", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"author": "sasa+1 <sasaplus1@gmail.com>", | ||
"contributors": [ | ||
"Michael Ficarra <github.public.email@michael.ficarra.me>" | ||
], | ||
"description": "mktemp command for node.js", | ||
@@ -16,3 +19,3 @@ "main": "./index.js", | ||
"lint": "gjslint --strict --nojsdoc -r ./lib", | ||
"test": "./node_modules/.bin/mocha" | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha" | ||
}, | ||
@@ -19,0 +22,0 @@ "devDependencies": { |
@@ -44,5 +44,5 @@ # mktemp [![Build Status](https://travis-ci.org/sasaplus1/mktemp.png)](https://travis-ci.org/sasaplus1/mktemp) | ||
### createFile(path, callback) | ||
### createFile(template, callback) | ||
* `path` string - file path | ||
* `template` string - filename template | ||
* `callback` function(err, path) - callback function | ||
@@ -53,6 +53,7 @@ * `err` - error object | ||
create blank file of random filename. | ||
permission is `0600`. | ||
### createFileSync(path) | ||
### createFileSync(template) | ||
* `path` string - file path | ||
* `template` string - filename template | ||
* `return` string - replaced path | ||
@@ -62,6 +63,5 @@ | ||
### createDir(path, [mode,] callback) | ||
### createDir(template, callback) | ||
* `path` string - dir path | ||
* `mode` number - permission (default: 0777) | ||
* `template` string - dirname template | ||
* `callback` function(err, path) - callback function | ||
@@ -72,7 +72,7 @@ * `err` - error object | ||
create directory of random dirname. | ||
permission is `0700`. | ||
### createDirSync(path, [mode]) | ||
### createDirSync(template) | ||
* `path` string - dir path | ||
* `mode` number - permission (default: 0777) | ||
* `template` string - dirname template | ||
* `return` string - replaced path | ||
@@ -89,4 +89,8 @@ | ||
## Contributors | ||
* [Michael Ficarra](https://github.com/michaelficarra) | ||
## License | ||
The MIT License. Please see LICENSE file. |
@@ -9,4 +9,6 @@ var fs = require('fs'), | ||
suiteSetup(function() { | ||
sinon.stub(fs, 'writeFile').callsArgWith(2, null); | ||
sinon.stub(fs, 'writeFileSync'); | ||
sinon.stub(fs, 'open').callsArgWith(3, null); | ||
sinon.stub(fs, 'close').callsArgWith(1, null); | ||
sinon.stub(fs, 'openSync'); | ||
sinon.stub(fs, 'closeSync'); | ||
sinon.stub(fs, 'mkdir').callsArgWith(2, null); | ||
@@ -17,4 +19,6 @@ sinon.stub(fs, 'mkdirSync'); | ||
suiteTeardown(function() { | ||
fs.writeFile.restore(); | ||
fs.writeFileSync.restore(); | ||
fs.open.restore(); | ||
fs.openSync.restore(); | ||
fs.close.restore(); | ||
fs.closeSync.restore(); | ||
fs.mkdir.restore(); | ||
@@ -21,0 +25,0 @@ fs.mkdirSync.restore(); |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
9918
183
92
3