1.1.2 • Published 4 years ago

with-tmp-dir-promise v1.1.2

Weekly downloads
292
License
MIT
Repository
github
Last release
4 years ago

with-tmp-dir-promise

Run a promise-based function with a temporary directory, deleting it afterwards.

Requires NodeJS 8+.

Installation

npm install --save with-tmp-dir-promise

Usage example

const {WithTempDir} = require('with-tmp-dir-promise');

WithTempDir(function (tmpdir) {
    // tmpdir is a string like '/tmp/q3q235',
    // pointing to an existing temporary directory

    // Your code goes here

    // When this function returns, the tmp directory will be deleted
    // Remember to return a Promise here!
    return Promise.resolve("foobar"); // Example
}).then(function(arg) {
    // arg === 'foobar'

    // tmpdir, including all files in the directory,
    // is deleted here
});

WithTempDir documentation

/**
 * Run the given callback with a newly created temporary directory,
 * automatically cleaning up once the promise
 * This function returns a promise that resolves of rejects depending on
 * A failure in creating the temporary directory causes
 * immediate rejection without calling the callback.
 * @param {*} callback A function(tmpPath) retuning a promise
 * @param {*} opts Options for the tmp library. Default: Also delete files in the directory.
 * Also supports cleanupAfter opt: If true, cleanup after resolving/rejecting the returned Promise.
 * If false (default), cleanup before
 */
function WithTempDir (callback, opts = {unsafeCleanup: true, cleanupAfter: false}) {
    // ...
}
1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago