Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

publish-staged-docs.ts

49 lines1.5 KB

1import { existsSync } from 'node:fs'
2import { cp, rm } from 'node:fs/promises'
3import { resolve } from 'node:path'
4​
5/**
6 * Replace `outputDir` with `stagingDir` only after TypeDoc wrote index.md.
7 * Copy the new tree beside dest first so a failed swap cannot leave dest empty.
8 */
9export async function publishStagedDocs(
10 stagingDir: string,
11 outputDir: string,
12): Promise<void> {
13 const stagedIndex = resolve(stagingDir, 'index.md')
14 if (!existsSync(stagedIndex)) {
15 await rm(stagingDir, { recursive: true, force: true })
16 throw new Error(
17 `TypeDoc did not write ${stagedIndex}. Existing docs/reference was not replaced.`,
18 )
19 }
20​
21 const nextDir = `${outputDir}.next`
22 await rm(nextDir, { recursive: true, force: true })
23 await cp(stagingDir, nextDir, { recursive: true })
24​
25 try {
26 await rm(outputDir, { recursive: true, force: true })
27 await cp(nextDir, outputDir, { recursive: true })
28 } catch (error) {
29 if (
30 !existsSync(resolve(outputDir, 'index.md')) &&
31 existsSync(resolve(nextDir, 'index.md'))
32 ) {
33 try {
34 await cp(nextDir, outputDir, { recursive: true })
35 } catch {
36 // Fall through to the dest-missing throw.
37 }
38 }
39 if (!existsSync(resolve(outputDir, 'index.md'))) {
40 throw new Error(
41 `Failed to replace ${outputDir}. New docs are in ${nextDir}.`,
42 { cause: error },
43 )
44 }
45 }
46​
47 await rm(nextDir, { recursive: true, force: true })
48 await rm(stagingDir, { recursive: true, force: true })