From 0214ed1354deccf535ce21c53c2274ecbd59f824 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 26 Apr 2024 18:55:18 +0000 Subject: [PATCH] Version Packages --- .changeset/many-bags-repeat.md | 5 -- CHANGELOG.md | 6 +++ dist/using-temporary-files.d.ts | 10 ++++ dist/using-temporary-files.js | 89 +++++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 106 insertions(+), 6 deletions(-) delete mode 100644 .changeset/many-bags-repeat.md create mode 100644 dist/using-temporary-files.d.ts create mode 100644 dist/using-temporary-files.js diff --git a/.changeset/many-bags-repeat.md b/.changeset/many-bags-repeat.md deleted file mode 100644 index 964c79c..0000000 --- a/.changeset/many-bags-repeat.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"using-temporary-files": patch ---- - -forgot to build the package in CI diff --git a/CHANGELOG.md b/CHANGELOG.md index 3801726..c379793 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # using-temporary-files +## 2.2.1 + +### Patch Changes + +- b6b3e9c: forgot to build the package in CI + ## 2.2.0 ### Minor Changes diff --git a/dist/using-temporary-files.d.ts b/dist/using-temporary-files.d.ts new file mode 100644 index 0000000..91a97b8 --- /dev/null +++ b/dist/using-temporary-files.d.ts @@ -0,0 +1,10 @@ +interface Operations { + add: (path: string, contents: string) => Promise; + addDirectory: (path: string) => Promise; + path: (relativePaths: string) => string; + read: (path: string) => Promise; + remove: (path: string) => Promise; +} +type Callback = (operations: Readonly) => Promise; +export declare function usingTemporaryFiles(...callbacks: Readonly): Promise; +export {}; diff --git a/dist/using-temporary-files.js b/dist/using-temporary-files.js new file mode 100644 index 0000000..1f95749 --- /dev/null +++ b/dist/using-temporary-files.js @@ -0,0 +1,89 @@ +/* eslint-disable total-functions/no-unsafe-readonly-mutable-assignment */ +/* eslint-disable @typescript-eslint/naming-convention */ +/* eslint-disable no-await-in-loop */ +import { constants as fsConstants } from "node:fs"; +import fs from "node:fs/promises"; +import os from "node:os"; +import nodePath from "node:path"; +const RETRIES = 5; +const RETRY_TIMEOUT_MILLISECONDS = 200; +// eslint-disable-next-line n/no-process-env +const DEBUG = process.env.USING_TEMPORARY_FILES_DEBUG === "1"; +async function ensureDirectoryExists(filePath) { + const directory = nodePath.dirname(filePath); + try { + await fs.access(directory, fsConstants.W_OK); + } + catch { + await fs.mkdir(directory, { + recursive: true, + }); + } +} +function createAddFunction(basePath) { + return async function add(filePath, content) { + const fullPath = nodePath.join(basePath, filePath); + await ensureDirectoryExists(fullPath); + await fs.writeFile(fullPath, content); + }; +} +function createAddDirectoryFunction(basePath) { + return async function addDirectory(filePath) { + const fullPath = nodePath.join(basePath, filePath); + await fs.mkdir(fullPath, { + recursive: true, + }); + }; +} +function createRemoveFunction(basePath) { + return async function remove(filePath) { + const fullPath = nodePath.join(basePath, filePath); + await ensureDirectoryExists(fullPath); + await fs.rm(fullPath); + }; +} +function createReadFunction(basePath) { + return async function read(filePath, encoding = "utf8") { + const fullPath = nodePath.join(basePath, filePath); + return await fs.readFile(fullPath, encoding); + }; +} +// eslint-disable-next-line max-statements +export async function usingTemporaryFiles(...callbacks) { + const baseDirectory = DEBUG + ? nodePath.resolve(process.cwd(), "./") + : os.tmpdir(); + const temporaryDirectory = String(await fs.mkdtemp(nodePath.join(baseDirectory, "utf-"))); + try { + for (const callback of callbacks) { + // eslint-disable-next-line n/callback-return + await callback({ + add: createAddFunction(temporaryDirectory), + addDirectory: createAddDirectoryFunction(temporaryDirectory), + path(...relativePaths) { + return nodePath.join(temporaryDirectory, ...relativePaths); + }, + read: createReadFunction(temporaryDirectory), + remove: createRemoveFunction(temporaryDirectory), + }); + } + } + finally { + let retries = RETRIES; + while (retries > 0) { + try { + await fs.rm(temporaryDirectory, { + recursive: true, + }); + break; + } + catch { + // eslint-disable-next-line promise/avoid-new, compat/compat + await new Promise((resolve) => { + setTimeout(resolve, RETRY_TIMEOUT_MILLISECONDS); + }); + retries -= 1; + } + } + } +} diff --git a/package.json b/package.json index 442b3a3..c3eea00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "using-temporary-files", - "version": "2.2.0", + "version": "2.2.1", "description": "usingTemporaryFiles() - a utility for testing code that accesses the file system", "type": "module", "main": "./dist/using-temporary-files.js",