Skip to content

Commit 1364772

Browse files
committed
Adjustment
1 parent 5cedeb4 commit 1364772

File tree

5 files changed

+169
-180
lines changed

5 files changed

+169
-180
lines changed

cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* @license GPL-3.0
1212
*/
1313

14-
const fs = require('fs')
15-
const path = require('path')
16-
const { parseChangelog } = require('./src/parser')
14+
import fs from 'fs'
15+
import path from 'path'
16+
import { parseChangelog } from './src/parser.js'
1717

1818
function printUsage() {
1919
console.log(`

dist/index.js

Lines changed: 159 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -27253,190 +27253,177 @@ var coreExports = requireCore();
2725327253
* @param {string|null} version - Specific version to extract (e.g., "v1.14.0" or "1.14.0"), or null/undefined for all versions
2725427254
* @returns {Array<{name: string, sections: Object, contents: string}>} Array of version objects
2725527255
*/
27256+
function parseChangelog(changelogContent, version = null) {
27257+
const versions = [];
27258+
const lines = changelogContent.split('\n');
27259+
27260+
let currentVersion = null;
27261+
let currentSection = null;
27262+
let currentSectionContent = [];
27263+
let generalContent = []; // Content without section headers
27264+
27265+
// Regex to match version headers: ## v1.14.0 or ## v1.14.0 - 2024-04-29
27266+
const versionRegex = /^##\s+v?(\d+\.\d+\.\d+(?:-[^\s]+)?)\s*(?:-\s*(.*))?$/;
27267+
// Regex to match section headers: ### Added, ### Changed, etc.
27268+
const sectionRegex = /^###\s+(.+)$/;
27269+
27270+
for (let i = 0; i < lines.length; i++) {
27271+
const line = lines[i];
27272+
27273+
// Check for version header
27274+
const versionMatch = line.match(versionRegex);
27275+
if (versionMatch) {
27276+
// Save previous version if exists
27277+
if (currentVersion) {
27278+
saveCurrentSection();
27279+
saveGeneralContent();
27280+
versions.push(currentVersion);
27281+
}
27282+
27283+
// Start new version
27284+
currentVersion = {
27285+
name: versionMatch[1], // Version without 'v' prefix
27286+
sections: {},
27287+
contents: ''
27288+
};
27289+
currentSection = null;
27290+
currentSectionContent = [];
27291+
generalContent = [];
27292+
continue
27293+
}
2725627294

27257-
var parser;
27258-
var hasRequiredParser;
27259-
27260-
function requireParser () {
27261-
if (hasRequiredParser) return parser;
27262-
hasRequiredParser = 1;
27263-
function parseChangelog(changelogContent, version = null) {
27264-
const versions = [];
27265-
const lines = changelogContent.split('\n');
27266-
27267-
let currentVersion = null;
27268-
let currentSection = null;
27269-
let currentSectionContent = [];
27270-
let generalContent = []; // Content without section headers
27271-
27272-
// Regex to match version headers: ## v1.14.0 or ## v1.14.0 - 2024-04-29
27273-
const versionRegex = /^##\s+v?(\d+\.\d+\.\d+(?:-[^\s]+)?)\s*(?:-\s*(.*))?$/;
27274-
// Regex to match section headers: ### Added, ### Changed, etc.
27275-
const sectionRegex = /^###\s+(.+)$/;
27276-
27277-
for (let i = 0; i < lines.length; i++) {
27278-
const line = lines[i];
27279-
27280-
// Check for version header
27281-
const versionMatch = line.match(versionRegex);
27282-
if (versionMatch) {
27283-
// Save previous version if exists
27284-
if (currentVersion) {
27285-
saveCurrentSection();
27286-
saveGeneralContent();
27287-
versions.push(currentVersion);
27288-
}
27289-
27290-
// Start new version
27291-
currentVersion = {
27292-
name: versionMatch[1], // Version without 'v' prefix
27293-
sections: {},
27294-
contents: ''
27295-
};
27296-
currentSection = null;
27297-
currentSectionContent = [];
27298-
generalContent = [];
27299-
continue
27300-
}
27301-
27302-
// Check for section header
27303-
const sectionMatch = line.match(sectionRegex);
27304-
if (sectionMatch && currentVersion) {
27305-
// If we have general content, save it before starting a new section
27306-
if (generalContent.length > 0) {
27307-
saveGeneralContent();
27308-
}
27309-
27310-
// Save previous section if exists
27311-
saveCurrentSection();
27312-
27313-
// Start new section
27314-
currentSection = sectionMatch[1].toLowerCase();
27315-
currentSectionContent = [];
27316-
continue
27317-
}
27318-
27319-
// Add content to current section or general content
27320-
if (currentVersion) {
27321-
if (currentSection) {
27322-
currentSectionContent.push(line);
27323-
} else {
27324-
// Content without a section header goes to general
27325-
generalContent.push(line);
27326-
}
27327-
}
27328-
}
27329-
27330-
// Save final version and section
27331-
if (currentVersion) {
27332-
saveCurrentSection();
27333-
saveGeneralContent();
27334-
versions.push(currentVersion);
27335-
}
27336-
27337-
// Helper function to save the current section
27338-
function saveCurrentSection() {
27339-
if (currentVersion && currentSection && currentSectionContent.length > 0) {
27340-
// Trim empty lines from start and end
27341-
while (
27342-
currentSectionContent.length > 0 &&
27343-
currentSectionContent[0].trim() === ''
27344-
) {
27345-
currentSectionContent.shift();
27346-
}
27347-
while (
27348-
currentSectionContent.length > 0 &&
27349-
currentSectionContent[currentSectionContent.length - 1].trim() === ''
27350-
) {
27351-
currentSectionContent.pop();
27352-
}
27353-
27354-
const content = currentSectionContent.join('\n');
27355-
if (content.trim()) {
27356-
currentVersion.sections[currentSection] = content;
27357-
}
27358-
}
27359-
}
27360-
27361-
// Helper function to save general content (content without section headers)
27362-
function saveGeneralContent() {
27363-
if (currentVersion && generalContent.length > 0) {
27364-
// Trim empty lines from start and end
27365-
let trimmedContent = [...generalContent];
27366-
while (trimmedContent.length > 0 && trimmedContent[0].trim() === '') {
27367-
trimmedContent.shift();
27368-
}
27369-
while (
27370-
trimmedContent.length > 0 &&
27371-
trimmedContent[trimmedContent.length - 1].trim() === ''
27372-
) {
27373-
trimmedContent.pop();
27374-
}
27295+
// Check for section header
27296+
const sectionMatch = line.match(sectionRegex);
27297+
if (sectionMatch && currentVersion) {
27298+
// If we have general content, save it before starting a new section
27299+
if (generalContent.length > 0) {
27300+
saveGeneralContent();
27301+
}
27302+
27303+
// Save previous section if exists
27304+
saveCurrentSection();
27305+
27306+
// Start new section
27307+
currentSection = sectionMatch[1].toLowerCase();
27308+
currentSectionContent = [];
27309+
continue
27310+
}
2737527311

27376-
const content = trimmedContent.join('\n');
27377-
if (content.trim()) {
27378-
currentVersion.sections['general'] = content;
27379-
}
27380-
generalContent = [];
27381-
}
27382-
}
27312+
// Add content to current section or general content
27313+
if (currentVersion) {
27314+
if (currentSection) {
27315+
currentSectionContent.push(line);
27316+
} else {
27317+
// Content without a section header goes to general
27318+
generalContent.push(line);
27319+
}
27320+
}
27321+
}
2738327322

27384-
// Build contents for each version (all sections combined)
27385-
for (const ver of versions) {
27386-
const contentParts = [];
27323+
// Save final version and section
27324+
if (currentVersion) {
27325+
saveCurrentSection();
27326+
saveGeneralContent();
27327+
versions.push(currentVersion);
27328+
}
2738727329

27388-
// Order sections in conventional order, with general at the end
27389-
const sectionOrder = [
27390-
'added',
27391-
'changed',
27392-
'deprecated',
27393-
'removed',
27394-
'fixed',
27395-
'security'
27396-
];
27330+
// Helper function to save the current section
27331+
function saveCurrentSection() {
27332+
if (currentVersion && currentSection && currentSectionContent.length > 0) {
27333+
// Trim empty lines from start and end
27334+
while (
27335+
currentSectionContent.length > 0 &&
27336+
currentSectionContent[0].trim() === ''
27337+
) {
27338+
currentSectionContent.shift();
27339+
}
27340+
while (
27341+
currentSectionContent.length > 0 &&
27342+
currentSectionContent[currentSectionContent.length - 1].trim() === ''
27343+
) {
27344+
currentSectionContent.pop();
27345+
}
27346+
27347+
const content = currentSectionContent.join('\n');
27348+
if (content.trim()) {
27349+
currentVersion.sections[currentSection] = content;
27350+
}
27351+
}
27352+
}
2739727353

27398-
for (const sectionName of sectionOrder) {
27399-
if (ver.sections[sectionName]) {
27400-
contentParts.push(
27401-
`### ${sectionName.charAt(0).toUpperCase() + sectionName.slice(1)}\n\n${ver.sections[sectionName]}`
27402-
);
27403-
}
27404-
}
27354+
// Helper function to save general content (content without section headers)
27355+
function saveGeneralContent() {
27356+
if (currentVersion && generalContent.length > 0) {
27357+
// Trim empty lines from start and end
27358+
let trimmedContent = [...generalContent];
27359+
while (trimmedContent.length > 0 && trimmedContent[0].trim() === '') {
27360+
trimmedContent.shift();
27361+
}
27362+
while (
27363+
trimmedContent.length > 0 &&
27364+
trimmedContent[trimmedContent.length - 1].trim() === ''
27365+
) {
27366+
trimmedContent.pop();
27367+
}
27368+
27369+
const content = trimmedContent.join('\n');
27370+
if (content.trim()) {
27371+
currentVersion.sections['general'] = content;
27372+
}
27373+
generalContent = [];
27374+
}
27375+
}
2740527376

