|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const readFrontmatter = require('../../lib/read-frontmatter') |
| 6 | +const earlyAccessDir = path.posix.join(process.cwd(), 'content', 'early-access') |
| 7 | +const { sentenceCase } = require('change-case') |
| 8 | + |
| 9 | +updateOrCreateToc(earlyAccessDir) |
| 10 | + |
| 11 | +console.log('Updated Early Access TOCs!') |
| 12 | + |
| 13 | +function updateOrCreateToc (directory) { |
| 14 | + const children = fs.readdirSync(directory) |
| 15 | + .filter(subpath => !subpath.endsWith('index.md')) |
| 16 | + |
| 17 | + if (!children.length) return |
| 18 | + |
| 19 | + const tocFile = path.posix.join(directory, 'index.md') |
| 20 | + |
| 21 | + let content, data |
| 22 | + |
| 23 | + if (fs.existsSync(tocFile)) { |
| 24 | + const matter = readFrontmatter(fs.readFileSync(tocFile, 'utf8')) |
| 25 | + content = matter.content |
| 26 | + data = matter.data |
| 27 | + } else { |
| 28 | + content = '' |
| 29 | + data = { |
| 30 | + title: sentenceCase(path.basename(directory)), |
| 31 | + versions: '*', |
| 32 | + hidden: true |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + data.children = children.map(child => `/${child.replace('.md', '')}`) |
| 37 | + const newContents = readFrontmatter.stringify(content, data, { lineWidth: 10000 }) |
| 38 | + fs.writeFileSync(tocFile, newContents) |
| 39 | + |
| 40 | + children.forEach(child => { |
| 41 | + if (child.endsWith('.md')) return |
| 42 | + updateOrCreateToc(path.posix.join(directory, child)) |
| 43 | + }) |
| 44 | +} |
0 commit comments