temp-write
Advanced tools
+16
-26
@@ -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; | ||
| }; |
+4
-4
| { | ||
| "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": { |
+16
-11
@@ -1,2 +0,2 @@ | ||
| # temp-write [](http://travis-ci.org/sindresorhus/temp-write) | ||
| # temp-write [](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
9.06%64
8.47%27
-22.86%Updated
Updated
Updated