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:20000419 Fauxnix translateCommandList

From Leeroopedia
Knowledge Sources
Domains Translation, Code_Generation, PowerShell
Last Updated 2026-10-26 00:00 GMT

Overview

Concrete transpiler that converts a parsed bash CommandList AST into an array of SegmentPlan objects, each carrying a wrapped PowerShell script plus redirect metadata, in one of two translation modes.

Description

The translateCommandList function is the heart of fauxnix. It walks each ListSegment of the parsed AST, dispatches each pipeline command through the command registry (lookup(name) → generator), and emits PowerShell statements following the "Fauxnix contract": string-per-line stdout, [Console]::Error.WriteLine for stderr, $script:fx_exit for exit codes, and $input for stdin. Two translation modes control whether the output is prepared for real execution (EXECUTE_TRANSLATION) or side-effect-free inspection (PURE_TRANSLATION). Each segment is wrapped by wrapScript into a self-contained PowerShell script. The function also collects redirect metadata (redirects, outputRedirects, stdinRedirects) so the executor can prepare file descriptors in source order.

Usage

Import this function after parsing a bash command. Use EXECUTE_TRANSLATION when you intend to run the result via FauxnixSession.run, and PURE_TRANSLATION for preflight validation (batch compilation) or the translate CLI verb (show-only). Used by runCli, the MCP single-call and batch handlers, the facade runScript, and the differential runner.

Code Reference

Source Location

  • Repository: fauxnix
  • File: src/translator.ts
  • Lines: L1466-1505 (translateCommandList); delegates to translatePipelineBody (L1361+), wrapScript (L1665+); SegmentPlan interface at L1452-1464

Signature

export interface SegmentPlan {
  op: ';' | '&&' | '||';
  script: string;       // wrapScript output for one-shot spawn mode
  body: string;         // pipeline body before wrapScript (host mode re-wraps)
  redirects: Redirect[];
  outputRedirects: Redirect[];
  stdinRedirects: Redirect[];
}

export function translateCommandList(
  list: CommandList,
  translation: TranslationContext = EXECUTE_TRANSLATION,
): SegmentPlan[]

Import

import {
  EXECUTE_TRANSLATION,
  PURE_TRANSLATION,
  translateCommandList,
} from './translator.js';

I/O Contract

Inputs

Name Type Required Description
list CommandList Yes Parsed AST from parseCommand
translation TranslationContext No 'pure' }; defaults to EXECUTE_TRANSLATION

Outputs

Name Type Description
(return) SegmentPlan[] One plan per list segment, each with a wrapped PowerShell script and redirect metadata

Usage Examples

Execute Mode (Default)

import { parseCommand } from './parser.js';
import { translateCommandList, EXECUTE_TRANSLATION } from './translator.js';
import { FauxnixSession } from './executor.js';

const list = parseCommand('ls -la | head -5');
const plans = translateCommandList(list, EXECUTE_TRANSLATION);
const session = new FauxnixSession();
const result = await session.run(plans);

Pure Mode (Preflight / Show Only)

import { translateCommandList, PURE_TRANSLATION } from './translator.js';

const plans = translateCommandList(parseCommand('grep foo *.txt'), PURE_TRANSLATION);
console.log(plans.map(p => p.script).join('\n# ---- next segment ----\n'));

Related Pages

Implements Principle

Requires Environment

Page Connections

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