temp-write
Advanced tools
+8
-10
@@ -7,2 +7,3 @@ 'use strict'; | ||
| var uuid = require('uuid'); | ||
| var pify = require('pify'); | ||
| var TMP_DIR = osTmpdir(); | ||
@@ -14,15 +15,12 @@ | ||
| module.exports = function (str, filepath, cb) { | ||
| if (typeof filepath === 'function') { | ||
| cb = filepath; | ||
| filepath = null; | ||
| } | ||
| module.exports = function (str, filepath) { | ||
| var fullpath = tempfile(filepath); | ||
| mkdirp(path.dirname(fullpath), function (err) { | ||
| fs.writeFile(fullpath, str, function (err) { | ||
| cb(err, fullpath); | ||
| return pify(mkdirp)(path.dirname(fullpath)) | ||
| .then(function () { | ||
| return pify(fs.writeFile)(fullpath, str); | ||
| }) | ||
| .then(function () { | ||
| return fullpath; | ||
| }); | ||
| }); | ||
| }; | ||
@@ -29,0 +27,0 @@ |
+11
-4
| { | ||
| "name": "temp-write", | ||
| "version": "1.1.2", | ||
| "version": "2.0.0", | ||
| "description": "Write string/buffer to a random temp file", | ||
@@ -10,3 +10,3 @@ "license": "MIT", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "http://sindresorhus.com" | ||
| "url": "sindresorhus.com" | ||
| }, | ||
@@ -17,3 +17,3 @@ "engines": { | ||
| "scripts": { | ||
| "test": "mocha" | ||
| "test": "xo && ava" | ||
| }, | ||
@@ -42,7 +42,14 @@ "files": [ | ||
| "os-tmpdir": "^1.0.0", | ||
| "pify": "^2.2.0", | ||
| "uuid": "^2.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "mocha": "*" | ||
| "ava": "*", | ||
| "xo": "*" | ||
| }, | ||
| "xo": { | ||
| "ignores": [ | ||
| "test.js" | ||
| ] | ||
| } | ||
| } |
+17
-17
@@ -8,3 +8,3 @@ # temp-write [](https://travis-ci.org/sindresorhus/temp-write) | ||
| ```sh | ||
| ``` | ||
| $ npm install --save temp-write | ||
@@ -17,17 +17,17 @@ ``` | ||
| ```js | ||
| var fs = require('fs'); | ||
| var tempWrite = require('temp-write'); | ||
| const fs = require('fs'); | ||
| const tempWrite = require('temp-write'); | ||
| var filepath = tempWrite.sync('unicorn'); | ||
| //=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b | ||
| const filepath = tempWrite.sync('unicorn'); | ||
| //=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b' | ||
| fs.readFileSync(filepath, 'utf8'); | ||
| //=> unicorn | ||
| //=> 'unicorn' | ||
| tempWrite.sync('unicorn', 'pony.png'); | ||
| //=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/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 | ||
| //=> '/var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/rainbow/cake/pony.png' | ||
| ``` | ||
@@ -38,7 +38,12 @@ | ||
| ### tempWrite(input, [filepath], callback) | ||
| ### tempWrite(input, [filepath]) | ||
| Returns a promise that resolves to the filepath of the temp file. | ||
| ### tempWrite.sync(input, [filepath]) | ||
| Returns the filepath of the temp file. | ||
| #### input | ||
| *Required* | ||
| Type: `string`, `buffer` | ||
@@ -53,15 +58,10 @@ | ||
| #### callback(err, filepath) | ||
| *Required* | ||
| Type: `function` | ||
| ## Related | ||
| ### tempWrite.sync(input) | ||
| - [tempfile](https://github.com/sindresorhus/tempfile) - Get a random temp file path | ||
| Type: `string`, `buffer` | ||
| Returns: the filepath | ||
| ## License | ||
| MIT © [Sindre Sorhus](http://sindresorhus.com) |
4092
4.15%5
25%2
100%27
-3.57%+ Added
+ Added