temppath

Description:
  • Main entry of temppath module.

Source:
Author:
  • Ryuu Mitsuki

Main entry of temppath module.

Methods

(async, inner) createTempPath(tmpdiropt, optionsopt, callback)

Description:
  • 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. To limit the characters' length of generated temporary directory or file name, set the options.maxLen to the desired value and it must be a positive number. Thus, the function will trim the name to the desired maximum length.

Source:
Since:
  • 0.2.0
Examples

Only specify a callback function

// Without any argument but a callback function
// This will create a temporary directory in system's temporary path
createTempPath(function (err, createdPath) {
  if (err) console.error(err);
  else console.log(createdPath);
  // Unix: "$TMPDIR/<TEMPPATH_DIR>"
  // Termux Android: "$TMPDIR/<TEMPPATH_DIR>" or "$PREFIX/tmp/<TEMPPATH_DIR>"
  // Windows: "%TMP%\<TEMPPATH_DIR>" or "%TEMP%\<TEMPPATH_DIR>"
});

Create a temporary file in system's temporary path

// This will create a temporary file in system's temporary path
// with extension name of '.foo_temp'
createTempPath({ asFile: true, ext: 'foo_temp' }, function (err, createdPath) {
  if (err) console.error(err);
  else console.log(createdPath);
  // Unix: "$TMPDIR/<TEMPPATH_FILE>.foo_temp"
  // Termux Android: "$TMPDIR/<TEMPPATH_FILE>.foo_temp" or "$PREFIX/tmp/<TEMPPATH_FILE>.foo_temp"
  // Windows: "%TMP%\<TEMPPATH_FILE>.foo_temp" or "%TEMP%\<TEMPPATH_FILE>.foo_temp"
});

Create a temporary file in current directory

const path = require('node:path');
// For ESM: import path from 'node:path';

// Use `process.cwd()` to get current directory path
createTempPath(path.join(process.cwd(), 'tmp'), { asFile: true }, function (err, createdPath) {
  if (err) console.error(err);
  else console.log(createdPath);
  // Unix: "$PWD/tmp/<TEMPPATH_FILE>.tmp"
  // Termux Android: "$PWD/tmp/<TEMPPATH_FILE>.tmp"
  // Windows: "%CD%\tmp\<TEMPPATH_FILE>.tmp"
});
Parameters:
Name Type Attributes Description
tmpdir string | TempPathOptions | CreateTempPathCallback <optional>

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 system-based temporary directory.

options TempPathOptions | CreateTempPathCallback <optional>

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).

Properties
Name Type Attributes Default Description
asFile boolean <optional>
false

If true, create a temporary file. Otherwise, create a directory.

ext string <optional>
'.tmp'

The extension for the temporary file. If asFile option is false, this option will be ignored. Default is '.tmp'.

maxLen number <optional>
32

The maximum characters' length of the generated directory or file name. Defaults to 32 characters.

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:

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

Type
TypeError

(inner) createTempPathSync(tmpdiropt, optionsopt) → {string}

Description:
  • 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. To limit the characters' length of generated temporary directory or file name, set the options.maxLen to the desired value and it must be a positive number. Thus, the function will trim the name to the desired maximum length.

Source:
Since:
  • 0.2.0
Examples

Call the function without any argument

// If no argument specified, it will creates a temporary directory
// in system's temporary path
const tmpDirPath = createTempPathSync();
console.log(tmpDirPath);
// Unix: "$TMPDIR/<TEMPPATH_DIR>"
// Termux Android: "$TMPDIR/<TEMPPATH_DIR>" or "$PREFIX/tmp/<TEMPPATH_DIR>"
// Windows: "%TMP%\<TEMPPATH_DIR>" or "%TEMP%\<TEMPPATH_DIR>"

Create a temporary file with custom extension

const tmpFilePath = createTempPathSync({ asFile: true, ext: 'txt' });
console.log(tmpFilePath);
// Unix: "$TMPDIR/<TEMPPATH_FILE>.txt"
// Termux Android: "$TMPDIR/<TEMPPATH_FILE>.txt" or "$PREFIX/tmp/<TEMPPATH_FILE>.txt"
// Windows: "%TMP%\<TEMPPATH_FILE>.txt" or "%TEMP%\<TEMPPATH_FILE>.txt"

Create a temporary file in current directory

const path = require('node:path');
// For ESM: import path from 'node:path';

// Use `process.cwd()` to get current directory path
const customTmpFilePath = createTempPathSync(
  path.join(process.cwd(), 'tmp'), { asFile: true });
console.log(customTmpFilePath);
// Unix: "$PWD/tmp/<TEMPPATH_FILE>.tmp"
// Termux Android: "$PWD/tmp/<TEMPPATH_FILE>.tmp"
// Windows: "%CD%\tmp\<TEMPPATH_FILE>.tmp"
Parameters:
Name Type Attributes Description
tmpdir string | TempPathOptions <optional>

The temporary directory path. If an object is provided, it is treated as the options parameter, and tmpdir will fallback to system-based temporary directory.

options Object <optional>

Options for creating the temporary path.

Properties
Name Type Attributes Default Description
asFile boolean <optional>
false

If true, create a temporary file. Otherwise, create a directory.

ext string <optional>
'.tmp'

The extension for the temporary file. If asFile option is false, this option will be ignored. Default is '.tmp'.

maxLen number <optional>
32

The maximum characters' length of the generated directory or file name. Defaults to 32 characters.

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

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

    Type
    Error
Returns:

The path of the created temporary directory or file.

Type
string

(inner) getTempPath(tmpdiropt, maxLenopt) → {string}

Description:
  • 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 user preferences.

    To limit the characters' length of generated temporary directory or file name, set the maxLen to the desired value and it must be a positive number. Thus, the function will trim the name to the desired maximum length.

Source:
Since:
  • 0.1.0
Parameters:
Name Type Attributes Default Description
tmpdir string <optional>

The temporary directory path. If not provided or empty, it defaults to the system's temporary directory.

maxLen number <optional>
32

The maximum characters' length of the generated temporary path. Must be a positive number and greater than zero.

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

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

    Type
    RangeError
Returns:

The generated temporary path.

Type
string