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: optimized the directory traversal in check-edit-links script #3590

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
59 changes: 33 additions & 26 deletions scripts/markdown/check-edit-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,42 @@ function determineEditLink(urlPath, filePath, editOptions) {
* @param {object[]} [result=[]] - Accumulator for results
* @returns {Promise<object[]>} Array of objects containing file paths and edit links
*/
async function generatePaths(folderPath, editOptions, relativePath = '', result = []) {
try {
const files = await fs.readdir(folderPath);

await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const relativeFilePath = path.join(relativePath, file);

// Skip _section.md files
if (file === '_section.md') {
return;
}
async function* walkDirectory(dir) {

const stats = await fs.stat(filePath);

if (stats.isDirectory()) {
await generatePaths(filePath, editOptions, relativeFilePath, result);
} else if (stats.isFile() && file.endsWith('.md')) {
const urlPath = relativeFilePath.split(path.sep).join('/').replace('.md', '');
result.push({
filePath,
urlPath,
editLink: determineEditLink(urlPath, filePath, editOptions)
});
}
})
);
try {
const files = await fs.readdir(dir);
for (const file of files) {
if (file === '_section.md') continue;

const filePath = path.join(dir, file);
const stats = await fs.stat(filePath);

if (stats.isDirectory()) {
yield* walkDirectory(filePath);
} else if (stats.isFile() && file.endsWith('.md')) {
yield filePath;
}
}
} catch (err) {
throw new Error(`Error processing directory ${dir}: ${err.message}`);
}
}

async function generatePaths(folderPath, editOptions) {

const result = [];
try {
for await (const filePath of walkDirectory(folderPath)) {
const relativePath = path.relative(folderPath, filePath);
const urlPath = relativePath.split(path.sep).join('/').replace('.md', '');

result.push({
filePath,
urlPath,
editLink: determineEditLink(urlPath, filePath, editOptions)
});
}
return result;
} catch (err) {
throw new Error(`Error processing directory ${folderPath}: ${err.message}`);
Expand Down
Loading