forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withApp.ts
41 lines (38 loc) · 1.16 KB
/
withApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os from "os";
import path from "path";
import fse from "fs-extra";
const retry = async (
callback: () => Promise<void>,
times: number,
delayMs: number = 0
) => {
try {
await callback();
} catch (error: unknown) {
if (times === 0) throw error;
setTimeout(() => retry(callback, times - 1), delayMs);
}
};
export default async <Result>(
fixture: string,
callback: (projectDir: string) => Promise<Result>
): Promise<Result> => {
let TEMP_DIR = path.join(
fse.realpathSync(os.tmpdir()),
`remix-tests-${Math.random().toString(32).slice(2)}`
);
let projectDir = path.join(TEMP_DIR);
await fse.remove(TEMP_DIR);
await fse.ensureDir(TEMP_DIR);
fse.copySync(fixture, projectDir);
try {
let result = await callback(projectDir);
return result;
} finally {
// Windows sometimes throws `EBUSY: resource busy or locked, rmdir`
// errors when attempting to removing the temporary directory.
// Retrying a couple times seems to get it to succeed.
// See https://github.com/jprichardson/node-fs-extra/issues?q=EBUSY%3A+resource+busy+or+locked%2C+rmdir
await retry(async () => await fse.remove(TEMP_DIR), 3, 200);
}
};