@@ -3,25 +3,32 @@ import * as fs from 'fs-extra';
3
3
import * as os from 'os' ;
4
4
import * as path from 'path' ;
5
5
6
- export async function withTempDirectory < T > ( fn : ( directory : string ) => Promise < T > ) : Promise < T > {
7
- const directory = await fs . mkdtemp ( path . resolve ( os . tmpdir ( ) , 'electron-download-' ) ) ;
8
-
6
+ async function useAndRemoveDirectory < T > (
7
+ directory : string ,
8
+ fn : ( directory : string ) => Promise < T > ,
9
+ ) : Promise < T > {
9
10
let result : T ;
10
11
try {
11
12
result = await fn ( directory ) ;
12
- } catch ( err ) {
13
- await fs . remove ( directory ) ;
14
- throw err ;
15
- }
16
-
17
- try {
13
+ } finally {
18
14
await fs . remove ( directory ) ;
19
- } catch {
20
- // Ignore error, it's just a temp dir
21
15
}
22
16
return result ;
23
17
}
24
18
19
+ export async function withTempDirectoryIn < T > (
20
+ parentDirectory : string = os . tmpdir ( ) ,
21
+ fn : ( directory : string ) => Promise < T > ,
22
+ ) : Promise < T > {
23
+ const tempDirectoryPrefix = 'electron-download-' ;
24
+ const tempDirectory = await fs . mkdtemp ( path . resolve ( parentDirectory , tempDirectoryPrefix ) ) ;
25
+ return useAndRemoveDirectory ( tempDirectory , fn ) ;
26
+ }
27
+
28
+ export async function withTempDirectory < T > ( fn : ( directory : string ) => Promise < T > ) : Promise < T > {
29
+ return withTempDirectoryIn ( undefined , fn ) ;
30
+ }
31
+
25
32
export function normalizeVersion ( version : string ) {
26
33
if ( ! version . startsWith ( 'v' ) ) {
27
34
return `v${ version } ` ;
0 commit comments