Description
What is the problem this feature will solve?
I often find myself wanting to make a temporary directory for use during execution of a single function, for example because I'm going to execSync
some process which creates temporary files in the current directory. mkdtemp
works great to create the thing, but then I have to wrap in in a try
/finally
to handle cleanup.
What is the feature you are proposing to solve the problem?
This seems like a good case for the fancy new using
syntax. It would have to return an object with a [Symbol.dispose]
and a path
property instead of just a string, but then instead of
let tempDir;
try {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prefix-'));
spawnSync(command, [], { cwd: tempDir });
// etc
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
we could have
using tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'prefix-'), { disposable: true }); // or whatever
spawnSync(command, [], { cwd: tempDir.path });
// cleanup handled automatically
Compare Python's tempfile
module, which you can use as a context manager for the same sort of behavior:
# create a temporary directory using the context manager
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
# directory and contents have been removed
For bonus points it would be nice if this got cleaned up on process exit, but that's more work and it seems fine to just leave "might not get cleaned up if the process exits in the middle of your function" as a documented behavior.
What alternatives have you considered?
We can of course just keep using try
/finally
, or not cleaning up.
Metadata
Metadata
Assignees
Type
Projects
Status