Skip to content

Commit 7913af2

Browse files
committed
Another attempt at fixing the CodeQL race condition
1 parent fac3a72 commit 7913af2

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

tests/frontend/transformers/jekyll-js-transformer.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,33 @@ function loadDirData(dir, basePath = '') {
6464
// Loop over all items in the directory
6565
for (const item of dirItems) {
6666
const fullPath = path.join(dir, item);
67-
const stat = fs.statSync(fullPath);
67+
const ext = path.extname(item);
68+
const name = path.basename(item, ext);
6869

69-
if (stat.isDirectory()) {
70-
// Item is a subdirectory, so recurse on this function
71-
dirData[item] = loadDirData(fullPath, path.join(basePath, item));
72-
} else if (stat.isFile()) {
73-
// Item is a file, so process the data appropriately
74-
const ext = path.extname(item);
75-
const name = path.basename(item, ext);
70+
try {
71+
// Try to read as a file
72+
const fileContent = fs.readFileSync(fullPath, 'utf-8');
73+
74+
if (ext === ".json") {
75+
dirData[name] = JSON.parse(fileContent);
76+
} else if (ext === '.yml' || ext === '.yaml') {
77+
// Target YMLs might use multi-doc syntax, so we need to use loadAll
78+
const ymlDocs = yaml.loadAll(fileContent);
7679

80+
// If only one document, return just the one. Otherwise return array
81+
dirData[name] = ymlDocs.length === 1 ? ymlDocs[0] : ymlDocs;
82+
}
83+
} catch {
84+
// Might be a directory instead
7785
try {
78-
if (ext === ".json") {
79-
dirData[name] = JSON.parse(fs.readFileSync(fullPath, 'utf-8'));
80-
} else if (ext === '.yml' || ext === '.yaml') {
81-
// Target YMLs might use multi-doc syntax, so we need to use loadAll
82-
const ymlDocs = yaml.loadAll(fs.readFileSync(fullPath, 'utf-8'));
83-
84-
// If only one document, return just the one. Otherwise return array
85-
dirData[name] = ymlDocs.length === 1 ? ymlDocs[0] : ymlDocs;
86+
const stat = fs.statSync(fullPath);
87+
if (stat.isDirectory()) {
88+
// Item is a subdirectory, so recurse on this functoin
89+
dirData[item] = loadDirData(fullPath, path.join(basePath, item));
8690
}
87-
} catch (err) {
88-
console.warn(`Could not load ${fullPath}: ${err.message}`);
91+
} catch (error) {
92+
// File or directory isn't accessible
93+
console.warn(`Could not access ${fullPath}: ${error}`);
8994
}
9095
}
9196
}

0 commit comments

Comments
 (0)