DEV Community

CBT Tools
CBT Tools

Posted on

I Built a CLI Tool for Mental Health Check-ins in 50 Lines of Node.js

I track my anxious thoughts in a text file. Not a fancy app, not a cloud service — just thoughts.txt in my home directory. Every night I dump whatever is spinning in my head.

The problem: I write them down but I never analyze them. The file grows. The patterns stay invisible.

So I built a 50-line Node.js CLI that reads my thought journal and flags cognitive distortions — the mental shortcuts that fuel anxiety (all-or-nothing thinking, catastrophizing, mind-reading, etc.). No dependencies. No API. No cloud. Just node analyze.js thoughts.txt.

The 50-Line CLI

#!/usr/bin/env node
// analyze.js — CBT cognitive distortion detector, zero dependencies
const fs = require('fs');

const DISTORTIONS = [
  { name: 'All-or-Nothing',      regex: /\b(always|never|everyone|no one|nothing|everything|completely|total)\b/gi, weight: 1 },
  { name: 'Catastrophizing',     regex: /\b(ruin|disaster|end of|over|can't survive|won't survive|terrible|awful)\b/gi, weight: 1 },
  { name: 'Mind-Reading',        regex: /\b(they think|they'll think|he thinks|she thinks|people will think)\b/gi, weight: 1 },
  { name: 'Should Statements',   regex: /\b(should|must|have to|ought to)\b/gi, weight: 0.5 },
  { name: 'Labeling',            regex: /\b(I'm a |I am a |he's a |she's a )(failure|loser|idiot|fraud|burden)\b/gi, weight: 1 },
  { name: 'Personalization',     regex: /\b(my fault|I caused|because of me|I ruined)\b/gi, weight: 1 },
  { name: 'Emotional Reasoning', regex: /\b(I feel|it feels)(.*)(so I am|so I must|so it must|therefore)\b/gi, weight: 1 },
  { name: 'Fortune-Telling',     regex: /\b(it will|it's going to|I'll fail|I'll lose|things will)\b/gi, weight: 0.5 },
];

const file = process.argv[2];
if (!file) { console.error('Usage: node analyze.js <thoughts.txt>'); process.exit(1); }

const text = fs.readFileSync(file, 'utf-8');
const entries = text.split(/\n\n+/).filter(e => e.trim());
let totalScore = 0, distortionsFound = 0;

console.log('=== CBT Thought Analysis ===');
console.log(`Analyzed ${entries.length} entries, ${text.length} chars\n`);

for (const [i, entry] of entries.entries()) {
  const found = [];
  for (const d of DISTORTIONS) {
    const matches = entry.match(d.regex);
    if (matches) found.push({ name: d.name, count: matches.length, weight: d.weight * matches.length });
  }
  if (found.length) {
    const score = found.reduce((s, f) => s + f.weight, 0);
    totalScore += score; distortionsFound += found.length;
    console.log(`[Entry ${i+1}] score=${score.toFixed(1)}${found.map(f => f.name).join(', ')}`);
    console.log(`  "${entry.slice(0,80)}..."\n`);
  }
}

console.log('=== Summary ===');
console.log(`Total distortions: ${distortionsFound}`);
console.log(`Risk score: ${totalScore.toFixed(1)} (${totalScore > 10 ? 'HIGH' : totalScore > 5 ? 'MODERATE' : 'LOW'})`);
console.log(`\nTop pattern: ${DISTORTIONS[0].name}`);
console.log('\n💡 Try reframing: replace "always/never" with "sometimes", "disaster" with "setback".');
Enter fullscreen mode Exit fullscreen mode

How It Works

  1. Read a plain-text thought journal (entries separated by blank lines)
  2. Match each entry against 8 cognitive distortion patterns (regex, weighted)
  3. Score the entry (all-or-nothing = 1.0, should-statements = 0.5)
  4. Report which distortions appear, a risk score, and a reframing prompt

Why a CLI?

I tried 6 mental health apps. They all had the same problem: they wanted my data in their cloud. My anxious thoughts are the most private thing I own. I'm not putting them on someone's server.

A CLI runs locally. The file never leaves my machine. Zero dependencies means zero supply-chain risk. 50 lines means I can read and audit every line.

Sample Output

$ node analyze.js thoughts.txt
=== CBT Thought Analysis ===
Analyzed 12 entries, 2847 chars

[Entry 3] score=2.0 — All-or-Nothing, Catastrophizing
  "I always mess everything up. This presentation will be a total disaster..."

[Entry 7] score=1.5 — Should Statements, Mind-Reading
  "I should have prepared more. They'll think I'm incompetent..."

=== Summary ===
Total distortions: 5
Risk score: 7.5 (MODERATE)

💡 Try reframing: replace "always/never" with "sometimes", "disaster" with "setback".
Enter fullscreen mode Exit fullscreen mode

The Full Toolkit

This CLI is one of 36 free mental health tools I built — all vanilla JS, all zero-dependency, all privacy-first. The full set covers:

  • Cognitive distortion detection (this CLI)
  • Core belief identification
  • Safety behavior mapping
  • Catastrophe prediction calibration
  • CBT thought records (Notion template)
  • Procrastination pattern analysis
  • Attachment style assessment

Free on GitHub: alexcoledev/cbt-toolkit — 157 cloners, 445 clones, zero telemetry.

Complete bundle ($9.99): All 36 tools + 7-day email course + Notion templates — Get it on Gumroad.

Why This Matters

CBT (Cognitive Behavioral Therapy) is the most evidence-backed treatment for anxiety and depression. The core insight: your thoughts are not facts. A distortion detector doesn't fix your thoughts — it makes them visible. Once you see "I always mess everything up" as an all-or-nothing distortion (not reality), you can question it.

That's what this CLI does. 50 lines. No cloud. No subscription. Just clarity.


What CLI tools do you use for self-reflection? I'm curious what else fits the "local, private, zero-dep" philosophy.

Top comments (0)