Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
spacevx committed Mar 7, 2024
1 parent bc55379 commit df4440b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
56 changes: 28 additions & 28 deletions .github/scripts/checkDocs.js
Original file line number Diff line number Diff line change
@@ -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.");
}
3 changes: 1 addition & 2 deletions .github/workflows/error.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit df4440b

Please sign in to comment.