Skip to content

Commit 0320871

Browse files
author
abrulic
committed
updated generate-docs
1 parent 5b217bf commit 0320871

1 file changed

Lines changed: 80 additions & 100 deletions

File tree

scripts/generate-docs.ts

Lines changed: 80 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { type ExecSyncOptions, execSync } from "node:child_process"
22
import { cpSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
33
import os from "node:os"
4-
import path, { join, resolve } from "node:path"
4+
import { join, resolve } from "node:path"
55
import { parseArgs } from "node:util"
66
import chalk from "chalk"
77
import semver from "semver"
8-
import { getServerEnv } from "~/env.server"
8+
// import { getServerEnv } from "~/env.server"
99

10-
const APP_ENV = getServerEnv().APP_ENV as "development" | "production"
10+
// const APP_ENV = getServerEnv().APP_ENV as "development" | "production"
1111
const CONTENT_DIR = "content"
1212
const OUTPUT_DIR = "generated-docs"
1313
const CURRENT_WORKSPACE = process.cwd()
@@ -40,11 +40,11 @@ function resetDir(dir: string) {
4040
ensureDir(dir)
4141
}
4242

43-
function repoPath(...segments: string[]) {
44-
return path.normalize(path.join(...segments.filter(Boolean)))
45-
}
43+
// function repoPath(...segments: string[]) {
44+
// return path.normalize(path.join(...segments.filter(Boolean)))
45+
// }
4646

47-
const REPO_ROOT = run("git rev-parse --show-toplevel", { cwd: CURRENT_WORKSPACE })
47+
// const REPO_ROOT = run("git rev-parse --show-toplevel", { cwd: CURRENT_WORKSPACE })
4848

4949
const WORKSPACE_RELATIVE = (() => {
5050
try {
@@ -72,28 +72,28 @@ function resolveTagsFromSpec(spec: string) {
7272
return matchedTags.sort(semver.rcompare)
7373
}
7474

75-
function hasLocalRef(ref: string) {
76-
try {
77-
run(`git show-ref --verify --quiet ${ref}`)
78-
return true
79-
} catch {
80-
return false
81-
}
82-
}
83-
84-
function refHasPath(ref: string, pathFromRepoRoot: string): boolean {
85-
const cleanPath = pathFromRepoRoot.replace(/^\/+/, "").replace(/\/+$/, "")
86-
try {
87-
run(`git -C "${REPO_ROOT}" rev-parse --verify --quiet "${ref}:${cleanPath}"`)
88-
return true
89-
} catch {
90-
return false
91-
}
92-
}
93-
94-
function fetchBranch(branch: string) {
95-
run(`git fetch --tags --prune origin ${branch}`, { cwd: CURRENT_WORKSPACE, inherit: true })
96-
}
75+
// function hasLocalRef(ref: string) {
76+
// try {
77+
// run(`git show-ref --verify --quiet ${ref}`)
78+
// return true
79+
// } catch {
80+
// return false
81+
// }
82+
// }
83+
84+
// function refHasPath(ref: string, pathFromRepoRoot: string): boolean {
85+
// const cleanPath = pathFromRepoRoot.replace(/^\/+/, "").replace(/\/+$/, "")
86+
// try {
87+
// run(`git -C "${REPO_ROOT}" rev-parse --verify --quiet "${ref}:${cleanPath}"`)
88+
// return true
89+
// } catch {
90+
// return false
91+
// }
92+
// }
93+
94+
// function fetchBranch(branch: string) {
95+
// run(`git fetch --tags --prune origin ${branch}`, { cwd: CURRENT_WORKSPACE, inherit: true })
96+
// }
9797

9898
function buildDocs(sourceDir: string, outDir: string) {
9999
if (!existsSync(sourceDir)) {
@@ -167,12 +167,12 @@ function buildFromRef(ref: string, label: string) {
167167
}
168168
}
169169

170-
function buildFromBranch(branch: string, label: string) {
171-
fetchBranch(branch)
172-
const localRef = `refs/heads/${branch}`
173-
const targetRef = hasLocalRef(localRef) ? localRef : `origin/${branch}`
174-
buildFromRef(targetRef, label)
175-
}
170+
// function buildFromBranch(branch: string, label: string) {
171+
// fetchBranch(branch)
172+
// const localRef = `refs/heads/${branch}`
173+
// const targetRef = hasLocalRef(localRef) ? localRef : `origin/${branch}`
174+
// buildFromRef(targetRef, label)
175+
// }
176176

177177
function buildFromTag(tag: string) {
178178
buildFromRef(`refs/tags/${tag}`, tag)
@@ -183,42 +183,20 @@ function buildCurrentWorkspace(label: string) {
183183
buildDocs(CURRENT_WORKSPACE, outDir)
184184
}
185185

186-
function buildMainBranchFallback(branch: string) {
187-
const contentPath = repoPath(WORKSPACE_RELATIVE, CONTENT_DIR)
188-
fetchBranch(branch)
189-
190-
const hasContent = refHasPath(`origin/${branch}`, contentPath)
191-
if (!hasContent) {
192-
throw new Error(`Branch 'origin/${branch}' has no '${contentPath}'. Cannot build docs.`)
193-
}
194-
195-
// biome-ignore lint/suspicious/noConsole: build logging
196-
console.log(chalk.cyan(`Building fallback: branch '${branch}' → ${branch}`))
197-
buildFromBranch(branch, branch)
198-
return [branch]
199-
}
200-
201-
function parseCliArgs() {
202-
const { values } = parseArgs({
203-
args: process.argv.slice(2),
204-
options: {
205-
versions: { type: "string" },
206-
branch: { type: "string" },
207-
},
208-
})
209-
210-
const branch = (values.branch as string | undefined)?.trim()
211-
if (!branch) {
212-
throw new Error(
213-
`❌ Missing required --branch flag
214-
Usage: pnpm run generate:docs --branch main [--versions ">=1.0.0"]`
215-
)
216-
}
186+
// function buildMainBranchFallback(branch: string) {
187+
// const contentPath = repoPath(WORKSPACE_RELATIVE, CONTENT_DIR)
188+
// fetchBranch(branch)
217189

218-
const versions = (values.versions as string | undefined)?.trim() ?? ""
190+
// const hasContent = refHasPath(`origin/${branch}`, contentPath)
191+
// if (!hasContent) {
192+
// throw new Error(`Branch 'origin/${branch}' has no '${contentPath}'. Cannot build docs.`)
193+
// }
219194

220-
return { branch, versions }
221-
}
195+
// // biome-ignore lint/suspicious/noConsole: build logging
196+
// console.log(chalk.cyan(`Building fallback: branch '${branch}' → ${branch}`))
197+
// buildFromBranch(branch, branch)
198+
// return [branch]
199+
// }
222200

223201
function writeVersionsFile(versions: string[]) {
224202
const versionsFile = resolve("app/utils/versions.ts")
@@ -230,51 +208,53 @@ export const versions = ${JSON.stringify(versions, null, 2)} as const
230208
// biome-ignore lint/suspicious/noConsole: build logging
231209
console.log(chalk.green(`✔ Wrote versions.ts → ${versionsFile}`))
232210
}
211+
function parseCliArgs() {
212+
const { values } = parseArgs({
213+
args: process.argv.slice(2),
214+
options: {
215+
versions: { type: "string" }, // optional
216+
},
217+
})
218+
219+
const versions = (values.versions as string | undefined)?.trim() ?? ""
220+
return { versions }
221+
}
233222

234223
async function main() {
235-
const { branch, versions: versionsSpec } = parseCliArgs()
224+
const { versions: versionsSpec } = parseCliArgs()
236225
let builtVersions: string[]
237226

238-
if (APP_ENV === "development") {
239-
// Development: always build current workspace
240-
// biome-ignore lint/suspicious/noConsole: build logging
241-
console.log(chalk.cyan("[dev] Building current workspace → current"))
242-
buildCurrentWorkspace("current")
243-
builtVersions = ["current"]
244-
} else {
245-
// Production
246-
if (versionsSpec) {
247-
// Try to build from version tags
248-
const tags = resolveTagsFromSpec(versionsSpec)
249-
250-
if (tags.length > 0) {
251-
// Build all matching tags
252-
// biome-ignore lint/suspicious/noConsole: build logging
253-
console.log(chalk.cyan(`[prod] Building tags: ${tags.join(", ")}`))
254-
for (const tag of tags) {
255-
buildFromTag(tag)
256-
}
257-
builtVersions = tags
258-
} else {
259-
// No tags matched → fallback to main branch
260-
// biome-ignore lint/suspicious/noConsole: build logging
261-
console.log(chalk.yellow(`[prod] No tags matched "${versionsSpec}", falling back to branch`))
262-
builtVersions = buildMainBranchFallback(branch)
227+
// Try to build version tags if specified
228+
if (versionsSpec) {
229+
const tags = resolveTagsFromSpec(versionsSpec)
230+
231+
if (tags.length > 0) {
232+
// Build version tags
233+
// biome-ignore lint/suspicious/noConsole: build logging
234+
console.log(chalk.cyan(`Building tags: ${tags.join(", ")}`))
235+
for (const tag of tags) {
236+
buildFromTag(tag)
263237
}
238+
builtVersions = tags
264239
} else {
265-
// No versions specifiedbuild main branch
240+
// No tags matchedfallback to current workspace
266241
// biome-ignore lint/suspicious/noConsole: build logging
267-
console.log(chalk.cyan(`[prod] No versions specified, building branch '${branch}'`))
268-
builtVersions = buildMainBranchFallback(branch)
242+
console.log(chalk.yellow(`No tags matched "${versionsSpec}", building current workspace`))
243+
buildCurrentWorkspace("current")
244+
builtVersions = ["current"]
269245
}
246+
} else {
247+
// No versions specified → build current workspace
248+
// biome-ignore lint/suspicious/noConsole: build logging
249+
console.log(chalk.cyan("Building current workspace → current"))
250+
buildCurrentWorkspace("current")
251+
builtVersions = ["current"]
270252
}
271253

272254
writeVersionsFile(builtVersions)
273-
274255
// biome-ignore lint/suspicious/noConsole: build logging
275256
console.log(chalk.green("✅ Done"))
276257
}
277-
278258
main().catch((error) => {
279259
// biome-ignore lint/suspicious/noConsole: error logging
280260
console.error(chalk.red("❌ Build failed:"), error)

0 commit comments

Comments
 (0)