Skip to content

Commit

Permalink
refactor: remove duplication (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
npalm authored Mar 9, 2024
1 parent dc70838 commit 168f763
Showing 1 changed file with 37 additions and 45 deletions.
82 changes: 37 additions & 45 deletions src/action-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,53 +321,45 @@ async function updateReadme(

if (sourceOrActionMatches) {
const sourceOrAction = sourceOrActionMatches[1];

if (section === "usage" || section === "all") {
const match = readmeFileText.match(
new RegExp(
`<!-- action-docs-${section} ${sourceOrAction}="${escapeRegExp(sourceFile)}" project="(.*)" version="(.*)" -->.?`,
),
) as string[];

if (match) {
const commentExpression = `<!-- action-docs-${section} ${sourceOrAction}="${sourceFile}" project="${match[1]}" version="${match[2]}" -->`;
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 = `<!-- action-docs-${section} ${sourceOrAction}="${sourceFile}" -->`;
const regexp = new RegExp(
`${escapeRegExp(commentExpression)}(?:(?:\r\n|\r|\n.*)+${escapeRegExp(commentExpression)})?`,
const matchProjectVersion = readmeFileText.match(
new RegExp(
`<!-- action-docs-${section} ${sourceOrAction}="${escapeRegExp(sourceFile)}" project="(.*)" version="(.*)" -->.?`,
),
) as string[];

let commentExpression = `<!-- action-docs-${section} ${sourceOrAction}="${sourceFile}" PROJECT_VERSION-->`;
commentExpression = commentExpression.replace(
"PROJECT_VERSION",
matchProjectVersion
? `project="${matchProjectVersion[1]}" version="${matchProjectVersion[2]}" `
: "",
);

const regexp = new RegExp(
`${escapeRegExp(commentExpression)}(?:(?:\r\n|\r|\n.*)+${escapeRegExp(commentExpression)})?`,
);

const processedText = text
.trim()
.replace(
"***PROJECT***",
matchProjectVersion ? matchProjectVersion[1] : "",
)
.replace(
"***VERSION***",
matchProjectVersion ? matchProjectVersion[2] : "",
);

await replaceInFile.replaceInFile({
files: options.readmeFile,
from: regexp,
to:
commentExpression +
lineBreak +
text.trim() +
lineBreak +
commentExpression,
});
}
await replaceInFile.replaceInFile({
files: options.readmeFile,
from: regexp,
to:
commentExpression +
lineBreak +
processedText +
lineBreak +
commentExpression,
});
}
}

Expand Down

0 comments on commit 168f763

Please sign in to comment.