Skip to content

Commit

Permalink
Update checkDocs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
spacevx committed Mar 7, 2024
1 parent 4eb033b commit 6afe6f1
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions .github/scripts/checkDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');
// Define an array of directories to exclude
const excludedDirectories = ['.git', '.github', '.ci'];

// List all directories at the project root, excluding specific directories
// List all directories at the project root, excluding specific directories defined in excludedDirectories
const rootFolders = fs.readdirSync('./', { withFileTypes: true })
.filter(dirent => dirent.isDirectory() && !excludedDirectories.includes(dirent.name))
.map(dirent => dirent.name); // Map to directory names
Expand All @@ -18,41 +18,49 @@ const errors = [];
/**
* Checks the documentation of the provided file to ensure it matches specific criteria.
* Adjusted to find 'ns' header anywhere in the file and collect errors without stopping.
* Also checks against excludedDirectories to ignore specific folders.
* @param {string} filePath The path of the file to be checked.
*/
const checkFile = (filePath) => {
const content = fs.readFileSync(filePath, 'utf8');

// Adjusted regex to find 'ns' header anywhere in the file
const nsHeaderRegex = /---\nns: (.*)\n/m;
const nsMatch = content.match(nsHeaderRegex);

if (!nsMatch) {
errors.push(`Invalid or missing 'ns' header in ${filePath}`);
return;
}

const nsValue = nsMatch[1].trim();
const folderName = path.basename(path.dirname(filePath));

if (nsValue !== folderName) {
errors.push(`The file \`${path.basename(filePath)}\` is located in the '${folderName}' directory but should be in '${nsValue}' (based on the ns).`);
}

// First, check if the ns exists as a folder name in the project
if (!rootFolders.includes(nsValue)) {
errors.push(`The namespace '${nsValue}' specified in ${filePath} does not exist as a directory in the project. It should be one of ${rootFolders.join(', ')}.`);
return;
}

// Now that we know ns exists, verify if ns matches the directory name (workspace)
const folderName = path.basename(path.dirname(filePath));
if (nsValue !== folderName) {
errors.push(`The file \`${path.basename(filePath)}\` is located in the '${folderName}' directory but should be in '${nsValue}' (based on the ns).`);
}
};

/**
* Processes each file passed to the script, ignoring files in excluded directories and README.md.
* Processes each file passed to the script, using excludedDirectories to ignore files in specific directories.
* @param {string[]} files The array of file paths to be processed.
*/
const processFiles = (files) => {
files.forEach((file) => {
// Normalize file path for consistent comparison across platforms
const normalizedFile = path.normalize(file);
const directoryName = normalizedFile.split(path.sep)[0]; // Get the top-level directory name

// Determine if the file is in an excluded directory
const directoryName = normalizedFile.split(path.sep).find(part => excludedDirectories.includes(part));

// Check for exclusions, including specific files and directories
if (excludedDirectories.includes(directoryName) || path.basename(normalizedFile) === 'README.md') {
// Check for exclusions, including README.md and directories in excludedDirectories
if (directoryName || path.basename(normalizedFile) === 'README.md') {
console.log(`Ignoring file: ${file}`);
return; // Skip this file
}
Expand All @@ -64,7 +72,7 @@ const processFiles = (files) => {
// Get the file paths from the command line arguments
const files = process.argv.slice(2);

// Process each file, excluding specific files and directories
// Process each file, taking into account the exclusions
processFiles(files);

// After all files have been processed, check if there were any errors
Expand Down

0 comments on commit 6afe6f1

Please sign in to comment.