'use strict';

var JBSG7YXG_cjs = require('./JBSG7YXG.cjs');
var promises = require('fs/promises');
var path = require('path');
var toml = require('toml');
var vmTooling = require('@layerzerolabs/vm-tooling');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var path__default = /*#__PURE__*/_interopDefault(path);

// src/commands/sync-toolchain.ts
JBSG7YXG_cjs.init_cjs_shims();
var syncedCwds = /* @__PURE__ */ new Set();
async function syncToolchain(context, options) {
  if (syncedCwds.has(options.cwd)) {
    return;
  }
  const filePath = path__default.default.join(options.cwd, "rust-toolchain.toml");
  let installCmd;
  const hasToolchainFile = await promises.access(filePath).then(() => true, () => false);
  if (hasToolchainFile) {
    const parsed = toml.parse(await promises.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 vmTooling.executeToolCommand(context, "stellar", [], {
    ...options,
    script,
    volumes: [
      ...options.volumes,
      {
        type: "isolate",
        containerPath: "/cache/rustup",
        name: "stellar-rustup",
        shared: true,
        locked: true
      }
    ]
  });
}
JBSG7YXG_cjs.__name(syncToolchain, "syncToolchain");

exports.syncToolchain = syncToolchain;
//# sourceMappingURL=MDHNNMKJ.cjs.map
//# sourceMappingURL=MDHNNMKJ.cjs.map