diff --git a/tools/cli/extract-changelog-section.js b/tools/cli/extract-changelog-section.js new file mode 100644 index 0000000000..8ebc8a03d2 --- /dev/null +++ b/tools/cli/extract-changelog-section.js @@ -0,0 +1,45 @@ +/* eslint-disable */ + +const fs = require('fs'); +const path = require('path'); + +const CHANGELOG_PATH = path.resolve(__dirname, '../../packages/teams-js/CHANGELOG.md'); + +/** + * Returns the changelog body for a specific version (the content between the + * `## ` header and the next `## ` header). If no version is provided, + * the full changelog is returned. + * + * @param {string} [version] semver string, e.g. "2.53.1" + * @param {string} [changelogPath] override path to CHANGELOG.md (used in tests) + * @returns {string} the trimmed changelog section, or the full changelog + */ +function extractChangelogSection(version, changelogPath = CHANGELOG_PATH) { + if (!fs.existsSync(changelogPath)) { + throw new Error(`Changelog was not found at ${changelogPath}`); + } + const fullChangelog = fs.readFileSync(changelogPath, 'utf8'); + if (!version) { + return fullChangelog; + } + // Split on level-2 headers, keeping the headers as delimiters so the array + // alternates between header and body entries. + const parts = fullChangelog.split(/(^## .*$)/m); + const index = parts.findIndex((part) => part.trim() === `## ${version}`); + if (index === -1) { + throw new Error(`Matching version ${version} in changelog was not found`); + } + return parts[index + 1] ? parts[index + 1].trim() : ''; +} + +module.exports = { extractChangelogSection, CHANGELOG_PATH }; + +if (require.main === module) { + const version = process.argv[2]; + try { + process.stdout.write(extractChangelogSection(version)); + } catch (e) { + console.error(e.message || e); + process.exit(1); + } +} diff --git a/tools/cli/preRelease.js b/tools/cli/preRelease.js index 08c74b032c..3431c5345d 100644 --- a/tools/cli/preRelease.js +++ b/tools/cli/preRelease.js @@ -58,7 +58,7 @@ const updateVersionAndIntegrity = async (absolutePath, version, integrityHash) = } const readme = fs.readFileSync(absolutePath, 'utf8'); const result = readme - .replace(/integrity=\".*?\"/, `integrity="${integrityHash}"`) + .replace(/integrity=".*?"/g, `integrity="${integrityHash}"`) .replace( /res\.cdn\.office.net\/teams-js\/.*\/js\/MicrosoftTeams\.min\.js/g, `res.cdn.office.net/teams-js/${version}/js/MicrosoftTeams.min.js`, diff --git a/tools/cli/readChangelog.js b/tools/cli/readChangelog.js index afa8a9acc9..cdb1e0e6c2 100644 --- a/tools/cli/readChangelog.js +++ b/tools/cli/readChangelog.js @@ -1,33 +1,12 @@ /* eslint-disable */ -const fs = require('fs'); -const path = require('path'); - -const readChangeLog = version => { - const relativePathToChangelog = '../../packages/teams-js/CHANGELOG.md'; - const absolutePathToChangelog = path.resolve(__dirname, relativePathToChangelog); - if (!fs.existsSync(absolutePathToChangelog)) { - throw `ERROR: ${absolutePathToChangelog} was not found.`; - } - const fullChangelog = fs.readFileSync(absolutePathToChangelog, 'utf8'); - if (!version) { - return fullChangelog; - } else { - const result = fullChangelog.split(/(## .*\d)/); - const index = result.findIndex(substr => substr.startsWith(`## ${version}`)); - if (index !== -1) { - const log = result[index + 1]; - return log; - } - throw new Error('Matching version in changelog was not found'); - } -}; +const { extractChangelogSection } = require('./extract-changelog-section'); (async () => { const args = process.argv.slice(2); const version = args[0]; try { - const section = readChangeLog(version); + const section = extractChangelogSection(version); console.log(section); } catch (e) { console.log('Something went wrong!');