27406-
// Add any sections not in the standard order (except 'general')
27407-
for (const [sectionName, sectionContent] of Object.entries(ver.sections)) {
27408-
if (!sectionOrder.includes(sectionName) && sectionName !== 'general') {
27409-
contentParts.push(
27410-
`### ${sectionName.charAt(0).toUpperCase() + sectionName.slice(1)}\n\n${sectionContent}`
27411-
);
27412-
}
27413-
}
27377+
// Build contents for each version (all sections combined)
27378+
for (const ver of versions) {
27379+
const contentParts = [];
27380+
27381+
// Order sections in conventional order, with general at the end
27382+
const sectionOrder = [
27383+
'added',
27384+
'changed',
27385+
'deprecated',
27386+
'removed',
27387+
'fixed',
27388+
'security'
27389+
];
27390+
27391+
for (const sectionName of sectionOrder) {
27392+
if (ver.sections[sectionName]) {
27393+
contentParts.push(
27394+
`### ${sectionName.charAt(0).toUpperCase() + sectionName.slice(1)}\n\n${ver.sections[sectionName]}`
27395+
);
27396+
}
27397+
}
2741427398

27415-
// Add general section last if it exists (without a header since it's raw content)
27416-
if (ver.sections['general']) {
27417-
contentParts.push(ver.sections['general']);
27418-
}
27399+
// Add any sections not in the standard order (except 'general')
27400+
for (const [sectionName, sectionContent] of Object.entries(ver.sections)) {
27401+
if (!sectionOrder.includes(sectionName) && sectionName !== 'general') {
27402+
contentParts.push(
27403+
`### ${sectionName.charAt(0).toUpperCase() + sectionName.slice(1)}\n\n${sectionContent}`
27404+
);
27405+
}
27406+
}
2741927407

27420-
ver.contents = contentParts.join('\n\n');
27421-
}
27408+
// Add general section last if it exists (without a header since it's raw content)
27409+
if (ver.sections['general']) {
27410+
contentParts.push(ver.sections['general']);
27411+
}
2742227412

