Created
March 5, 2025 06:17
-
-
Save NuckChorris/0357d401987dc753beb2f068ae09aff7 to your computer and use it in GitHub Desktop.
Disposable Temporary Files and Directories
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
import { chmodSync as chmod, rmSync as rm } from 'node:fs'; | |
import { | |
temporaryDirectory as _temporaryDirectory, | |
temporaryFile as _temporaryFile, | |
} from 'tempy'; | |
function makeDisposableFilename(file: string) { | |
return Object.assign(new String(file), { | |
[Symbol.dispose]() { | |
chmod(file, 0o777); | |
rm(file, { recursive: true, force: true, maxRetries: 2 }); | |
}, | |
}) as string & { [Symbol.dispose](): void }; | |
} | |
export function temporaryDirectory( | |
...options: Parameters<typeof _temporaryDirectory> | |
) { | |
return makeDisposableFilename(_temporaryDirectory(...options)); | |
} | |
export function temporaryFile(...options: Parameters<typeof _temporaryFile>) { | |
return makeDisposableFilename(_temporaryFile(...options)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment