Skip to content

Instantly share code, notes, and snippets.

@woolinsilver
Created November 4, 2024 09:52
Show Gist options
  • Save woolinsilver/0d6f19b2b8e1df0f450451485e529056 to your computer and use it in GitHub Desktop.
Save woolinsilver/0d6f19b2b8e1df0f450451485e529056 to your computer and use it in GitHub Desktop.
Node.js create temporary directory
/**
* @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