From bd71f414681e3c1ea9b1acf81b75b81fef0b326b Mon Sep 17 00:00:00 2001 From: Laura Armitage Date: Tue, 5 Mar 2024 18:09:10 +0000 Subject: [PATCH] GH-240 Make replace in readme backwards compatible with source/action --- src/action-docs.ts | 73 ++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/src/action-docs.ts b/src/action-docs.ts index 16e3069..1805671 100644 --- a/src/action-docs.ts +++ b/src/action-docs.ts @@ -309,52 +309,61 @@ async function updateReadme( actionFile: string, ): Promise { const lineBreak = getLineBreak(options.lineBreaks); - if (section === "usage") { - const readmeFileText = String(readFileSync(options.readmeFile, "utf-8")); - const match = readmeFileText.match( - new RegExp( - `.?`, - ), - ) as string[]; - - if (match) { - const commentExpression = ``; + + const readmeFileText = String(readFileSync(options.readmeFile, "utf-8")); + const sourceOrActionMatches = readmeFileText.match( + new RegExp(`.?`, + ), + ) as string[]; + + if (match) { + const commentExpression = ``; + const regexp = new RegExp( + `${escapeRegExp(commentExpression)}(?:(?:\r\n|\r|\n.*)+${escapeRegExp(commentExpression)})?`, + ); + + const processedText = text + .trim() + .replace("***PROJECT***", match[1]) + .replace("***VERSION***", match[2]); + + await replaceInFile.replaceInFile({ + files: options.readmeFile, + from: regexp, + to: + commentExpression + + lineBreak + + processedText + + lineBreak + + commentExpression, + }); + } + } else { + const commentExpression = ``; const regexp = new RegExp( `${escapeRegExp(commentExpression)}(?:(?:\r\n|\r|\n.*)+${escapeRegExp(commentExpression)})?`, ); - const processedText = text - .trim() - .replace("***PROJECT***", match[1]) - .replace("***VERSION***", match[2]); - await replaceInFile.replaceInFile({ files: options.readmeFile, from: regexp, to: commentExpression + lineBreak + - processedText + + text.trim() + lineBreak + commentExpression, }); } - } else { - const commentExpression = ``; - const regexp = new RegExp( - `${escapeRegExp(commentExpression)}(?:(?:\r\n|\r|\n.*)+${escapeRegExp(commentExpression)})?`, - ); - - await replaceInFile.replaceInFile({ - files: options.readmeFile, - from: regexp, - to: - commentExpression + - lineBreak + - text.trim() + - lineBreak + - commentExpression, - }); } }