Skip to content

Commit

Permalink
Added concurrenly limit in markdown file processing to improve perfor…
Browse files Browse the repository at this point in the history
…mance
  • Loading branch information
Aditya0733 committed Dec 10, 2024
1 parent 7f81142 commit 22779e9
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions scripts/markdown/check-markdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const fs = require('fs');
const fs = require('fs').promises;

Check warning on line 1 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L1

Added line #L1 was not covered by tests
const matter = require('gray-matter');
const path = require('path');
const pLimit = require('p-limit');

Check warning on line 4 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L4

Added line #L4 was not covered by tests

// Use environment variable for concurrency limit, default to 10
const CONCURRENCY_LIMIT = process.env.CONCURRENCY_LIMIT

Check warning on line 7 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L7

Added line #L7 was not covered by tests
? parseInt(process.env.CONCURRENCY_LIMIT, 10)
: 10;

/**
* Checks if a given string is a valid URL.
Expand All @@ -19,7 +25,6 @@ function isValidURL(str) {
/**
* Validates the frontmatter of a blog post.
* @param {object} frontmatter - The frontmatter object to validate.
* @param {string} filePath - The path to the file being validated.
* @returns {string[]|null} An array of validation error messages, or null if no errors.
*/
function validateBlogs(frontmatter) {
Expand Down Expand Up @@ -73,7 +78,6 @@ function validateBlogs(frontmatter) {
/**
* Validates the frontmatter of a documentation file.
* @param {object} frontmatter - The frontmatter object to validate.
* @param {string} filePath - The path to the file being validated.
* @returns {string[]|null} An array of validation error messages, or null if no errors.
*/
function validateDocs(frontmatter) {
Expand All @@ -98,14 +102,16 @@ function validateDocs(frontmatter) {
* @param {Function} validateFunction - The function used to validate the frontmatter.
* @param {string} [relativePath=''] - The relative path of the folder for logging purposes.
*/
function checkMarkdownFiles(folderPath, validateFunction, relativePath = '') {
fs.readdir(folderPath, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
async function checkMarkdownFiles(folderPath, validateFunction, relativePath = '') {
// Create a concurrency limit
const limit = pLimit(CONCURRENCY_LIMIT);

Check warning on line 107 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L107

Added line #L107 was not covered by tests

files.forEach(file => {
try {

Check warning on line 109 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L109

Added line #L109 was not covered by tests
// Read directory contents
const files = await fs.readdir(folderPath);

Check warning on line 111 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L111

Added line #L111 was not covered by tests

// Process files with concurrency control
const filePromises = files.map(file => limit(async () => {

Check warning on line 114 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L114

Added line #L114 was not covered by tests
const filePath = path.join(folderPath, file);
const relativeFilePath = path.join(relativePath, file);

Expand All @@ -114,29 +120,32 @@ function checkMarkdownFiles(folderPath, validateFunction, relativePath = '') {
return;
}

fs.stat(filePath, (err, stats) => {
if (err) {
console.error('Error reading file stats:', err);
return;
}
try {

Check warning on line 123 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L123

Added line #L123 was not covered by tests
// Get file stats
const stats = await fs.stat(filePath);

Check warning on line 125 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L125

Added line #L125 was not covered by tests

// Recurse if directory, otherwise validate markdown file
if (stats.isDirectory()) {
checkMarkdownFiles(filePath, validateFunction, relativeFilePath);
await checkMarkdownFiles(filePath, validateFunction, relativeFilePath);

Check warning on line 129 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L129

Added line #L129 was not covered by tests
} else if (path.extname(file) === '.md') {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const fileContent = await fs.readFile(filePath, 'utf-8');

Check warning on line 131 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L131

Added line #L131 was not covered by tests
const { data: frontmatter } = matter(fileContent);

const errors = validateFunction(frontmatter);
if (errors) {
console.log(`Errors in file ${relativeFilePath}:`);
errors.forEach(error => console.log(` - ${error}`));
process.exitCode = 1;
}
}
});
});
});
} catch (statError) {
throw statError;

Check warning on line 140 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L140

Added line #L140 was not covered by tests
}
}));

// Wait for all file promises to complete
await Promise.all(filePromises);

Check warning on line 145 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L145

Added line #L145 was not covered by tests
} catch (filesError) {
throw filesError;

Check warning on line 147 in scripts/markdown/check-markdown.js

View check run for this annotation

Codecov / codecov/patch

scripts/markdown/check-markdown.js#L147

Added line #L147 was not covered by tests
}
}

const docsFolderPath = path.resolve(__dirname, '../../markdown/docs');
Expand Down

0 comments on commit 22779e9

Please sign in to comment.