-
Notifications
You must be signed in to change notification settings - Fork 44
docs: add bun tab to all install code-groups #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,88 @@ | ||
| import { readdirSync, readFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| const PAGES_DIR = join(import.meta.dirname, "../src/pages"); | ||
| const REQUIRED_TABS = ["npm", "pnpm", "bun"] as const; | ||
|
|
||
| /** | ||
| * Recursively find all .mdx files under a directory. | ||
| */ | ||
| function findMdxFiles(dir: string): string[] { | ||
| const results: string[] = []; | ||
| for (const entry of readdirSync(dir, { withFileTypes: true })) { | ||
| const fullPath = join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| results.push(...findMdxFiles(fullPath)); | ||
| } else if (entry.name.endsWith(".mdx")) { | ||
| results.push(fullPath); | ||
| } | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Find install code-groups in MDX content and check they have all three tabs. | ||
| * Returns an array of violations with line numbers and missing tabs. | ||
| */ | ||
| function checkInstallTabs( | ||
| content: string, | ||
| ): Array<{ line: number; missing: string[] }> { | ||
| const lines = content.split("\n"); | ||
| const violations: Array<{ line: number; missing: string[] }> = []; | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| // Look for a bash [npm] block that contains an install command | ||
| const line = lines[i]; | ||
| if (!/^```bash \[npm\]/.test(line)) continue; | ||
|
|
||
| // Check the next line for an install command (npm install / npx) | ||
| const cmdLine = lines[i + 1] ?? ""; | ||
| const isInstall = | ||
| /^\$?\s*npm install\b/.test(cmdLine) || /^\$?\s*npx\b/.test(cmdLine); | ||
| if (!isInstall) continue; | ||
|
|
||
| // Scan the surrounding code-group for which tabs exist | ||
| const found = new Set<string>(["npm"]); | ||
|
|
||
| // Scan forward until ::: closing | ||
| for (let j = i + 1; j < lines.length; j++) { | ||
| if (/^:::$/.test(lines[j].trim())) break; | ||
| const tabMatch = lines[j].match(/^```bash \[(npm|pnpm|bun)\]/); | ||
| if (tabMatch) found.add(tabMatch[1]); | ||
| } | ||
|
|
||
| const missing = REQUIRED_TABS.filter((t) => !found.has(t)); | ||
| if (missing.length > 0) { | ||
| violations.push({ line: i + 1, missing }); | ||
| } | ||
| } | ||
|
|
||
| return violations; | ||
| } | ||
|
|
||
| describe("install code-groups must include npm, pnpm, and bun", () => { | ||
| const files = findMdxFiles(PAGES_DIR); | ||
|
|
||
| for (const file of files) { | ||
| const relative = file.slice(PAGES_DIR.length + 1); | ||
| const content = readFileSync(file, "utf-8"); | ||
| const violations = checkInstallTabs(content); | ||
|
|
||
| if (violations.length > 0) { | ||
| it(`${relative}`, () => { | ||
| const messages = violations.map( | ||
| (v) => | ||
| `line ${v.line}: missing ${v.missing.map((t) => `[${t}]`).join(", ")} tab(s)`, | ||
| ); | ||
| expect(violations).toHaveLength(0); | ||
| // Show details on failure: | ||
| expect.fail(`Install code-group missing tabs:\n${messages.join("\n")}`); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| it("found mdx files to check", () => { | ||
| expect(files.length).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkInstallTabsonly validates blocks after matching ````bash [npm], so an install code-group that accidentally omits the npm tab is skipped entirely. In that case the test passes even though the new policy requires all three tabs (npm`, `pnpm`, and `bun`), so this guardrail can miss real violations.Useful? React with 👍 / 👎.