Skip to content

Instantly share code, notes, and snippets.

@maganap
Created July 14, 2021 15:47
Show Gist options
  • Save maganap/2beee1caf094b184fa15136019687fef to your computer and use it in GitHub Desktop.
Save maganap/2beee1caf094b184fa15136019687fef to your computer and use it in GitHub Desktop.
Create and remove temporary working directory - Node JavaScript
// node 14 tested
const fs = require('fs');
const os = require('os');
const path = require('path');
// this creates a random name directory within the OS temporary directory
const getWorkingPath = () => fs.mkdtempSync(`${os.tmpdir()}${path.sep}`);
// make sure to remove the whole path when finished
const removeWorkingPath = (path) => fs.rmSync(path, { recursive: true });
// how to use:
async function doSomeWork() {
const workingPath = getWorkingPath();
// --> do your stuff here within workingPath
removeWorkingPath(workingPath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment