@@ -9,6 +9,12 @@ const BADGE_PATTERN = new RegExp(
99) ;
1010const README_NAMES = [ 'README.md' , 'readme.md' , 'README' , 'readme' ] ;
1111
12+ /**
13+ * Resolve owner and repo from the project's git remote origin URL.
14+ * Supports HTTPS and SSH formats.
15+ * @param {string } projectDir - Project root directory
16+ * @returns {{ owner: string, repo: string } | null } Parsed owner/repo or null
17+ */
1218function resolveGitRemote ( projectDir ) {
1319 try {
1420 const raw = execSync ( 'git remote get-url origin' , {
@@ -17,7 +23,6 @@ function resolveGitRemote(projectDir) {
1723 stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
1824 } ) . trim ( ) ;
1925
20- // https://github.com/owner/repo.git
2126 const httpsMatch = raw . match ( / g i t h u b \. c o m [: / ] ( [ ^ / ] + ) \/ ( [ ^ / ] + ?) (?: \. g i t ) ? \/ ? $ / i) ;
2227 if ( httpsMatch ) {
2328 return { owner : httpsMatch [ 1 ] , repo : httpsMatch [ 2 ] } ;
@@ -28,6 +33,12 @@ function resolveGitRemote(projectDir) {
2833 return null ;
2934}
3035
36+ /**
37+ * Find the first README file in the project directory.
38+ * Checks common README naming variants (case-insensitive).
39+ * @param {string } projectDir - Project root directory
40+ * @returns {Promise<string | null> } Absolute path to README or null
41+ */
3142async function findReadme ( projectDir ) {
3243 for ( const name of README_NAMES ) {
3344 const fullPath = path . join ( projectDir , name ) ;
@@ -38,14 +49,33 @@ async function findReadme(projectDir) {
3849 return null ;
3950}
4051
52+ /**
53+ * Check whether the content already contains a BMAD badge.
54+ * @param {string } content - README file content
55+ * @returns {boolean } True if badge is present
56+ */
4157function hasBadge ( content ) {
4258 return BADGE_PATTERN . test ( content ) ;
4359}
4460
61+ /**
62+ * Generate the BMAD badge markdown line.
63+ * @param {string } owner - Repository owner
64+ * @param {string } repo - Repository name
65+ * @returns {string } Badge markdown string
66+ */
4567function generateBadgeMarkdown ( owner , repo ) {
4668 return `[](https://github.com/bmad-code-org/BMAD-METHOD)` ;
4769}
4870
71+ /**
72+ * Inject the BMAD badge into README content.
73+ * Places the badge after the first heading, alongside any existing badges.
74+ * @param {string } content - Original README content
75+ * @param {string } owner - Repository owner
76+ * @param {string } repo - Repository name
77+ * @returns {string } Updated README content with badge
78+ */
4979function injectBadge ( content , owner , repo ) {
5080 const badgeLine = generateBadgeMarkdown ( owner , repo ) ;
5181
@@ -64,18 +94,29 @@ function injectBadge(content, owner, repo) {
6494 insertAt ++ ;
6595 }
6696
67- // Insert badge line
6897 lines . splice ( insertAt , 0 , badgeLine ) ;
6998 return lines . join ( '\n' ) ;
7099}
71100
101+ /**
102+ * Remove the BMAD badge from README content.
103+ * @param {string } content - README file content
104+ * @returns {string } Cleaned README content without the badge line
105+ */
72106function removeBadge ( content ) {
73107 return content
74108 . split ( '\n' )
75109 . filter ( ( line ) => ! BADGE_PATTERN . test ( line . trim ( ) ) )
76110 . join ( '\n' ) ;
77111}
78112
113+ /**
114+ * Create a minimal README.md content with project heading and BMAD badge.
115+ * @param {string } owner - Repository owner
116+ * @param {string } repo - Repository name
117+ * @param {string } projectName - Project name for the heading
118+ * @returns {string } New README content
119+ */
79120function createReadmeWithBadge ( owner , repo , projectName ) {
80121 const badgeLine = generateBadgeMarkdown ( owner , repo ) ;
81122 return `# ${ projectName } \n\n${ badgeLine } \n` ;
0 commit comments