27423-
// Filter by specific version if requested
27424-
if (version) {
27425-
const normalizedVersion = version.replace(/^v/, ''); // Remove 'v' prefix if present
27426-
const filtered = versions.filter((v) => v.name === normalizedVersion);
27427-
return filtered
27428-
}
27413+
ver.contents = contentParts.join('\n\n');
27414+
}
2742927415

27430-
// Return all versions by default
27431-
return versions
27432-
}
27416+
// Filter by specific version if requested
27417+
if (version) {
27418+
const normalizedVersion = version.replace(/^v/, ''); // Remove 'v' prefix if present
27419+
const filtered = versions.filter((v) => v.name === normalizedVersion);
27420+
return filtered
27421+
}
2743327422

27434-
parser = { parseChangelog };
27435-
return parser;
27423+
// Return all versions by default
27424+
return versions
2743627425
}
2743727426

27438-
var parserExports = requireParser();
27439-
2744027427
/**
2744127428
* Main action entry point
2744227429
*/
@@ -27458,7 +27445,7 @@ async function run() {
2745827445
const changelogContent = require$$1.readFileSync(fullPath, 'utf8');
2745927446

2746027447
// Parse changelog
27461-
const results = parserExports.parseChangelog(changelogContent, version);
27448+
const results = parseChangelog(changelogContent, version);
2746227449

2746327450
if (results.length === 0) {
2746427451
coreExports.warning(
@@ -27488,6 +27475,5 @@ async function run() {
2748827475
* main logic.
2748927476
*/
2749027477

27491-
/* istanbul ignore next */
2749227478
run();
2749327479
//# sourceMappingURL=index.js.map

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)