email-content-compose/scripts/fetch.ts

52 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2026-03-11 23:36:44 +00:00
import { existsSync, readFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { fetchLeads } from "../src/index.js";
function loadEnvLocal(): void {
const scriptDir = dirname(fileURLToPath(import.meta.url));
const envPath = join(scriptDir, "../.env.local");
if (!existsSync(envPath)) return;
for (const line of readFileSync(envPath, "utf-8").split("\n")) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) continue;
const eq = trimmed.indexOf("=");
if (eq <= 0) continue;
const key = trimmed.slice(0, eq).trim();
let val = trimmed.slice(eq + 1).trim();
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
val = val.slice(1, -1);
}
if (!process.env[key]) process.env[key] = val;
}
}
function parseArgs(args: string[]): { workflowId: string; dryRun: boolean } {
let dryRun = false;
let workflowId = "";
for (const arg of args) {
if (arg === "--dry-run") { dryRun = true; continue; }
if (arg === "-h" || arg === "--help") {
console.error("Usage: bun run fetch -- --workflow-id=<outreach-xxx> [--dry-run]");
process.exit(0);
}
if (arg.startsWith("--workflow-id=")) { workflowId = arg.slice("--workflow-id=".length).trim(); continue; }
if (!workflowId) workflowId = arg;
}
return { workflowId, dryRun };
}
async function main(): Promise<void> {
loadEnvLocal();
const args = parseArgs(process.argv.slice(2));
const result = await fetchLeads(args.workflowId, args.dryRun);
console.log(JSON.stringify(result, null, 2));
if (result.status === "failed") process.exit(1);
}
main().catch((err) => {
console.error(JSON.stringify({ status: "failed", error: err instanceof Error ? err.message : String(err) }));
process.exit(1);
});