temp-write
Advanced tools
Comparing version 0.1.1 to 0.2.0
46
index.js
'use strict'; | ||
var path = require('path'); | ||
var tmpdir = require('os').tmpdir(); | ||
var fs = require('graceful-fs'); | ||
var tempfile = require('tempfile'); | ||
var uuid = require('uuid'); | ||
module.exports = function (str, ext, cb) { | ||
if (typeof ext === 'function') { | ||
cb = ext; | ||
ext = null; | ||
function tempfile(filename) { | ||
return path.join(tmpdir, uuid.v4(), (filename || '')); | ||
} | ||
module.exports = function (str, filename, cb) { | ||
if (typeof filename === 'function') { | ||
cb = filename; | ||
filename = null; | ||
} | ||
var filePath = tempfile(ext); | ||
fs.writeFile(filePath, str, function (err) { | ||
cb(err, 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); | ||
}); | ||
}); | ||
}; | ||
module.exports.sync = function (str, ext) { | ||
var filePath = tempfile(ext); | ||
fs.writeFileSync(filePath, str); | ||
return filePath; | ||
module.exports.sync = function (str, filename) { | ||
var filepath = tempfile(filename); | ||
try { | ||
fs.mkdirSync(path.dirname(filepath)); | ||
} catch (err) { | ||
if (err.code !== 'EEXIST') { | ||
throw err; | ||
} | ||
} | ||
fs.writeFileSync(filepath, str); | ||
return filepath; | ||
}; |
{ | ||
"name": "temp-write", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Write String/Buffer to a random temp file", | ||
@@ -27,2 +27,3 @@ "license": "MIT", | ||
"file", | ||
"filename", | ||
"path", | ||
@@ -38,3 +39,4 @@ "random", | ||
"graceful-fs": "~2.0.0", | ||
"tempfile": "~0.1.2" | ||
"uuid": "~1.4.1", | ||
"mkdirp": "~0.3.5" | ||
}, | ||
@@ -41,0 +43,0 @@ "devDependencies": { |
@@ -21,6 +21,6 @@ # temp-write [![Build Status](https://travis-ci.org/sindresorhus/temp-write.png?branch=master)](http://travis-ci.org/sindresorhus/temp-write) | ||
var filePath = tempWrite.sync('unicorn'); | ||
var filepath = tempWrite.sync('unicorn'); | ||
//=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b | ||
fs.readFileSync(filePath, 'utf8'); | ||
fs.readFileSync(filepath, 'utf8'); | ||
//=> unicorn | ||
@@ -32,17 +32,25 @@ ``` | ||
### tempWrite(input, callback) | ||
### tempWrite(input, [filename], callback) | ||
#### input | ||
*Required* | ||
Type: `String`|`Buffer` | ||
#### callback(err, filePath) | ||
#### filename | ||
Type: `String` | ||
Example: `'img.png'` | ||
Optionally supply a custom filename. | ||
#### callback(err, filepath) | ||
*Required* | ||
Type: `Function` | ||
### tempWrite.sync(input) | ||
Type: `String`|`Buffer` | ||
Returns: the file path | ||
Returns: the filepath | ||
@@ -49,0 +57,0 @@ |
2562
35
59
3
+ Addedmkdirp@~0.3.5
+ Addeduuid@~1.4.1
+ Addedmkdirp@0.3.5(transitive)
- Removedtempfile@~0.1.2
- Removedtempfile@0.1.3(transitive)