@@ -63,6 +63,15 @@ function resolveTagsFromSpec(spec: string) {
6363 return matched . sort ( semver . rcompare )
6464}
6565
66+ function hasLocalRef ( ref : string ) {
67+ try {
68+ run ( `git show-ref --verify --quiet ${ ref } ` )
69+ return true
70+ } catch {
71+ return false
72+ }
73+ }
74+
6675function buildDocs ( sourceDir : string , outDir : string ) {
6776 if ( ! existsSync ( sourceDir ) ) {
6877 throw new Error (
@@ -143,6 +152,16 @@ function buildRef(ref: string, labelForOutDir: string) {
143152 }
144153}
145154
155+ function buildBranch ( branch : string , labelForOutDir : string ) {
156+ run ( `git fetch --tags --prune origin ${ branch } ` , {
157+ cwd : workspaceRoot ,
158+ inherit : true ,
159+ } )
160+ const localRef = `refs/heads/${ branch } `
161+ const targetRef = hasLocalRef ( localRef ) ? localRef : `origin/${ branch } `
162+ return buildRef ( targetRef , labelForOutDir )
163+ }
164+
146165function buildTag ( tag : string ) {
147166 return buildRef ( `refs/tags/${ tag } ` , tag )
148167}
@@ -154,6 +173,31 @@ function getCurrentBranch(): string {
154173 throw new Error ( "Failed to get current branch" )
155174 }
156175}
176+
177+ function isOnDefaultBranch ( defaultBranch : string ) : boolean {
178+ const currentBranch = getCurrentBranch ( )
179+ return currentBranch === defaultBranch
180+ }
181+
182+ /**
183+ * Build the current workspace snapshot labelled `labelForOutDir`.
184+ * If we're on the default branch, fetch and build that branch; otherwise
185+ * build the working tree in-place (useful for PR/feature branches).
186+ */
187+ function buildCurrentSnapshot ( defaultBranch : string , onDefaultBranch : boolean , labelForOutDir = "current" ) {
188+ if ( onDefaultBranch ) {
189+ // build from the remote/default branch so the snapshot exactly matches the
190+ // default branch state (not the local working tree)
191+ // biome-ignore lint/suspicious/noConsole: keep for logging
192+ console . log ( chalk . cyan ( `Building default branch '${ defaultBranch } ' → ${ labelForOutDir } ` ) )
193+ buildBranch ( defaultBranch , labelForOutDir )
194+ } else {
195+ // build the current workspace directly
196+ // biome-ignore lint/suspicious/noConsole: keep for logging
197+ console . log ( chalk . cyan ( `Building current workspace → ${ labelForOutDir } ` ) )
198+ buildDocs ( workspaceRoot , join ( outputDir , labelForOutDir ) )
199+ }
200+ }
157201; ( async ( ) => {
158202 const { values } = parseArgs ( {
159203 args : process . argv . slice ( 2 ) ,
@@ -163,12 +207,22 @@ function getCurrentBranch(): string {
163207 } ,
164208 } )
165209
210+ const defaultBranch = ( values . branch as string | undefined ) ?. trim ( )
211+ if ( ! defaultBranch ) {
212+ throw new Error (
213+ `❌ Missing required --branch flag.
214+ Please specify the default branch name (e.g., --branch main)
215+ Example: pnpm run generate:docs --branch main`
216+ )
217+ }
218+
166219 const rawVersions = ( values . versions as string | undefined ) ?. trim ( ) ?? ""
167220 const hasVersionsArg = rawVersions . length > 0
168221
169222 let builtVersions : string [ ] = [ ]
170223
171- const currentBranchToBuild = getCurrentBranch ( )
224+ const onDefaultBranch = isOnDefaultBranch ( defaultBranch )
225+ const currentBranchToBuild = onDefaultBranch ? defaultBranch : getCurrentBranch ( )
172226
173227 // biome-ignore lint/suspicious/noConsole: keep for logging
174228 console . log ( chalk . cyan ( `Building from branch: ${ currentBranchToBuild } ` ) )
@@ -181,15 +235,11 @@ function getCurrentBranch(): string {
181235 console . log ( chalk . cyan ( `Building tags: ${ tags . join ( ", " ) } ` ) )
182236 for ( const t of tags ) buildTag ( t )
183237
184- // biome-ignore lint/suspicious/noConsole: keep for logging
185- console . log ( chalk . cyan ( "Building current workspace → current" ) )
186- buildDocs ( workspaceRoot , join ( outputDir , "current" ) )
238+ buildCurrentSnapshot ( defaultBranch , onDefaultBranch , "current" )
187239
188240 builtVersions = [ "current" , ...tags ]
189241 } else {
190- // biome-ignore lint/suspicious/noConsole: keep for logging
191- console . log ( chalk . cyan ( "Building current workspace → current" ) )
192- buildDocs ( workspaceRoot , join ( outputDir , "current" ) )
242+ buildCurrentSnapshot ( defaultBranch , onDefaultBranch , "current" )
193243
194244 builtVersions = [ "current" ]
195245 }
0 commit comments