@@ -174,7 +174,8 @@ function isOnDefaultBranch(defaultBranch: string): boolean {
174174 const currentBranch = getCurrentBranch ( )
175175 return currentBranch === defaultBranch
176176}
177- ; ( async ( ) => {
177+
178+ function parseCliArgs ( ) {
178179 const { values } = parseArgs ( {
179180 args : process . argv . slice ( 2 ) ,
180181 options : {
@@ -186,69 +187,65 @@ function isOnDefaultBranch(defaultBranch: string): boolean {
186187 const defaultBranch = ( values . branch as string | undefined ) ?. trim ( )
187188 if ( ! defaultBranch ) {
188189 throw new Error (
189- ` ❌ Missing required --branch flag.
190- Please specify the default branch name (e.g., --branch main)
191- Example: pnpm run generate:docs --branch main`
190+ " ❌ Missing required --branch flag.\n" +
191+ " Please specify the default branch name (e.g., --branch main)\n" +
192+ " Example: pnpm run generate:docs --branch main"
192193 )
193194 }
194195
195- const rawVersions = ( values . versions as string | undefined ) ?. trim ( ) ?? ""
196- const hasVersionsArg = rawVersions . length > 0
196+ const versionsSpec = ( values . versions as string | undefined ) ?. trim ( ) || undefined
197+
198+ return { defaultBranch, versionsSpec }
199+ }
200+
201+ function buildLatestVersion ( onDefaultBranch : boolean , defaultBranch : string ) {
202+ if ( onDefaultBranch ) {
203+ buildBranch ( defaultBranch , "latest" )
204+ } else {
205+ buildDocs ( workspaceRoot , join ( outputDir , "latest" ) )
206+ }
207+ }
208+
209+ function writeVersionsFile ( versions : string [ ] ) {
210+ const versionsFile = resolve ( "app/utils/versions.ts" )
211+ const content = `// Auto-generated file. Do not edit manually.\nexport const versions = ${ JSON . stringify ( versions , null , 2 ) } as const\n`
212+
213+ writeFileSync ( versionsFile , content )
214+ }
197215
198- let builtVersions : string [ ] = [ ]
216+ async function main ( ) {
217+ const { defaultBranch, versionsSpec } = parseCliArgs ( )
199218
200219 const onDefaultBranch = isOnDefaultBranch ( defaultBranch )
201- const currentBranchToBuild = onDefaultBranch ? defaultBranch : getCurrentBranch ( )
202220
203- // biome-ignore lint/suspicious/noConsole: keep for logging
204- console . log ( chalk . cyan ( `Building from branch: ${ currentBranchToBuild } ` ) )
221+ let builtVersions : string [ ]
205222
206- if ( hasVersionsArg ) {
207- const tags = resolveTagsFromSpec ( rawVersions )
208- if ( ! tags . length ) throw new Error ( `No tags matched spec "${ rawVersions } ".` )
223+ if ( versionsSpec ) {
224+ const tags = resolveTagsFromSpec ( versionsSpec )
225+ if ( tags . length === 0 ) {
226+ throw new Error ( `No tags matched spec "${ versionsSpec } ".` )
227+ }
209228
210- // biome-ignore lint/suspicious/noConsole: keep for logging
229+ // biome-ignore lint/suspicious/noConsole: keep for debugging
211230 console . log ( chalk . cyan ( `Building tags: ${ tags . join ( ", " ) } ` ) )
212- for ( const t of tags ) buildTag ( t )
213-
214- if ( onDefaultBranch ) {
215- // biome-ignore lint/suspicious/noConsole: keep for logging
216- console . log ( chalk . cyan ( `Building default branch '${ defaultBranch } ' → current` ) )
217- buildBranch ( defaultBranch , "current" )
218- } else {
219- // biome-ignore lint/suspicious/noConsole: keep for logging
220- console . log ( chalk . cyan ( "Building current workspace → current" ) )
221- buildDocs ( workspaceRoot , join ( outputDir , "current" ) )
231+ for ( const tag of tags ) {
232+ buildTag ( tag )
222233 }
223234
224- builtVersions = [ "current" , ...tags ]
235+ buildLatestVersion ( onDefaultBranch , defaultBranch )
236+ builtVersions = [ "latest" , ...tags ]
225237 } else {
226- if ( onDefaultBranch ) {
227- // biome-ignore lint/suspicious/noConsole: keep for logging
228- console . log ( chalk . cyan ( `Building default branch '${ defaultBranch } ' → current` ) )
229- buildBranch ( defaultBranch , "current" )
230- } else {
231- // biome-ignore lint/suspicious/noConsole: keep for logging
232- console . log ( chalk . cyan ( "Building current workspace → current" ) )
233- buildDocs ( workspaceRoot , join ( outputDir , "current" ) )
234- }
235-
236- builtVersions = [ "current" ]
238+ buildLatestVersion ( onDefaultBranch , defaultBranch )
239+ builtVersions = [ "latest" ]
237240 }
238241
239- const versionsFile = resolve ( "app/utils/versions.ts" )
240- writeFileSync (
241- versionsFile ,
242- `// Auto-generated file. Do not edit manually.
243- export const versions = ${ JSON . stringify ( builtVersions , null , 2 ) } as const
244- `
245- )
246- // biome-ignore lint/suspicious/noConsole: keep for logging
247- console . log ( chalk . green ( `✔ Wrote versions.ts → ${ versionsFile } ` ) )
248- // biome-ignore lint/suspicious/noConsole: keep for logging
242+ writeVersionsFile ( builtVersions )
243+ // biome-ignore lint/suspicious/noConsole: keep for debugging
249244 console . log ( chalk . green ( "✅ Done" ) )
250- } ) ( ) . catch ( ( e ) => {
251- // biome-ignore lint/suspicious/noConsole: keep for logging
252- console . error ( chalk . red ( "❌ Build failed:" ) , e )
245+ }
246+
247+ main ( ) . catch ( ( error ) => {
248+ // biome-ignore lint/suspicious/noConsole: keep for debugging
249+ console . error ( chalk . red ( "❌ Build failed:" ) , error )
253250 process . exit ( 1 )
254251} )
0 commit comments