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
|
- #!/usr/bin/env node
- const fs = require('fs')
- const path = require('path')
- const mkdirp = require('mkdirp').sync
- const program = require('commander')
- const { execSync } = require('child_process')
- const frontmatter = require('../lib/read-frontmatter')
- const addRedirectToFrontmatter = require('./helpers/add-redirect-to-frontmatter')
- const walkFiles = require('./helpers/walk-files')
- const contentFiles = walkFiles('content', '.md')
- const contentDir = path.posix.join(process.cwd(), 'content')
- // [start-readme]
- //
- // Move the files from a category directory to a top-level product and add redirects.
- //
- // [end-readme]
- program
- .description('Move a category-level docs set to the product level.')
- .requiredOption('-c, --category <PATH>', 'Provide the path of the existing category, e.g., github/github-pages')
- .requiredOption('-p, --product <PATH>', 'Provide the path of the new product, e.g., pages')
- .parse(process.argv)
- const oldCategory = program.opts().category.replace('content/', '')
- const newProduct = program.opts().product.replace('content/', '')
- const [oldProductId, oldCategoryId] = oldCategory.split('/')
- const oldCategoryPath = path.posix.join(contentDir, oldCategory)
- const oldProductPath = path.posix.join(contentDir, oldProductId)
- if (!fs.existsSync(oldProductPath)) {
- console.error(`Error! Can't find ${oldProductPath}`)
- process.exit(1)
- }
- const oldCategoryFiles = contentFiles.filter(file => file.includes(`/${oldCategoryId}/`))
- if (!oldCategoryFiles.length) {
- console.error(`Error! Can't find ${oldCategory} files`)
- process.exit(1)
- }
- const newProductPath = path.posix.join(process.cwd(), 'content', newProduct)
- main()
- function main () {
- // Create the new product dir.
- mkdirp(newProductPath)
- // Add redirects to the frontmatter of the to-be-moved files.
- oldCategoryFiles.forEach(file => {
- const { content, data } = frontmatter(fs.readFileSync(file, 'utf8'))
- const redirectString = file
- .replace(contentDir, '')
- .replace('index.md', '')
- .replace('.md', '')
- data.redirect_from = addRedirectToFrontmatter(data.redirect_from, redirectString)
- fs.writeFileSync(file, frontmatter.stringify(content, data, { lineWidth: 10000 }))
- })
- // Move the files.
- execSync(`git mv ${oldCategoryPath}/* ${newProductPath}`)
- // Remove the category from the old product TOC.
- const oldProductTocPath = path.posix.join(oldProductPath, 'index.md')
- const productToc = frontmatter(fs.readFileSync(oldProductTocPath, 'utf8'))
- productToc.data.children = productToc.data.children.filter(child => child !== `/${oldCategoryId}`)
- fs.writeFileSync(oldProductTocPath, frontmatter.stringify(productToc.content, productToc.data, { lineWidth: 10000 }))
-
- // Add the new product to the homepage TOC.
- const homepage = path.posix.join(contentDir, 'index.md')
- const homepageToc = frontmatter(fs.readFileSync(homepage, 'utf8'))
- homepageToc.data.children.push(newProduct)
- fs.writeFileSync(homepage, frontmatter.stringify(homepageToc.content, homepageToc.data, { lineWidth: 10000 }))
- console.log(`Moved ${oldCategory} files to ${newProduct}, added redirects, and updated TOCs!`)
- }
|