Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to section loss when parameters section is empty #80

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,25 @@ function insertReadmeTable(readmeFilePath, sections, config) {
const data = fs.readFileSync(readmeFilePath, 'UTF-8');
const lines = data.split(/\r?\n/);
let lineNumberSigns; // Store section # starting symbols
// This array contains the index of the first and the last line to update in the README file
const paramsSectionLimits = [];
lines.forEach((line, i) => {
// Find parameters section start
const match = line.match(new RegExp(`^(##+) ${config.regexp.paramsSectionTitle}`)); // use minimun two # symbols since just one is the README title
if (match) {
/* eslint-disable prefer-destructuring */
lineNumberSigns = match[1];
paramsSectionLimits.push(i);
paramsSectionLimits.push(i + 1);
console.log(`INFO: Found parameters section at line: ${i + 1}`);
}
});
if (paramsSectionLimits.length === 1) {
// Find parameters section end
let nextSectionFound = false;
lines.slice(paramsSectionLimits[0] + 1).forEach((line, i) => {
lines.slice(paramsSectionLimits[0]).forEach((line, i) => {
const nextSectionRegExp = new RegExp(`^${lineNumberSigns}\\s`); // Match same level section
if (!nextSectionFound && line.match(nextSectionRegExp)) {
const index = paramsSectionLimits[0] + 1 + i;
const index = paramsSectionLimits[0] + i;
console.log(`INFO: Found section end at line: ${index + 1}`);
paramsSectionLimits.push(index);
nextSectionFound = true;
Expand All @@ -99,13 +100,21 @@ function insertReadmeTable(readmeFilePath, sections, config) {
// Detect last table-like line bottom to top to ignore description text between tables
let lastTableLikeLineFound = false;
const endParamsSectionRegExp = new RegExp('(?!.*\\|).*\\S(?<!\\|.*)(?<!#.*)'); // Match non empty or with non table format lines
lines.slice(paramsSectionLimits[0] + 1, paramsSectionLimits[1]).reverse().forEach((line, i) => {
lines.slice(paramsSectionLimits[0], paramsSectionLimits[1]).reverse().forEach((line, i) => {
if (!lastTableLikeLineFound && line && !line.match(endParamsSectionRegExp)) {
lastTableLikeLineFound = true;
// renderReadmeTable adds a blank line at the end, we have to remove it also.
// The index points to that blank line.
paramsSectionLimits[1] -= i;
// This log makes reference to the last line in the table.
console.log(`INFO: Last parameter table line found at line: ${paramsSectionLimits[1]}`);
}
});
if (!lastTableLikeLineFound) {
// If there is no parameter table, we will add the new table at the begining of the section.
paramsSectionLimits[1] = paramsSectionLimits[0];
console.log('INFO: No parameters table found');
}
}
if (paramsSectionLimits.length !== 2) {
throw new Error('ERROR: error getting current Parameters section from README');
Expand All @@ -115,9 +124,9 @@ function insertReadmeTable(readmeFilePath, sections, config) {
// Build the table adding the proper number of # to the section headers
const newParamsSection = renderReadmeTable(sections, `${lineNumberSigns}#`);
// Delete the old parameters section
lines.splice(paramsSectionLimits[0] + 1, paramsSectionLimits[1] - paramsSectionLimits[0]);
lines.splice(paramsSectionLimits[0], paramsSectionLimits[1] - paramsSectionLimits[0] + 1);
// Add the new parameters section
lines.splice(paramsSectionLimits[0] + 1, 0, ...newParamsSection.split(/\r?\n/));
lines.splice(paramsSectionLimits[0], 0, ...newParamsSection.split(/\r?\n/));

fs.writeFileSync(readmeFilePath, lines.join('\n'));
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitnami/readme-generator-for-helm",
"version": "2.6.0",
"version": "2.6.1",
"description": "Autogenerate READMEs tables and OpenAPI schemas for Helm Charts",
"main": "index.js",
"scripts": {
Expand Down
Loading