GC-301g · Module 3
Node.js Wrappers & Cron Scheduling
3 min read
Node.js wrappers around Gemini CLI use child_process.execFile() or execa for subprocess management, with the same pattern as Python — execute, capture, parse, route. The advantage of Node.js wrappers is ecosystem alignment: if your project is already JavaScript/TypeScript, your automation scripts share the same language, package manager, and CI configuration. You can import your project's types, validators, and utility functions directly into the automation layer.
Cron scheduling transforms one-off automation scripts into recurring workflows. A cron job that runs gemini -p every morning to analyze overnight commits, every hour to check for security advisories in dependencies, or every deploy to generate updated documentation — these are the building blocks of an AI-augmented operations workflow. The critical rule: cron scripts must be idempotent and self-healing. If the last run failed, the next run should detect incomplete state and recover, not duplicate or corrupt data.
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { writeFile, readFile } from "node:fs/promises";
const exec = promisify(execFile);
async function geminiAnalyze(prompt: string): Promise<Record<string, unknown> | null> {
try {
const { stdout } = await exec("gemini", [
"-p", prompt, "--output-format", "json"
], { timeout: 60_000 });
return JSON.parse(stdout);
} catch (err) {
console.error("Gemini failed:", (err as Error).message);
return null;
}
}
// Daily report generator (called from cron)
async function dailyReport() {
const changes = await exec("git", ["log", "--since=24 hours ago", "--oneline"]);
const analysis = await geminiAnalyze(
`Summarize these commits for a daily standup:\n${changes.stdout}`
);
if (analysis) {
const date = new Date().toISOString().slice(0, 10);
await writeFile(`reports/daily-${date}.json`, JSON.stringify(analysis, null, 2));
console.log(`Report generated: reports/daily-${date}.json`);
}
}