sindresorhus / tempy

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

Add `rootDirectory` option #35

Open sindresorhus opened 3 years ago

sindresorhus commented 3 years ago

As requested in https://github.com/sindresorhus/tempy/pull/33.

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. This is not really useful for the .task methods though as they clean up themselves.

I'm not entirely sold on this though, as every option adds complexity for the user and most users will not need this. I'm also concerned users will think they need this when most often, they don't.

Richienb commented 2 years ago

Earlier @aminya stated that they want to be able to manually clear temporary objects created by tempyFile and tempyDirectory at their discretion with a single command and that putting everything in a single folder would make it really easy. https://github.com/sindresorhus/tempy/pull/33#issuecomment-805650086

ludicast commented 5 months ago

I respect keeping the surface area small, but I'm pretty surprised that you are skeptical re its value. Many people who need or have cases for this might not want to comment publicly on why (private projects, NDAs, non-confrontational temperaments, etc). Added to which software is going to be used in contexts the creator doesn't anticipate.

An example that comes to mind immediately (not my case) is that a different filesystem might be needed for these temporary files (e.g. /tmp, /home, and /var are sometimes mounted from different "drives"). Or somebody might want to use a journaling filesystem or something similar for reasons. Yes there are easy workarounds (I'm guessing making /private/var/folders/ a symlink to the intended directory structure does the trick), but they push knowledge of your tool's internals to an unrelated part of somebody's codebase.

I imagine most just use something else that gives them more filesystem control (even if by a less trusted name in software :tm:). So you're not hearing about this from them because they just go someplace else :smile_cat: .

But obv your lib & call o/c. Just my 2 cents that is parameterizing the directory seems on the surface to provide value at minimal maintenance cost (plus I think there was already a 10-line PR someone did for this).

Richienb commented 5 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?

aminya commented 5 months ago

For those who are still waiting on tempy fix this:

I ended up using node-temp which allows setting dir in its options. https://github.com/bruce/node-temp#affixes

Originally posted in 2021 in https://github.com/sindresorhus/tempy/pull/33#issuecomment-805650086

sindresorhus commented 4 months ago

https://github.com/sindresorhus/tempy/issues/46 would solve the use-case of grouping.

sindresorhus commented 4 months ago

I'm still willing to add an option called rootDirectory for niche use-cases, but the parentDirectory option should be recommended instead in the docs.

sindresorhus commented 4 months ago

@Richienb That's an interesting idea. I like the idea of a more fluent syntax, but I think adding a rootDirectory option would still be required. Could you move your comment to a new issue?