11import { type ExecSyncOptions , execSync } from "node:child_process" ;
22import { cpSync , existsSync , mkdirSync , mkdtempSync , rmSync , writeFileSync } from "node:fs" ;
33import os from "node:os" ;
4- import { join , resolve , posix as pathPosix } from "node:path" ;
4+ import path , { join , resolve , posix as pathPosix } from "node:path" ;
55import { parseArgs } from "node:util" ;
66import chalk from "chalk" ;
77import semver from "semver" ;
4141}
4242
4343const repoPath = ( ...segs : string [ ] ) =>
44- pathPosix . join ( ...segs . filter ( Boolean ) . map ( s => s . replace ( / ^ \/ + | \/ + $ / g, "" ) ) ) ;
45-
44+ path . normalize ( path . join ( ...segs . filter ( Boolean ) ) ) ;
4645
4746const allTags = ( ) => run ( "git tag --list" ) . split ( "\n" ) . filter ( Boolean ) ;
4847
@@ -70,13 +69,14 @@ function hasLocalRef(ref: string) {
7069 try { run ( `git show-ref --verify --quiet ${ ref } ` ) ; return true ; } catch { return false ; }
7170}
7271
72+ const REPO_ROOT = run ( "git rev-parse --show-toplevel" , { cwd : currentDocsWorkspace } ) ;
73+
7374function refHasPath ( ref : string , pathFromRepoRoot : string ) : boolean {
75+ const p = pathFromRepoRoot . replace ( / ^ \/ + / , "" ) . replace ( / \/ + $ / , "" ) ;
7476 try {
75- const out = run ( `git ls-tree -r --name-only ${ ref } -- "${ pathFromRepoRoot } "` ) ;
76- return out . trim ( ) . length > 0 ;
77- } catch {
78- return false ;
79- }
77+ run ( `git -C "${ REPO_ROOT } " rev-parse --verify --quiet "${ ref } :${ p } "` ) ;
78+ return true ;
79+ } catch { return false ; }
8080}
8181
8282function buildDocs ( sourceDir : string , outDir : string ) {
@@ -211,11 +211,11 @@ function isPullRequestCI() {
211211 builtVersions = [ ...tags ] ;
212212 } else {
213213 // Fallback: build default branch (useful if you want a "main" channel)
214- const checkPath = repoPath ( docsRelative , contentDir ) ; // e.g. "docs/content"
215- run ( `git fetch --prune origin ${ defaultBranch } ` , { cwd : currentDocsWorkspace , inherit : true } ) ;
216- const hasOnDefault = refHasPath ( `origin/${ defaultBranch } ` , checkPath ) ;
214+ const checkPath = repoPath ( docsRelative , contentDir ) ; // "docs/content"
215+ run ( `git fetch --prune origin ${ defaultBranch } ` , { cwd : currentDocsWorkspace , inherit : true } ) ;
217216
218- if ( ! hasOnDefault ) {
217+ const hasOnDefault = refHasPath ( `origin/${ defaultBranch } ` , checkPath ) ;
218+ if ( ! hasOnDefault ) {
219219 throw new Error ( `Default branch 'origin/${ defaultBranch } ' has no '${ checkPath } '. Pass --versions to build tags.` ) ;
220220 }
221221 console . log ( chalk . cyan ( `(ci) Building docs from '${ defaultBranch } ' → ${ defaultBranch } ` ) ) ;
@@ -237,4 +237,4 @@ export const versions = ${JSON.stringify(builtVersions, null, 2)} as const
237237} ) ( ) . catch ( ( e ) => {
238238 console . error ( chalk . red ( "❌ Build failed:" ) , e ) ;
239239 process . exit ( 1 ) ;
240- } ) ;
240+ } ) ;
0 commit comments