Skip to content

Commit

Permalink
feat: readme script to add more sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Colten Krauter committed Jul 20, 2024
1 parent ab51185 commit 1dd466b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Typescript Core
A TypeScript Repository that stands as a starting point for all other TypeScript repositories.


## Usage


## Collaboration


## Development

55 changes: 45 additions & 10 deletions scripts/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ console.debug(`Current directory [${__dirname}]`)

const packageJsonPath = join(__dirname, '../package.json')
const readmePath = join(__dirname, '../README.md')
const firstMarkdownTitleRegex = /^# (.+)$/m
const packageNameScopeRegex = /^@.*\//

// Heading level regex constants
const h1HeadingRegex = /^# (.+)$/m

/**
* Converts a package name to a formatted title.
* @param {string} packageName - The original package name.
Expand Down Expand Up @@ -56,21 +58,54 @@ const writeReadme = (readmePath, content) => {
console.info(`README.md updated successfully at [${readmePath}]`)
}

/**
* Ensures the specified section exists in the README content.
* @param {string} content - The README content.
* @param {string} sectionTitle - The section title to ensure exists.
* @returns {string} - The updated README content.
*/
const ensureSectionExists = (content, sectionTitle) => {
const sectionRegex = new RegExp(`^## ${sectionTitle}$`, 'm')
if (!sectionRegex.test(content)) {
console.info(`[${sectionTitle}] section not found, adding it to README.md`)
content += `\n\n## ${sectionTitle}\n\n`
}
return content
}

/**
* Ensures the title exists in the README content and updates it if necessary.
* @param {string} content - The README content.
* @param {RegExp} titleRegex - The regex to match the title.
* @param {string} newTitle - The new title to set.
* @returns {string} - The updated README content.
*/
const ensureTitleExists = (content, titleRegex, newTitle) => {
const currentTitleMatch = content.match(titleRegex)
const currentTitle = currentTitleMatch ? currentTitleMatch[1] : ''
console.info(`Current README title [${currentTitle}]`)

if (currentTitle !== newTitle) {
content = content.replace(titleRegex, `# ${newTitle}`)
console.info(`README.md title updated to [${newTitle}]`)
}
return content
}

try {
const packageJson = readPackageJson(packageJsonPath)
const packageName = packageJson.name
const desiredTitle = packageNameToTitle(packageName)
const readmeContent = readReadme(readmePath)
let readmeContent = readReadme(readmePath)

const currentTitleMatch = readmeContent.match(firstMarkdownTitleRegex)
const currentTitle = currentTitleMatch ? currentTitleMatch[1] : ''
console.info(`Current README title [${currentTitle}]`)
readmeContent = ensureTitleExists(readmeContent, h1HeadingRegex, desiredTitle)

if (currentTitle !== desiredTitle) {
const updatedContent = readmeContent.replace(firstMarkdownTitleRegex, `# ${desiredTitle}`)
writeReadme(readmePath, updatedContent)
console.info(`README.md title updated to [${desiredTitle}]`)
}
const sections = ['Usage', 'Collaboration', 'Development']
sections.forEach(section => {
readmeContent = ensureSectionExists(readmeContent, section)
})

writeReadme(readmePath, readmeContent)
} catch (error) {
console.error(`Failed with error [${error}]`)
}

0 comments on commit 1dd466b

Please sign in to comment.