Comparing version 2.0.0 to 3.0.0
188
index.d.ts
@@ -1,2 +0,1 @@ | ||
/* eslint-disable @typescript-eslint/member-ordering */ | ||
import {Buffer} from 'node:buffer'; | ||
@@ -44,130 +43,115 @@ import {MergeExclusive, TypedArray} from 'type-fest'; | ||
declare const tempy: { | ||
file: { | ||
/** | ||
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. | ||
/** | ||
Get a temporary file path you can write to. | ||
@returns A promise that resolves after the callback is executed and the file is cleaned up. | ||
@example | ||
``` | ||
import {temporaryFile, temporaryDirectory} from 'tempy'; | ||
@example | ||
``` | ||
import tempy from 'tempy'; | ||
temporaryFile(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
await tempy.file.task(tempFile => { | ||
console.log(tempFile); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
}); | ||
``` | ||
*/ | ||
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>; | ||
temporaryFile({extension: 'png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png' | ||
/** | ||
Get a temporary file path you can write to. | ||
temporaryFile({name: 'unicorn.png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png' | ||
@example | ||
``` | ||
import tempy from 'tempy'; | ||
temporaryDirectory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
export function temporaryFile(options?: FileOptions): string; | ||
tempy.file(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
/** | ||
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. | ||
tempy.file({extension: 'png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png' | ||
@returns A promise that resolves after the callback is executed and the file is cleaned up. | ||
tempy.file({name: 'unicorn.png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png' | ||
@example | ||
``` | ||
import {temporaryFileTask} from 'tempy'; | ||
tempy.directory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
(options?: FileOptions): string; | ||
}; | ||
await temporaryFileTask(tempFile => { | ||
console.log(tempFile); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
}); | ||
``` | ||
*/ | ||
export function temporaryFileTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise <ReturnValueType>; | ||
directory: { | ||
/** | ||
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. | ||
/** | ||
Get a temporary directory path. The directory is created for you. | ||
@returns A promise that resolves after the callback is executed and the directory is cleaned up. | ||
@example | ||
``` | ||
import {temporaryDirectory} from 'tempy'; | ||
@example | ||
``` | ||
import tempy from 'tempy'; | ||
temporaryDirectory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
await tempy.directory.task(tempDirectory => { | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
}) | ||
``` | ||
*/ | ||
task: <ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions) => Promise<ReturnValueType>; | ||
temporaryDirectory({prefix: 'a'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' | ||
``` | ||
*/ | ||
export function temporaryDirectory(options?: DirectoryOptions): string; | ||
/** | ||
Get a temporary directory path. The directory is created for you. | ||
/** | ||
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 from 'tempy'; | ||
@returns A promise that resolves after the callback is executed and the directory is cleaned up. | ||
tempy.directory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
@example | ||
``` | ||
import {temporaryDirectoryTask} from 'tempy'; | ||
tempy.directory({prefix: 'a'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' | ||
``` | ||
*/ | ||
(options?: DirectoryOptions): string; | ||
}; | ||
await temporaryDirectoryTask(tempDirectory => { | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
}) | ||
``` | ||
*/ | ||
export function temporaryDirectoryTask<ReturnValueType>(callback: TaskCallback<ReturnValueType>, options?: DirectoryOptions): Promise<ReturnValueType>; | ||
write: { | ||
/** | ||
Write data to a random temp file. The file is automatically cleaned up after the callback is executed. | ||
/** | ||
Write data to a random temp file. | ||
@returns A promise that resolves after the callback is executed and the file is cleaned up. | ||
@example | ||
``` | ||
import {temporaryWrite} from 'tempy'; | ||
@example | ||
``` | ||
import tempy from 'tempy'; | ||
await temporaryWrite('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
export function temporaryWrite(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>; | ||
await tempy.write.task('🦄', tempFile => { | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
}); | ||
``` | ||
*/ | ||
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions) => Promise<ReturnValueType>; | ||
/** | ||
Write data to a random temp file. The file is automatically cleaned up after the callback is executed. | ||
/** | ||
Write data to a random temp file. | ||
@returns A promise that resolves after the callback is executed and the file is cleaned up. | ||
@example | ||
``` | ||
import tempy from 'tempy'; | ||
@example | ||
``` | ||
import {temporaryWriteTask} from 'tempy'; | ||
await tempy.write('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: FileOptions): Promise<string>; | ||
}; | ||
await temporaryWriteTask('🦄', tempFile => { | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
}); | ||
``` | ||
*/ | ||
export function temporaryWriteTask<ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: TaskCallback<ReturnValueType>, options?: FileOptions): Promise<ReturnValueType>; | ||
/** | ||
Synchronously write data to a random temp file. | ||
/** | ||
Synchronously write data to a random temp file. | ||
@example | ||
``` | ||
import tempy from 'tempy'; | ||
@example | ||
``` | ||
import {temporaryWriteSync} from 'tempy'; | ||
tempy.writeSync('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions) => string; | ||
temporaryWriteSync('🦄'); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
``` | ||
*/ | ||
export function temporaryWriteSync(fileContent: string | Buffer | TypedArray | DataView, options?: FileOptions): string; | ||
/** | ||
Get the root temporary directory path. | ||
For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`. | ||
*/ | ||
readonly root: string; | ||
}; | ||
export default tempy; | ||
export {default as rootTemporaryDirectory} from 'temp-dir'; |
69
index.js
@@ -1,2 +0,3 @@ | ||
import fs, {promises as fsPromises} from 'node:fs'; | ||
import fs from 'node:fs'; | ||
import fsPromises from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
@@ -8,3 +9,2 @@ import stream from 'node:stream'; | ||
import {isStream} from 'is-stream'; | ||
import del from 'del'; // TODO: Replace this with `fs.rm` when targeting Node.js 14. | ||
@@ -17,62 +17,47 @@ const pipeline = promisify(stream.pipeline); // TODO: Use `node:stream/promises` when targeting Node.js 16. | ||
const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => { | ||
const [callback, options] = arguments_.slice(extraArguments); | ||
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options); | ||
async function runTask(temporaryPath, callback) { | ||
try { | ||
return await callback(result); | ||
return await callback(temporaryPath); | ||
} finally { | ||
await del(result, {force: true}); | ||
await fsPromises.rm(temporaryPath, {recursive: true, force: true}); | ||
} | ||
}; | ||
} | ||
const tempy = {}; | ||
tempy.file = options => { | ||
options = { | ||
...options, | ||
}; | ||
if (options.name) { | ||
if (options.extension !== undefined && options.extension !== null) { | ||
export function temporaryFile({name, extension} = {}) { | ||
if (name) { | ||
if (extension !== undefined && extension !== null) { | ||
throw new Error('The `name` and `extension` options are mutually exclusive'); | ||
} | ||
return path.join(tempy.directory(), options.name); | ||
return path.join(temporaryDirectory(), name); | ||
} | ||
return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, '')); | ||
}; | ||
return getPath() + (extension === undefined || extension === null ? '' : '.' + extension.replace(/^\./, '')); | ||
} | ||
tempy.file.task = createTask(tempy.file); | ||
export const temporaryFileTask = async (callback, options) => runTask(temporaryFile(options), callback); | ||
tempy.directory = ({prefix = ''} = {}) => { | ||
export function temporaryDirectory({prefix = ''} = {}) { | ||
const directory = getPath(prefix); | ||
fs.mkdirSync(directory); | ||
return directory; | ||
}; | ||
} | ||
tempy.directory.task = createTask(tempy.directory); | ||
export const temporaryDirectoryTask = async (callback, options) => runTask(temporaryDirectory(options), callback); | ||
tempy.write = async (data, options) => { | ||
const filename = tempy.file(options); | ||
const write = isStream(data) ? writeStream : fsPromises.writeFile; | ||
await write(filename, data); | ||
export async function temporaryWrite(fileContent, options) { | ||
const filename = temporaryFile(options); | ||
const write = isStream(fileContent) ? writeStream : fsPromises.writeFile; | ||
await write(filename, fileContent); | ||
return filename; | ||
}; | ||
} | ||
tempy.write.task = createTask(tempy.write, {extraArguments: 1}); | ||
export const temporaryWriteTask = async (fileContent, callback, options) => runTask(await temporaryWrite(fileContent, options), callback); | ||
tempy.writeSync = (data, options) => { | ||
const filename = tempy.file(options); | ||
fs.writeFileSync(filename, data); | ||
export function temporaryWriteSync(fileContent, options) { | ||
const filename = temporaryFile(options); | ||
fs.writeFileSync(filename, fileContent); | ||
return filename; | ||
}; | ||
} | ||
Object.defineProperty(tempy, 'root', { | ||
get() { | ||
return tempDir; | ||
}, | ||
}); | ||
export default tempy; | ||
export {default as rootTemporaryDirectory} from 'temp-dir'; |
{ | ||
"name": "tempy", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Get a random temporary file or directory path", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
"node": ">=14.16" | ||
}, | ||
@@ -41,15 +41,15 @@ "scripts": { | ||
"dependencies": { | ||
"del": "^6.0.0", | ||
"is-stream": "^3.0.0", | ||
"temp-dir": "^2.0.0", | ||
"type-fest": "^2.0.0", | ||
"type-fest": "^2.12.2", | ||
"unique-string": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^4.0.0-alpha.2", | ||
"@types/node": "^17.0.24", | ||
"ava": "^4.2.0", | ||
"path-exists": "^5.0.0", | ||
"touch": "^3.1.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
"tsd": "^0.20.0", | ||
"xo": "^0.48.0" | ||
} | ||
} |
@@ -7,5 +7,5 @@ # tempy | ||
```sh | ||
npm install tempy | ||
``` | ||
$ npm install tempy | ||
``` | ||
@@ -15,17 +15,17 @@ ## Usage | ||
```js | ||
import tempy from 'tempy'; | ||
import {temporaryFile, temporaryDirectory} from 'tempy'; | ||
tempy.file(); | ||
temporaryFile(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd' | ||
tempy.file({extension: 'png'}); | ||
temporaryFile({extension: 'png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png' | ||
tempy.file({name: 'unicorn.png'}); | ||
temporaryFile({name: 'unicorn.png'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png' | ||
tempy.directory(); | ||
temporaryDirectory(); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6' | ||
tempy.directory({prefix: 'name'}); | ||
temporaryDirectory({prefix: 'name'}); | ||
//=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41' | ||
@@ -36,7 +36,7 @@ ``` | ||
### tempy.file(options?) | ||
### temporaryFile(options?) | ||
Get a temporary file path you can write to. | ||
### tempy.file.task(callback, options?) | ||
### temporaryFileTask(callback, options?) | ||
@@ -69,7 +69,7 @@ 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 with the return value of the callback after it is executed and the file is cleaned up. | ||
### tempy.directory(options?) | ||
### temporaryDirectory(options?) | ||
Get a temporary directory path. The directory is created for you. | ||
### tempy.directory.task(callback, options?) | ||
### temporaryDirectoryTask(callback, options?) | ||
@@ -98,7 +98,7 @@ 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 with the return value of the callback after it is executed and the directory is cleaned up. | ||
### tempy.write(fileContent, options?) | ||
### temporaryWrite(fileContent, options?) | ||
Write data to a random temp file. | ||
### tempy.write.task(fileContent, callback, options?) | ||
### temporaryWriteTask(fileContent, callback, options?) | ||
@@ -123,3 +123,3 @@ Write data to a random temp file. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up. | ||
### tempy.writeSync(fileContent, options?) | ||
### temporaryWriteSync(fileContent, options?) | ||
@@ -138,4 +138,4 @@ Synchronously write data to a random temp file. | ||
### tempy.root | ||
### rootTemporaryDirectory | ||
Get the root temporary directory path. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T` |
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
4
12388
6
165
- Removeddel@^6.0.0
- Removed@nodelib/fs.scandir@2.1.5(transitive)
- Removed@nodelib/fs.stat@2.0.5(transitive)
- Removed@nodelib/fs.walk@1.2.8(transitive)
- Removedaggregate-error@3.1.0(transitive)
- Removedarray-union@2.1.0(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbraces@3.0.3(transitive)
- Removedclean-stack@2.2.0(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddel@6.1.1(transitive)
- Removeddir-glob@3.0.1(transitive)
- Removedfast-glob@3.3.2(transitive)
- Removedfastq@1.17.1(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedglob-parent@5.1.2(transitive)
- Removedglobby@11.1.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedignore@5.3.2(transitive)
- Removedindent-string@4.0.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-number@7.0.0(transitive)
- Removedis-path-cwd@2.2.0(transitive)
- Removedis-path-inside@3.0.3(transitive)
- Removedmerge2@1.4.1(transitive)
- Removedmicromatch@4.0.8(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-map@4.0.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-type@4.0.0(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedqueue-microtask@1.2.3(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedrun-parallel@1.2.0(transitive)
- Removedslash@3.0.0(transitive)
- Removedto-regex-range@5.0.1(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedtype-fest@^2.12.2