Repository navigation
Expand file tree
/
Copy patharchive-docs-version.ts
More file actions
276 lines (232 loc) · 7.25 KB
/
Copy patharchive-docs-version.ts
File metadata and controls
276 lines (232 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { execFile as execFileCallback } from 'node:child_process'
import { access, mkdir, readdir, rm, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { promisify } from 'node:util'
import { DOCS_VERSION_ID_PATTERN } from '../lib/docs-version-order'
import { i18n } from '../lib/i18n'
const execFile = promisify(execFileCallback)
const MAX_BUFFER = 100 * 1024 * 1024
const PREFIX = '[docs-archive]'
export function isValidVersionId(version: string): boolean {
return DOCS_VERSION_ID_PATTERN.test(version)
}
export function isLocalizedFile(
filePath: string,
languages: readonly string[] = i18n.languages,
): boolean {
const basename = filePath.replaceAll('\\', '/').split('/').pop() ?? filePath
return languages.some(
(locale) => basename.endsWith(`.${locale}.mdx`) || basename === `meta.${locale}.json`,
)
}
function rewriteLinksWithCount(
content: string,
version: string,
): { content: string; count: number } {
const targetPattern =
/(\]\(\s*|href\s*=\s*(?:["']|\{\s*["'])|url:\s*)(\/docs)(?=\/|[?#)]|["'\s}])/g
let count = 0
let inFence = false
const segments = content.split(/(?<=\n)/)
const rewritten = segments.map((segment) => {
if (/^\s*```/.test(segment)) {
inFence = !inFence
return segment
}
if (inFence) {
return segment
}
return segment.replaceAll(targetPattern, (_match, prefix: string) => {
count += 1
return `${prefix}/${version}/docs`
})
})
return { content: rewritten.join(''), count }
}
export function rewriteArchivedLinks(content: string, version: string): string {
return rewriteLinksWithCount(content, version).content
}
export interface ArchiveOptions {
version: string
ref?: string
force?: boolean
cwd?: string
}
export interface ArchiveResult {
version: string
ref: string
filesWritten: number
filesSkippedLocalized: number
linksRewritten: number
}
async function pathExists(filePath: string): Promise<boolean> {
try {
await access(filePath)
return true
} catch {
return false
}
}
async function gitOutput(args: string[], cwd: string): Promise<Buffer> {
const result = await execFile('git', args, {
cwd,
encoding: 'buffer',
maxBuffer: MAX_BUFFER,
})
return result.stdout as Buffer
}
async function gitText(args: string[], cwd: string): Promise<string> {
return (await gitOutput(args, cwd)).toString('utf8')
}
async function prepareDestination(destination: string, force: boolean): Promise<void> {
if (await pathExists(destination)) {
let entries: string[]
try {
entries = await readdir(destination)
} catch {
entries = ['<non-directory>']
}
if (force) {
await rm(destination, { recursive: true, force: true })
} else if (entries.length > 0) {
throw new Error(
`${PREFIX} destination ${destination} is not empty; pass --force to replace it`,
)
}
}
await mkdir(destination, { recursive: true })
}
export async function archiveDocsVersion({
version,
ref = 'HEAD',
force = false,
cwd = process.cwd(),
}: ArchiveOptions): Promise<ArchiveResult> {
if (!isValidVersionId(version)) {
throw new Error(
`${PREFIX} invalid version "${version}"; expected e.g. v0.8.7, v0.8.8-rc2 or v0.8.x`,
)
}
const destination = path.join(cwd, 'content', 'docs-archive', version)
let paths: string[]
try {
const listing = await gitText(['ls-tree', '-r', '--name-only', ref, '--', 'content/docs'], cwd)
paths = listing.split('\n').filter(Boolean)
} catch {
throw new Error(`${PREFIX} git ref not found or cannot be read: ${ref}`)
}
await prepareDestination(destination, force)
let filesWritten = 0
let filesSkippedLocalized = 0
let linksRewritten = 0
for (const sourcePath of paths) {
if (isLocalizedFile(sourcePath)) {
filesSkippedLocalized += 1
continue
}
const relativePath = sourcePath.slice('content/docs/'.length)
const destinationPath = path.join(destination, ...relativePath.split('/'))
let contents: Buffer
try {
contents = await gitOutput(['show', `${ref}:${sourcePath}`], cwd)
} catch {
throw new Error(`${PREFIX} could not read ${sourcePath} from git ref ${ref}`)
}
if (sourcePath.endsWith('.mdx')) {
const rewritten = rewriteLinksWithCount(contents.toString('utf8'), version)
contents = Buffer.from(rewritten.content, 'utf8')
linksRewritten += rewritten.count
}
await mkdir(path.dirname(destinationPath), { recursive: true })
await writeFile(destinationPath, contents)
filesWritten += 1
}
return { version, ref, filesWritten, filesSkippedLocalized, linksRewritten }
}
function usage(): string {
return [
'Usage: pnpm docs:archive <version> [--ref=<git-ref>] [--force] [--help]',
'',
'Create an English-only archived snapshot of content/docs from a git ref.',
'',
'Options:',
' --ref=<git-ref> Read docs from this git ref (default: HEAD)',
' --force Replace an existing archive destination',
' --help Show this help message',
].join('\n')
}
interface CliOptions {
version?: string
ref: string
force: boolean
help: boolean
}
function parseArgs(args: string[]): CliOptions {
const options: CliOptions = { ref: 'HEAD', force: false, help: false }
for (const arg of args) {
if (arg === '--help' || arg === '-h') {
options.help = true
continue
}
if (arg === '--force') {
options.force = true
continue
}
if (arg.startsWith('--ref=')) {
options.ref = arg.slice('--ref='.length)
if (!options.ref) {
throw new Error(`${PREFIX} --ref requires a git ref`)
}
continue
}
if (arg.startsWith('-')) {
throw new Error(`${PREFIX} unknown option: ${arg}`)
}
if (options.version) {
throw new Error(`${PREFIX} expected one version argument, got: ${arg}`)
}
options.version = arg
}
if (!options.help && !options.version) {
throw new Error(`${PREFIX} a version is required`)
}
return options
}
export async function main(): Promise<void> {
let options: CliOptions
try {
options = parseArgs(process.argv.slice(2))
} catch (error) {
console.error(error instanceof Error ? error.message : `${PREFIX} ${String(error)}`)
console.error(usage())
process.exitCode = 1
return
}
if (options.help) {
console.log(usage())
return
}
try {
const result = await archiveDocsVersion({
// parseArgs rejects a missing version unless --help short-circuits above.
version: options.version!,
ref: options.ref,
force: options.force,
})
console.log(
`${PREFIX} version=${result.version} ref=${result.ref} files written=${result.filesWritten} files skipped as localized=${result.filesSkippedLocalized} links rewritten=${result.linksRewritten}`,
)
} catch (error) {
console.error(error instanceof Error ? error.message : `${PREFIX} ${String(error)}`)
process.exitCode = 1
}
}
const invokedPath = process.argv[1] ? path.resolve(process.argv[1]) : null
const modulePath = fileURLToPath(import.meta.url)
if (invokedPath === modulePath) {
main().catch((error) => {
console.error(error instanceof Error ? error.message : `${PREFIX} ${String(error)}`)
process.exitCode = 1
})
}