sindresorhus / tempy

Get a random temporary file or directory path
MIT License
423 stars 26 forks source link

Create temp folder, then automate `rootDirectory` option #50

Open Richienb opened 4 months ago

Richienb commented 4 months ago

Idea: We could use a higher-level approach to discourage bad behaviour.

I guess it could be useful for when you want to semantically group all the temp directory usage in one directory for easier manual cleanup.

Something like:

import {tempDirectory} from 'tempy';

const root = temporaryDirectory();

const tempFile = root.temporaryFile();
import {temporaryDirectoryTask} from 'tempy';

temporaryDirectoryTask(root => {
    root.temporaryFileTask(tempFile => {

    });
})

Perhaps we could call them temporaryDirectoryContext and temporaryDirectoryContextTask? Perhaps we only need the latter?

Context: https://github.com/sindresorhus/tempy/issues/35#issuecomment-2093947983

Richienb commented 4 months ago

Maybe we can call the functions temporaryDirectoryContext and temporaryDirectoryTaskContext?

sindresorhus commented 4 months ago

temporaryDirectoryTaskContext

Couldn't this just be temporaryDirectoryContext().task?

Richienb commented 4 months ago

Couldn't this just be temporaryDirectoryContext().task?

We could, but I don't think this is a good idea. Methods in the returned object are for things happening within the temporary directory, not the temporary directory itself.

Richienb commented 4 months ago

On second thought, temporaryDirectoryContext().task might be a good idea if we suppose that we want to clear/delete the same directory after different parts of the run. This would be adding new functionality only accessible by context. I wonder how essential/nice it would be to re-use the same temporary directory rather than creating a new one for each task.