パッケージの詳細

mktemp

sasaplus12.9mMIT2.0.1

create temporary files and directories

readme

mktemp

create temporary files and directories

Installation

$ npm install mktemp

Usage

import * as os from 'node:os';
import * as path from 'node:path';
import * as mktemp from 'mktemp';

const tempDir = os.tmpdir();

mktemp.createFile(path.join(tempDir, 'XXXXX.txt'), function (err, path) {
  if (err) throw err;
  // path match a /^[\da-zA-Z]{5}\.txt$/
  console.log(path);
});

// return value match a /^[\da-zA-Z]{5}\.tmp$/
mktemp.createFileSync(path.join(tempDir, 'XXXXX.tmp'));

mktemp.createDir(path.join(tempDir, 'XXXXXXX'), function (err, path) {
  if (err) throw err;
  // path match a /^[\da-zA-Z]{7}$/
  console.log(path);
});

// return value match a /^XXX-[\da-zA-Z]{3}$/
mktemp.createDirSync(path.join(tempDir, 'XXX-XXX'));

if support Promise, can use Promise style.

import * as mktemp from 'mktemp';

mktemp
  .createFile('XXXXX.txt')
  .then(function (path) {
    // path match a /^[\da-zA-Z]{5}\.txt$/
    console.log(path);
  })
  .catch(function (err) {
    console.error(err);
  });

mktemp
  .createDir('XXXXX')
  .then(function (path) {
    // path match a /^[\da-zA-Z]{5}$/
    console.log(path);
  })
  .catch(function (err) {
    console.error(err);
  });

mktemp functions are replace to random string from placeholder "X" in template. see example:

mktemp.createFileSync('XXXXXXX'); // match a /^[\da-zA-Z]{7}$/
mktemp.createFileSync('XXX.tmp'); // match a /^[\da-zA-Z]{3}\.tmp$/
mktemp.createFileSync('XXX-XXX'); // match a /^XXX-[\da-zA-Z]{3}$/

Functions

createFile(template[, mode = 0o600[, callback]])

  • template
    • String - filename template
  • mode
    • Number - file permission mode (default: 0o600)
  • callback
    • function(err, path) - callback function
      • err : Error|Null - error object
      • path : String - path

create blank file of unique filename. return Promise if callback is not passed.

createFileSync(template[, mode = 0o600])

  • template
    • String - filename template
  • mode
    • Number - file permission mode (default: 0o600)
  • return
    • String - path

sync version createFile.

createDir(template[, mode = 0o700[, callback]])

  • template
    • String - dirname template
  • mode
    • Number - directory permission mode (default: 0o700)
  • callback
    • function(err, path) - callback function
      • err : Error|Null - error object
      • path : String - path

create directory of unique dirname. return Promise if callback is not passed.

createDirSync(template[, mode = 0o700])

  • template
    • String - dirname template
  • mode
    • Number - directory permission mode (default: 0o700)
  • return
    • String - path

sync version createDir.

Contributors

License

The MIT license.

更新履歴

2.0.1 / 2025-06-22

  • update engines
  • use devEngines

2.0.0 / 2025-06-21

  • modernize
  • permissions can be set
  • retries increased

1.0.1 / 2023-05-03

  • add missing type files

1.0.0 / 2019-05-27

  • modernize

0.4.0 / 2015-02-16

  • supported Promise
  • changed to use power-assert from expect.js

0.3.5 / 2014-06-25

  • changed to use setImmediate in async functions
  • refactored

0.3.4 / 2013-11-23

  • changed to throw error if error code is not EEXIST

0.3.3 / 2013-11-14

  • changed from fs.exists to fs.open/fs.close/fs.mkdir

0.3.2 / 2013-09-08

  • changed from chai to expect.js

0.3.1 / 2013-06-19

  • changed to fast mkrand

0.3.0 / 2013-04-06

  • changed to create random name again if file exists
  • deleted mode parameter

0.2.1 / 2013-02-29

  • added support for node.js 0.10

0.2.0 / 2013-02-05

  • changed to replace placeholder near end of line

0.1.0 / 2013-01-16

  • initial release