Skip to content

Commit

Permalink
feat: Added concurrency limit in markdown files processing to improve…
Browse files Browse the repository at this point in the history
… performance
  • Loading branch information
Aditya0733 committed Dec 16, 2024
1 parent ce0e986 commit 129b9c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
10 changes: 5 additions & 5 deletions scripts/markdown/check-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ function getConcurrencyLimit() {
// Validate the parsed limit
if (Number.isNaN(parsedLimit)) {
console.warn(
`Invalid MARKDOWN_CONCURRENCY_LIMIT: '${envLimit}'. Falling back to default of 10.`,
`Invalid MARKDOWN_CONCURRENCY_LIMIT: '${envLimit}'. Falling back to default of 10.`
);
return 10;
}

// Check for non-positive integers
if (parsedLimit <= 0) {
console.warn(
`MARKDOWN_CONCURRENCY_LIMIT must be a positive integer. Received: ${parsedLimit}. Falling back to default of 10.`,
`MARKDOWN_CONCURRENCY_LIMIT must be a positive integer. Received: ${parsedLimit}. Falling back to default of 10.`
);
return 10;
}
Expand All @@ -44,7 +44,7 @@ function getConcurrencyLimit() {
*/
function isValidURL(str) {
try {
new URL(str);
URL(str);
return true;
} catch (err) {
return false;
Expand Down Expand Up @@ -209,7 +209,7 @@ async function main() {

await Promise.all([
checkMarkdownFiles(docsFolderPath, validateDocs, '', limit),
checkMarkdownFiles(blogsFolderPath, validateBlogs, '', limit),
checkMarkdownFiles(blogsFolderPath, validateBlogs, '', limit)
]);
} catch (error) {
console.error('Failed to validate markdown files:', error);
Expand All @@ -228,5 +228,5 @@ module.exports = {
checkMarkdownFiles,
main,
isValidURL,
getConcurrencyLimit,
getConcurrencyLimit
};
15 changes: 5 additions & 10 deletions tests/markdown/check-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
validateBlogs,
validateDocs,
checkMarkdownFiles,
getConcurrencyLimit,
getConcurrencyLimit
} = require('../../scripts/markdown/check-markdown');

describe('Frontmatter Validator', () => {
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Frontmatter Validator', () => {

describe('Concurrency Limit Validation', () => {
it('returns default concurrency limit when no env var is set', () => {
delete process.env.MARKDOWN_CONCURRENCY_LIMIT;
process.env.MARKDOWN_CONCURRENCY_LIMIT = undefined;
const limit = getConcurrencyLimit();
expect(limit).toBe(10);
});
Expand Down Expand Up @@ -83,18 +83,15 @@ describe('Frontmatter Validator', () => {
'Author at index 0 is missing a photo',
'Author at index 1 is missing a name',
'Invalid URL for author at index 2: not-a-url',
]),
])
);
});

it('validates docs frontmatter for required fields', async () => {
const frontmatter = { title: 123, weight: 'not-a-number' };
const errors = validateDocs(frontmatter);
expect(errors).toEqual(
expect.arrayContaining([
'Title is missing or not a string',
'Weight is missing or not a number',
]),
expect.arrayContaining(['Title is missing or not a string', 'Weight is missing or not a number'])
);
});

Expand Down Expand Up @@ -166,9 +163,7 @@ describe('Frontmatter Validator', () => {
const filePath = path.join(tempDir, 'invalid.md');
await fs.writeFile(filePath, `---\ntitle: Valid Title\n---`);

const mockReadFile = jest
.spyOn(fs, 'readFile')
.mockRejectedValue(new Error('Test readFile error'));
const mockReadFile = jest.spyOn(fs, 'readFile').mockRejectedValue(new Error('Test readFile error'));

await expect(
checkMarkdownFiles(tempDir, validateBlogs, '', pLimit(10)),
Expand Down

0 comments on commit 129b9c9

Please sign in to comment.