Skip to content

Commit

Permalink
Merge pull request #19 from pmcelhaney/changeset-release/main
Browse files Browse the repository at this point in the history
Version Packages
  • Loading branch information
pmcelhaney authored Apr 26, 2024
2 parents b7f5275 + 0214ed1 commit 98f128c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 6 deletions.
5 changes: 0 additions & 5 deletions .changeset/many-bags-repeat.md

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions dist/using-temporary-files.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface Operations {
add: (path: string, contents: string) => Promise<void>;
addDirectory: (path: string) => Promise<void>;
path: (relativePaths: string) => string;
read: (path: string) => Promise<string>;
remove: (path: string) => Promise<void>;
}
type Callback = (operations: Readonly<Operations>) => Promise<void>;
export declare function usingTemporaryFiles(...callbacks: Readonly<Callback[]>): Promise<void>;
export {};
89 changes: 89 additions & 0 deletions dist/using-temporary-files.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 98f128c

Please sign in to comment.