Compare commits

..

2 Commits

Author SHA1 Message Date
NEE f96800d330
Merge 4a4a7c9079 into 9debc165aa 2026-05-01 14:11:18 +00:00
nick 4a4a7c9079 feat(installer): auto-inject BMAD version badge into project README
- Add badge.js module for git remote resolution, README detection, and badge injection
- Integrate badge prompt into install flow with --no-badge opt-out
- Support badge update when owner/repo changes on re-install
- Auto-create README.md with badge if missing
- Pass badge config through Config.build() for both install and quick-update paths

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 22:10:42 +08:00
4 changed files with 54 additions and 18 deletions

View File

@ -109,6 +109,29 @@ module.exports = {
}));
}
// Resolve owner/repo for badge (git remote → prompt fallback)
if (!config.noBadge) {
const badge = require('../core/badge');
let remote = badge.resolveGitRemote(config.directory);
if (!remote) {
const input = await prompts.text({
message: 'Enter your GitHub owner/repo for the badge (e.g., nick/my-project):',
placeholder: 'owner/repo',
validate: (v) => (!v || !v.includes('/') ? 'Format: owner/repo' : undefined),
});
if (input) {
const [owner, repo] = input.split('/');
remote = { owner, repo };
}
}
if (remote) {
config.badgeOwner = remote.owner;
config.badgeRepo = remote.repo;
} else {
config.noBadge = true;
}
}
// Handle cancel
if (config.actionType === 'cancel') {
await prompts.log.warn('Installation cancelled.');

View File

@ -3,10 +3,8 @@ const { execSync } = require('node:child_process');
const fs = require('../fs-native');
const BADGE_URL = 'https://bmad-badge.vercel.app';
const escapedBadgeUrl = BADGE_URL.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const BADGE_PATTERN = new RegExp(
`\\[!\\[BMAD\\]\\(${escapedBadgeUrl}/[^)]+\\)\\]\\(https://github\\.com/bmad-code-org/BMAD-METHOD\\)`,
);
const escapedBadgeUrl = BADGE_URL.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
const BADGE_PATTERN = new RegExp(`\\[!\\[BMAD\\]\\(${escapedBadgeUrl}/[^)]+\\)\\]\\(https://github\\.com/bmad-code-org/BMAD-METHOD\\)`);
const README_NAMES = ['README.md', 'readme.md', 'README', 'readme'];
/**
@ -83,9 +81,9 @@ function injectBadge(content, owner, repo) {
// Find the first heading (# title)
let headingEnd = 0;
for (let i = 0; i < lines.length; i++) {
for (const [i, line] of lines.entries()) {
headingEnd = i + 1;
if (lines[i].startsWith('#')) break;
if (line.startsWith('#')) break;
}
// Check if there are existing badges right after the heading

View File

@ -15,6 +15,9 @@ class Config {
quickUpdate,
channelOptions,
setOverrides,
noBadge,
badgeOwner,
badgeRepo,
}) {
this.directory = directory;
this.modules = Object.freeze([...modules]);
@ -32,6 +35,9 @@ class Config {
// Intentionally NOT integrated with the prompt/template/schema flow; see
// `tools/installer/set-overrides.js` for the rationale and tradeoffs.
this.setOverrides = setOverrides || {};
this.noBadge = noBadge || false;
this.badgeOwner = badgeOwner || null;
this.badgeRepo = badgeRepo || null;
Object.freeze(this);
}
@ -58,6 +64,9 @@ class Config {
quickUpdate: userInput._quickUpdate || false,
channelOptions: userInput.channelOptions || null,
setOverrides: userInput.setOverrides || {},
noBadge: userInput.noBadge || false,
badgeOwner: userInput.badgeOwner || null,
badgeRepo: userInput.badgeRepo || null,
});
}

View File

@ -106,7 +106,7 @@ class Installer {
// Inject BMAD badge into README if applicable
if (!config.noBadge) {
try {
await this._injectBadgeIfNeeded(paths.projectRoot, addResult);
await this._injectBadgeIfNeeded(paths.projectRoot, addResult, config);
} catch (error) {
addResult('Badge', 'warn', `skipped: ${error.message}`);
}
@ -1048,26 +1048,26 @@ class Installer {
}
/**
* Inject BMAD version badge into project README if applicable.
* Skipped when --no-badge is set, when no git remote is found,
* or when no README exists.
* Inject BMAD version badge into project README.
* Uses owner/repo from config (resolved in UI layer).
* @param {string} projectDir - Project root directory
* @param {Function} addResult - Callback to record results
* @param {Object} config - Installation config with badgeOwner/badgeRepo
*/
async _injectBadgeIfNeeded(projectDir, addResult) {
async _injectBadgeIfNeeded(projectDir, addResult, config) {
const badge = require('../core/badge');
const remote = badge.resolveGitRemote(projectDir);
if (!remote) {
addResult('Badge', 'warn', 'no git remote found');
const owner = config.badgeOwner;
const repo = config.badgeRepo;
if (!owner || !repo) {
addResult('Badge', 'warn', 'no owner/repo provided');
return;
}
const readmePath = await badge.findReadme(projectDir);
if (!readmePath) {
// No README — create one with the badge
const projectName = path.basename(projectDir);
const content = badge.createReadmeWithBadge(remote.owner, remote.repo, projectName);
const content = badge.createReadmeWithBadge(owner, repo, projectName);
const newReadmePath = path.join(projectDir, 'README.md');
await fs.writeFile(newReadmePath, content, 'utf8');
addResult('Badge', 'ok', 'created README.md with badge');
@ -1076,11 +1076,15 @@ class Installer {
const content = await fs.readFile(readmePath, 'utf8');
if (badge.hasBadge(content)) {
addResult('Badge', 'ok', 'already present');
// Update badge if owner/repo changed
const updated = badge.removeBadge(content);
const injected = badge.injectBadge(updated, owner, repo);
await fs.writeFile(readmePath, injected, 'utf8');
addResult('Badge', 'ok', `updated in ${path.basename(readmePath)}`);
return;
}
const updated = badge.injectBadge(content, remote.owner, remote.repo);
const updated = badge.injectBadge(content, owner, repo);
await fs.writeFile(readmePath, updated, 'utf8');
addResult('Badge', 'ok', `added to ${path.basename(readmePath)}`);
}
@ -1342,6 +1346,8 @@ class Installer {
modules: modulesToUpdate,
ides: configuredIdes,
noBadge: config.noBadge,
badgeOwner: config.badgeOwner,
badgeRepo: config.badgeRepo,
coreConfig: quickModules.collectedConfig.core,
moduleConfigs: quickModules.collectedConfig,
// Forward `--set` overrides so the post-install patch step