6
6
import * as fs from 'fs' ;
7
7
import * as os from 'os' ;
8
8
import * as path from 'path' ;
9
+ import { lazy } from './lazy' ;
9
10
10
11
function makeRandomHexString ( length : number ) : string {
11
12
const chars = [ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] ;
@@ -17,31 +18,17 @@ function makeRandomHexString(length: number): string {
17
18
return result ;
18
19
}
19
20
20
- const getRootTempDir = ( ( ) => {
21
- let dir : string | undefined ;
22
- return ( ) => {
23
- if ( ! dir ) {
24
- const filename = `vscode-typescript${ process . platform !== 'win32' && process . getuid ? process . getuid ( ) : '' } ` ;
25
- dir = path . join ( os . tmpdir ( ) , filename ) ;
26
- }
27
- if ( ! fs . existsSync ( dir ) ) {
28
- fs . mkdirSync ( dir ) ;
29
- }
30
- return dir ;
31
- } ;
32
- } ) ( ) ;
21
+ const rootTempDir = lazy ( ( ) => {
22
+ const filename = `vscode-typescript${ process . platform !== 'win32' && process . getuid ? process . getuid ( ) : '' } ` ;
23
+ return path . join ( os . tmpdir ( ) , filename ) ;
24
+ } ) ;
33
25
34
- export const getInstanceTempDir = ( ( ) => {
35
- let dir : string | undefined ;
36
- return ( ) => {
37
- dir ??= path . join ( getRootTempDir ( ) , makeRandomHexString ( 20 ) ) ;
38
- if ( ! fs . existsSync ( dir ) ) {
39
- fs . mkdirSync ( dir ) ;
40
- }
41
- return dir ;
42
- } ;
43
- } ) ( ) ;
26
+ export const instanceTempDir = lazy ( ( ) => {
27
+ const dir = path . join ( rootTempDir . value , makeRandomHexString ( 20 ) ) ;
28
+ fs . mkdirSync ( dir , { recursive : true } ) ;
29
+ return dir ;
30
+ } ) ;
44
31
45
32
export function getTempFile ( prefix : string ) : string {
46
- return path . join ( getInstanceTempDir ( ) , `${ prefix } -${ makeRandomHexString ( 20 ) } .tmp` ) ;
33
+ return path . join ( instanceTempDir . value , `${ prefix } -${ makeRandomHexString ( 20 ) } .tmp` ) ;
47
34
}
0 commit comments