sindresorhus/tempy

Add ability to easily cleanup #11

sindresorhus posted onGitHub

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 has funded $80.00 to this issue.


posted by IssueHuntBot almost 6 years ago

@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.

posted by Richienb over 4 years ago

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.

posted by sindresorhus over 4 years ago

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

posted by Richienb over 4 years ago

Yes, but ignoring any failure.

posted by sindresorhus over 4 years ago

@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

posted by Richienb over 4 years ago

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

  • :moneybag: Total deposit: $80.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $8.00
posted by issuehunt-app[bot] over 4 years ago

Fund this Issue

$80.00
Rewarded

Rewarded pull request

Other pull requests

Recent activities

richienb was rewarded by sindresorhus for sindresorhus/tempy# 11
over 4 years ago
richienb submitted an output to  sindresorhus/ tempy# 11
over 4 years ago