Created
November 4, 2024 09:52
-
-
Save woolinsilver/0d6f19b2b8e1df0f450451485e529056 to your computer and use it in GitHub Desktop.
Node.js create temporary directory
This file contains hidden or 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
/** | |
* @callback NeedsTemporaryDirectory | |
* @param {string} path path of temporary directory | |
*/ | |
/** | |
* @param {string} prefix prepended to name of temporary directory | |
* @param {NeedsTemporaryDirectory} lambda will be given access to temporary directory | |
*/ | |
async function withTemporaryDirectory (prefix, lambda) { | |
const temp = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); | |
try { | |
await lambda(temp) | |
} finally { | |
fs.rmSync(temp, { recursive: true }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment