import { CliExitError } from './chunk-7M6WZ65V.js';
import { detectWorkspaceInfo, ansis_default } from './chunk-LTYLEVHC.js';
import { __name } from './chunk-IBAHCH6O.js';
import path from 'path';
import fs, { existsSync } from 'fs';
import { execSync } from 'child_process';

function detectJsPackageManager(opts) {
  const info = detectWorkspaceInfo({ cwd: opts?.cwd });
  if (!info) {
    if (opts?.exitIfNotFound) {
      throw new CliExitError("Unable to detect your JavaScript package manager!", {
        suggestion: "We look for lock files (ex: package-lock.json) so you may just need to run a dependency install (ie `npm install`)",
        forceExit: true
      });
    }
    return void 0;
  }
  return info.packageManager;
}
__name(detectJsPackageManager, "detectJsPackageManager");
function installJsDependency(opts) {
  const packageJsonPath = path.join(opts.packagePath || process.cwd(), "package.json");
  if (!existsSync(packageJsonPath)) return false;
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
  if (packageJson.dependencies?.varlock) return false;
  execSync([
    // move to the correct directory if needed
    opts.packagePath && `cd ${opts.packagePath} &&`,
    // `add` works in all of them
    `${opts.packageManager} add ${opts.packageName}`,
    // tells pnpm to either install in the workspace root explicitly
    // or to not check if we are the in the root
    opts.packageManager === "pnpm" && (opts.isMonoRepoRoot ? "-w" : "--ignore-workspace-root-check")
  ].filter(Boolean).join(" "));
  return true;
}
__name(installJsDependency, "installJsDependency");

// src/cli/helpers/pretty-format.ts
var fmt = {
  decorator: /* @__PURE__ */ __name((s) => ansis_default.magenta(s), "decorator"),
  filePath: /* @__PURE__ */ __name((s) => `\u{1F4C2} ${ansis_default.cyan.italic(s)}`, "filePath"),
  fileName: /* @__PURE__ */ __name((s) => `${ansis_default.cyan.italic(s)}`, "fileName"),
  command: /* @__PURE__ */ __name((s, opts) => {
    let jsPackageManager;
    if (opts?.jsPackageManager === true) {
      jsPackageManager = detectJsPackageManager();
    } else if (opts?.jsPackageManager) {
      jsPackageManager = opts.jsPackageManager;
    }
    if (jsPackageManager) {
      s = `${jsPackageManager.exec} ${s}`;
    }
    return ansis_default.green.italic(s);
  }, "command"),
  packageName: /* @__PURE__ */ __name((s) => ansis_default.green.italic(s), "packageName")
};
var logLines = /* @__PURE__ */ __name((lines) => {
  for (const line of lines) {
    if (!line && line !== "") continue;
    console.log(line);
  }
}, "logLines");

export { detectJsPackageManager, fmt, installJsDependency, logLines };
//# sourceMappingURL=chunk-NEWKASA7.js.map
//# sourceMappingURL=chunk-NEWKASA7.js.map