Comparing version 0.6.0 to 0.7.0
136
index.d.ts
@@ -38,56 +38,116 @@ /// <reference types="node"/> | ||
}; | ||
/** | ||
The temporary path created by the function. Can be asynchronous. | ||
*/ | ||
type TaskCallback = (tempPath: string) => Promise<void> | void; | ||
} | ||
declare const tempy: { | ||
/** | ||
Get a temporary file path you can write to. | ||
file: { | ||
/** | ||
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
@returns A promise that resolves after the callback is executed and the file is cleaned up. | ||
tempy.file(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
tempy.file({extension: 'png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png' | ||
await tempy.file.task(tempFile => { | ||
console.log(tempFile); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
}); | ||
``` | ||
*/ | ||
task: (callback: tempy.TaskCallback, options?: tempy.FileOptions) => Promise<void>; | ||
tempy.file({name: 'unicorn.png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png' | ||
/** | ||
Get a temporary file path you can write to. | ||
tempy.directory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
file: (options?: tempy.FileOptions) => string; | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
/** | ||
Get a temporary directory path. The directory is created for you. | ||
tempy.file(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
tempy.file({extension: 'png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png' | ||
tempy.directory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
tempy.file({name: 'unicorn.png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png' | ||
tempy.directory({prefix: 'a'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' | ||
``` | ||
*/ | ||
directory: (options?: tempy.DirectoryOptions) => string; | ||
tempy.directory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
(options?: tempy.FileOptions): string; | ||
}; | ||
/** | ||
Write data to a random temp file. | ||
directory: { | ||
/** | ||
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
@returns A promise that resolves after the callback is executed and the directory is cleaned up. | ||
await tempy.write('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
write: (fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions) => Promise<string>; | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
await tempy.directory.task(tempDirectory => { | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
}) | ||
``` | ||
*/ | ||
task: (callback: tempy.TaskCallback, options?: tempy.DirectoryOptions) => Promise<void>; | ||
/** | ||
Get a temporary directory path. The directory is created for you. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
tempy.directory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
tempy.directory({prefix: 'a'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' | ||
``` | ||
*/ | ||
(options?: tempy.DirectoryOptions): string; | ||
}; | ||
write: { | ||
/** | ||
Write data to a random temp file. The file is automatically cleaned up after the callback is executed. | ||
@returns A promise that resolves after the callback is executed and the file is cleaned up. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
await tempy.write.task('🦄', tempFile => { | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
}); | ||
``` | ||
*/ | ||
task: (fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: tempy.TaskCallback, options?: tempy.FileOptions) => Promise<void>; | ||
/** | ||
Write data to a random temp file. | ||
@example | ||
``` | ||
import tempy = require('tempy'); | ||
await tempy.write('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions): Promise<string>; | ||
}; | ||
/** | ||
@@ -94,0 +154,0 @@ Synchronously write data to a random temp file. |
14
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
const isStream = require('is-stream'); | ||
const del = require('del'); | ||
const stream = require('stream'); | ||
@@ -18,2 +19,9 @@ const {promisify} = require('util'); | ||
const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => { | ||
const [callback, options] = arguments_.slice(extraArguments); | ||
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options); | ||
await callback(result); | ||
await del(result, {force: true}); | ||
}; | ||
module.exports.file = options => { | ||
@@ -35,2 +43,4 @@ options = { | ||
module.exports.file.task = createTask(module.exports.file); | ||
module.exports.directory = ({prefix = ''} = {}) => { | ||
@@ -42,2 +52,4 @@ const directory = getPath(prefix); | ||
module.exports.directory.task = createTask(module.exports.directory); | ||
module.exports.write = async (data, options) => { | ||
@@ -50,2 +62,4 @@ const filename = module.exports.file(options); | ||
module.exports.write.task = createTask(module.exports.write, {extraArguments: 1}); | ||
module.exports.writeSync = (data, options) => { | ||
@@ -52,0 +66,0 @@ const filename = module.exports.file(options); |
{ | ||
"name": "tempy", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Get a random temporary file or directory path", | ||
@@ -38,2 +38,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"del": "^5.1.0", | ||
"is-stream": "^2.0.0", | ||
@@ -46,2 +47,4 @@ "temp-dir": "^2.0.0", | ||
"ava": "^2.4.0", | ||
"path-exists": "^4.0.0", | ||
"touch": "^3.1.0", | ||
"tsd": "^0.13.1", | ||
@@ -48,0 +51,0 @@ "xo": "^0.32.1" |
@@ -38,2 +38,12 @@ # tempy [![Build Status](https://travis-ci.com/sindresorhus/tempy.svg?branch=master)](https://travis-ci.com/github/sindresorhus/tempy) | ||
### tempy.file.task(callback, options?) | ||
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves after the callback is executed and the file is cleaned up. | ||
#### callback | ||
Type: `(tempPath: string) => void` | ||
A callback that is executed with the temp file path. Can be asynchronous. | ||
#### options | ||
@@ -57,6 +67,16 @@ | ||
### tempy.directory([options]) | ||
### tempy.directory(options?) | ||
Get a temporary directory path. The directory is created for you. | ||
### tempy.directory.task(callback, options?) | ||
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. Returns a promise that resolves after the callback is executed and the directory is cleaned up. | ||
##### callback | ||
Type: `(tempPath: string) => void` | ||
A callback that is executed with the temp directory path. Can be asynchronous. | ||
#### options | ||
@@ -68,3 +88,2 @@ | ||
Type: `string` | ||
@@ -82,2 +101,6 @@ | ||
### tempy.write.task(fileContent, callback, options?) | ||
Write data to a random temp file. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves after the callback is executed and the file is cleaned up. | ||
##### fileContent | ||
@@ -89,2 +112,8 @@ | ||
##### callback | ||
Type: `(tempPath: string) => void` | ||
A callback that is executed with the temp file path. Can be asynchronous. | ||
##### options | ||
@@ -111,7 +140,1 @@ | ||
Get the root temporary directory path. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T` | ||
## FAQ | ||
#### Why doesn't it have a cleanup method? | ||
Temp files will be periodically cleaned up on macOS. Most Linux distros will clean up on reboot. If you're generating a lot of temp files, it's recommended to use a complementary module like [`del`](https://github.com/sindresorhus/del) for cleanup. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12348
188
135
5
5
+ Addeddel@^5.1.0
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Added@types/glob@7.2.0(transitive)
+ Added@types/minimatch@5.1.2(transitive)
+ Added@types/node@22.10.2(transitive)
+ Addedaggregate-error@3.1.0(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedclean-stack@2.2.0(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddel@5.1.0(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@10.0.2(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedindent-string@4.0.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedis-path-cwd@2.2.0(transitive)
+ Addedis-path-inside@3.0.3(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-map@3.0.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrimraf@3.0.2(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedwrappy@1.0.2(transitive)