|
| 1 | +import path from 'path'; |
| 2 | +import { mkdirp } from 'mkdirp'; |
| 3 | +import { writeFile } from 'fs/promises'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import api from './githubAPI.mjs'; |
| 6 | +import yamlHeadmatter from './yaml-headmatter.mjs'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +const owner = 'webpack'; |
| 12 | +const repo = 'governance'; |
| 13 | + |
| 14 | +// Output directly under /contribute (no Governance subfolder) |
| 15 | +const outputDir = path.resolve(__dirname, '../content/contribute'); |
| 16 | + |
| 17 | +// Generate readable title from filename |
| 18 | +function generateTitle(filename) { |
| 19 | + if (filename === 'README.md') return 'Governance Overview'; |
| 20 | + return filename |
| 21 | + .replace('.md', '') |
| 22 | + .replace(/_/g, ' ') |
| 23 | + .replace(/-/g, ' ') |
| 24 | + .toLowerCase() |
| 25 | + .replace(/\b\w/g, (c) => c.toUpperCase()); |
| 26 | +} |
| 27 | + |
| 28 | +// Fix internal markdown links (.md → /) |
| 29 | +function fixMarkdownLinks(content) { |
| 30 | + return content.replace(/(\]\([A-Z0-9_-]+)\.md(\))/gi, '$1/$2'); |
| 31 | +} |
| 32 | + |
| 33 | +async function fetchGovernanceDocs() { |
| 34 | + console.log('Fetching governance markdown files from webpack/governance...'); |
| 35 | + |
| 36 | + await mkdirp(outputDir); |
| 37 | + |
| 38 | + try { |
| 39 | + // Get markdown files from governance repo |
| 40 | + const { data: files } = await api.repos.getContent({ |
| 41 | + owner, |
| 42 | + repo, |
| 43 | + path: '', |
| 44 | + }); |
| 45 | + |
| 46 | + const markdownFiles = files.filter((file) => file.name.endsWith('.md')); |
| 47 | + |
| 48 | + for (const file of markdownFiles) { |
| 49 | + const filename = file.name; |
| 50 | + |
| 51 | + // Create Capitalized prefixed filenames |
| 52 | + const baseName = filename |
| 53 | + .replace('.md', '') |
| 54 | + .replace(/_/g, '-') |
| 55 | + .toLowerCase(); |
| 56 | + |
| 57 | + const destFile = |
| 58 | + filename === 'README.md' |
| 59 | + ? 'Governance-Overview.mdx' |
| 60 | + : `Governance-${baseName}.mdx`; |
| 61 | + |
| 62 | + // Fetch content from GitHub and fix markdown links |
| 63 | + const response = await fetch(file.download_url); |
| 64 | + let content = await response.text(); |
| 65 | + content = fixMarkdownLinks(content); |
| 66 | + |
| 67 | + // Generate title and sorting order |
| 68 | + const title = generateTitle(filename); |
| 69 | + const sortOrder = |
| 70 | + { |
| 71 | + 'README.md': 0, |
| 72 | + 'CHARTER.md': 1, |
| 73 | + 'MEMBER_EXPECTATIONS.md': 2, |
| 74 | + 'MODERATION_POLICY.md': 3, |
| 75 | + 'WORKING_GROUPS.md': 4, |
| 76 | + }[filename] ?? 10; |
| 77 | + |
| 78 | + // Build YAML frontmatter |
| 79 | + const fm = { |
| 80 | + title, |
| 81 | + group: 'Contribute', |
| 82 | + sort: sortOrder, |
| 83 | + source: `https://github.com/${owner}/${repo}/blob/main/${filename}`, |
| 84 | + edit: `https://github.com/${owner}/${repo}/edit/main/${filename}`, |
| 85 | + }; |
| 86 | + |
| 87 | + const frontmatter = yamlHeadmatter(fm); |
| 88 | + |
| 89 | + // Write .mdx file |
| 90 | + const destPath = path.join(outputDir, destFile); |
| 91 | + await writeFile(destPath, frontmatter + content, 'utf8'); |
| 92 | + console.log(`Synced: ${filename} → ${destFile}`); |
| 93 | + } |
| 94 | + |
| 95 | + console.log( |
| 96 | + '\nGovernance content generated successfully with Capitalized prefix!' |
| 97 | + ); |
| 98 | + } catch (error) { |
| 99 | + console.error('Error fetching governance files:', error.message); |
| 100 | + process.exitCode = 1; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +fetchGovernanceDocs(); |
0 commit comments