()
| 51 | main() |
| 52 | |
| 53 | async function main() { |
| 54 | // Create the new product dir. |
| 55 | await mkdirp(newProductPath) |
| 56 | |
| 57 | // Add redirects to the frontmatter of the to-be-moved files. |
| 58 | oldCategoryFiles.forEach((file) => { |
| 59 | const { content, data } = frontmatter(fs.readFileSync(file, 'utf8')) |
| 60 | const redirectString = file.replace(contentDir, '').replace('index.md', '').replace('.md', '') |
| 61 | data.redirect_from = addRedirectToFrontmatter(data.redirect_from, redirectString) |
| 62 | fs.writeFileSync(file, frontmatter.stringify(content, data, { lineWidth: 10000 })) |
| 63 | }) |
| 64 | |
| 65 | // Move the files. |
| 66 | execSync(`git mv ${oldCategoryPath}/* ${newProductPath}`) |
| 67 | |
| 68 | // Remove the category from the old product TOC. |
| 69 | const oldProductTocPath = path.posix.join(oldProductPath, 'index.md') |
| 70 | const productToc = frontmatter(fs.readFileSync(oldProductTocPath, 'utf8')) |
| 71 | productToc.data.children = productToc.data.children.filter( |
| 72 | (child) => child !== `/${oldCategoryId}` |
| 73 | ) |
| 74 | fs.writeFileSync( |
| 75 | oldProductTocPath, |
| 76 | frontmatter.stringify(productToc.content, productToc.data, { lineWidth: 10000 }) |
| 77 | ) |
| 78 | |
| 79 | // Add the new product to the homepage TOC. |
| 80 | const homepage = path.posix.join(contentDir, 'index.md') |
| 81 | const homepageToc = frontmatter(fs.readFileSync(homepage, 'utf8')) |
| 82 | homepageToc.data.children.push(newProduct) |
| 83 | fs.writeFileSync( |
| 84 | homepage, |
| 85 | frontmatter.stringify(homepageToc.content, homepageToc.data, { lineWidth: 10000 }) |
| 86 | ) |
| 87 | |
| 88 | console.log(`Moved ${oldCategory} files to ${newProduct}, added redirects, and updated TOCs!`) |
| 89 | } |
no test coverage detected