with-local-tmp-dir
Version:
Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.
21 lines • 584 B
JavaScript
import chdir from '@dword-design/chdir';
import tmp from 'tmp-promise';
export default (async (...args) => {
let options = args.find(arg => typeof arg === 'object');
options = {
dir: process.cwd(),
tmpdir: process.cwd(),
unsafeCleanup: true,
...options
};
const callback = args.find(arg => typeof arg === 'function');
if (callback) {
return tmp.withDir(context => chdir(context.path, callback), options);
}
const context = await tmp.dir(options);
const back = chdir(context.path);
return () => {
back();
return context.cleanup();
};
});