-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp.ts
More file actions
37 lines (34 loc) · 1.06 KB
/
tmp.ts
File metadata and controls
37 lines (34 loc) · 1.06 KB
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
/**
* @fileoverview `withTmp` — run a callback with a freshly-created
* temp directory under the cacache root. Wraps cacache's `tmp.withTmp`
* with a corrected type signature (the DefinitelyTyped definition
* incorrectly declares `void` even though the function forwards the
* callback's return value).
*/
import { getSocketCacacheDir } from '../paths/socket'
import { getCacache } from './_internal'
/**
* Execute a callback with a temporary directory for cache operations.
*
* @example
* ```typescript
* const result = await withTmp(async (tmpDir) => {
* // Use tmpDir for temporary cache work
* return 'done'
* })
* ```
*/
export async function withTmp<T>(
callback: (tmpDirPath: string) => Promise<T>,
): Promise<T> {
const cacache = getCacache()
// The DefinitelyTyped types for cacache.tmp.withTmp are incorrect.
// It actually returns the callback's return value, not void.
/* c8 ignore start - External cacache call */
return (await cacache.tmp.withTmp(
getSocketCacacheDir(),
{},
callback as any,
)) as T
/* c8 ignore stop */
}