// src/actions/package/compile/validateFullPackage.ts
import fs from "node:fs";
import path from "node:path";
import chalk2 from "chalk";
import ts, { formatDiagnosticsWithColorAndContext, sys } from "typescript";

// src/actions/package/compile/runTsc.ts
import { cwd } from "node:process";
import chalk from "chalk";

// src/lib/crossSpawn.ts
import crossSpawn from "cross-spawn";
function spawnSync(command, args, options) {
  return crossSpawn.sync(command, args, options);
}

// src/actions/package/compile/runTsc.ts
var runTsc = (configPath, verbose = false, extraArgs = []) => {
  const args = ["-p", configPath, ...extraArgs];
  if (verbose) {
    console.log(chalk.gray(`tsc ${args.join(" ")}`));
  }
  const result = spawnSync("tsc", args, {
    cwd: cwd(),
    stdio: "inherit"
  });
  return result.status ?? (result.error === void 0 ? 0 : 1);
};

// src/actions/package/compile/validateFullPackage.ts
var toPosixPath = (value) => value.split(path.sep).join("/");
var relativePath = (from, to) => {
  const result = toPosixPath(path.relative(from, to));
  return result.startsWith(".") ? result : `./${result}`;
};
var formatTsConfigDiagnostics = (diagnostics, pkg) => {
  return formatDiagnosticsWithColorAndContext(diagnostics, {
    getCanonicalFileName: (fileName) => fileName,
    getCurrentDirectory: () => pkg,
    getNewLine: () => sys.newLine
  });
};
var isInPackageSubdirectory = (pkg, fileName, directoryName) => {
  const relative = toPosixPath(path.relative(path.resolve(pkg, directoryName), fileName));
  return relative !== "" && !relative.startsWith("../") && relative !== "..";
};
var createFullValidationConfig = (pkg, tsconfigPath) => {
  const configFile = ts.readConfigFile(tsconfigPath, (fileName) => ts.sys.readFile(fileName));
  if (configFile.error) {
    return { configPath: tsconfigPath, diagnostics: [configFile.error] };
  }
  const parsed = ts.parseJsonConfigFileContent(
    configFile.config,
    ts.sys,
    pkg,
    void 0,
    tsconfigPath
  );
  const buildDir = path.resolve(pkg, "build");
  fs.mkdirSync(buildDir, { recursive: true });
  const configPath = path.resolve(buildDir, "tsconfig.package-validate.json");
  const files = parsed.fileNames.filter((fileName) => !isInPackageSubdirectory(pkg, fileName, "packages")).map((fileName) => relativePath(buildDir, fileName));
  if (files.length === 0) {
    const emptyFile = path.resolve(buildDir, "tsconfig.package-validate-empty.d.ts");
    fs.writeFileSync(emptyFile, "");
    files.push(relativePath(buildDir, emptyFile));
  }
  fs.writeFileSync(
    configPath,
    `${JSON.stringify({
      compilerOptions: {
        noEmit: true,
        rootDir: relativePath(buildDir, pkg)
      },
      extends: relativePath(buildDir, tsconfigPath),
      files,
      include: []
    }, null, 2)}
`
  );
  return { configPath, diagnostics: parsed.errors };
};
var validateFullPackage = (pkg, verbose = false) => {
  const tsconfigPath = path.resolve(pkg, "tsconfig.json");
  if (!fs.existsSync(tsconfigPath)) {
    console.warn(chalk2.yellow("Compile:Validate skipped \u2014 tsconfig.json not found"));
    return 0;
  }
  if (verbose) {
    console.log(chalk2.cyan("Compile:Validate [tsconfig.json]"));
  }
  const { configPath, diagnostics } = createFullValidationConfig(pkg, tsconfigPath);
  if (diagnostics.length > 0) {
    console.error(formatTsConfigDiagnostics(diagnostics, pkg));
    return 1;
  }
  const result = runTsc(configPath, verbose);
  if (result !== 0) {
    console.error(chalk2.red(`Compile:Validate failed (tsc exit code ${result})`));
  }
  return result;
};
export {
  validateFullPackage
};
//# sourceMappingURL=validateFullPackage.mjs.map
