DEV Community

Roberto Luna
Roberto Luna

Posted on

Automating Multi‑Platform Content Publishing with a Simple Node CLI

Automating Multi‑Platform Content Publishing with a Simple Node CLI

TL;DR: I added a tiny Node CLI that walks the content/ tree, flips the *_generated flags in metadata.json, and spits out ready‑to‑post JSON/Markdown files for Medium, Substack and Bluesky. The change removes manual copy‑paste, keeps platform payloads in sync, and makes the daily “post‑everything” step a one‑liner.


The Problem

Our content-automation repo stores a weekly batch of articles in a date‑based hierarchy (e.g., content/2026/12/01/ai-business-academy/). Each article lives as a set of Markdown files (medium_en.md, substack_es.md, etc.) plus a metadata.json that tracks whether we’ve already published to each platform:

{
  "title": "Scaling the VIP Labs for a Real‑World Classroom",
  "medium_generated": false,
  "substack_generated": false,
  "bluesky_generated": false,
  ...
}
Enter fullscreen mode Exit fullscreen mode

Before the automation, the workflow was:

  1. Open each Markdown file, copy the body into the platform UI.
  2. Manually edit metadata.json to true after a successful post.
  3. Run a handful of ad‑hoc scripts to generate a changelog.

The process was error‑prone: a missed flag caused duplicate posts, and copying large Markdown blocks into the Bluesky JSON payload introduced subtle formatting bugs (e.g., stray \r characters). The biggest pain point was the lack of a single source of truth for “what has been published”.

What I Tried First

My first attempt was to write a Bash loop that grepped for *_generated": false across all metadata.json files and then opened each corresponding Markdown file in my editor. The script looked something like:

#!/usr/bin/env bash
find content -name metadata.json | while read meta; do
  if grep -q '"medium_generated": false' "$meta"; then
    md=$(dirname "$meta")/medium_en.md
    code "$md"
  fi
done
Enter fullscreen mode Exit fullscreen mode

What went wrong?

  • The loop only handled Medium; adding Substack or Bluesky required duplicating the logic.
  • Bash string handling choked on spaces in file names (our folder names contain hyphens and dates).
  • Most importantly, the script never updated the flags, so I still had to edit JSON by hand after each post. The result was a half‑automated, half‑manual process that didn’t solve the core problem.

The Implementation

I rewrote the whole thing as a Node CLI (scripts/publish.js). Node gives us native JSON parsing, path handling via path and fs.promises, and a clean way to emit platform‑specific payloads. Below is the core of the new implementation.

1. Project layout

content-automation/
├─ scripts/
│  └─ publish.js          ← new CLI
├─ content/
│  └─ 2026/
│     └─ 12/
│        └─ 01/
│           ├─ ai-business-academy/
│           │   ├─ changelog.md
│           │   ├─ medium_en.md
│           │   ├─ substack_es.md
│           │   ├─ bluesky_en.json
│           │   └─ metadata.json
│           └─ content-automation/
│               └─ …
└─ package.json
Enter fullscreen mode Exit fullscreen mode

2. package.json script entry

{
  "name": "content-automation",
  "version": "0.4.0",
  "scripts": {
    "publish": "node scripts/publish.js"
  },
  "dependencies": {
    "chalk": "^5.3.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Running npm run publish now triggers the full pipeline.

3. The CLI (scripts/publish.js)

#!/usr/bin/env node
// scripts/publish.js
import { promises as fs } from 'fs';
import path from 'path';
import chalk from 'chalk';

const ROOT = path.resolve(import.meta.url, '../../content');

async function* walk(dir) {
  const entries = await fs.readdir(dir, { withFileTypes: true });
  for (const entry of entries) {
    const res = path.resolve(dir, entry.name);
    if (entry.isDirectory()) yield* walk(res);
    else if (entry.name === 'metadata.json') yield res;
  }
}

/**
 * Load a JSON file, mutate the generated flags, and write it back.
 */
async function updateMetadata(metaPath, updates) {
  const raw = await fs.readFile(metaPath, 'utf8');
  const meta = JSON.parse(raw);
  Object.assign(meta, updates);
  await fs.writeFile(metaPath, JSON.stringify(meta, null, 2));
}

/**
 * Generate a platform payload (markdown or JSON) and write it to the
 * `dist/` folder next to the source file.
 */
async function generatePayload(baseDir, platform, sourceFile) {
  const srcPath = path.join(baseDir, sourceFile);
  const content = await fs.readFile(srcPath, 'utf8');
  const outDir = path.join(baseDir, 'dist');
  await fs.mkdir(outDir, { recursive: true });

  const outPath = path.join(outDir, `${platform}.${sourceFile.split('.').pop()}`);
  await fs.writeFile(outPath, content);
  console.log(chalk.green(`✔ ${platform} payload written → ${outPath}`));
}

/**
 * Main driver – walks every metadata.json, checks flags, and emits payloads.
 */
async function main() {
  for await (const metaPath of walk(ROOT)) {
    const baseDir = path.dirname(metaPath);
    const meta = JSON.parse(await fs.readFile(metaPath, 'utf8'));

    // Medium
    if (!meta.medium_generated && await fileExists(baseDir, 'medium_en.md')) {
      await generatePayload(baseDir, 'medium', 'medium_en.md');
      await updateMetadata(metaPath, { medium_generated: true });
    }

    // Substack
    if (!meta.substack_generated && await fileExists(baseDir, 'substack_es.md')) {
      await generatePayload(baseDir, 'substack', 'substack_es.md');
      await updateMetadata(metaPath, { substack_generated: true });
    }

    // Bluesky (JSON payload)
    if (!meta.bluesky_generated && await fileExists(baseDir, 'bluesky_en.json')) {
      await generatePayload(baseDir, 'bluesky', 'bluesky_en.json');
      await updateMetadata(metaPath, { bluesky_generated: true });
    }
  }
}

/**
 * Helper – checks if a file exists (non‑throwing).
 */
async function fileExists(dir, filename) {
  try {
    await fs.access(path.join(dir, filename));
    return true;
  } catch {
    return false;
  }
}

main().catch(err => {
  console.error(chalk.red('❌ Publishing failed:'), err);
  process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode

What the diff added

  • scripts/publish.js – new file (≈ 120 lines).
  • package.json – added "publish" script and chalk dependency.
  • content/2026/12/01/ai-business-academy/metadata.json – the flags medium_generated and substack_generated were flipped to true by the CLI (see diff showing "medium_generated": true).
  • content/2026/12/01/ai-business-academy/bluesky_en.json – a new JSON payload that the CLI now copies to dist/bluesky.json (the

Part of my Build in Public series — sharing the real process of building SaaS projects from Playa del Carmen, México.

Repo: zaerohell/content-automation · 2026-12-02

#playadev #buildinpublic

Top comments (0)