{{ message }}
Repository navigation
Expand file tree
/
Copy pathmove-by-content-type.ts
More file actions
614 lines (519 loc) · 22.5 KB
/
Copy pathmove-by-content-type.ts
File metadata and controls
614 lines (519 loc) · 22.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// @purpose Writer tool
// @description Move files to the relevant directory based on `contentType` frontmatter
import { program } from 'commander'
import fs from 'fs/promises'
import path from 'path'
import chalk from 'chalk'
import { execFileSync } from 'child_process'
import walkFiles from '@/workflows/walk-files.js'
import readFrontmatter from '@/frame/lib/read-frontmatter.js'
import { contentTypesEnum } from '@/frame/lib/frontmatter'
const CONTENT_TYPES = contentTypesEnum.filter(
(type) => type !== 'homepage' && type !== 'other' && type !== 'landing',
)
// Three segments identify content/<product>/index.md and top-level content-type directories.
const PRODUCT_LEVEL_PATH_SEGMENTS = 3
const contentTypeToDir = (contentType: string): string => {
return contentType === 'rai' ? 'responsible-use' : contentType
}
const validContentTypeDirs = new Set(CONTENT_TYPES.map(contentTypeToDir))
function shouldSkipIndexFile(filePath: string): boolean {
const relativePath = path.relative(process.cwd(), filePath)
const parts = relativePath.split(path.sep)
const contentIndex = parts.indexOf('content')
// Keep product-level index.md files in place.
if (parts.length === contentIndex + PRODUCT_LEVEL_PATH_SEGMENTS) return true
// Keep content-type index.md files that already sit at content/product/content-type/index.md.
if (parts.length === contentIndex + 4) {
const parentDir = parts[parts.length - 2]
if (validContentTypeDirs.has(parentDir)) return true
}
return false
}
function calculateTarget(filePath: string, contentType: string, productDir: string) {
const relativePath = path.relative(process.cwd(), filePath)
const parts = relativePath.split(path.sep)
const contentIndex = parts.indexOf('content')
const fileName = path.basename(filePath)
const targetContentType = contentTypeToDir(contentType)
if (targetContentType === 'how-tos') {
// How-to pages keep their product subdirectory structure.
const pathAfterProduct = parts.slice(contentIndex + 2, -1)
if (pathAfterProduct[0] === 'how-tos') {
return { targetDir: path.dirname(filePath), targetPath: filePath }
} else {
const targetDir = path.join(productDir, targetContentType, ...pathAfterProduct)
return { targetDir, targetPath: path.join(targetDir, fileName) }
}
} else {
// Other content types flatten into their content-type directory.
const targetDir = path.join(productDir, targetContentType)
return { targetDir, targetPath: path.join(targetDir, fileName) }
}
}
interface FileMove {
filePath: string
targetDir: string
targetPath: string
contentType: string
}
program
.name('content-type-based-move')
.description('Reorganize content files into subdirectories based on their contentType property')
.argument('[paths...]', 'Content paths to process')
.action(async (paths: string[]) => {
const filesToProcess: string[] = []
if (paths?.length > 0) {
for (const p of paths) {
const stats = await fs.stat(p)
if (stats.isDirectory()) {
filesToProcess.push(...(await walkFiles(p, ['.md'])))
} else if (p.endsWith('.md')) {
filesToProcess.push(p)
}
}
} else {
filesToProcess.push(...(await walkFiles(path.join(process.cwd(), 'content'), ['.md'])))
}
console.log(chalk.white(`Processing ${filesToProcess.length} files...\n`))
console.log(chalk.white('Analyzing files...\n'))
const filesToMove: FileMove[] = []
const skipped: Array<{ file: string; reason: string }> = []
const targetDirs = new Set<string>()
const subdirTargets = new Set<string>()
const productDirs = new Set<string>()
const productsWithRai = new Set<string>()
for (const filePath of filesToProcess) {
const relativePath = path.relative(process.cwd(), filePath)
try {
if (path.basename(filePath) === 'index.md' && shouldSkipIndexFile(filePath)) {
continue
}
const fileContent = await fs.readFile(filePath, 'utf-8')
const { data } = readFrontmatter(fileContent)
if (!data?.contentType) {
skipped.push({ file: relativePath, reason: 'No contentType property found' })
console.log(chalk.yellow(`⚠ Skipping ${relativePath}: No contentType property`))
continue
}
const contentType = data.contentType as string
const parts = relativePath.split(path.sep)
const contentIndex = parts.indexOf('content')
// Landing pages belong at product-level index.md files; this script does not move them.
if (contentType === 'landing') {
console.log(chalk.gray(`→ Skipping ${relativePath}: landing pages don't move`))
continue
}
if (!CONTENT_TYPES.includes(contentType)) {
skipped.push({ file: relativePath, reason: `Invalid contentType: ${contentType}` })
console.log(
chalk.yellow(`⚠ Skipping ${relativePath}: Invalid contentType "${contentType}"`),
)
continue
}
if (contentIndex === -1 || contentIndex + 1 >= parts.length) {
console.log(
chalk.yellow(`⚠ Skipping ${relativePath}: Cannot determine product directory`),
)
continue
}
const productName = parts[contentIndex + 1]
const productDir = path.join(process.cwd(), 'content', productName)
productDirs.add(productDir)
if (contentType === 'rai') productsWithRai.add(productName)
const { targetDir, targetPath } = calculateTarget(filePath, contentType, productDir)
if (path.dirname(filePath) === targetDir) continue
try {
await fs.access(targetPath)
skipped.push({ file: relativePath, reason: 'Target already exists' })
console.log(chalk.yellow(`⚠ Skipping ${relativePath}: Target file already exists`))
continue
} catch {
// Missing target means the move can proceed.
}
filesToMove.push({ filePath, targetDir, targetPath, contentType })
const relativeTargetDir = path.relative(process.cwd(), targetDir)
targetDirs.add(relativeTargetDir)
if (
path.basename(filePath) === 'index.md' &&
relativeTargetDir.split(path.sep).length > PRODUCT_LEVEL_PATH_SEGMENTS
) {
subdirTargets.add(relativeTargetDir)
}
} catch (error) {
if (error instanceof Error) {
console.error(
chalk.red(`✗ Error analyzing ${relativePath}: ${error.message}\n${error.stack}`),
)
} else {
console.error(chalk.red(`✗ Error analyzing ${relativePath}: ${String(error)}`))
}
skipped.push({ file: relativePath, reason: `Error: ${error}` })
}
}
console.log(chalk.white('Ensuring standard content-type directories exist...\n'))
if (paths?.length > 0) {
for (const p of paths) {
const fullPath = path.resolve(process.cwd(), p)
const relativePath = path.relative(process.cwd(), fullPath)
const parts = relativePath.split(path.sep)
const contentIndex = parts.indexOf('content')
if (contentIndex !== -1 && contentIndex + 1 < parts.length) {
const productName = parts[contentIndex + 1]
for (const ct of CONTENT_TYPES.filter((t) => t !== 'rai' && t !== 'landing')) {
targetDirs.add(path.join('content', productName, contentTypeToDir(ct)))
}
if (productsWithRai.has(productName)) {
targetDirs.add(path.join('content', productName, 'responsible-use'))
}
}
}
}
console.log(chalk.white('Creating placeholder index.md files...\n'))
const newPlaceholders: string[] = []
const titleMap: Record<string, string> = {
'get-started': 'Get started',
concepts: 'Concepts',
'how-tos': 'How-tos',
reference: 'Reference',
tutorials: 'Tutorials',
'responsible-use': 'Responsible use',
}
for (const dirPath of targetDirs) {
const absoluteDirPath = path.join(process.cwd(), dirPath)
const indexPath = path.join(absoluteDirPath, 'index.md')
try {
await fs.access(indexPath)
console.log(chalk.gray(`- Skipping ${dirPath}/index.md (already exists)`))
} catch {
// Create placeholders only for top-level content-type directories.
if (dirPath.split(path.sep).length > PRODUCT_LEVEL_PATH_SEGMENTS) continue
// Moved index.md files become the placeholder for their target directory.
if (subdirTargets.has(dirPath)) {
console.log(chalk.gray(`- Skipping ${dirPath}/index.md (will be moved)`))
continue
}
const contentTypeName = path.basename(dirPath)
const title = titleMap[contentTypeName] || contentTypeName
const placeholderContentType =
contentTypeName === 'responsible-use' ? 'rai' : contentTypeName
const content = `---
title: ${title}
versions:
fpt: '*'
ghec: '*'
ghes: '*'
contentType: ${placeholderContentType}
---
`
await fs.mkdir(absoluteDirPath, { recursive: true })
await fs.writeFile(indexPath, content, 'utf-8')
newPlaceholders.push(indexPath)
console.log(chalk.green(`✓ Created ${dirPath}/index.md`))
}
}
// This step used to shell out to `npm run ai-tools`. Those scripts now live
// in github/technical-content at .github/scripts/ai-tools, so the intros are
// generated as a separate step and the paths are reported here.
const placeholdersNeedingIntros: string[] = []
for (const placeholderPath of newPlaceholders) {
try {
const fileContent = await fs.readFile(placeholderPath, 'utf-8')
const { data } = readFrontmatter(fileContent)
if (data?.intro) continue
placeholdersNeedingIntros.push(path.relative(process.cwd(), placeholderPath))
} catch (error) {
const detail = error instanceof Error ? error.message : String(error)
console.error(chalk.yellow(`⚠ Could not read ${placeholderPath}: ${detail}`))
}
}
console.log(chalk.white('\nMoving files...\n'))
const moved: Array<{ file: string; from: string; to: string }> = []
// Move regular files and index.md files in separate groups to avoid path conflicts.
const regularFiles = filesToMove.filter((f) => path.basename(f.filePath) !== 'index.md')
const topLevelIndexFiles = filesToMove.filter((f) => {
if (path.basename(f.filePath) !== 'index.md') return false
return (
path.relative(process.cwd(), f.targetDir).split(path.sep).length ===
PRODUCT_LEVEL_PATH_SEGMENTS
)
})
const subdirIndexFiles = filesToMove.filter((f) => {
if (path.basename(f.filePath) !== 'index.md') return false
return (
path.relative(process.cwd(), f.targetDir).split(path.sep).length >
PRODUCT_LEVEL_PATH_SEGMENTS
)
})
// Copy subdirectory index.md files first; delete sources after regular files move.
const indexFilesToDeleteLater: string[] = []
for (const file of subdirIndexFiles) {
try {
await fs.mkdir(file.targetDir, { recursive: true })
const content = await fs.readFile(file.filePath, 'utf-8')
const { data, content: body } = readFrontmatter(content)
// Clear children because the new content-type directory structure invalidates child paths.
if (data?.children) data.children = []
await fs.writeFile(
file.targetPath,
readFrontmatter.stringify(body || '', data || {}),
'utf-8',
)
indexFilesToDeleteLater.push(file.filePath)
moved.push({
file: path.relative(process.cwd(), file.filePath),
from: path.relative(process.cwd(), file.filePath),
to: path.relative(process.cwd(), file.targetPath),
})
console.log(chalk.green(`✓ Copied ${path.relative(process.cwd(), file.filePath)}`))
} catch (error) {
skipped.push({
file: path.relative(process.cwd(), file.filePath),
reason: `Error: ${error}`,
})
console.log(
chalk.red(`✗ Error copying ${path.relative(process.cwd(), file.filePath)}: ${error}`),
)
}
}
for (const file of regularFiles) {
try {
await fs.mkdir(file.targetDir, { recursive: true })
const relativeFilePath = path.relative(process.cwd(), file.filePath)
const relativeTargetPath = path.relative(process.cwd(), file.targetPath)
execFileSync(
'npm',
['run', 'move-content', '--', relativeFilePath, relativeTargetPath, '--no-git'],
{
cwd: process.cwd(),
stdio: 'inherit',
},
)
moved.push({ file: relativeFilePath, from: relativeFilePath, to: relativeTargetPath })
console.log(chalk.green(`✓ Moved ${relativeFilePath}`))
} catch (error) {
skipped.push({
file: path.relative(process.cwd(), file.filePath),
reason: `Error: ${error}`,
})
console.log(
chalk.red(`✗ Error moving ${path.relative(process.cwd(), file.filePath)}: ${error}`),
)
}
}
for (const sourcePath of indexFilesToDeleteLater) {
try {
await fs.unlink(sourcePath)
console.log(chalk.gray(`✓ Deleted source ${path.relative(process.cwd(), sourcePath)}`))
} catch (error) {
console.log(chalk.yellow(`⚠ Could not delete ${sourcePath}: ${error}`))
}
}
for (const file of topLevelIndexFiles) {
try {
await fs.mkdir(file.targetDir, { recursive: true })
await fs.copyFile(file.filePath, file.targetPath)
await fs.unlink(file.filePath)
moved.push({
file: path.relative(process.cwd(), file.filePath),
from: path.relative(process.cwd(), file.filePath),
to: path.relative(process.cwd(), file.targetPath),
})
console.log(chalk.green(`✓ Moved ${path.relative(process.cwd(), file.filePath)}`))
} catch (error) {
skipped.push({
file: path.relative(process.cwd(), file.filePath),
reason: `Error: ${error}`,
})
console.log(
chalk.red(`✗ Error moving ${path.relative(process.cwd(), file.filePath)}: ${error}`),
)
}
}
console.log(
chalk.white('\nCleaning up old directories and updating parent index.md files...\n'),
)
const deletedByProduct = new Map<string, string[]>()
for (const productDir of productDirs) {
const productName = path.basename(productDir)
const retainedChildren = new Set<string>()
try {
const entries = await fs.readdir(productDir, { withFileTypes: true })
for (const entry of entries) {
if (entry.isDirectory() && !validContentTypeDirs.has(entry.name)) {
const oldDirPath = path.join(productDir, entry.name)
const relativeOldDirPath = path.relative(process.cwd(), oldDirPath)
try {
// Unmoved articles and typed indexes would be lost; untyped category indexes go with the directory.
const remaining: string[] = []
for (const file of walkFiles(oldDirPath, ['.md'], { includeEarlyAccess: true })) {
if (path.basename(file) !== 'index.md') {
remaining.push(file)
continue
}
const { data } = readFrontmatter(await fs.readFile(file, 'utf-8'))
if (!data || data.contentType) remaining.push(file)
}
if (remaining.length > 0) {
const examples = remaining
.slice(0, 3)
.map((file) => path.relative(process.cwd(), file))
.join(', ')
console.log(
chalk.yellow(
`⚠ Not deleting ${relativeOldDirPath}: ${remaining.length} unmoved articles remain (${examples})`,
),
)
skipped.push({
file: relativeOldDirPath,
reason: `Not deleted: ${remaining.length} unmoved articles remain`,
})
try {
await fs.access(path.join(oldDirPath, 'index.md'))
retainedChildren.add(`/${entry.name}`)
} catch {
// A category without an index cannot be listed as a child.
}
continue
}
await fs.rm(oldDirPath, { recursive: true, force: true })
console.log(chalk.gray(`✓ Deleted old directory: ${relativeOldDirPath}`))
if (!deletedByProduct.has(productName)) deletedByProduct.set(productName, [])
deletedByProduct.get(productName)!.push(`/${productName}/${entry.name}`)
} catch (error) {
console.log(chalk.yellow(`⚠ Could not delete ${oldDirPath}: ${error}`))
}
}
}
} catch (error) {
console.log(chalk.yellow(`⚠ Could not read product directory ${productDir}: ${error}`))
}
const productIndexPath = path.join(productDir, 'index.md')
try {
const content = await fs.readFile(productIndexPath, 'utf-8')
const { data, content: body } = readFrontmatter(content)
if (data) {
let updated = false
const productRelativePath = path.relative(process.cwd(), productDir)
const newChildren: string[] = []
for (const ct of CONTENT_TYPES.map(contentTypeToDir)) {
const dirPath = path.join(productRelativePath, ct)
if (targetDirs.has(dirPath)) newChildren.push(`/${ct}`)
}
if (newChildren.length > 0) {
// Retained categories stay reachable in the tree until their remaining articles move.
const keptChildren = Array.isArray(data.children)
? data.children.filter((child: string) => retainedChildren.has(child))
: []
data.children = [...newChildren, ...keptChildren]
updated = true
}
const deletedPaths = deletedByProduct.get(productName) || []
if (deletedPaths.length > 0) {
if (!data.redirect_from) data.redirect_from = []
else if (!Array.isArray(data.redirect_from)) data.redirect_from = [data.redirect_from]
for (const deletedPath of deletedPaths) {
if (!data.redirect_from.includes(deletedPath)) {
data.redirect_from.push(deletedPath)
}
}
updated = true
}
if (updated) {
await fs.writeFile(productIndexPath, readFrontmatter.stringify(body, data), 'utf-8')
const changes = []
if (newChildren.length > 0) changes.push(`${newChildren.length} children`)
if (deletedPaths.length > 0) changes.push(`${deletedPaths.length} redirects`)
console.log(
chalk.green(
`✓ Updated ${path.relative(process.cwd(), productIndexPath)} (${changes.join(', ')})`,
),
)
}
}
} catch (error) {
console.log(chalk.yellow(`⚠ Could not update ${productIndexPath}: ${error}`))
}
}
console.log(chalk.white('\nSorting children arrays...\n'))
for (const dirPath of targetDirs) {
const absoluteDirPath = path.join(process.cwd(), dirPath)
const indexPath = path.join(absoluteDirPath, 'index.md')
try {
const content = await fs.readFile(indexPath, 'utf-8')
const { data, content: body } = readFrontmatter(content)
if (!data) continue
// how-tos children point to subdirectories.
if (path.basename(dirPath) === 'how-tos') {
const entries = await fs.readdir(absoluteDirPath, { withFileTypes: true })
const subdirs = entries
.filter((e) => e.isDirectory())
.map((e) => `/${e.name}`)
.sort()
if (subdirs.length > 0) {
data.children = subdirs
await fs.writeFile(indexPath, readFrontmatter.stringify(body, data), 'utf-8')
console.log(
chalk.green(
`✓ Added children to ${path.relative(process.cwd(), indexPath)} (${subdirs.length} subdirectories)`,
),
)
}
}
// Other content types sort about-* pages first.
else if (data.children && Array.isArray(data.children) && data.children.length > 0) {
const sorted = [...data.children].sort((a, b) => {
const aBasename = path.basename(a)
const bBasename = path.basename(b)
const aIsAbout = aBasename.startsWith('about-')
const bIsAbout = bBasename.startsWith('about-')
if (aIsAbout && !bIsAbout) return -1
if (!aIsAbout && bIsAbout) return 1
return aBasename.localeCompare(bBasename)
})
if (JSON.stringify(sorted) !== JSON.stringify(data.children)) {
data.children = sorted
await fs.writeFile(indexPath, readFrontmatter.stringify(body, data), 'utf-8')
console.log(
chalk.green(`✓ Sorted children in ${path.relative(process.cwd(), indexPath)}`),
)
}
}
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
console.log(chalk.yellow(`⚠ Could not update ${indexPath}: ${error}`))
}
}
}
console.log(chalk.white(`\n${'='.repeat(60)}`))
console.log(chalk.white('Summary:'))
console.log(chalk.white(` Moved: ${moved.length} files`))
console.log(chalk.white(` Skipped: ${skipped.length} files`))
if (newPlaceholders.length > 0) {
console.log(
chalk.cyan(`\nNote: ${newPlaceholders.length} placeholder index.md files were created.`),
)
}
if (placeholdersNeedingIntros.length > 0) {
console.log(
chalk.cyan(`\n${placeholdersNeedingIntros.length} placeholder files still need an intro.`),
)
console.log(chalk.cyan('Generate them from a github/technical-content checkout:'))
console.log(
chalk.gray(
`\n cd .github/scripts && npm run ai-tools -- --prompt intro --write \\\n` +
` --docs-dir ${process.cwd()} \\\n` +
` --files ${placeholdersNeedingIntros.join(' ')}\n`,
),
)
console.log(chalk.cyan('Review the generated intros before committing.'))
}
console.log(chalk.blue('='.repeat(60)))
if (skipped.length > 0) {
console.log(chalk.yellow('\nSkipped files:'))
for (const skip of skipped) {
console.log(chalk.gray(` ${skip.file}: ${skip.reason}`))
}
}
})
program.parse()
You can’t perform that action at this time.
