diff --git a/.github/scripts/checkDocs.js b/.github/scripts/checkDocs.js index 897746faf..0a3dd040a 100644 --- a/.github/scripts/checkDocs.js +++ b/.github/scripts/checkDocs.js @@ -1,59 +1,59 @@ const fs = require('fs'); const path = require('path'); -const rootFolders = fs.readdirSync('./', { withFileTypes: true }) - .filter(dirent => dirent.isDirectory()) - .map(dirent => dirent.name); - +// Define an array of directories to exclude const excludedDirectories = ['.git', '.github', '.ci']; -const errors = []; -const processFile = (filePath) => { - if (!fs.existsSync(filePath)) { - console.log(`File does not exist: ${filePath}`); - return; - } +// List all directories at the project root, excluding specific directories +const rootFolders = fs.readdirSync('./', { withFileTypes: true }) + .filter(dirent => dirent.isDirectory() && !excludedDirectories.includes(dirent.name)) + .map(dirent => dirent.name); // Map to directory names + +const errors = []; // Collects errors instead of immediately exiting the process. +const checkFile = (filePath) => { const content = fs.readFileSync(filePath, 'utf8'); const nsHeaderRegex = /---\nns: (.*)\n/m; const nsMatch = content.match(nsHeaderRegex); if (!nsMatch) { errors.push(`Invalid or missing 'ns' header in ${filePath}`); - return; + return; // Continue to next file instead of exiting. } const nsValue = nsMatch[1].trim(); + if (!rootFolders.includes(nsValue)) { + // Report error if ns doesn't exist as a directory errors.push(`The namespace '${nsValue}' specified in ${filePath} does not exist as a directory in the project.`); } else { const folderName = path.basename(path.dirname(filePath)); if (nsValue !== folderName) { + // Report error if file is in the wrong directory based on ns errors.push(`The file \`${path.basename(filePath)}\` is located in the '${folderName}' directory but should be in '${nsValue}' (based on the ns).`); } } }; -const main = () => { - const files = process.argv.slice(2).join(' ').split(' '); - files.forEach(file => { +const processFiles = (files) => { + files.forEach((file) => { const normalizedFile = path.normalize(file); - const directoryName = path.dirname(normalizedFile); - // Skip if in an excluded directory or is README.md - if (excludedDirectories.includes(directoryName) || path.basename(normalizedFile) === 'README.md') { - return; + if (excludedDirectories.includes(path.basename(path.dirname(normalizedFile))) || path.basename(normalizedFile) === 'README.md') { + console.log(`Ignoring file: ${file}`); + return; // Skip this file } - - processFile(normalizedFile); + console.log(file); + checkFile(normalizedFile); }); - - if (errors.length > 0) { - console.error("Errors found:\n" + errors.join("\n")); - process.exit(1); - } else { - console.log("No errors found."); - } }; -main(); +const files = process.argv.slice(2); +processFiles(files); + +if (errors.length > 0) { + console.error("Errors found:\n" + errors.join("\n")); + process.exit(1); +} else { + console.log("No errors found."); +} diff --git a/.github/workflows/error.yml b/.github/workflows/error.yml index f146251ca..9bda1da85 100644 --- a/.github/workflows/error.yml +++ b/.github/workflows/error.yml @@ -22,9 +22,8 @@ jobs: node-version: '12' - name: Get modified .md files + run: echo "::set-output name=modified_md_files::$(git diff --name-only HEAD HEAD~ -- '*.md')" id: get-modified-files - run: | - echo "::set-output name=modified_md_files::$(git diff --name-only HEAD HEAD~ -- '*.md')" - name: Check Markdown Documentation if: always()