Created
July 14, 2021 15:47
-
-
Save maganap/2beee1caf094b184fa15136019687fef to your computer and use it in GitHub Desktop.
Create and remove temporary working directory - Node JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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