Skip to content

Commit 0850211

Browse files
committed
bug: fixed bug in the publishing pipeline.
1 parent 2e233c7 commit 0850211

3 files changed

Lines changed: 115 additions & 4 deletions

File tree

content/published/safely-bracket-resource-usage.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22

33
title: "Safely Bracket Resource Usage with `acquireRelease`"
4-
id: "acquire-release-bracket"
4+
id: "safely-bracket-resource-usage"
55
skillLevel: "beginner"
66
useCase: ["Resource Management", "File Handling", "Database Connections", "Network Requests"]
77
summary: "Use `Effect.acquireRelease` to guarantee a resource's cleanup logic runs, even if errors or interruptions occur."

scripts/frontmatter-validator.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env tsx
2+
3+
import fs from 'fs/promises';
4+
import path from 'path';
5+
import { Command } from 'commander';
6+
import matter from 'gray-matter';
7+
8+
// Define command line options
9+
const program = new Command();
10+
program
11+
.requiredOption('--indir <directory>', 'Input directory containing MDX files')
12+
.option('--count <number>', 'Number of files to process', parseInt)
13+
.parse(process.argv);
14+
15+
const options = program.opts();
16+
17+
// Validate required options
18+
if (!options.indir) {
19+
console.error('❌ Error: --indir is required');
20+
process.exit(1);
21+
}
22+
23+
const inputDir = path.resolve(options.indir);
24+
const count = options.count;
25+
26+
const main = async () => {
27+
try {
28+
// Ensure directory exists
29+
await fs.access(inputDir);
30+
31+
console.log(`Validating frontmatter in MDX files in ${inputDir}`);
32+
33+
// Get all MDX files from input directory
34+
const files = await fs.readdir(inputDir);
35+
const mdxFiles = files.filter(file => file.endsWith('.mdx') && file !== 'index.mdx');
36+
37+
console.log(`Found ${mdxFiles.length} MDX files`);
38+
39+
// Limit the number of files to process if count is specified
40+
const filesToProcess = count ? mdxFiles.slice(0, count) : mdxFiles;
41+
console.log(`Processing ${filesToProcess.length} files from ${inputDir}`);
42+
43+
let hasErrors = false;
44+
let errorCount = 0;
45+
46+
for (const mdxFile of filesToProcess) {
47+
const baseName = path.basename(mdxFile, '.mdx');
48+
const mdxFilePath = path.join(inputDir, mdxFile);
49+
50+
try {
51+
// Read MDX file content
52+
const mdxContent = await fs.readFile(mdxFilePath, 'utf-8');
53+
54+
// Parse frontmatter
55+
const { data } = matter(mdxContent);
56+
57+
// Validate frontmatter has required fields
58+
if (!data.id) {
59+
console.error(`❌ Error: No 'id' field in frontmatter of ${mdxFile}`);
60+
hasErrors = true;
61+
errorCount++;
62+
continue;
63+
}
64+
65+
// Check if frontmatter id matches filename
66+
if (data.id !== baseName) {
67+
console.error(`❌ Error: Frontmatter id "${data.id}" does not match filename "${baseName}" in ${mdxFile}`);
68+
hasErrors = true;
69+
errorCount++;
70+
continue;
71+
}
72+
73+
// Verify the file exists
74+
const mdxExists = await fs.access(mdxFilePath)
75+
.then(() => true)
76+
.catch(() => false);
77+
78+
if (!mdxExists) {
79+
console.error(`❌ Error: MDX file ${mdxFile} does not exist but is referenced in frontmatter`);
80+
hasErrors = true;
81+
errorCount++;
82+
continue;
83+
}
84+
85+
console.log(`✅ Validated frontmatter for ${mdxFile}`);
86+
} catch (error: any) {
87+
console.error(`❌ Error processing ${mdxFile}: ${error?.message || String(error)}`);
88+
hasErrors = true;
89+
errorCount++;
90+
}
91+
}
92+
93+
if (hasErrors) {
94+
console.error(`❌ Fatal error: Found ${errorCount} validation errors`);
95+
process.exit(1);
96+
} else {
97+
console.log(`✨ Validation complete! All ${filesToProcess.length} files have valid frontmatter.`);
98+
}
99+
} catch (error: any) {
100+
console.error(`❌ Fatal error: ${error?.message || String(error)}`);
101+
process.exit(1);
102+
}
103+
};
104+
105+
main();

scripts/validate_and_generate.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@ const SRC_DIR = path.join(process.cwd(), "content/src");
1515
console.log("Starting validation and README generation process...");
1616

1717
try {
18-
// Step 1: Run the pattern validator to ensure all TypeScript code blocks match source files
18+
// Step 1: Validate frontmatter ids match filenames
19+
console.log("\n🔍 Validating frontmatter in MDX files...");
20+
execSync(`npx tsx ${path.join(SCRIPTS_DIR, "frontmatter-validator.ts")} --indir ${PUBLISHED_DIR}`, {
21+
stdio: "inherit",
22+
});
23+
24+
// Step 2: Run the pattern validator to ensure all TypeScript code blocks match source files
1925
console.log("\n🔍 Validating that all TypeScript code blocks match source files...");
2026
execSync(`npx tsx ${path.join(SCRIPTS_DIR, "pattern-validator.ts")} --indir ${PUBLISHED_DIR} --srcdir ${SRC_DIR}`, {
2127
stdio: "inherit",
2228
});
2329

24-
// Step 2: Generate the README.md
30+
// Step 3: Generate the README.md
2531
console.log("\n📝 Generating README.md...");
2632
execSync(`npx tsx ${path.join(SCRIPTS_DIR, "generate_readme.ts")}`, {
2733
stdio: "inherit",
2834
});
2935

3036
console.log("\n✅ Validation and README generation completed successfully!");
31-
console.log("All TypeScript code blocks in MDX files match their source files.");
37+
console.log("All MDX files have valid frontmatter and TypeScript code blocks match source files.");
3238
console.log("README.md has been generated with links to all validated patterns.");
3339

3440
} catch (error) {

0 commit comments

Comments
 (0)