diff --git a/README.md b/README.md index 243424d..948a6fc 100644 --- a/README.md +++ b/README.md @@ -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. "/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 diff --git a/package-lock.json b/package-lock.json index 7c022e9..76891c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "using-temporary-files", - "version": "2.1.1", + "version": "2.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "using-temporary-files", - "version": "2.1.1", + "version": "2.2.1", "license": "MIT", "devDependencies": { "@changesets/cli": "^2.26.2",