1
1
import path from 'node:path'
2
2
import * as fsp from 'node:fs/promises'
3
- import { mkdtempSync } from 'node:fs'
3
+ import { mkdirSync } from 'node:fs'
4
4
import crypto from 'node:crypto'
5
5
import { deepEqual , rootRouteId } from '@tanstack/router-core'
6
6
import { logging } from './logger'
@@ -61,7 +61,6 @@ interface fs {
61
61
stat : (
62
62
filePath : string ,
63
63
) => Promise < { mtimeMs : bigint ; mode : number ; uid : number ; gid : number } >
64
- mkdtempSync : ( prefix : string ) => string
65
64
rename : ( oldPath : string , newPath : string ) => Promise < void >
66
65
writeFile : ( filePath : string , content : string ) => Promise < void >
67
66
readFile : (
@@ -83,7 +82,6 @@ const DefaultFileSystem: fs = {
83
82
gid : Number ( res . gid ) ,
84
83
}
85
84
} ,
86
- mkdtempSync : mkdtempSync ,
87
85
rename : ( oldPath , newPath ) => fsp . rename ( oldPath , newPath ) ,
88
86
writeFile : ( filePath , content ) => fsp . writeFile ( filePath , content ) ,
89
87
readFile : async ( filePath : string ) => {
@@ -178,7 +176,7 @@ export class Generator {
178
176
179
177
private root : string
180
178
private routesDirectoryPath : string
181
- private tmpDir : string
179
+ private sessionId ? : string
182
180
private fs : fs
183
181
private logger : Logger
184
182
private generatedRouteTreePath : string
@@ -196,9 +194,6 @@ export class Generator {
196
194
this . logger = logging ( { disabled : this . config . disableLogging } )
197
195
this . root = opts . root
198
196
this . fs = opts . fs || DefaultFileSystem
199
- this . tmpDir = this . fs . mkdtempSync (
200
- path . join ( this . config . tmpDir , 'router-generator-' ) ,
201
- )
202
197
this . generatedRouteTreePath = path . resolve ( this . config . generatedRouteTree )
203
198
this . targetTemplate = getTargetTemplate ( this . config )
204
199
@@ -1089,7 +1084,13 @@ ${acc.routeTree.map((child) => `${child.variableName}${exportName}: typeof ${get
1089
1084
private getTempFileName ( filePath : string ) {
1090
1085
const absPath = path . resolve ( filePath )
1091
1086
const hash = crypto . createHash ( 'md5' ) . update ( absPath ) . digest ( 'hex' )
1092
- return path . join ( this . tmpDir , hash )
1087
+ // lazy initialize sessionId to only create tmpDir when it is first needed
1088
+ if ( ! this . sessionId ) {
1089
+ // ensure the directory exists
1090
+ mkdirSync ( this . config . tmpDir , { recursive : true } )
1091
+ this . sessionId = crypto . randomBytes ( 4 ) . toString ( 'hex' )
1092
+ }
1093
+ return `${ this . sessionId } -${ hash } `
1093
1094
}
1094
1095
1095
1096
private async isRouteFileCacheFresh ( node : RouteNode ) : Promise <
0 commit comments