DEV Community

Roberto Luna
Roberto Luna

Posted on

Fixing the Daily Content Generator’s Bluesky Payload – A Code‑First Walkthrough

Fixing the Daily Content Generator’s Bluesky Payload – A Code‑First Walkthrough

TL;DR: I rewrote the payload builder for the daily post generator so it correctly writes bluesky_es.json and updates metadata.json. The change eliminates a silent write‑failure and lets the CI pipeline publish to Bluesky without manual intervention.


The Problem

Our content-automation pipeline runs every morning, pulling the latest Markdown drafts and turning them into JSON payloads for three publishing targets: Medium, Substack, and Bluesky. While Medium and Substack posts were being generated without issue, the Bluesky step kept failing silently. The CI logs showed:

Error: ENOENT: no such file or directory, open 'content/2026/11/14/content-automation/bluesky_es.json'
Enter fullscreen mode Exit fullscreen mode

Because the file never existed, the subsequent git add and git commit steps were skipped, leaving metadata.json.bluesky_published stuck at false. The result was a daily “missing post” on Bluesky, while the rest of the ecosystem thought everything was fine.

What I Tried First

The first instinct was to add a try/catch around the file write in src/generate/bluesky.js:

try {
  await fs.promises.writeFile(payloadPath, JSON.stringify(payload));
} catch (e) {
  console.error('Bluesky write failed', e);
}
Enter fullscreen mode Exit fullscreen mode

That logged the error, but the pipeline still continued, and the metadata.json flag never flipped. I also attempted to create the missing file with an empty object before writing:

await fs.promises.writeFile(payloadPath, '{}');
await fs.promises.writeFile(payloadPath, JSON.stringify(payload));
Enter fullscreen mode Exit fullscreen mode

Both approaches were work‑arounds; they didn’t address why the path was wrong in the first place.

The Implementation

1. Aligning the file naming convention

The generator was using a hard‑coded English suffix (bluesky_en.json) for all locales, even when processing the Spanish draft. The function buildBlueskyPayload lived in src/generate/bluesky.js:

function buildBlueskyPayload(locale, content) {
  const filename = `bluesky_${locale}.json`; // <-- bug: locale was always 'en'
  const outPath = path.join(__dirname, '../../content/2026/11/14/content-automation', filename);
  // …
}
Enter fullscreen mode Exit fullscreen mode

The locale argument was being ignored because the call site passed a constant:

// src/generate/index.js
await buildBlueskyPayload('en', enContent); // second call mistakenly omitted
await buildBlueskyPayload('en', esContent); // <-- should be 'es'
Enter fullscreen mode Exit fullscreen mode

Fix

I introduced a small utility src/utils/locale.js:

// src/utils/locale.js
export const SUPPORTED = ['en', 'es'] as const;
export type Locale = typeof SUPPORTED[number];
Enter fullscreen mode Exit fullscreen mode

And rewrote the generator to enforce the locale:

// src/generate/bluesky.js
import type { Locale } from '../utils/locale';

export async function buildBlueskyPayload(locale: Locale, content: string) {
  const filename = `bluesky_${locale}.json`;
  const outPath = path.resolve(__dirname, '../../content/2026/11/14/content-automation', filename);

  const payload = {
    type: 'post',
    text: content,
    createdAt: new Date().toISOString(),
  };

  await fs.promises.mkdir(path.dirname(outPath), { recursive: true });
  await fs.promises.writeFile(outPath, JSON.stringify(payload, null, 2));
}
Enter fullscreen mode Exit fullscreen mode

And updated the call site:

// src/generate/index.js
await buildBlueskyPayload('en', enContent);
await buildBlueskyPayload('es', esContent); // fixed!
Enter fullscreen mode Exit fullscreen mode

2. Updating metadata.json atomically

metadata.json lives at content/2026/11/14/content-automation/metadata.json. The previous implementation performed two separate writes:

// src/generate/metadata.js
metadata.bluesky_published = true;
fs.writeFileSync(metaPath, JSON.stringify(metadata));
metadata.bluesky_uris[locale] = uri;
fs.writeFileSync(metaPath, JSON.stringify(metadata));
Enter fullscreen mode Exit fullscreen mode

If the process crashed between the two writes, the file could end up in an inconsistent state (e.g., bluesky_published: true but no URI). I replaced this with a single atomic update:

// src/generate/metadata.ts
import { promises as fs } from 'fs';
import path from 'path';

export async function updateBlueskyMeta(locale: 'en' | 'es', uri: string) {
  const metaPath = path.resolve(__dirname, '../../content/2026/11/14/content-automation/metadata.json');
  const raw = await fs.readFile(metaPath, 'utf-8');
  const meta = JSON.parse(raw);

  meta.bluesky_published = true;
  meta.bluesky_uris = { ...(meta.bluesky_uris || {}), [locale]: uri };

  await fs.writeFile(metaPath, JSON.stringify(meta, null, 2));
}
Enter fullscreen mode Exit fullscreen mode

The CI job now calls updateBlueskyMeta after a successful post:

// src/publish/bluesky.js
const uri = await postToBluesky(payload);
await updateBlueskyMeta(locale, uri);
Enter fullscreen mode Exit fullscreen mode

3. Adding a validation step

To avoid future silent failures, I added a JSON schema validator (src/validate/bluesky-schema.json) and a tiny CLI npm run validate:bluesky that runs before the commit step:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Bluesky Payload",
  "type": "object",
  "required": ["type", "text", "createdAt"],
  "properties": {
    "type": { "enum": ["post"] },
    "text": { "type": "string" },
    "createdAt": { "type": "string", "format": "date-time" }
  }
}
Enter fullscreen mode Exit fullscreen mode

The validation script:

// scripts/validate-bluesky.js
import Ajv from 'ajv';
import fs from 'fs';
import path from 'path';
import schema from '../src/validate/bluesky-schema.json';

const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(schema);

['en', 'es'].forEach(locale => {
  const file = path.resolve(__dirname, `../content/2026/11/14/content-automation/bluesky_${locale}.json`);
  const payload = JSON.parse(fs.readFileSync(file, 'utf-8'));
  if (!validate(payload)) {
    console.error(`Invalid payload for ${locale}:`, validate.errors);
    process.exit(1);
  }
});
console.log('✅ Bluesky payloads are valid');
Enter fullscreen mode Exit fullscreen mode

The CI workflow (.github/workflows/publish.yml) now includes:

- name: Validate Bluesky payloads
  run: npm run validate:bluesky
Enter fullscreen mode Exit fullscreen mode

4. Resulting diff snapshot

Below is the actual diff that landed in the repo (excerpted for brevity):


diff
--- a/src/generate/bluesky.js
+++ b/src/generate/bluesky.js
@@ -1,12 +1,31 @@
-import fs from 'fs';
-import path from 'path';
-
-export async function buildBlueskyPayload(locale, content) {
-  const filename = `bluesky_${locale}.json`; // bug: locale always 'en'
-  const outPath = path.join(__dirname, '../../content/2026/11/14/content-automation', filename);
+import { promises as fs } from 'fs';
+import path from 'path';
+import type { Locale } from '../utils/locale';
+
+/** Build a Blues

---

*Part of my [Build in Public](https://dev.to/zaerohell) series — sharing the real process of building SaaS projects from Playa del Carmen, México.*

*Repo: `zaerohell/content-automation` · 2026-11-15*

\#playadev #buildinpublic
Enter fullscreen mode Exit fullscreen mode

Top comments (0)