What is tempy?
Tempy is a utility package for creating temporary files and directories. It provides a simple API to generate temporary paths, files, and directories that are automatically cleaned up when no longer needed.
What are tempy's main functionalities?
Create a temporary file
This feature allows you to create a temporary file. The `tempy.file()` method generates a unique file path in the system's temporary directory.
const tempy = require('tempy');
const tempFilePath = tempy.file();
console.log(tempFilePath);
Create a temporary directory
This feature allows you to create a temporary directory. The `tempy.directory()` method generates a unique directory path in the system's temporary directory.
const tempy = require('tempy');
const tempDirPath = tempy.directory();
console.log(tempDirPath);
Create a temporary file with a specific extension
This feature allows you to create a temporary file with a specific extension. The `tempy.file({ extension: 'txt' })` method generates a unique file path with the specified extension in the system's temporary directory.
const tempy = require('tempy');
const tempFilePath = tempy.file({ extension: 'txt' });
console.log(tempFilePath);
Create a temporary directory with a specific name
This feature allows you to create a temporary directory with a specific name. The `tempy.directory({ name: 'my-temp-dir' })` method generates a unique directory path with the specified name in the system's temporary directory.
const tempy = require('tempy');
const tempDirPath = tempy.directory({ name: 'my-temp-dir' });
console.log(tempDirPath);
Other packages similar to tempy
tmp
The 'tmp' package provides similar functionality for creating temporary files and directories. It offers more control over the lifecycle of temporary files and directories, including the ability to set custom cleanup callbacks.
fs-extra
The 'fs-extra' package extends the native Node.js 'fs' module with additional methods, including methods for creating temporary files and directories. It is a more general-purpose file system utility library compared to tempy.
temp
The 'temp' package is another utility for creating temporary files and directories. It provides a simple API and automatic cleanup, similar to tempy, but with a focus on ease of use and minimal configuration.
tempy
Get a random temporary file or directory path
Install
$ npm install tempy
Usage
const tempy = require('tempy');
tempy.file();
tempy.file({extension: 'png'});
tempy.file({name: 'unicorn.png'});
tempy.directory();
tempy.directory({prefix: 'name'});
API
tempy.file(options?)
Get a temporary file path you can write to.
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
Type: object
You usually won't need either the extension
or name
option. Specify them only when actually needed.
extension
Type: string
File extension.
name
Type: string
Filename. Mutually exclusive with the extension
option.
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
Type: Object
prefix
Type: string
Directory prefix.
Useful for testing by making it easier to identify cache directories that are created.
You usually won't need this option. Specify it only when actually needed.
tempy.write(fileContent, options?)
Write data to a random temp file.
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
Type: string | Buffer | TypedArray | DataView | stream.Readable
Data to write to the temp file.
callback
Type: (tempPath: string) => void
A callback that is executed with the temp file path. Can be asynchronous.
options
See 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.
tempy.root
Get the root temporary directory path. For example: /private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T