Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2aronS Duel Agents mergeEnvFile

From Leeroopedia
Knowledge Sources
Domains Configuration, Developer_Tools
Last Updated 2026-10-27 14:00 GMT

Overview

Concrete tool for idempotently merging environment variables into a `.env` file while preserving unrelated lines and warning on conflicting provider keys, provided by `@duel-agents/core`.

Description

The mergeEnvFile function reads an existing `.env` file (if present), drops any existing lines whose key is in the new set (removing stale duplicates), keeps all unrelated lines verbatim (including comments), trims trailing blank lines, appends the new `KEY=VALUE` lines, and writes the result back. Values containing special characters are quoted via the internal `quoteEnv` helper.

Before writing, it calls scanEnvConflicts to detect existing provider keys (`ANTHROPIC_API_KEY`, `OPENAI_API_KEY`) set to non-`duel_` values, returning an array of warning strings. The caller (e.g., `installForTarget`) prints these warnings so the user knows a previous provider credential is being replaced.

Helper functions:

  • parseEnvLine — parses a single `KEY=VALUE` line, stripping `export` prefix and surrounding quotes
  • scanEnvConflicts — scans existing content for conflicting provider keys
  • quoteEnv — quotes a value if it contains characters outside the safe set

Usage

Import this function when you need to:

  • Write Duel env vars into a shared `.env` file without destroying existing content
  • Detect and warn about conflicting provider credentials before overwriting
  • Achieve idempotent re-install (running twice produces the same file)

Code Reference

Source Location

  • Repository: Duel-Agents
  • File: packages/core/src/index.ts (lines 57-133)

Signature

const PROVIDER_KEYS = new Set(["ANTHROPIC_API_KEY", "OPENAI_API_KEY"]);

/** Parse a single env line; returns null for comments/blanks. */
function parseEnvLine(line: string): { key: string; value: string } | null;

/** Scan existing env content for conflicting provider keys. */
export function scanEnvConflicts(
  existing: string,
  vars: Record<string, string>,
): string[];

/** Merge vars into the .env file at filePath; returns warnings. */
export async function mergeEnvFile(
  filePath: string,
  vars: Record<string, string>,
): Promise<string[]>;

/** Quote a value if it contains special characters. */
function quoteEnv(value: string): string;

Import

import { mergeEnvFile, scanEnvConflicts } from "@duel-agents/core";

I/O Contract

Inputs

Name Type Required Description
filePath string Yes Path to the .env file
vars Record<string, string> Yes Env vars to merge in

Outputs

Name Type Description
returns Promise<string[]> Array of warning strings for conflicting provider keys
side effect filesystem write Updated .env file at filePath

Usage Examples

Merge Duel Vars into Claude .env

import { mergeEnvFile } from "@duel-agents/core";
import { homedir } from "node:os";
import { join } from "node:path";

const envPath = join(homedir(), ".claude", ".env");
const warnings = await mergeEnvFile(envPath, {
  ANTHROPIC_BASE_URL: "https://duelagents.com/v1",
  ANTHROPIC_API_KEY: "duel_abcd1234_...",
  DUEL_API_KEY: "duel_abcd1234_...",
});

for (const w of warnings) console.warn(`⚠ ${w}`);

Scan for Conflicts Before Merge

import { scanEnvConflicts } from "@duel-agents/core";
import { readFile } from "node:fs/promises";

const existing = await readFile(".env", "utf8").catch(() => "");
const conflicts = scanEnvConflicts(existing, { ANTHROPIC_API_KEY: "duel_..." });
// ["ANTHROPIC_API_KEY will be replaced with your Duel API key..."]

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment