import { init_esm_shims, __name } from './PHXMTVK6.js';
import { access, readFile } from 'fs/promises';
import path from 'path';
import { parse } from 'toml';
import { executeToolCommand } from '@layerzerolabs/vm-tooling';

// src/commands/sync-toolchain.ts
init_esm_shims();
var syncedCwds = /* @__PURE__ */ new Set();
async function syncToolchain(context, options) {
  if (syncedCwds.has(options.cwd)) {
    return;
  }
  const filePath = path.join(options.cwd, "rust-toolchain.toml");
  let installCmd;
  const hasToolchainFile = await access(filePath).then(() => true, () => false);
  if (hasToolchainFile) {
    const parsed = parse(await readFile(filePath, "utf-8"));
    const channel = parsed.toolchain?.channel;
    if (typeof channel !== "string") {
      throw new Error(`Missing 'toolchain.channel' in ${filePath}`);
    }
    const targets = parsed.toolchain?.targets ?? [];
    const components = parsed.toolchain?.components ?? [];
    const installArgs = [
      `rustup toolchain install ${channel} --no-self-update`
    ];
    for (const target of targets) {
      installArgs.push(`--target ${target}`);
    }
    for (const component of components) {
      installArgs.push(`--component ${component}`);
    }
    installCmd = installArgs.join(" ");
  } else {
    installCmd = "rustup default stable";
  }
  const script = [
    "mkdir -p $CARGO_HOME/bin",
    "ln -sf /usr/local/cargo/bin/rustup $CARGO_HOME/bin/rustup",
    installCmd
  ].join(" && ");
  console.info(`\u{1F527} Syncing Rust toolchain: ${installCmd}`);
  syncedCwds.add(options.cwd);
  await executeToolCommand(context, "stellar", [], {
    ...options,
    script,
    volumes: [
      ...options.volumes,
      {
        type: "isolate",
        containerPath: "/cache/rustup",
        name: "stellar-rustup",
        shared: true,
        locked: true
      }
    ]
  });
}
__name(syncToolchain, "syncToolchain");

export { syncToolchain };
//# sourceMappingURL=ALSWLO4X.js.map
//# sourceMappingURL=ALSWLO4X.js.map