tempy
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,2 +0,3 @@ | ||
import {MergeExclusive} from 'type-fest'; | ||
/// <reference types="node"/> | ||
import {MergeExclusive, TypedArray} from 'type-fest'; | ||
@@ -65,2 +66,28 @@ declare namespace tempy { | ||
/** | ||
Write data to a random temp file. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
await tempy.write('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
write(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.Options): Promise<string>; | ||
/** | ||
Synchronously write data to a random temp file. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
tempy.writeSync('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
writeSync(fileContent: string | Buffer | TypedArray | DataView, options?: tempy.Options): string; | ||
/** | ||
Get the root temporary directory path. | ||
@@ -67,0 +94,0 @@ |
21
index.js
@@ -6,5 +6,13 @@ 'use strict'; | ||
const tempDir = require('temp-dir'); | ||
const isStream = require('is-stream'); | ||
const stream = require('stream'); | ||
const {promisify} = require('util'); | ||
const pipeline = promisify(stream.pipeline); | ||
const {writeFile} = fs.promises; | ||
const getPath = () => path.join(tempDir, uniqueString()); | ||
const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStream(filePath)); | ||
module.exports.file = options => { | ||
@@ -32,2 +40,15 @@ options = { | ||
module.exports.write = async (data, options) => { | ||
const filename = module.exports.file(options); | ||
const write = isStream(data) ? writeStream : writeFile; | ||
await write(filename, data); | ||
return filename; | ||
}; | ||
module.exports.writeSync = (data, options) => { | ||
const filename = module.exports.file(options); | ||
fs.writeFileSync(filename, data); | ||
return filename; | ||
}; | ||
Object.defineProperty(module.exports, 'root', { | ||
@@ -34,0 +55,0 @@ get() { |
{ | ||
"name": "tempy", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Get a random temporary file or directory path", | ||
@@ -11,3 +11,3 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -39,4 +39,5 @@ "engines": { | ||
"dependencies": { | ||
"is-stream": "^2.0.0", | ||
"temp-dir": "^2.0.0", | ||
"type-fest": "^0.10.0", | ||
"type-fest": "^0.12.0", | ||
"unique-string": "^2.0.0" | ||
@@ -43,0 +44,0 @@ }, |
@@ -57,2 +57,30 @@ # tempy [![Build Status](https://travis-ci.org/sindresorhus/tempy.svg?branch=master)](https://travis-ci.org/sindresorhus/tempy) | ||
### tempy.write(fileContent, options?) | ||
Write data to a random temp file. | ||
##### fileContent | ||
Type: `string | Buffer | TypedArray | DataView | stream.Readable` | ||
Data to write to the temp file. | ||
##### options | ||
See [options](#options). | ||
### tempy.writeSync(fileContent, options?) | ||
Synchronously write data to a random temp file. | ||
##### fileContent | ||
Type: `string | Buffer | TypedArray | DataView` | ||
Data to write to the temp file. | ||
##### options | ||
See [options](#options). | ||
### tempy.root | ||
@@ -59,0 +87,0 @@ |
Sorry, the diff of this file is not supported yet
7946
121
94
4
+ Addedis-stream@^2.0.0
+ Addedis-stream@2.0.1(transitive)
+ Addedtype-fest@0.12.0(transitive)
- Removedtype-fest@0.10.0(transitive)
Updatedtype-fest@^0.12.0