import process from 'node:process';globalThis._importMeta_=globalThis._importMeta_||{url:"file:///_entry.js",env:process.env};import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { createJiti } from 'jiti';
import { findUpSync } from 'find-up';
import { existsSync } from 'node:fs';
import treeKill from 'tree-kill';
import { spawn as spawn$1 } from 'node:child_process';
import { spawn } from '@lydell/node-pty';
import { existsSync as existsSync$1 } from 'fs';
import { resolve as resolve$1, join } from 'path';
import { createRequire } from 'node:module';

const getVaxlaConfig = async (configPath) => {
  const contextPath = findUpSync("package.json", { cwd: configPath });
  if (!contextPath)
    throw new Error(`Could not find closest package.json to ${configPath}.`);
  const jiti = createJiti(pathToFileURL(resolve(contextPath, "..")).href, {
    debug: true
  });
  let configFile = resolve(configPath, "config.json");
  if (!existsSync(configFile)) configFile = resolve(configPath, "config.ts");
  const configurationFileUrl = pathToFileURL(configFile).toString();
  const config = await jiti.import(configurationFileUrl);
  return config.default;
};

const crossPlatformSpawn = (command, options, meta = {}) => {
  let file = process.platform === "win32" ? "cmd.exe" : "/bin/sh";
  let args = process.platform === "win32" ? ["/s", "/c", '"' + command + '"'] : ["-c", command];
  if (process.platform === "win32") {
    options = Object.assign({}, options);
  }
  if (meta.interactiveTerminal) {
    return spawn(command, args, {
      cwd: options.cwd,
      env: {
        ...process.env,
        FORCE_COLOR: "1"
      }
    });
  } else {
    return spawn$1(file, args, options);
  }
};
const spawnWrapper = (command, options) => {
  return crossPlatformSpawn(command, {
    cwd: options?.cwd,
    env: {
      ...process.env,
      FORCE_COLOR: "1"
    }
  }, {
    interactiveTerminal: options?.interactiveTerminal
  });
};
const execCommand = async (command, options) => {
  const childProcess = spawnWrapper(command, {
    cwd: options?.cwd,
    signal: options?.abortController?.signal,
    interactiveTerminal: options?.interactiveTerminal
  });
  options?.onChildProcessInit?.(childProcess);
  process.on("SIGINT", () => treeKill(childProcess.pid));
  process.on("SIGTERM", () => treeKill(childProcess.pid));
  process.on("exit", () => treeKill(childProcess.pid));
  if ("onData" in childProcess) {
    childProcess.onData((data) => {
      if (options?.onLog) options.onLog(data);
    });
    childProcess.onExit((data) => {
      if (options?.onClose) options.onClose(data.exitCode);
    });
  } else {
    childProcess.stdout.on("data", async (data = "") => {
      if (options?.onLog) options.onLog(data);
    });
    childProcess.stderr.on("data", (data = "") => {
      if (options?.onError) options.onError(data);
    });
    childProcess.on("exit", (data) => {
      if (options?.onClose) options.onClose(data.exitCode);
    });
  }
};
const execCommandPromise = (command, options) => {
  return new Promise((resolve) => {
    execCommand(command, {
      ...options ?? {},
      onClose: (data) => {
        resolve(data);
        options?.onClose?.(data);
      }
    });
  });
};

createRequire(process.argv[1] ? new URL(process.argv[1], "file://").href : globalThis._importMeta_.url);
const getUserRootDirectory = () => {
  const configDir = process.env.VAXLA_CONFIG ?? process.cwd();
  let currentDir = configDir;
  while (currentDir !== "/") {
    const parentDir = resolve$1(currentDir, "..");
    const packageJsonPath = join(parentDir, "package.json");
    if (existsSync$1(packageJsonPath)) {
      return parentDir;
    }
    currentDir = parentDir;
  }
  return configDir;
};

export { getUserRootDirectory as a, execCommandPromise as e, getVaxlaConfig as g };
//# sourceMappingURL=index.mjs.map
