import { __name } from './chunk-IBAHCH6O.js';
import path from 'path';
import fs from 'fs';
import os from 'os';
import { execSync, execFileSync } from 'child_process';

var platform = os.platform();
var isWindows = platform.match(/^win/i);
function findVarlockBin(startDir) {
  const binNames = isWindows ? ["varlock.exe", "varlock.cmd"] : ["varlock"];
  let currentDir = startDir;
  while (currentDir) {
    const possibleBinPath = path.join(currentDir, "node_modules", ".bin");
    if (fs.existsSync(possibleBinPath)) {
      for (const binName of binNames) {
        const possibleVarlockPath = path.join(possibleBinPath, binName);
        if (fs.existsSync(possibleVarlockPath)) {
          return possibleVarlockPath;
        }
      }
    }
    const parentDir = path.dirname(currentDir);
    if (parentDir === currentDir) break;
    currentDir = parentDir;
  }
  return null;
}
__name(findVarlockBin, "findVarlockBin");
var VarlockExecError = class extends Error {
  constructor(message, stdout, stderr, exitCode) {
    super(message);
    this.stdout = stdout;
    this.stderr = stderr;
    this.exitCode = exitCode;
  }
  stdout;
  stderr;
  exitCode;
  static {
    __name(this, "VarlockExecError");
  }
};
function execSyncVarlock(command, opts) {
  try {
    try {
      const result = execSync(`varlock ${command}`, {
        ...opts?.env && { env: opts.env },
        ...opts?.cwd && { cwd: opts.cwd },
        stdio: "pipe"
      });
      return opts?.fullResult ? { stdout: result.toString(), stderr: "" } : result.toString();
    } catch (err) {
      if (!isWindows && err.status !== 127 && err.code !== "ENOENT") throw err;
    }
    const cwdStr = opts?.cwd ? String(opts.cwd) : void 0;
    const searchDirs = [
      ...cwdStr ? [cwdStr] : [],
      ...opts?.callerDir ? [opts.callerDir] : [],
      process.cwd()
    ];
    for (const startDir of searchDirs) {
      const varlockPath = findVarlockBin(startDir);
      if (varlockPath) {
        const needsShell = varlockPath.endsWith(".cmd");
        const result = execFileSync(varlockPath, command.split(" "), {
          ...opts,
          stdio: "pipe",
          ...needsShell && { shell: true }
        });
        return opts?.fullResult ? { stdout: result.toString(), stderr: "" } : result.toString();
      }
    }
    throw new Error("Unable to find varlock executable");
  } catch (err) {
    if (opts?.fullResult) {
      if (err instanceof VarlockExecError) throw err;
      const errAny2 = err;
      if (errAny2.status != null) {
        throw new VarlockExecError(
          `varlock ${command} failed (exit code ${errAny2.status})`,
          errAny2.stdout?.toString() ?? "",
          errAny2.stderr?.toString() ?? "",
          errAny2.status ?? 1
        );
      }
      throw err;
    }
    const errAny = err;
    if (opts?.showLogsOnError) {
      if (errAny.stdout) console.log(errAny.stdout.toString());
      if (errAny.stderr) console.error(errAny.stderr.toString());
      if (!errAny.stdout && !errAny.stderr) {
        console.error(errAny);
      }
    }
    if (opts?.exitOnError) {
      process.exit(err.status ?? 1);
    }
    throw err;
  }
}
__name(execSyncVarlock, "execSyncVarlock");

export { VarlockExecError, execSyncVarlock };
//# sourceMappingURL=chunk-BUYR7R2U.js.map
//# sourceMappingURL=chunk-BUYR7R2U.js.map