|
| 1 | +const path = require('node:path'); |
| 2 | +const { execSync } = require('node:child_process'); |
| 3 | +const fs = require('../fs-native'); |
| 4 | + |
| 5 | +const BADGE_URL = 'https://bmad-badge.vercel.app'; |
| 6 | +const escapedBadgeUrl = BADGE_URL.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`); |
| 7 | +const BADGE_PATTERN = new RegExp(`\\[!\\[BMAD\\]\\(${escapedBadgeUrl}/[^)]+\\)\\]\\(https://github\\.com/bmad-code-org/BMAD-METHOD\\)`); |
| 8 | +const README_NAMES = ['README.md', 'readme.md', 'README', 'readme']; |
| 9 | + |
| 10 | +/** |
| 11 | + * Resolve owner and repo from the project's git remote origin URL. |
| 12 | + * Supports HTTPS and SSH formats. |
| 13 | + * @param {string} projectDir - Project root directory |
| 14 | + * @returns {{ owner: string, repo: string } | null} Parsed owner/repo or null |
| 15 | + */ |
| 16 | +function resolveGitRemote(projectDir) { |
| 17 | + try { |
| 18 | + const raw = execSync('git remote get-url origin', { |
| 19 | + cwd: projectDir, |
| 20 | + encoding: 'utf8', |
| 21 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 22 | + }).trim(); |
| 23 | + |
| 24 | + const httpsMatch = raw.match(/github\.com[:/]([^/]+)\/([^/]+?)(?:\.git)?\/?$/i); |
| 25 | + if (httpsMatch) { |
| 26 | + return { owner: httpsMatch[1], repo: httpsMatch[2] }; |
| 27 | + } |
| 28 | + } catch { |
| 29 | + // no git remote |
| 30 | + } |
| 31 | + return null; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Find the first README file in the project directory. |
| 36 | + * Checks common README naming variants (case-insensitive). |
| 37 | + * @param {string} projectDir - Project root directory |
| 38 | + * @returns {Promise<string | null>} Absolute path to README or null |
| 39 | + */ |
| 40 | +async function findReadme(projectDir) { |
| 41 | + for (const name of README_NAMES) { |
| 42 | + const fullPath = path.join(projectDir, name); |
| 43 | + if (await fs.pathExists(fullPath)) { |
| 44 | + return fullPath; |
| 45 | + } |
| 46 | + } |
| 47 | + return null; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Check whether the content already contains a BMAD badge. |
| 52 | + * @param {string} content - README file content |
| 53 | + * @returns {boolean} True if badge is present |
| 54 | + */ |
| 55 | +function hasBadge(content) { |
| 56 | + return BADGE_PATTERN.test(content); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Generate the BMAD badge markdown line. |
| 61 | + * @param {string} owner - Repository owner |
| 62 | + * @param {string} repo - Repository name |
| 63 | + * @returns {string} Badge markdown string |
| 64 | + */ |
| 65 | +function generateBadgeMarkdown(owner, repo) { |
| 66 | + return `[](https://github.com/bmad-code-org/BMAD-METHOD)`; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Inject the BMAD badge into README content. |
| 71 | + * Places the badge after the first heading, alongside any existing badges. |
| 72 | + * @param {string} content - Original README content |
| 73 | + * @param {string} owner - Repository owner |
| 74 | + * @param {string} repo - Repository name |
| 75 | + * @returns {string} Updated README content with badge |
| 76 | + */ |
| 77 | +function injectBadge(content, owner, repo) { |
| 78 | + const badgeLine = generateBadgeMarkdown(owner, repo); |
| 79 | + |
| 80 | + const lines = content.split('\n'); |
| 81 | + |
| 82 | + // Find the first heading (# title) |
| 83 | + let headingEnd = 0; |
| 84 | + for (const [i, line] of lines.entries()) { |
| 85 | + headingEnd = i + 1; |
| 86 | + if (line.startsWith('#')) break; |
| 87 | + } |
| 88 | + |
| 89 | + // Check if there are existing badges right after the heading |
| 90 | + let insertAt = headingEnd; |
| 91 | + while (insertAt < lines.length && /^\[!\[.*?\]\(.*?\)\]\(.*?\)/.test(lines[insertAt].trim())) { |
| 92 | + insertAt++; |
| 93 | + } |
| 94 | + |
| 95 | + lines.splice(insertAt, 0, badgeLine); |
| 96 | + return lines.join('\n'); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Remove the BMAD badge from README content. |
| 101 | + * @param {string} content - README file content |
| 102 | + * @returns {string} Cleaned README content without the badge line |
| 103 | + */ |
| 104 | +function removeBadge(content) { |
| 105 | + return content |
| 106 | + .split('\n') |
| 107 | + .filter((line) => !BADGE_PATTERN.test(line.trim())) |
| 108 | + .join('\n'); |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Create a minimal README.md content with project heading and BMAD badge. |
| 113 | + * @param {string} owner - Repository owner |
| 114 | + * @param {string} repo - Repository name |
| 115 | + * @param {string} projectName - Project name for the heading |
| 116 | + * @returns {string} New README content |
| 117 | + */ |
| 118 | +function createReadmeWithBadge(owner, repo, projectName) { |
| 119 | + const badgeLine = generateBadgeMarkdown(owner, repo); |
| 120 | + return `# ${projectName}\n\n${badgeLine}\n`; |
| 121 | +} |
| 122 | + |
| 123 | +module.exports = { |
| 124 | + resolveGitRemote, |
| 125 | + findReadme, |
| 126 | + hasBadge, |
| 127 | + generateBadgeMarkdown, |
| 128 | + injectBadge, |
| 129 | + removeBadge, |
| 130 | + createReadmeWithBadge, |
| 131 | +}; |
0 commit comments