11import fs from "node:fs" ;
22import path from "node:path" ;
33import { onExit } from "signal-exit" ;
4+ import { devBranchPathSegment } from "./devBranch.js" ;
5+
6+ /**
7+ * Resolves the `.trigger/tmp` root for a dev session, scoped to the branch so
8+ * concurrent sessions on different branches don't share (and clobber) a build
9+ * tree. The default branch keeps the original `.trigger/tmp` path; branches get
10+ * a sibling root (e.g. `.trigger/tmp-feature-foo`) so a default-branch
11+ * `clearTmpDirs` can't reach into a branch's tree, and vice versa.
12+ */
13+ export function getTmpRoot ( projectRoot : string | undefined , branch ?: string ) : string {
14+ projectRoot ??= process . cwd ( ) ;
15+ const safeBranch = devBranchPathSegment ( branch ) ;
16+ const tmpDirName = safeBranch ? `tmp-${ safeBranch } ` : "tmp" ;
17+ return path . join ( projectRoot , ".trigger" , tmpDirName ) ;
18+ }
419
520/**
621 * A short-lived directory. Automatically removed when the process exits, but
@@ -21,10 +36,10 @@ export interface EphemeralDirectory {
2136export function getTmpDir (
2237 projectRoot : string | undefined ,
2338 prefix : string ,
24- keep : boolean = false
39+ keep : boolean = false ,
40+ branch ?: string
2541) : EphemeralDirectory {
26- projectRoot ??= process . cwd ( ) ;
27- const tmpRoot = path . join ( projectRoot , ".trigger" , "tmp" ) ;
42+ const tmpRoot = getTmpRoot ( projectRoot , branch ) ;
2843 fs . mkdirSync ( tmpRoot , { recursive : true } ) ;
2944
3045 const tmpPrefix = path . join ( tmpRoot , `${ prefix } -` ) ;
@@ -48,9 +63,8 @@ export function getTmpDir(
4863 } ;
4964}
5065
51- export function clearTmpDirs ( projectRoot : string | undefined ) {
52- projectRoot ??= process . cwd ( ) ;
53- const tmpRoot = path . join ( projectRoot , ".trigger" , "tmp" ) ;
66+ export function clearTmpDirs ( projectRoot : string | undefined , branch ?: string ) {
67+ const tmpRoot = getTmpRoot ( projectRoot , branch ) ;
5468
5569 try {
5670 fs . rmSync ( tmpRoot , { recursive : true , force : true } ) ;
@@ -65,9 +79,12 @@ export function clearTmpDirs(projectRoot: string | undefined) {
6579 * identical chunk files between build versions.
6680 * Automatically cleaned up when the process exits.
6781 */
68- export function getStoreDir ( projectRoot : string | undefined , keep : boolean = false ) : string {
69- projectRoot ??= process . cwd ( ) ;
70- const storeDir = path . join ( projectRoot , ".trigger" , "tmp" , "store" ) ;
82+ export function getStoreDir (
83+ projectRoot : string | undefined ,
84+ keep : boolean = false ,
85+ branch ?: string
86+ ) : string {
87+ const storeDir = path . join ( getTmpRoot ( projectRoot , branch ) , "store" ) ;
7188 fs . mkdirSync ( storeDir , { recursive : true } ) ;
7289
7390 // Register exit handler to clean up the store directory
0 commit comments