Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to easily cleanup #11

Closed
sindresorhus opened this issue May 13, 2019 · 7 comments · Fixed by #25
Closed

Add ability to easily cleanup #11

sindresorhus opened this issue May 13, 2019 · 7 comments · Fixed by #25
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented May 13, 2019

Issuehunt badges

Offer both cleanup when the process exits, manual cleanup method call, and also a context-based cleanup, for examples:

tempy.context(directory => {
	// Use the temp
});

// Temp directory is cleaned up here.

(The temp directory is cleaned up after the callback scope.)
(The above is just an idea. Please consider a better API and naming.)
(The context callback should also be able to be an async function.)

Inspiration: Python Context Managers


If you decide to work on this, please put a good effort into the implementation, docs (and TS definition), and tests.


IssueHunt Summary

richienb richienb has been rewarded.

Backers (Total: $80.00)

Submitted pull Requests


Tips

@IssueHuntBot
Copy link

@IssueHunt has funded $80.00 to this issue.


@Richienb
Copy link
Contributor

Richienb commented Aug 13, 2020

@sindresorhus Maybe all the functions could accept an option that make it return an array with the original result and a cleanup method.

const tempy = require("tempy");

const [tempFile, cleanup] = tempy.file({cleanup: true});

// Use tempFile...

cleanup()

We could also add a .task or .cleanable function into all of them:

const tempy = require("tempy");

const [tempFile, cleanup] = tempy.file.cleanable();

// Use tempFile...

cleanup()

We could also stick with a callback-like method like in the original proposal:

const tempy = require("tempy");

tempy.file.task(tempFile => {
	// Use tempFile...
});

I think either of these strategies works best since they are all simple to understand.

@sindresorhus
Copy link
Owner Author

sindresorhus commented Aug 13, 2020

The first suggestion is not desirable as it would complicate the TS types and it would be better to just add a .cleanup() method:

const tempy = require("tempy");

const tempFile = tempy.file();

// Use tempFile...

tempy.cleanup(tempFile);

The second suggestion would also not be better than what's above.

I think the last suggestion there is the best.

@Richienb
Copy link
Contributor

@sindresorhus Would it also be useful to remove temporary content when the process is killed?

@sindresorhus
Copy link
Owner Author

Yes, but ignoring any failure.

@Richienb
Copy link
Contributor

@sindresorhus exit-hook gets us most of the way, we just need to detect when an error is a reason for exiting. https://stackoverflow.com/a/23197430/8384910

@issuehunt-oss
Copy link

issuehunt-oss bot commented Sep 13, 2020

@sindresorhus has rewarded $72.00 to @richienb. See it on IssueHunt

  • 💰 Total deposit: $80.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $8.00

@issuehunt-oss issuehunt-oss bot added 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt and removed 💵 Funded on Issuehunt This issue has been funded on Issuehunt labels Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt help wanted
Projects
None yet
3 participants