temp-write
Advanced tools
Comparing version 0.2.0 to 0.3.0
42
index.js
@@ -5,24 +5,20 @@ 'use strict'; | ||
var fs = require('graceful-fs'); | ||
var mkdirp = require('mkdirp'); | ||
var uuid = require('uuid'); | ||
function tempfile(filename) { | ||
return path.join(tmpdir, uuid.v4(), (filename || '')); | ||
function tempfile(filepath) { | ||
return path.join(tmpdir, uuid.v4(), (filepath || '')); | ||
} | ||
module.exports = function (str, filename, cb) { | ||
if (typeof filename === 'function') { | ||
cb = filename; | ||
filename = null; | ||
module.exports = function (str, filepath, cb) { | ||
if (typeof filepath === 'function') { | ||
cb = filepath; | ||
filepath = null; | ||
} | ||
var fullpath = tempfile(filepath); | ||
var filepath = tempfile(filename); | ||
fs.mkdir(path.dirname(filepath), function (err) { | ||
if (err && err.code !== 'EEXIST') { | ||
return cb(err); | ||
} | ||
fs.writeFile(filepath, str, function (err) { | ||
cb(err, filepath); | ||
mkdirp(path.dirname(fullpath), function (err) { | ||
fs.writeFile(fullpath, str, function (err) { | ||
cb(err, fullpath); | ||
}); | ||
@@ -32,15 +28,9 @@ }); | ||
module.exports.sync = function (str, filename) { | ||
var filepath = tempfile(filename); | ||
module.exports.sync = function (str, filepath) { | ||
var fullpath = tempfile(filepath); | ||
try { | ||
fs.mkdirSync(path.dirname(filepath)); | ||
} catch (err) { | ||
if (err.code !== 'EEXIST') { | ||
throw err; | ||
} | ||
} | ||
mkdirp.sync(path.dirname(fullpath)); | ||
fs.writeFileSync(fullpath, str); | ||
fs.writeFileSync(filepath, str); | ||
return filepath; | ||
return fullpath; | ||
}; |
{ | ||
"name": "temp-write", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Write String/Buffer to a random temp file", | ||
@@ -37,5 +37,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"graceful-fs": "~2.0.0", | ||
"uuid": "~1.4.1", | ||
"mkdirp": "~0.3.5" | ||
"graceful-fs": "^2.0.0", | ||
"mkdirp": "^0.3.5", | ||
"uuid": "^1.4.1" | ||
}, | ||
@@ -42,0 +42,0 @@ "devDependencies": { |
@@ -1,2 +0,2 @@ | ||
# temp-write [![Build Status](https://travis-ci.org/sindresorhus/temp-write.png?branch=master)](http://travis-ci.org/sindresorhus/temp-write) | ||
# temp-write [![Build Status](https://travis-ci.org/sindresorhus/temp-write.svg?branch=master)](https://travis-ci.org/sindresorhus/temp-write) | ||
@@ -8,10 +8,8 @@ > Write String/Buffer to a random temp file | ||
Install with [npm](https://npmjs.org/package/temp-write) | ||
```bash | ||
$ npm install --save temp-write | ||
``` | ||
npm install --save temp-write | ||
``` | ||
## Example | ||
## Usage | ||
@@ -27,2 +25,9 @@ ```js | ||
//=> unicorn | ||
tempWrite.sync('unicorn', 'pony.png'); | ||
//=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/pony.png | ||
tempWrite.sync('unicorn', 'rainbow/cake/pony.png'); | ||
//=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/rainbow/cake/pony.png | ||
``` | ||
@@ -33,3 +38,3 @@ | ||
### tempWrite(input, [filename], callback) | ||
### tempWrite(input, [filepath], callback) | ||
@@ -41,8 +46,8 @@ #### input | ||
#### filename | ||
#### filepath | ||
Type: `String` | ||
Example: `'img.png'` | ||
Example: `'img.png'`, `'foo/bar/baz.png'` | ||
Optionally supply a custom filename. | ||
Optionally supply a filepath which is appended to the random path. | ||
@@ -62,2 +67,2 @@ #### callback(err, filepath) | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) |
2794
64
27
Updatedgraceful-fs@^2.0.0
Updatedmkdirp@^0.3.5
Updateduuid@^1.4.1