npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mitsuki31/temppath

v0.5.0

Published

Multi-platform temporary directories and files generator

Downloads

135

Readme

temppath

A lightweight and multi-platform Node.js module designed to create temporary files and directories. It can utilize the system's default temporary path or a user-specified directory, offering flexibility and ease of use across different environments.

This module leverages system environment variables to determine the temporary directory and will fallback to the built-in function os.tmpdir() if the module is incapable to determine the temporary directory provided by the system.

Here’s a detailed example demonstrating how to create a temporary directory using this module:

const temppath = require('@mitsuki31/temppath');

temppath.createTempPath((err, tempDir) => {
  if (err) console.error('Error creating temporary directory:', err);
  else console.log('Created temporary directory:', tempDir);
});

And here’s how you can create a temporary file:

const temppath = require('@mitsuki31/temppath');

temppath.createTempPath({ asFile: true }, (err, tempFile) => {
  if (err) console.error('Error creating temporary file:', err);
  else console.log('Created temporary file:', tempFile);
});

In the examples above:

  • The first snippet creates a temporary directory within the system's default temporary path.
  • The second snippet creates a temporary file within the same system-based temporary path.

It's that simple! With just a few lines of code, temppath allows you to handle temporary file and directory creation effortlessly.

If you don't like callback-based function and want the createTempPath function as a promise-based function. Try to promisify it using util.promisify function, like this:

const { promisify } = require('node:util');
const temppath = require('@mitsuki31/temppath');
// Promisify the function
const createTempPath = promisify(temppath.createTempPath);

(async () => {  // Need this block if use CommonJS module
  // Create a temporary file in current directory with extension '.foo' and
  // limit the maximum file name's length to 20 characters
  const tempFile = await createTempPath('.', {
    asFile: true,
    ext: 'foo',
    maxLen: 20
  });
  console.log(tempFile);
  // Output: b3du3b2156sb36bo9hgi.foo
})();

About System Temporary Directory

On different operating systems, the path to the temporary directory is specified by various environment variables. These paths are used by the operating system and applications to store temporary files. Below are the details for each operating system:

Linux and macOS

On Linux and macOS systems, the temporary directory path is provided by the environment variable TMPDIR. This variable is widely recognized and used by applications for temporary storage. If TMPDIR is not set, applications may fallback to using standard directories such as /tmp.

You can check the temporary path provided by the system echoing the TMPDIR environment variable:

echo $TMPDIR

Windows

On Windows systems, there are two primary environment variables that typically store the path to the temporary directory: TEMP and TMP. Both variables usually point to the same directory and are used interchangeably by the operating system and applications to store temporary files.

You can check the temporary path provided by the system using this command:

echo $env:TMP
echo $env:TEMP

If you're using Command Prompt try this instead:

echo %TMP%
echo %TEMP%

In summary, these environment variables ensure that temporary files are stored in a standard location that can be easily accessed and cleaned up by the system or applications.

APIs

getTempPath (Function)

function getTempPath(tmpdir?: string, maxLen?: number = 32): string

Generates a temporary path based on the provided or system temporary directory.

This function utilizes a random UUID for the directory name, ensuring that each time it is called, the path will be different from previous calls. The returned path can be used for either a temporary file or directory, according to your preferences.

Parameters

| Name | Type | Description | | ---- | ---- | ----------- | | tmpdir | string | The temporary directory to be used. If not provided or empty, it defaults to the system's temporary directory. | | maxLen | number | The maximum characters' length of the generated temporary path. |

Returns

A string representing a generated temporary path with a random UUID that can be used as temporary file or directory name.

Type: string

Throws

  • TypeError
    Throws a TypeError if the provided tmpdir is not a string.

  • RangeError
    If the given maxLen is less than or equal to zero.

createTempPath (Function)

function createTempPath(
  tmpdir?: string | TempPathOptions | CreateTempPathCallback,
  options?: TempPathOptions | CreateTempPathCallback,
  callback: CreateTempPathCallback
): void

Asynchronously creates a temporary path, either as a directory or file, based on the provided or system temporary directory.

This function utilizes the getTempPath function.

Parameters

| Name | Type | Description | | ---- | ---- | ----------- | | tmpdir | string | TempPathOptions | CreateTempPathCallback | The temporary directory path. If an object is provided, it is treated as the options parameter. If a function is provided, it is treated as the callback parameter, and tmpdir will fallback to the temporary directory provided by the system. | | options | TempPathOptions | CreateTempPathCallback | Options for creating the temporary path. If a function is provided, it is treated as the callback parameter, and options is set to {} (an empty object). | | callback | CreateTempPathCallback | A callback function to handle the result path or error. This is crucial and required, even when you wanted to omit all arguments.

Throws

  • TypeError
    If the given arguments or the extension name specified with incorrect type.

createTempPathSync (Function)

function createTempPathSync(
  tmpdir?: string | TempPathOptions,
  options?: TempPathOptions
): string

Synchronously creates a temporary path, either as a directory or file, based on the provided or system temporary directory and then returns a path that refers to the generated temporary directory or file.

This function utilizes the getTempPath function.

Parameters

| Name | Type | Description | | ---- | ---- | ----------- | | tmpdir | string | TempPathOptions | CreateTempPathCallback | The temporary directory path. If an object is provided, it is treated as the options parameter, and tmpdir will fallback to the temporary directory provided by the system. | | options | TempPathOptions | CreateTempPathCallback | Options for creating the temporary path. |

Returns

A string representating the path of the created temporary directory or file.

Type: string

Throws

  • TypeError
    If the given arguments or the extension name specified with incorrect type.

  • Error
    Throws an Error if there is an issue creating the temporary directory or file.

Development

Initialize and Install Dependencies

npm install

Build JSDoc

Generated JSDocs will be in docs directory.

npm run build:docs

Test

Tests are written using inbuilt node:assert module.

npm test

Alternatively, you can run each test manually.

  • Run CommonJS test.

    npm run test:cjs
  • Run ESModule test.

    npm run test:esm

License

This project is licensed under the MIT License. For more details, see the LICENSE file.