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();
API
tempy.file([options])
Get a temporary file path you can write to.
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()
Get a temporary directory path. The directory is created for you.
tempy.root
The root temporary directory path. For example: /private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T
FAQ
Why doesn't it have a cleanup method?
The operating system will clean up when needed. No point in us wasting resources and adding complexity.
Related
- temp-write - Write string/buffer/stream to a random temp file
License
MIT © Sindre Sorhus