Creates a temporary folder inside CWD and passes the path to a function. Afterwards, the folder is removed. Especially useful for testing.


Keywords
cleanup, cwd, directory, function, local, path, remove, temp, temporary, test, tmp, with
License
MIT
Install
npm install with-local-tmp-dir@2.2.5

Documentation

with-local-tmp-dir

npm version Build Status Coverage Status dependency status

Creates a temporary folder inside CWD and passes the path to a function. Afterwards, the folder is removed. Especially useful for testing.

Installation

# via NPM
npm install --save with-local-tmp-dir

# via Yarn
yarn add with-local-tmp-dir

Usage

const withLocalTmpDir = require('with-local-tmp-dir')

await withLocalTmpDir(() => {
  console.log(process.cwd())
  //> /Users/max/project/tmp-18815DudQxmdn03Rz
})
// Now the folder does not exist anymore

// Also works with async functions =)
await withLocalTmpDir(async () => await ...)

// The folder is removed even if an exception is thrown
await withLocalTmpDir(async () => throw new Error('File could not be found'))

Migrating from Version 1.x to 2.x

Version 2 automatically changes the current working directory into the temporary folder. That's why the path parameter is not needed anymore and the path can be directly queried from process.cwd.

License

MIT © Sebastian Landwehr