Skip to content

Commit 98f128c

Browse files
authored
Merge pull request #19 from pmcelhaney/changeset-release/main
Version Packages
2 parents b7f5275 + 0214ed1 commit 98f128c

5 files changed

+106
-6
lines changed

.changeset/many-bags-repeat.md

-5
This file was deleted.

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# using-temporary-files
22

3+
## 2.2.1
4+
5+
### Patch Changes
6+
7+
- b6b3e9c: forgot to build the package in CI
8+
39
## 2.2.0
410

511
### Minor Changes

dist/using-temporary-files.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
interface Operations {
2+
add: (path: string, contents: string) => Promise<void>;
3+
addDirectory: (path: string) => Promise<void>;
4+
path: (relativePaths: string) => string;
5+
read: (path: string) => Promise<string>;
6+
remove: (path: string) => Promise<void>;
7+
}
8+
type Callback = (operations: Readonly<Operations>) => Promise<void>;
9+
export declare function usingTemporaryFiles(...callbacks: Readonly<Callback[]>): Promise<void>;
10+
export {};

dist/using-temporary-files.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* eslint-disable total-functions/no-unsafe-readonly-mutable-assignment */
2+
/* eslint-disable @typescript-eslint/naming-convention */
3+
/* eslint-disable no-await-in-loop */
4+
import { constants as fsConstants } from "node:fs";
5+
import fs from "node:fs/promises";
6+
import os from "node:os";
7+
import nodePath from "node:path";
8+
const RETRIES = 5;
9+
const RETRY_TIMEOUT_MILLISECONDS = 200;
10+
// eslint-disable-next-line n/no-process-env
11+
const DEBUG = process.env.USING_TEMPORARY_FILES_DEBUG === "1";
12+
async function ensureDirectoryExists(filePath) {
13+
const directory = nodePath.dirname(filePath);
14+
try {
15+
await fs.access(directory, fsConstants.W_OK);
16+
}
17+
catch {
18+
await fs.mkdir(directory, {
19+
recursive: true,
20+
});
21+
}
22+
}
23+
function createAddFunction(basePath) {
24+
return async function add(filePath, content) {
25+
const fullPath = nodePath.join(basePath, filePath);
26+
await ensureDirectoryExists(fullPath);
27+
await fs.writeFile(fullPath, content);
28+
};
29+
}
30+
function createAddDirectoryFunction(basePath) {
31+
return async function addDirectory(filePath) {
32+
const fullPath = nodePath.join(basePath, filePath);
33+
await fs.mkdir(fullPath, {
34+
recursive: true,
35+
});
36+
};
37+
}
38+
function createRemoveFunction(basePath) {
39+
return async function remove(filePath) {
40+
const fullPath = nodePath.join(basePath, filePath);
41+
await ensureDirectoryExists(fullPath);
42+
await fs.rm(fullPath);
43+
};
44+
}
45+
function createReadFunction(basePath) {
46+
return async function read(filePath, encoding = "utf8") {
47+
const fullPath = nodePath.join(basePath, filePath);
48+
return await fs.readFile(fullPath, encoding);
49+
};
50+
}
51+
// eslint-disable-next-line max-statements
52+
export async function usingTemporaryFiles(...callbacks) {
53+
const baseDirectory = DEBUG
54+
? nodePath.resolve(process.cwd(), "./")
55+
: os.tmpdir();
56+
const temporaryDirectory = String(await fs.mkdtemp(nodePath.join(baseDirectory, "utf-")));
57+
try {
58+
for (const callback of callbacks) {
59+
// eslint-disable-next-line n/callback-return
60+
await callback({
61+
add: createAddFunction(temporaryDirectory),
62+
addDirectory: createAddDirectoryFunction(temporaryDirectory),
63+
path(...relativePaths) {
64+
return nodePath.join(temporaryDirectory, ...relativePaths);
65+
},
66+
read: createReadFunction(temporaryDirectory),
67+
remove: createRemoveFunction(temporaryDirectory),
68+
});
69+
}
70+
}
71+
finally {
72+
let retries = RETRIES;
73+
while (retries > 0) {
74+
try {
75+
await fs.rm(temporaryDirectory, {
76+
recursive: true,
77+
});
78+
break;
79+
}
80+
catch {
81+
// eslint-disable-next-line promise/avoid-new, compat/compat
82+
await new Promise((resolve) => {
83+
setTimeout(resolve, RETRY_TIMEOUT_MILLISECONDS);
84+
});
85+
retries -= 1;
86+
}
87+
}
88+
}
89+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "using-temporary-files",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "usingTemporaryFiles() - a utility for testing code that accesses the file system",
55
"type": "module",
66
"main": "./dist/using-temporary-files.js",

0 commit comments

Comments
 (0)