-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-dev): conformance rule for blog post description
(cherry picked from commit 3d0b15e)
- Loading branch information
1 parent
c87571e
commit 3920bf8
Showing
5 changed files
with
131 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
tools/workspace-plugin/src/conformance-rules/blog-description/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
11 changes: 11 additions & 0 deletions
11
tools/workspace-plugin/src/conformance-rules/blog-description/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |