Skip to content

Commit

Permalink
feat(nx-dev): conformance rule for blog post description
Browse files Browse the repository at this point in the history
(cherry picked from commit 3d0b15e)
  • Loading branch information
juristr authored and FrozenPandaz committed Feb 6, 2025
1 parent c87571e commit 3920bf8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 20 deletions.
7 changes: 7 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@
"defaultBase": "master",
"conformance": {
"rules": [
{
"rule": "@nx/workspace-plugin/conformance-rules/blog-description",
"projects": ["docs"],
"options": {
"mdGlobPattern": "blog/**/*.md"
}
},
{
"rule": "@nx/workspace-plugin/conformance-rules/project-package-json",
"projects": [
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
"@nx/rsbuild": "20.4.0-beta.2",
"@nx/rspack": "20.4.0-beta.2",
"@nx/storybook": "20.4.0-beta.2",
"@nx/vite": "20.4.0-beta.2",
"@nx/web": "20.4.0-beta.2",
"@nx/webpack": "20.4.0-beta.2",
"@nx/vite": "20.4.0-beta.2",
"@phenomnomnominal/tsquery": "~5.0.1",
"@playwright/test": "^1.36.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
Expand All @@ -102,8 +102,8 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-url": "^8.0.2",
"@rspack/core": "1.1.6",
"@rsbuild/core": "1.1.8",
"@rspack/core": "1.1.6",
"@rspack/dev-server": "1.0.9",
"@rspack/plugin-minify": "^0.7.5",
"@rspack/plugin-react-refresh": "^1.0.0",
Expand Down Expand Up @@ -136,6 +136,7 @@
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/jest": "29.5.12",
"@types/js-yaml": "^4.0.9",
"@types/marked": "^2.0.0",
"@types/node": "20.16.10",
"@types/npm-package-arg": "6.1.1",
Expand Down Expand Up @@ -227,6 +228,7 @@
"jest-runtime": "29.7.0",
"jest-util": "29.7.0",
"js-tokens": "^4.0.0",
"js-yaml": "^4.1.0",
"jsonc-eslint-parser": "^2.1.0",
"jsonc-parser": "3.2.0",
"kill-port": "^1.6.1",
Expand Down
47 changes: 29 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { readFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { load as yamlLoad } from 'js-yaml';
import { workspaceRoot } from '@nx/devkit';
import { sync as globSync } from 'glob';
import {
createConformanceRule,
type ProjectFilesViolation,
} from '@nx/powerpack-conformance';

export default createConformanceRule<{ mdGlobPattern: string }>({
name: 'blog-description',
category: 'consistency',
description:
'Ensures that blog posts have a description in their frontmatter',
reporter: 'project-files-reporter',
implementation: async ({ projectGraph, ruleOptions }) => {
const violations: ProjectFilesViolation[] = [];
const { mdGlobPattern } = ruleOptions;

// Look for the docs project
const docsProject = Object.values(projectGraph.nodes).find(
(project) => project.name === 'docs'
);

if (!docsProject) {
return {
severity: 'low',
details: {
violations: [],
},
};
}

const blogPattern = join(
workspaceRoot,
docsProject.data.root,
mdGlobPattern
);
const files = findMarkdownFiles(blogPattern);

for (const file of files) {
const content = readFileSync(file, 'utf-8');
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);

// Only check files with frontmatter
if (frontmatterMatch) {
try {
const frontmatter = yamlLoad(frontmatterMatch[1]) as Record<
string,
unknown
>;

if (!frontmatter.description) {
violations.push({
message:
'Blog posts with frontmatter must have a description field',
sourceProject: docsProject.name,
file: file,
});
}
} catch (e) {
// If YAML parsing fails, we skip the file
continue;
}
}
}

return {
severity: 'high',
details: {
violations,
},
};
},
});

function findMarkdownFiles(pattern: string): string[] {
return globSync(pattern);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"mdGlobPattern": {
"type": "string",
"description": "The glob pattern to use to find the markdown files to analyze"
}
},
"additionalProperties": false
}

0 comments on commit 3920bf8

Please sign in to comment.