Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,41 @@ npm install --save-dev using-temporary-files
```js copy
import { usingTemporaryFiles } from "using-temporary-files";

await usingTemporaryFiles(async ({ path, add, addDirectory, remove }) => {
await usingTemporaryFiles(async ({ path, add, addDirectory, read, remove }) => {
path("."); // full path to the temporary directory
path("file.txt"); // full path to a particular file
await add("file.txt", "content"); // add a file
const text = await read("file.txt" /*, encoding (optional) */); // read the contents of a file
await addDirectory("dir"); // add a directory
path("a", "b", "c"); // path segments are joined, e.g. "<tmpdir>/a/b/c"
await add("file.txt", "content"); // add a file (creates parent directories as needed)
const text = await read("file.txt"); // read the contents of a file (encoding defaults to "utf8")
const binary = await read("file.bin", "base64"); // read with a specific encoding
await addDirectory("dir"); // add a directory (creates parent directories as needed)
await remove("file.txt"); // remove a file
});
```

### Multiple callbacks

`usingTemporaryFiles()` accepts any number of callbacks. They share the same temporary directory and are called in order.

```js copy
await usingTemporaryFiles(
async ({ add }) => {
await add("file.txt", "Hello, world!");
},
async ({ read }) => {
console.log(await read("file.txt")); // "Hello, world!"
}
);
```

### Debug mode

Set the environment variable `USING_TEMPORARY_FILES_DEBUG=1` to keep the temporary directory in the current working directory instead of deleting it. This is useful when you need to inspect the files after a test run.

```sh copy
USING_TEMPORARY_FILES_DEBUG=1 npm test
```

## Background

This code was extracted from [Counterfact](https://github.com/pmcelhaney/counterfact) so that it can be
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading