// src/actions/package/clean-outputs.ts
import path3 from "node:path";
import chalk2 from "chalk";

// src/lib/copyAssetFiles.ts
import { copyFileSync, mkdirSync } from "node:fs";
import path from "node:path/posix";
import { globSync } from "glob";
var ASSET_PATTERNS = [
  "**/*.jpg",
  "**/*.png",
  "**/*.gif",
  "**/*.svg",
  "**/*.webp",
  "**/*.sass",
  "**/*.scss",
  "**/*.css"
];
function copyAssetFiles(srcDir, destDir) {
  const matches = globSync(ASSET_PATTERNS, { cwd: srcDir, nodir: true });
  const copied = [];
  for (const relativePath4 of matches) {
    const from = path.join(srcDir, relativePath4);
    const to = path.join(destDir, relativePath4);
    mkdirSync(path.dirname(to), { recursive: true });
    copyFileSync(from, to);
    copied.push(to);
  }
  return copied;
}

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

// src/lib/deepMerge.ts
var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
function isPlainObject(value) {
  if (typeof value !== "object" || value === null) return false;
  const proto = Object.getPrototypeOf(value);
  return proto === Object.prototype || proto === null;
}
function isMergeable(value) {
  return Array.isArray(value) || isPlainObject(value);
}
function cloneValue(value) {
  if (Array.isArray(value)) {
    return value.map((item) => cloneValue(item));
  }
  if (isPlainObject(value)) {
    const result = {};
    for (const [key, item] of Object.entries(value)) {
      if (UNSAFE_KEYS.has(key)) continue;
      result[key] = cloneValue(item);
    }
    return result;
  }
  return value;
}
function mergeObject(target, source, options) {
  const destination = {};
  if (isPlainObject(target)) {
    for (const [key, item] of Object.entries(target)) {
      if (UNSAFE_KEYS.has(key)) continue;
      destination[key] = cloneValue(item);
    }
  }
  for (const [key, sourceValue] of Object.entries(source)) {
    if (UNSAFE_KEYS.has(key)) continue;
    if (Object.hasOwn(target, key) && isMergeable(sourceValue)) {
      const customMerge = options.customMerge?.(key);
      destination[key] = (customMerge ?? deepMerge)(target[key], sourceValue, options);
    } else {
      destination[key] = cloneValue(sourceValue);
    }
  }
  return destination;
}
function deepMerge(target, source, options = {}) {
  const sourceIsArray = Array.isArray(source);
  const targetIsArray = Array.isArray(target);
  if (sourceIsArray !== targetIsArray) {
    return cloneValue(source);
  }
  if (sourceIsArray) {
    return [...target, ...source].map((item) => cloneValue(item));
  }
  return mergeObject(target, source, options);
}

// src/lib/deleteGlob.ts
import fs from "node:fs";
import { glob } from "glob";
var deleteGlob = (globPath) => {
  const files = glob.sync(globPath);
  for (const file of files) {
    fs.rmSync(file, { recursive: true, force: true });
  }
};

// src/lib/initCwd.ts
function INIT_CWD() {
  return process.env.INIT_CWD ?? process.cwd();
}

// src/lib/loadConfig.ts
import chalk from "chalk";
import { cosmiconfig } from "cosmiconfig";
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
var config;
var rootConfigPath;
function createExplorer() {
  return cosmiconfig("xy", { cache: true, loaders: { ".ts": TypeScriptLoader() } });
}
var loadConfig = async (params) => {
  if (config === void 0) {
    const cosmicConfigResult = await createExplorer().search();
    config = cosmicConfigResult?.config ?? {};
    rootConfigPath = cosmicConfigResult?.filepath;
    const configFilePath = cosmicConfigResult?.filepath;
    if (configFilePath !== void 0) {
      console.log(chalk.green(`Loaded config from ${configFilePath}`));
      if (Boolean(config.verbose)) {
        console.log(chalk.gray(JSON.stringify(config, null, 2)));
      }
    }
  }
  return deepMerge(config, params ?? {});
};

// src/lib/match.ts
import { minimatch } from "minimatch";
function makeMatcher(pattern, options = {}) {
  const patterns = Array.isArray(pattern) ? pattern : [pattern];
  return (input) => patterns.some((p) => minimatch(input, p, options));
}

// src/lib/packageName.ts
import { readFileSync } from "node:fs";
import path2 from "node:path";
function packageName() {
  if (process.env.npm_package_name != null && process.env.npm_package_name.length > 0) {
    return process.env.npm_package_name;
  }
  try {
    const pkgPath = path2.join(INIT_CWD(), "package.json");
    const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
    return pkg.name;
  } catch {
    return void 0;
  }
}

// src/lib/reporting/xyReporter.ts
var strictMode = false;
function isStrictMode() {
  return strictMode || process.env.XY_STRICT === "1";
}

// src/actions/package/clean-outputs.ts
var packageCleanOutputs = () => {
  const pkg = INIT_CWD();
  const pkgName = packageName();
  const folders = [path3.join(pkg, "dist"), path3.join(pkg, "build"), path3.join(pkg, "docs")];
  console.log(chalk2.green(`Cleaning Outputs [${pkgName}]`));
  for (const folder of folders) {
    deleteGlob(folder);
  }
  return 0;
};

// src/actions/package/clean-typescript.ts
import path4 from "node:path";
import chalk3 from "chalk";
var packageCleanTypescript = () => {
  const pkg = INIT_CWD();
  const pkgName = packageName();
  console.log(chalk3.green(`Cleaning Typescript [${pkgName}]`));
  const files = [path4.join(pkg, "*.tsbuildinfo"), path4.join(pkg, ".tsconfig.*"), path4.join(pkg, ".eslintcache")];
  for (const file of files) {
    deleteGlob(file);
  }
  return 0;
};

// src/actions/package/clean.ts
var packageClean = () => {
  return [packageCleanOutputs(), packageCleanTypescript()].reduce((prev, value) => prev + value, 0);
};

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

// src/actions/package/compile/packageCompileEsbuild.ts
import fs3 from "node:fs";
import path6 from "node:path";
import { cwd as cwd3 } from "node:process";
import chalk8 from "chalk";
import { build } from "esbuild";

// src/actions/package/compile/inputs.ts
import chalk4 from "chalk";
import { glob as glob2 } from "glob";
var getAllInputs = (srcDir, verbose = false) => {
  return glob2.sync(`${srcDir}/**/*.{ts,tsx,mts,cts}`, { posix: true }).map((file) => {
    const result = file.slice(Math.max(0, srcDir.length + 1));
    if (verbose) {
      console.log(chalk4.gray(`getAllInputs: ${JSON.stringify(result, null, 2)}`));
    }
    return result;
  });
};

// src/actions/package/compile/buildEntries.ts
var bareSpecOrStoryRe = /(?:^|\/)(?:spec|stories)\.(?:ts|tsx|mts|cts)$/;
var isSpecOrStory = (entry) => {
  return entry.includes(".spec.") || entry.includes(".stories.") || entry.startsWith("spec/") || entry.includes("/spec/") || entry.startsWith("stories/") || entry.includes("/stories/") || bareSpecOrStoryRe.test(entry);
};
var isDeclaration = (entry) => /\.d\.(?:ts|mts|cts)$/.test(entry);
var buildEntries = (srcDir, entryMode = "single", options, excludeSpecAndStories = true, verbose = false) => {
  let entries;
  switch (entryMode) {
    case "platform": {
      entries = ["index-node.ts", "index-browser.ts"];
      break;
    }
    case "all": {
      entries = (excludeSpecAndStories ? getAllInputs(srcDir).filter((entry) => !isSpecOrStory(entry)) : getAllInputs(srcDir)).filter((entry) => !isDeclaration(entry));
      break;
    }
    case "custom": {
      entries = [];
      break;
    }
    default: {
      entries = ["index.ts"];
      break;
    }
  }
  if (typeof options !== "boolean" && Array.isArray(options?.entry)) {
    entries.push(...options.entry);
  }
  if (verbose) console.log(`buildEntries [${entryMode}] ${entries.length}`);
  return entries;
};

// src/actions/package/compile/deepMerge.ts
function deepMerge2(target, source) {
  for (const [key, sourceVal] of Object.entries(source)) {
    if (typeof sourceVal === "object" && sourceVal !== null && !Array.isArray(sourceVal)) {
      if (!Object.hasOwn(target, key) || typeof target[key] !== "object") {
        target[key] = {};
      }
      deepMerge2(target[key], sourceVal);
    } else {
      target[key] = sourceVal;
    }
  }
  return target;
}
function deepMergeObjects(objects) {
  const result = {};
  for (const obj of objects) {
    deepMerge2(result, obj);
  }
  return result;
}

// src/actions/package/compile/resolveCompilePlatforms.ts
import { cwd } from "node:process";
import chalk5 from "chalk";
var resolvePlatformRecord = (value) => {
  if (value === void 0 || value === true) return { src: {} };
  if (value === false) return {};
  const { esbuildOptions: _legacyEsbuildOptions, ...entries } = value;
  return entries;
};
var classifyPlatform = (value) => {
  if (value === void 0) {
    return {
      deprecatedEmptyObject: false,
      entries: {},
      state: "unset"
    };
  }
  if (typeof value === "boolean") {
    return {
      deprecatedEmptyObject: false,
      entries: value ? { src: {} } : {},
      state: value ? "build" : "off"
    };
  }
  const deprecatedEmptyObject = Object.keys(value).length === 0;
  const entries = resolvePlatformRecord(value);
  return {
    deprecatedEmptyObject,
    entries,
    state: Object.keys(entries).length > 0 ? "build" : "off"
  };
};
var resolveCompilePlatforms = (compile) => {
  const node = classifyPlatform(compile?.node);
  const browser = classifyPlatform(compile?.browser);
  const neutral = classifyPlatform(compile?.neutral);
  const anyBuild = node.state === "build" || browser.state === "build" || neutral.state === "build";
  const neutralEntries = neutral.state === "build" ? neutral.entries : !anyBuild && neutral.state === "unset" ? { src: {} } : {};
  const deprecatedEmptyObjectTargets = [
    ...node.deprecatedEmptyObject ? ["node"] : [],
    ...browser.deprecatedEmptyObject ? ["browser"] : [],
    ...neutral.deprecatedEmptyObject ? ["neutral"] : []
  ];
  return {
    browser: browser.state === "build" ? browser.entries : {},
    deprecatedEmptyObjectTargets,
    neutral: neutralEntries,
    node: node.state === "build" ? node.entries : {}
  };
};
var deprecatedEmptyObjectWarned = /* @__PURE__ */ new Set();
var warnDeprecatedEmptyObjectTargets = (targets) => {
  for (const target of targets) {
    const key = `${cwd()}:${target}`;
    if (deprecatedEmptyObjectWarned.has(key)) continue;
    deprecatedEmptyObjectWarned.add(key);
    console.warn(chalk5.yellow(
      `[xy] Deprecated: compile.${target} = {} is treated as "do not build". Set it to \`false\` or remove the field.`
    ));
  }
};

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

// src/actions/package/compile/validateFullPackage.ts
import fs2 from "node:fs";
import path5 from "node:path";
import chalk7 from "chalk";
import ts, { formatDiagnosticsWithColorAndContext, sys } from "typescript";
var toPosixPath = (value) => value.split(path5.sep).join("/");
var relativePath = (from, to) => {
  const result = toPosixPath(path5.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(path5.relative(path5.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 = path5.resolve(pkg, "build");
  fs2.mkdirSync(buildDir, { recursive: true });
  const configPath = path5.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 = path5.resolve(buildDir, "tsconfig.package-validate-empty.d.ts");
    fs2.writeFileSync(emptyFile, "");
    files.push(relativePath(buildDir, emptyFile));
  }
  fs2.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 = path5.resolve(pkg, "tsconfig.json");
  if (!fs2.existsSync(tsconfigPath)) {
    console.warn(chalk7.yellow("Compile:Validate skipped \u2014 tsconfig.json not found"));
    return 0;
  }
  if (verbose) {
    console.log(chalk7.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(chalk7.red(`Compile:Validate failed (tsc exit code ${result})`));
  }
  return result;
};

// src/actions/package/compile/packageCompileEsbuild.ts
var isBuildFormat = (value) => {
  return typeof value === "string" && ["cjs", "esm", "iife"].includes(value);
};
var legacyIgnoredOptions = /* @__PURE__ */ new Set([
  "clean",
  "cjsInterop",
  "dts",
  "entry",
  "outDir",
  "silent",
  "skipNodeModulesBundle"
]);
var supportedEsbuildOptions = /* @__PURE__ */ new Set([
  "absWorkingDir",
  "alias",
  "allowOverwrite",
  "assetNames",
  "banner",
  "bundle",
  "charset",
  "chunkNames",
  "color",
  "conditions",
  "define",
  "drop",
  "dropLabels",
  "entryNames",
  "entryPoints",
  "external",
  "footer",
  "format",
  "globalName",
  "ignoreAnnotations",
  "inject",
  "jsx",
  "jsxDev",
  "jsxFactory",
  "jsxFragment",
  "jsxImportSource",
  "jsxSideEffects",
  "keepNames",
  "legalComments",
  "lineLimit",
  "loader",
  "logLevel",
  "logLimit",
  "logOverride",
  "mainFields",
  "mangleCache",
  "mangleProps",
  "mangleQuoted",
  "metafile",
  "minify",
  "minifyIdentifiers",
  "minifySyntax",
  "minifyWhitespace",
  "nodePaths",
  "outExtension",
  "outbase",
  "outdir",
  "outfile",
  "packages",
  "platform",
  "plugins",
  "preserveSymlinks",
  "publicPath",
  "pure",
  "reserveProps",
  "resolveExtensions",
  "sourcemap",
  "sourceRoot",
  "sourcesContent",
  "splitting",
  "stdin",
  "supported",
  "target",
  "treeShaking",
  "tsconfig",
  "tsconfigRaw",
  "watch",
  "write"
]);
var toPosixPath2 = (value) => value.split(path6.sep).join("/");
var relativePath2 = (from, to) => {
  const result = toPosixPath2(path6.relative(from, to));
  return result.startsWith(".") ? result : `./${result}`;
};
var safeFilePart = (value) => value.replaceAll(/[^\dA-Za-z-]+/g, "-").replaceAll(/(?:^-|-$)/g, "") || "src";
var platformSourceSegments = /* @__PURE__ */ new Set(["browser", "neutral", "node"]);
var normalizeEntryPath = (entry) => toPosixPath2(entry).replace(/^\.?\//u, "");
var customEntryOutputName = (entry) => {
  const parsed = path6.posix.parse(normalizeEntryPath(entry));
  const withoutExtension = path6.posix.join(parsed.dir, parsed.name);
  const segments = withoutExtension.split("/");
  return segments.length > 1 && platformSourceSegments.has(segments[0]) ? segments.slice(1).join("/") : withoutExtension;
};
var createEsbuildEntryPoints = (srcDir, entries, entryMode = "single") => {
  if (entryMode !== "custom") {
    return entries.map((entry) => `${srcDir}/${normalizeEntryPath(entry)}`);
  }
  const entryPoints = {};
  for (const entry of entries) {
    const outputName = customEntryOutputName(entry);
    if (entryPoints[outputName] !== void 0) {
      throw new Error(`Duplicate custom entry output "${outputName}" from "${entry}"`);
    }
    entryPoints[outputName] = `${srcDir}/${normalizeEntryPath(entry)}`;
  }
  return entryPoints;
};
var createTypescriptConfig = ({
  declaration = false,
  outDir,
  pkg,
  srcDir
}) => {
  const buildDir = path6.resolve(pkg, "build");
  const sourceRoot = path6.resolve(pkg, srcDir);
  return {
    compilerOptions: {
      ...declaration ? {
        declaration: true,
        declarationMap: true,
        emitDeclarationOnly: true,
        noEmit: false,
        outDir: outDir === void 0 ? void 0 : relativePath2(buildDir, path6.resolve(pkg, outDir))
      } : { noEmit: true },
      rootDir: relativePath2(buildDir, sourceRoot)
    },
    exclude: [
      `${relativePath2(buildDir, sourceRoot)}/**/*.spec.*`,
      `${relativePath2(buildDir, sourceRoot)}/**/*.stories.*`,
      `${relativePath2(buildDir, sourceRoot)}/**/*.example.*`,
      `${relativePath2(buildDir, sourceRoot)}/**/spec/**`,
      `${relativePath2(buildDir, sourceRoot)}/**/stories/**`
    ],
    extends: relativePath2(buildDir, path6.resolve(pkg, "tsconfig.json")),
    include: [`${relativePath2(buildDir, sourceRoot)}/**/*`]
  };
};
var writeTypescriptConfig = (pkg, platform, srcDir, declaration, outDir) => {
  const buildDir = path6.resolve(pkg, "build");
  fs3.mkdirSync(buildDir, { recursive: true });
  const configPath = path6.resolve(buildDir, `tsconfig.package-${declaration ? "dts" : "check"}-${platform}-${safeFilePart(srcDir)}.json`);
  fs3.writeFileSync(
    configPath,
    `${JSON.stringify(createTypescriptConfig({
      declaration,
      outDir,
      pkg,
      srcDir
    }), null, 2)}
`
  );
  return configPath;
};
function copyDeclarationFiles(fromDir, toDir) {
  if (path6.resolve(fromDir) === path6.resolve(toDir) || !fs3.existsSync(fromDir)) return;
  const copyFrom = (dir) => {
    for (const entry of fs3.readdirSync(dir, { withFileTypes: true })) {
      const sourcePath = path6.join(dir, entry.name);
      const relative = path6.relative(fromDir, sourcePath);
      const targetPath = path6.join(toDir, relative);
      if (entry.isDirectory()) {
        copyFrom(sourcePath);
      } else if (entry.isFile() && (entry.name.endsWith(".d.ts") || entry.name.endsWith(".d.ts.map"))) {
        fs3.mkdirSync(path6.dirname(targetPath), { recursive: true });
        fs3.copyFileSync(sourcePath, targetPath);
      }
    }
  };
  copyFrom(fromDir);
}
var normalizeOptions = (options = {}, verbose = false) => {
  const normalized = {};
  const ignored = [];
  for (const [key, value] of Object.entries(options)) {
    if (legacyIgnoredOptions.has(key)) {
      ignored.push(key);
      continue;
    }
    if (key === "format" && Array.isArray(value)) {
      if (isBuildFormat(value[0])) {
        normalized.format = value[0];
      }
      continue;
    }
    if (key === "outExtension" && typeof value === "function") {
      ignored.push(key);
      continue;
    }
    if (supportedEsbuildOptions.has(key)) {
      normalized[key] = value;
      continue;
    }
    ignored.push(key);
  }
  if (verbose && ignored.length > 0) {
    console.warn(chalk8.yellow(`[xy] Ignoring unsupported compile options for esbuild: ${[...new Set(ignored)].toSorted((a, b) => a.localeCompare(b)).join(", ")}`));
  }
  return normalized;
};
var esbuildOptions = (options = [], verbose = false) => {
  const standardLoaders = {
    ".gif": "copy",
    ".html": "copy",
    ".jpg": "copy",
    ".json": "json",
    ".png": "copy",
    ".svg": "text",
    ".webp": "copy"
  };
  const standardOptions = {
    bundle: true,
    format: "esm",
    loader: standardLoaders,
    outExtension: { ".js": ".mjs" },
    packages: "external",
    sourcemap: true,
    target: "esnext"
  };
  return deepMergeObjects([standardOptions, ...options.map((option) => normalizeOptions(option, verbose))]);
};
var tsupOptions = esbuildOptions;
var resolvePlatformEsbuildOptions = (value) => {
  if (typeof value !== "object" || value === null) return {};
  const entry = value.esbuildOptions;
  return entry ?? {};
};
var getRootOutDir = (options) => {
  return options.outDir ?? options.outdir ?? "dist";
};
var compileFolder = async (srcDir, entries, options, platform, outDir, checkTypes, emitDeclarations, entryMode, verbose) => {
  const pkg = cwd3();
  if (verbose ?? false) {
    console.log(chalk8.gray(`compileFolder [${srcDir}, ${outDir}]`));
  }
  if (!fs3.existsSync(path6.resolve(pkg, srcDir))) {
    if (verbose ?? false) {
      console.warn(chalk8.yellow(`No source directory found in ${srcDir} to compile`));
    }
    return 0;
  }
  if (entries.length === 0) {
    console.warn(chalk8.yellow(`No entries found in ${srcDir} to compile`));
    return 0;
  }
  if (checkTypes) {
    const checkConfig = writeTypescriptConfig(pkg, platform, srcDir, false);
    const checkResult = runTsc(checkConfig, verbose);
    if (checkResult !== 0) {
      console.error(chalk8.red(`Compile:Validation had ${checkResult} errors`));
      return checkResult;
    }
  }
  const declarationResult = await emitDeclarations();
  if (declarationResult !== 0) {
    console.error(chalk8.red(`Compile:Declaration emit had ${declarationResult} errors`));
    return declarationResult;
  }
  const buildOptions = {
    ...options,
    entryPoints: createEsbuildEntryPoints(srcDir, entries, entryMode),
    outbase: srcDir,
    outdir: outDir,
    platform,
    tsconfig: "tsconfig.json"
  };
  if (verbose ?? false) {
    console.log(chalk8.cyan(`ESBUILD:build:start [${srcDir}]`));
    const pluginNames = buildOptions.plugins?.map((plugin) => plugin.name);
    const loggedOptions = { ...buildOptions, plugins: pluginNames };
    console.log(chalk8.gray(`ESBUILD:build:options [${JSON.stringify(loggedOptions, null, 2)}]`));
  }
  await build(buildOptions);
  if (verbose ?? false) {
    console.log(chalk8.cyan(`ESBUILD:build:stop [${srcDir}]`));
  }
  return 0;
};
function createDeclarationEmitter(verbose = false) {
  const declarationPromises = /* @__PURE__ */ new Map();
  return async (srcDir, rootOutDir, platform, outDir) => {
    const key = `${srcDir}\0${rootOutDir}`;
    let promise = declarationPromises.get(key);
    if (!promise) {
      promise = (async () => {
        const dtsConfig = writeTypescriptConfig(cwd3(), platform, srcDir, true, outDir);
        return { outDir, result: runTsc(dtsConfig, verbose) };
      })();
      declarationPromises.set(key, promise);
    }
    const emitted = await promise;
    if (emitted.result !== 0) return emitted.result;
    copyDeclarationFiles(emitted.outDir, outDir);
    return 0;
  };
}
function resolveCompileMode(config2, mode) {
  const compile = config2?.compile;
  const verbose = config2?.verbose ?? false;
  const shouldEmit = !(mode.validateOnly ?? false);
  const shouldRunFullValidation = !(mode.emitOnly ?? false) && (compile?.validate ?? true);
  return {
    compile,
    shouldEmit,
    shouldRunFolderChecks: !shouldRunFullValidation && !(mode.emitOnly ?? false),
    shouldRunFullValidation,
    verbose
  };
}
async function compilePlatformEntries({
  checkTypes,
  compile,
  emitDeclarations,
  entries,
  legacyTsupOptions,
  platform,
  platformEsbuild,
  verbose
}) {
  const results = await Promise.all(
    Object.entries(entries).map(async ([srcDir, options]) => {
      const rootOutDir = getRootOutDir(options);
      const outDir = `${rootOutDir}/${platform}`;
      const entry = buildEntries(srcDir, compile?.entryMode, options, true, verbose);
      return compileFolder(
        srcDir,
        entry,
        esbuildOptions([
          platformEsbuild,
          legacyTsupOptions ?? {},
          compile?.esbuild?.options ?? {},
          options,
          { outdir: outDir }
        ], verbose),
        platform,
        outDir,
        checkTypes,
        () => emitDeclarations(srcDir, rootOutDir, platform, outDir),
        compile?.entryMode,
        verbose
      );
    })
  );
  return results.reduce((prev, value) => prev + value, 0);
}
var packageCompileEsbuild = async (config2, mode = {}) => {
  const {
    compile,
    shouldEmit,
    shouldRunFolderChecks,
    shouldRunFullValidation,
    verbose
  } = resolveCompileMode(config2, mode);
  if (verbose) {
    console.log("Compiling with esbuild and tsc");
  }
  if (shouldRunFullValidation) {
    const validateResult = validateFullPackage(cwd3(), verbose);
    if (validateResult !== 0) {
      return validateResult;
    }
  }
  if (!shouldEmit) return 0;
  const platforms = resolveCompilePlatforms(compile);
  warnDeprecatedEmptyObjectTargets(platforms.deprecatedEmptyObjectTargets);
  const compileForNode = platforms.node;
  const compileForBrowser = platforms.browser;
  const compileForNeutral = platforms.neutral;
  const nodeEsbuild = resolvePlatformEsbuildOptions(compile?.node);
  const browserEsbuild = resolvePlatformEsbuildOptions(compile?.browser);
  const neutralEsbuild = resolvePlatformEsbuildOptions(compile?.neutral);
  const legacyTsupOptions = compile?.tsup?.options;
  const emitDeclarations = createDeclarationEmitter(verbose);
  const [nodeErrors, browserErrors, neutralErrors] = await Promise.all([
    compilePlatformEntries({
      checkTypes: shouldRunFolderChecks,
      compile,
      emitDeclarations,
      entries: compileForNode,
      legacyTsupOptions,
      platform: "node",
      platformEsbuild: nodeEsbuild,
      verbose
    }),
    compilePlatformEntries({
      checkTypes: shouldRunFolderChecks,
      compile,
      emitDeclarations,
      entries: compileForBrowser,
      legacyTsupOptions,
      platform: "browser",
      platformEsbuild: browserEsbuild,
      verbose
    }),
    compilePlatformEntries({
      checkTypes: shouldRunFolderChecks,
      compile,
      emitDeclarations,
      entries: compileForNeutral,
      legacyTsupOptions,
      platform: "neutral",
      platformEsbuild: neutralEsbuild,
      verbose
    })
  ]);
  return nodeErrors + browserErrors + neutralErrors;
};

// src/actions/package/compile/compile.ts
var packageCompile = async (inConfig = {}, mode = {}) => {
  const pkg = cwd4();
  console.log(chalk9.green(`Compiling ${pkg}`));
  const config2 = await loadConfig(inConfig);
  return await packageCompileEsbuild(config2, mode);
};

// src/actions/package/compile/packageCompileTsc.ts
import { cwd as cwd6 } from "node:process";
import chalk10 from "chalk";
import ts3, {
  DiagnosticCategory,
  formatDiagnosticsWithColorAndContext as formatDiagnosticsWithColorAndContext2,
  getPreEmitDiagnostics,
  sys as sys2
} from "typescript";

// src/actions/package/compile/createProgramFromConfig.ts
import ts2 from "typescript";
function createProgramFromConfig(options) {
  const {
    basePath,
    compilerOptions,
    configFilePath,
    exclude,
    files,
    include
  } = options;
  const readResult = ts2.readConfigFile(configFilePath, (fileName) => ts2.sys.readFile(fileName));
  if (readResult.error) {
    throw new Error(ts2.flattenDiagnosticMessageText(readResult.error.messageText, "\n"));
  }
  const config2 = readResult.config ?? {};
  config2.compilerOptions = { ...config2.compilerOptions, ...compilerOptions };
  if (include) config2.include = include;
  if (files) config2.files = files;
  if (exclude) config2.exclude = exclude;
  const parsed = ts2.parseJsonConfigFileContent(config2, ts2.sys, basePath, void 0, configFilePath);
  return ts2.createProgram({
    configFileParsingDiagnostics: parsed.errors,
    options: parsed.options,
    projectReferences: parsed.projectReferences,
    rootNames: parsed.fileNames
  });
}

// src/actions/package/compile/getCompilerOptions.ts
import { cwd as cwd5 } from "node:process";
import { getTsconfig } from "get-tsconfig";
var getCompilerOptions = (options = {}, fileName = "tsconfig.json") => {
  const resolvedTsConfigCompilerOptions = getTsconfig(cwd5(), fileName)?.config?.compilerOptions ?? {};
  return deepMerge(resolvedTsConfigCompilerOptions, options);
};

// src/actions/package/compile/packageCompileTsc.ts
var packageCompileTsc = (platform, entries, srcDir = "src", outDir = "dist", compilerOptionsParam, verbose = false) => {
  const pkg = cwd6();
  if (verbose) {
    console.log(chalk10.cyan(`Validating code START: ${entries.length} files to ${outDir} from ${srcDir}`));
  }
  const configFilePath = ts3.findConfigFile(
    pkg,
    (fileName) => ts3.sys.fileExists(fileName),
    "tsconfig.json"
  );
  if (configFilePath === void 0) {
    throw new Error("Could not find tsconfig.json");
  }
  const compilerOptions = {
    ...getCompilerOptions({
      removeComments: false,
      skipDefaultLibCheck: true,
      skipLibCheck: true,
      sourceMap: false
    }),
    ...compilerOptionsParam,
    outDir: `${outDir}/${platform}`,
    emitDeclarationOnly: true,
    noEmit: false
  };
  console.log(chalk10.cyan(`Validating Files: ${entries.length}`));
  if (verbose) {
    for (const entry of entries) {
      console.log(chalk10.grey(`Validating: ${entry}`));
    }
  }
  try {
    if (entries.length > 0) {
      const program = createProgramFromConfig({
        configFilePath,
        basePath: pkg,
        compilerOptions,
        files: entries.map((entry) => `${srcDir}/${entry}`),
        include: [`${srcDir}/**/*.*`]
      });
      const diagnostics = getPreEmitDiagnostics(program);
      if (diagnostics.length > 0) {
        const formattedDiagnostics = formatDiagnosticsWithColorAndContext2(
          diagnostics,
          {
            getCanonicalFileName: (fileName) => fileName,
            getCurrentDirectory: () => srcDir,
            getNewLine: () => sys2.newLine
          }
        );
        console.error(formattedDiagnostics);
      }
      const nonEmitPatterns = [".stories.", "/stories/", "/stories.", ".spec.", "/spec/", "/spec.", ".example."];
      program.emit(void 0, (fileName, text, writeByteOrderMark) => {
        if (nonEmitPatterns.some((pattern) => fileName.includes(pattern))) {
          return;
        }
        ts3.sys.writeFile(fileName, text, writeByteOrderMark);
      });
      const strict = isStrictMode();
      let count = 0;
      for (const diag of diagnostics) {
        if (diag.category === DiagnosticCategory.Error) {
          count++;
        } else if (strict && diag.category === DiagnosticCategory.Warning) {
          count++;
        }
      }
      return count;
    }
    return 0;
  } finally {
    if (verbose) {
      console.log(chalk10.cyan(`Validating code FINISH: ${entries.length} files to ${outDir} from ${srcDir}`));
    }
  }
};

// src/actions/package/compile/packageCompileTsup.ts
var packageCompileTsup = async (config2) => packageCompileEsbuild(config2);

// src/actions/package/compile/XyConfig.ts
var ALL_PUBLINT_CHECKS = [
  "compileTargets",
  "files",
  "importToDefault",
  "main",
  "module",
  "peerDeps",
  "platform",
  "publint",
  "resolutions",
  "rootSource",
  "rootTypes",
  "sideEffects",
  "source",
  "types"
];
var PUBLISH_ONLY_CHECKS = [
  "files",
  "importToDefault",
  "main",
  "module",
  "publint",
  "rootSource",
  "rootTypes",
  "sideEffects",
  "source",
  "types"
];

// src/actions/package/copy-assets.ts
import path7 from "node:path/posix";
import chalk11 from "chalk";
var copyTargetAssets = async (target, name, location) => {
  try {
    const srcDir = path7.join(location, "src");
    const destDir = path7.join(srcDir, "..", "dist", target);
    const values = copyAssetFiles(srcDir, destDir);
    if (values.length > 0) {
      console.log(chalk11.green(`Copying Assets [${target.toUpperCase()}] - ${name} - ${location}`));
    }
    for (const value of values) {
      console.log(`${value.split("/").pop()} => ./dist/${target}`);
    }
    return 0;
  } catch (ex) {
    const error = ex;
    console.log(`Copy Assets Failed: ${name}: ${error.message}`);
    return 1;
  }
};
var packageCopyAssets = async ({ target }) => {
  const pkg = INIT_CWD();
  const pkgName = packageName() ?? "Unknown";
  switch (target) {
    case "esm": {
      return await copyTargetAssets("esm", pkgName, pkg);
    }
    case "cjs": {
      return await copyTargetAssets("cjs", pkgName, pkg);
    }
    default: {
      return await copyTargetAssets("esm", pkgName, pkg) || await copyTargetAssets("cjs", pkgName, pkg);
    }
  }
};

// src/actions/package/lint.ts
import { readdirSync } from "node:fs";
import path8 from "node:path";
import { cwd as cwd7 } from "node:process";
import { pathToFileURL } from "node:url";
import chalk12 from "chalk";
import { ESLint } from "eslint";
import { findUp } from "find-up";
var dumpMessages = (lintResults) => {
  const colors = ["white", "yellow", "red"];
  const severity = ["none", "warning", "error"];
  for (const lintResult of lintResults) {
    if (lintResult.messages.length > 0) {
      console.log(chalk12.gray(`
${lintResult.filePath}`));
      for (const message of lintResult.messages) {
        console.log(
          chalk12.gray(`	${message.line}:${message.column}`),
          chalk12[colors[message.severity]](`	${severity[message.severity]}`),
          chalk12.white(`	${message.message}`),
          chalk12.gray(`	${message.ruleId}`)
        );
      }
    }
  }
};
async function getRootESLintConfig() {
  const configPath = await findUp(["eslint.config.ts", "eslint.config.mjs"]);
  if (configPath === void 0) {
    throw new Error("eslint.config.ts not found in the monorepo");
  }
  return pathToFileURL(configPath);
}
function getFiles(dir, ignoreFolders) {
  const currentDirectory = cwd7();
  const subdirectory = dir.split(currentDirectory)[1]?.split("/", 2)[1];
  if (ignoreFolders.includes(subdirectory)) return [];
  return readdirSync(dir, { withFileTypes: true }).flatMap((dirent) => {
    const res = path8.resolve(dir, dirent.name);
    const relativePath4 = subdirectory === void 0 ? dirent.name : `${subdirectory}/${dirent.name}`;
    if (makeMatcher(ignoreFolders)(relativePath4)) return [];
    return dirent.isDirectory() ? getFiles(res, ignoreFolders) : [res];
  });
}
var packageLint = async (fix = false, verbose = false, cache = true) => {
  const pkg = INIT_CWD();
  const configPath = await getRootESLintConfig();
  const { default: eslintConfig } = await import(configPath.href);
  const start = Date.now();
  const ignoreFolders = ["node_modules", "dist", "packages", ".git", "build", ".yarn", ".vscode", ".github"];
  const engine = new ESLint({
    baseConfig: [...eslintConfig],
    fix,
    warnIgnored: false,
    cache
  });
  const files = getFiles(cwd7(), ignoreFolders);
  console.log(chalk12.green(`Linting ${pkg} [files = ${files.length}]`));
  if (verbose) {
    for (const file of files) {
      console.log(chalk12.gray(`	${file}`));
    }
  }
  const lintResults = await engine.lintFiles(files);
  dumpMessages(lintResults);
  if (fix) {
    await ESLint.outputFixes(lintResults);
  }
  const filesCountColor = files.length < 100 ? "green" : files.length < 1e3 ? "yellow" : "red";
  const lintTime = Date.now() - start;
  const lintTimeColor = lintTime < 1e3 ? "green" : lintTime < 3e3 ? "yellow" : "red";
  console.log(chalk12.white(`Linted ${chalk12[filesCountColor](files.length)} files in ${chalk12[lintTimeColor](lintTime)}ms`));
  return lintResults.reduce((prev, lintResult) => prev + lintResult.errorCount, 0);
};

// src/actions/package/publint.ts
import { promises as fs6 } from "node:fs";
import path11 from "node:path";
import chalk15 from "chalk";
import { glob as glob3 } from "glob";
import sortPackageJson from "sort-package-json";

// src/actions/package/compileTargetsPublint.ts
import fs4 from "node:fs";
import path9 from "node:path";
import chalk13 from "chalk";
import ts4 from "typescript";
var COMPILE_TARGET_NAMES = /* @__PURE__ */ new Set(["browser", "neutral", "node"]);
var CONFIG_FILE_NAMES = [
  "xy.config.ts",
  "xy.config.mts",
  "xy.config.cts",
  "xy.config.js",
  "xy.config.mjs",
  "xy.config.cjs"
];
function emptyResult() {
  return {
    errors: 0,
    modified: false,
    warnings: 0
  };
}
function findConfigFile(pkgDir) {
  for (const name of CONFIG_FILE_NAMES) {
    const candidate = path9.join(pkgDir, name);
    if (fs4.existsSync(candidate)) return candidate;
  }
  return void 0;
}
function getPropertyName(prop) {
  if (!ts4.isPropertyAssignment(prop)) return void 0;
  const { name } = prop;
  if (ts4.isIdentifier(name) || ts4.isStringLiteral(name)) return name.text;
  return void 0;
}
function collectEmptyTargetProps(sourceFile) {
  const found = [];
  function visit(node) {
    if (ts4.isPropertyAssignment(node) && getPropertyName(node) === "compile" && ts4.isObjectLiteralExpression(node.initializer)) {
      for (const prop of node.initializer.properties) {
        if (ts4.isPropertyAssignment(prop) && COMPILE_TARGET_NAMES.has(getPropertyName(prop) ?? "") && ts4.isObjectLiteralExpression(prop.initializer) && prop.initializer.properties.length === 0) {
          found.push(prop);
        }
      }
    }
    ts4.forEachChild(node, visit);
  }
  visit(sourceFile);
  return found;
}
function isWhitespace(char) {
  return char !== void 0 && /\s/.test(char);
}
function removeProperties(text, props, sourceFile) {
  const sorted = props.toSorted((a, b) => b.getStart(sourceFile) - a.getStart(sourceFile));
  let result = text;
  for (const prop of sorted) {
    let start = prop.getFullStart();
    let end = prop.getEnd();
    let cursor = end;
    while (isWhitespace(result[cursor])) cursor++;
    if (result[cursor] === ",") {
      end = cursor + 1;
    } else {
      let back = prop.getStart(sourceFile) - 1;
      while (back >= 0 && isWhitespace(result[back])) back--;
      if (result[back] === ",") start = back;
    }
    result = result.slice(0, start) + result.slice(end);
  }
  return result;
}
function checkCompileTargets(pkgDir, fix) {
  const result = emptyResult();
  const configPath = findConfigFile(pkgDir);
  if (configPath === void 0) return result;
  const text = fs4.readFileSync(configPath, "utf8");
  const sourceFile = ts4.createSourceFile(path9.basename(configPath), text, ts4.ScriptTarget.Latest, true, ts4.ScriptKind.TS);
  const emptyProps = collectEmptyTargetProps(sourceFile);
  if (emptyProps.length === 0) return result;
  const configName = path9.basename(configPath);
  if (fix) {
    fs4.writeFileSync(configPath, removeProperties(text, emptyProps, sourceFile));
    for (const prop of emptyProps) {
      console.warn(chalk13.yellow(`Publint [compileTargets]: removed deprecated empty "compile.${getPropertyName(prop)}" target from ${configName}`));
    }
    result.modified = true;
  } else {
    for (const prop of emptyProps) {
      console.warn(chalk13.yellow(
        `Publint [compileTargets]: "compile.${getPropertyName(prop)} = {}" is treated as "do not build" \u2014 set it to \`false\` or remove it (use --fix to remove) in ${configName}`
      ));
    }
  }
  result.warnings += emptyProps.length;
  return result;
}

// src/actions/package/platformPublint.ts
import fs5 from "node:fs";
import { builtinModules } from "node:module";
import path10 from "node:path";
import chalk14 from "chalk";
import { globSync as globSync2 } from "glob";
import ts5 from "typescript";
var platformSegments = /* @__PURE__ */ new Set(["browser", "neutral", "node"]);
var nodeConditionNames = /* @__PURE__ */ new Set(["node", "node-addons"]);
var browserConditionNames = /* @__PURE__ */ new Set(["browser", "web", "webworker", "worker"]);
var neutralConditionNames = /* @__PURE__ */ new Set(["neutral"]);
var builtinModuleNames = new Set(
  builtinModules.map((mod) => mod.replace(/^node:/, "")).filter((mod) => !mod.startsWith("_"))
);
var codeExtensions = [".mjs", ".cjs", ".js"];
var declarationExtensions = [".d.mts", ".d.cts", ".d.ts"];
function emptyResult2() {
  return {
    errors: 0,
    modified: false,
    warnings: 0
  };
}
function toPosixPath3(value) {
  return value.split(path10.sep).join("/");
}
function relativePath3(pkgDir, filePath) {
  return toPosixPath3(path10.relative(pkgDir, filePath));
}
function isSubpathExportKey(key) {
  return key === "." || key.startsWith("./");
}
function formatExportPath(pathParts) {
  return pathParts.map((part, index) => {
    if (index === 0) return part;
    if (part.startsWith("[")) return part;
    return /^[a-zA-Z_$][\w$]*$/.test(part) ? `.${part}` : `[${JSON.stringify(part)}]`;
  }).join("");
}
function conditionPlatform(key) {
  if (nodeConditionNames.has(key)) return "node";
  if (browserConditionNames.has(key)) return "browser";
  if (neutralConditionNames.has(key)) return "neutral";
  return void 0;
}
function getConditionPlatform(conditionPath) {
  for (const condition of conditionPath.toReversed()) {
    const platform = conditionPlatform(condition);
    if (platform) return platform;
  }
  return void 0;
}
function getPathPlatform(target) {
  const segments = target.replace(/^\.\//, "").split(/[\\/]+/);
  const distIndex = segments.indexOf("dist");
  const platform = distIndex === -1 ? void 0 : segments[distIndex + 1];
  return platformSegments.has(platform) ? platform : void 0;
}
function collectExportTargets(value, pathParts = ["exports"], conditionPath = []) {
  if (typeof value === "string") {
    if (!value.startsWith("./")) return [];
    return [{
      conditionPath,
      conditionPlatform: getConditionPlatform(conditionPath),
      exportPath: formatExportPath(pathParts),
      pathPlatform: getPathPlatform(value),
      target: value
    }];
  }
  if (Array.isArray(value)) {
    return value.flatMap((entry, index) => collectExportTargets(entry, [...pathParts, `[${index}]`], conditionPath));
  }
  if (typeof value !== "object" || value === null) return [];
  return Object.entries(value).flatMap(([key, nested]) => {
    const nextConditionPath = isSubpathExportKey(key) ? conditionPath : [...conditionPath, key];
    return collectExportTargets(nested, [...pathParts, key], nextConditionPath);
  });
}
function isDeclarationFile(filePath) {
  return declarationExtensions.some((ext) => filePath.endsWith(ext));
}
function isCodeFile(filePath) {
  return codeExtensions.some((ext) => filePath.endsWith(ext));
}
function hasCodeOrDeclarationExtension(filePath) {
  return isCodeFile(filePath) || isDeclarationFile(filePath);
}
function pathCandidates(filePath, extensions) {
  const candidates = [filePath];
  const parsed = path10.parse(filePath);
  const withoutExtension = parsed.ext ? filePath.slice(0, -parsed.ext.length) : filePath;
  for (const ext of extensions) {
    candidates.push(`${withoutExtension}${ext}`, `${filePath}${ext}`, path10.join(filePath, `index${ext}`));
  }
  return [...new Set(candidates)];
}
function declarationCandidates(filePath) {
  const extensionCandidates = filePath.endsWith(".mjs") ? [filePath.replace(/\.mjs$/, ".d.mts"), filePath.replace(/\.mjs$/, ".d.ts")] : filePath.endsWith(".cjs") ? [filePath.replace(/\.cjs$/, ".d.cts"), filePath.replace(/\.cjs$/, ".d.ts")] : filePath.endsWith(".js") ? [filePath.replace(/\.js$/, ".d.ts")] : [];
  return [.../* @__PURE__ */ new Set([filePath, ...extensionCandidates, ...pathCandidates(filePath, declarationExtensions)])];
}
function resolveExistingFile(filePath, declaration) {
  const candidates = declaration ? declarationCandidates(filePath) : pathCandidates(filePath, codeExtensions);
  return candidates.find((candidate) => fs5.existsSync(candidate) && fs5.statSync(candidate).isFile());
}
function resolveExportTargetFiles(pkgDir, target) {
  if (!hasCodeOrDeclarationExtension(target) && !target.includes("*")) return [];
  const relativeTarget = target.replace(/^\.\//, "");
  if (relativeTarget.includes("*")) {
    return globSync2(relativeTarget, {
      cwd: pkgDir,
      nodir: true,
      absolute: true
    }).filter(hasCodeOrDeclarationExtension);
  }
  const absoluteTarget = path10.resolve(pkgDir, relativeTarget);
  const exact = fs5.existsSync(absoluteTarget) && fs5.statSync(absoluteTarget).isFile() ? absoluteTarget : resolveExistingFile(absoluteTarget, isDeclarationFile(relativeTarget));
  return exact != null && exact.length > 0 ? [exact] : [];
}
function isNodeBuiltin(specifier) {
  const normalized = specifier.replace(/^node:/, "");
  return specifier === "node" || specifier.startsWith("node:") || builtinModuleNames.has(normalized) || builtinModuleNames.has(normalized.split("/", 1)[0] ?? "");
}
function parseSourceFile(filePath) {
  const sourceCode = fs5.readFileSync(filePath, "utf8");
  const scriptKind = isCodeFile(filePath) ? ts5.ScriptKind.JS : ts5.ScriptKind.TS;
  return ts5.createSourceFile(path10.basename(filePath), sourceCode, ts5.ScriptTarget.Latest, true, scriptKind);
}
function stringLiteralText(node) {
  return node && ts5.isStringLiteral(node) ? node.text : void 0;
}
function collectModuleSpecifiers(sourceFile) {
  const specifiers = [];
  function addSpecifier(node) {
    const text = stringLiteralText(node);
    if (text != null && text.length > 0) specifiers.push(text);
  }
  function visit(node) {
    if (ts5.isImportDeclaration(node) || ts5.isExportDeclaration(node)) {
      addSpecifier(node.moduleSpecifier);
    } else if (ts5.isCallExpression(node) && node.expression.kind === ts5.SyntaxKind.ImportKeyword) {
      addSpecifier(node.arguments[0]);
    } else if (ts5.isCallExpression(node) && ts5.isIdentifier(node.expression) && node.expression.text === "require" && node.arguments.length > 0) {
      addSpecifier(node.arguments[0]);
    } else if (ts5.isImportTypeNode(node)) {
      if (ts5.isLiteralTypeNode(node.argument)) addSpecifier(node.argument.literal);
    } else if (ts5.isImportEqualsDeclaration(node) && ts5.isExternalModuleReference(node.moduleReference)) {
      addSpecifier(node.moduleReference.expression);
    }
    ts5.forEachChild(node, visit);
  }
  visit(sourceFile);
  for (const ref of sourceFile.typeReferenceDirectives) {
    specifiers.push(ref.fileName);
  }
  return specifiers;
}
function resolveLocalSpecifier(filePath, specifier, declaration) {
  if (!specifier.startsWith(".")) return void 0;
  return resolveExistingFile(path10.resolve(path10.dirname(filePath), specifier), declaration);
}
function nodeAmbientGlobalFromName(name) {
  if (name === "NodeJS") return "NodeJS";
  if (name === "Buffer") return "Buffer";
  if (name === "process") return "process";
  return void 0;
}
function getQualifiedRootName(name) {
  return ts5.isIdentifier(name) ? name.text : getQualifiedRootName(name.left);
}
function getPropertyAccessRootName(expression) {
  let current = expression;
  while (ts5.isPropertyAccessExpression(current)) {
    current = current.expression;
  }
  return ts5.isIdentifier(current) ? current.text : "";
}
function getNodeAmbientGlobal(sourceFile) {
  let ambient;
  function visit(node) {
    if (ambient != null && ambient.length > 0) return;
    if (ts5.isTypeReferenceNode(node)) {
      ambient = nodeAmbientGlobalFromName(getQualifiedRootName(node.typeName));
    } else if (ts5.isExpressionWithTypeArguments(node) && ts5.isIdentifier(node.expression)) {
      ambient = nodeAmbientGlobalFromName(node.expression.text);
    } else if (ts5.isExpressionWithTypeArguments(node) && ts5.isPropertyAccessExpression(node.expression)) {
      ambient = nodeAmbientGlobalFromName(getPropertyAccessRootName(node.expression));
    } else if (ts5.isTypeQueryNode(node)) {
      ambient = nodeAmbientGlobalFromName(getQualifiedRootName(node.exprName));
    }
    ts5.forEachChild(node, visit);
  }
  visit(sourceFile);
  return ambient;
}
function scanFileGraph(rootFile, platform, target) {
  const declaration = isDeclarationFile(rootFile);
  const queue = [rootFile];
  const seen = /* @__PURE__ */ new Set();
  const diagnostics = [];
  while (queue.length > 0) {
    const file = queue.shift();
    if (!(file != null && file.length > 0) || seen.has(file)) continue;
    seen.add(file);
    const sourceFile = parseSourceFile(file);
    for (const specifier of collectModuleSpecifiers(sourceFile)) {
      if (isNodeBuiltin(specifier)) {
        diagnostics.push({
          detail: specifier,
          exportPath: target.exportPath,
          file,
          kind: "node-builtin-import",
          platform,
          target: target.target
        });
      }
      const resolved = resolveLocalSpecifier(file, specifier, declaration);
      if (resolved != null && resolved.length > 0 && !seen.has(resolved)) queue.push(resolved);
    }
    if (declaration) {
      const ambient = getNodeAmbientGlobal(sourceFile);
      if (ambient != null && ambient.length > 0) {
        diagnostics.push({
          detail: ambient,
          exportPath: target.exportPath,
          file,
          kind: "node-ambient-type",
          platform,
          target: target.target
        });
      }
    }
  }
  return diagnostics;
}
function shouldScanForNodeUsage(target) {
  if (target.pathPlatform === "browser" || target.pathPlatform === "neutral") return target.pathPlatform;
  if (target.conditionPlatform === "browser" || target.conditionPlatform === "neutral") return target.conditionPlatform;
  return void 0;
}
function collectConditionTargetDiagnostics(targets) {
  const diagnostics = [];
  for (const target of targets) {
    const { conditionPlatform: condition, pathPlatform: output } = target;
    if (!condition || !output) continue;
    if (condition === "browser" && output === "node" || condition === "node" && output === "browser" || condition === "neutral" && output !== "neutral") {
      diagnostics.push({
        detail: `${condition} condition points to ${output} output`,
        exportPath: target.exportPath,
        kind: "condition-target-mismatch",
        platform: condition,
        target: target.target
      });
    }
  }
  return diagnostics;
}
function isRuntimeTarget(target) {
  return codeExtensions.some((ext) => target.target.endsWith(ext));
}
function isPortableTarget(target) {
  return target.conditionPlatform === "browser" || target.conditionPlatform === "neutral" || target.pathPlatform === "browser" || target.pathPlatform === "neutral" || !target.conditionPlatform && !target.pathPlatform;
}
function isNodeTarget(target) {
  return target.conditionPlatform === "node" || target.pathPlatform === "node";
}
function isNodeOnlyPackage(pkg, targets) {
  const runtimeTargets = targets.filter(isRuntimeTarget);
  const platformTargets = runtimeTargets.length > 0 ? runtimeTargets : targets;
  if (platformTargets.length === 0) return pkg.bin !== void 0;
  return platformTargets.some(isNodeTarget) && !platformTargets.some(isPortableTarget);
}
function readRootTsconfigTypes(pkgDir) {
  const configPath = path10.join(pkgDir, "tsconfig.json");
  if (!fs5.existsSync(configPath)) return [];
  const parsed = ts5.parseConfigFileTextToJson(configPath, fs5.readFileSync(configPath, "utf8"));
  const config2 = parsed.config;
  const types = config2?.compilerOptions?.types;
  return Array.isArray(types) ? types.filter((value) => typeof value === "string") : [];
}
function collectTsconfigDiagnostics(pkgDir, pkg, targets) {
  if (targets.length === 0) return [];
  if (isNodeOnlyPackage(pkg, targets)) return [];
  const types = readRootTsconfigTypes(pkgDir);
  return types.includes("node") ? [{
    detail: 'compilerOptions.types includes "node"',
    file: path10.join(pkgDir, "tsconfig.json"),
    kind: "tsconfig-node-types"
  }] : [];
}
function diagnosticKey(pkgDir, diagnostic) {
  return [
    diagnostic.kind,
    diagnostic.platform ?? "",
    diagnostic.target ?? "",
    diagnostic.exportPath ?? "",
    diagnostic.file != null && diagnostic.file.length > 0 ? relativePath3(pkgDir, diagnostic.file) : "",
    diagnostic.detail ?? ""
  ].join("\0");
}
function dedupeDiagnostics(pkgDir, diagnostics) {
  const seen = /* @__PURE__ */ new Set();
  const result = [];
  for (const diagnostic of diagnostics) {
    const key = diagnosticKey(pkgDir, diagnostic);
    if (seen.has(key)) continue;
    seen.add(key);
    result.push(diagnostic);
  }
  return result;
}
function renderDiagnostic(pkgDir, diagnostic) {
  switch (diagnostic.kind) {
    case "condition-target-mismatch": {
      return `Publint [platform]: ${diagnostic.detail} at ${diagnostic.exportPath} (${diagnostic.target})`;
    }
    case "node-builtin-import": {
      const file = relativePath3(pkgDir, diagnostic.file ?? "");
      return `Publint [platform]: ${diagnostic.platform} export ${diagnostic.target} imports Node builtin "${diagnostic.detail}" in ${file} (${diagnostic.exportPath})`;
    }
    case "node-ambient-type": {
      const file = relativePath3(pkgDir, diagnostic.file ?? "");
      return `Publint [platform]: ${diagnostic.platform} declaration ${diagnostic.target} exposes Node ambient "${diagnostic.detail}" in ${file} (${diagnostic.exportPath})`;
    }
    case "tsconfig-node-types": {
      return 'Publint [platform]: non-node package tsconfig.json should not include compilerOptions.types "node"';
    }
  }
}
function checkPlatform(pkgDir, pkg) {
  const result = emptyResult2();
  const targets = collectExportTargets(pkg.exports);
  const diagnostics = [...collectConditionTargetDiagnostics(targets)];
  for (const target of targets) {
    const platform = shouldScanForNodeUsage(target);
    if (!platform) continue;
    for (const file of resolveExportTargetFiles(pkgDir, target.target)) {
      diagnostics.push(...scanFileGraph(file, platform, target));
    }
  }
  diagnostics.push(...collectTsconfigDiagnostics(pkgDir, pkg, targets));
  for (const diagnostic of dedupeDiagnostics(pkgDir, diagnostics)) {
    console.error(chalk14.red(renderDiagnostic(pkgDir, diagnostic)));
    result.errors++;
  }
  return result;
}

// src/actions/package/publint.ts
var removeSourceFromExports = (exports) => {
  let removed = false;
  for (const [key, value] of Object.entries(exports)) {
    if (key === "source") {
      delete exports[key];
      removed = true;
    } else if (typeof value === "object" && value !== null && removeSourceFromExports(value)) removed = true;
  }
  return removed;
};
var hasSourceInExports = (exports) => {
  for (const [key, value] of Object.entries(exports)) {
    if (key === "source") return true;
    if (typeof value === "object" && value !== null && hasSourceInExports(value)) return true;
  }
  return false;
};
var hasImportKeyInExports = (exports) => {
  for (const [key, value] of Object.entries(exports)) {
    if (key === "import" && typeof value === "string" && value.endsWith(".mjs")) return true;
    if (typeof value === "object" && value !== null && hasImportKeyInExports(value)) return true;
  }
  return false;
};
var replaceImportWithDefault = (exports) => {
  let modified = false;
  for (const [key, value] of Object.entries(exports)) {
    if (key === "import" && typeof value === "string" && value.endsWith(".mjs")) {
      if (exports.default === void 0) {
        exports.default = value;
      }
      delete exports.import;
      if (exports.types === void 0) {
        exports.types = value.replace(/\.mjs$/, ".d.ts");
      }
      modified = true;
    } else if (typeof value === "object" && value !== null && replaceImportWithDefault(value)) modified = true;
  }
  return modified;
};
var hasTypesInExports = (exports) => {
  for (const [key, value] of Object.entries(exports)) {
    if (key === "types") return true;
    if (typeof value === "object" && value !== null && hasTypesInExports(value)) return true;
  }
  return false;
};
function ensureExportsPath(value) {
  if (value.startsWith("./") || value.startsWith("../")) return value;
  return `./${value}`;
}
function emptyCustomResult() {
  return {
    errors: 0,
    modified: false,
    warnings: 0
  };
}
function mergeResults(target, source) {
  target.errors += source.errors;
  target.warnings += source.warnings;
  target.modified = target.modified || source.modified;
}
function checkFiles(pkg, fix) {
  const result = emptyCustomResult();
  const files = pkg.files;
  if (files === void 0) {
    console.warn(chalk15.yellow('Publint [custom]: "files" field is missing'));
    result.warnings++;
  }
  if (Array.isArray(files) && !files.includes("README.md")) {
    files.push("README.md");
    console.warn(chalk15.yellow('Publint [custom]: added "README.md" to "files"'));
    result.modified = true;
    result.warnings++;
  }
  if (Array.isArray(files) && files.includes("src")) {
    if (fix) {
      pkg.files = files.filter((f) => f !== "src");
      console.warn(chalk15.yellow('Publint [custom]: removed "src" from "files"'));
      result.modified = true;
    } else {
      console.warn(chalk15.yellow('Publint [custom]: "src" should not be in "files" (use --fix to remove)'));
    }
    result.warnings++;
  }
  return result;
}
function checkExportsSource(pkg, fix) {
  const result = emptyCustomResult();
  const exports = pkg.exports;
  if (exports && typeof exports === "object" && hasSourceInExports(exports)) {
    if (fix) {
      removeSourceFromExports(exports);
      console.warn(chalk15.yellow('Publint [custom]: removed "source" entries from "exports"'));
      result.modified = true;
    } else {
      console.warn(chalk15.yellow('Publint [custom]: "source" entries should not be in "exports" (use --fix to remove)'));
    }
    result.warnings++;
  }
  return result;
}
function migrateFieldToExports(pkg, field, exportKey, fix) {
  const result = emptyCustomResult();
  if (pkg[field] === void 0) return result;
  const fieldValue = pkg[field];
  if (!fieldValue.endsWith(".js") && !fieldValue.endsWith(".mjs") && !fieldValue.endsWith(".cjs")) return result;
  if (fix) {
    const exportValue = ensureExportsPath(fieldValue);
    const exports = pkg.exports;
    if (exports && typeof exports === "object") {
      const dot = exports["."];
      if (dot != null && typeof dot === "object" && !Object.hasOwn(dot, exportKey)) {
        dot[exportKey] = exportValue;
        console.warn(chalk15.yellow(`Publint [custom]: migrated "${field}" to "exports['.'].${exportKey}" (${fieldValue})`));
      }
    } else if (pkg.exports == null) {
      pkg.exports = { ".": { [exportKey]: exportValue } };
      console.warn(chalk15.yellow(`Publint [custom]: migrated "${field}" to "exports" (.\u2192${fieldValue})`));
    }
    delete pkg[field];
    console.warn(chalk15.yellow(`Publint [custom]: removed deprecated "${field}" field`));
    result.modified = true;
  } else {
    console.warn(chalk15.yellow(`Publint [custom]: "${field}" field is deprecated, use "exports" instead (use --fix to remove)`));
  }
  result.warnings++;
  return result;
}
function checkSideEffects(pkg) {
  const result = emptyCustomResult();
  if (pkg.sideEffects !== false) {
    console.warn(chalk15.yellow('Publint [custom]: "sideEffects" field should be set to false'));
    result.warnings++;
  }
  return result;
}
function checkRootSource(pkg, fix) {
  const result = emptyCustomResult();
  for (const field of ["source", "src"]) {
    if (pkg[field] !== void 0) {
      if (fix) {
        delete pkg[field];
        console.warn(chalk15.yellow(`Publint [custom]: removed root-level "${field}" field`));
        result.modified = true;
      } else {
        console.warn(chalk15.yellow(`Publint [custom]: root-level "${field}" field should not be in package.json (use --fix to remove)`));
      }
      result.warnings++;
    }
  }
  return result;
}
function checkResolutions(pkg) {
  const result = emptyCustomResult();
  if (pkg.resolutions !== void 0) {
    console.warn(chalk15.yellow('Publint [custom]: "resolutions" in use'));
    console.warn(chalk15.gray(JSON.stringify(pkg.resolutions, null, 2)));
    result.warnings++;
  }
  return result;
}
function checkImportToDefault(pkg, fix) {
  const result = emptyCustomResult();
  const exports = pkg.exports;
  if (!exports || typeof exports !== "object") return result;
  if (!hasImportKeyInExports(exports)) return result;
  if (fix) {
    replaceImportWithDefault(exports);
    console.warn(chalk15.yellow('Publint [custom]: renamed "import" to "default" in "exports" and ensured "types" siblings'));
    result.modified = true;
  } else {
    console.warn(chalk15.yellow('Publint [custom]: "import" entries in "exports" should use "default" instead (use --fix to rename)'));
  }
  result.warnings++;
  return result;
}
function checkRootTypes(pkg, fix) {
  const result = emptyCustomResult();
  if (pkg.types === void 0) return result;
  const exports = pkg.exports;
  if (!exports || typeof exports !== "object" || !hasTypesInExports(exports)) return result;
  if (fix) {
    delete pkg.types;
    console.warn(chalk15.yellow('Publint [custom]: removed redundant root "types" field (already defined in "exports")'));
    result.modified = true;
  } else {
    console.warn(chalk15.yellow('Publint [custom]: root "types" field is redundant when "exports" defines types (use --fix to remove)'));
  }
  result.warnings++;
  return result;
}
function customPubLint(pkgDir, pkg, fix = false, exclude = /* @__PURE__ */ new Set()) {
  const result = emptyCustomResult();
  if (!exclude.has("files")) mergeResults(result, checkFiles(pkg, fix));
  if (!exclude.has("source")) mergeResults(result, checkExportsSource(pkg, fix));
  if (!exclude.has("rootSource")) mergeResults(result, checkRootSource(pkg, fix));
  if (!exclude.has("main")) mergeResults(result, migrateFieldToExports(pkg, "main", "default", fix));
  if (!exclude.has("types")) mergeResults(result, migrateFieldToExports(pkg, "types", "types", fix));
  if (!exclude.has("module")) mergeResults(result, migrateFieldToExports(pkg, "module", "default", fix));
  if (!exclude.has("importToDefault")) mergeResults(result, checkImportToDefault(pkg, fix));
  if (!exclude.has("rootTypes")) mergeResults(result, checkRootTypes(pkg, fix));
  if (!exclude.has("platform")) mergeResults(result, checkPlatform(pkgDir, pkg));
  if (!exclude.has("sideEffects")) mergeResults(result, checkSideEffects(pkg));
  if (!exclude.has("resolutions")) mergeResults(result, checkResolutions(pkg));
  return [result.errors, result.warnings, result.modified];
}
var ALWAYS_INCLUDED_PATTERNS = [
  "package.json",
  "README",
  "README.*",
  "LICENCE",
  "LICENCE.*",
  "LICENSE",
  "LICENSE.*",
  "CHANGELOG",
  "CHANGELOG.*"
];
async function resolvePackFiles(pkgDir, filesField) {
  const patterns = [...ALWAYS_INCLUDED_PATTERNS];
  if (filesField) {
    for (const pattern of filesField) {
      if (pattern.startsWith("!")) {
        patterns.push(pattern);
      } else {
        patterns.push(pattern, `${pattern}/**`);
      }
    }
  }
  const matched = await glob3(patterns, {
    cwd: pkgDir,
    nodir: true,
    dot: false
  });
  const files = await Promise.all(
    matched.map(async (rel) => {
      const abs = path11.join(pkgDir, rel);
      let data = "";
      try {
        data = await fs6.readFile(abs, "utf8");
      } catch {
      }
      return { name: path11.join(pkgDir, rel), data };
    })
  );
  return files;
}
function logPublintMessage(message, pkg, formatMessage) {
  const formatted = `[${message.code}] ${formatMessage(message, pkg) ?? message.code}`;
  switch (message.type) {
    case "error": {
      console.error(chalk15.red(formatted));
      return;
    }
    case "warning": {
      console.warn(chalk15.yellow(formatted));
      return;
    }
    default: {
      console.log(chalk15.white(formatted));
    }
  }
}
async function runPublintLibrary(pkgDir, pkg, strict, pack) {
  const { publint } = await import("publint");
  let packOption = false;
  if (pack) {
    const files = await resolvePackFiles(pkgDir, pkg.files);
    packOption = { files };
  }
  const { messages } = await publint({
    level: "suggestion",
    pack: packOption,
    pkgDir,
    strict
  });
  const { formatMessage } = await import("publint/utils");
  for (const message of messages) {
    logPublintMessage(message, pkg, formatMessage);
  }
  return {
    errors: messages.filter((message) => message.type === "error").length,
    warnings: messages.filter((message) => message.type !== "error").length
  };
}
var packagePublintDetailed = async ({
  exclude = /* @__PURE__ */ new Set(),
  fix = false,
  pack = true,
  pkgDir: pkgDirParam,
  strict = isStrictMode(),
  verbose: _verbose = false
} = {}) => {
  const pkgDir = path11.resolve(pkgDirParam ?? INIT_CWD());
  const sortedPkg = sortPackageJson(await fs6.readFile(`${pkgDir}/package.json`, "utf8"));
  await fs6.writeFile(`${pkgDir}/package.json`, sortedPkg);
  const pkg = JSON.parse(await fs6.readFile(`${pkgDir}/package.json`, "utf8"));
  const effectiveExclude = Boolean(pkg.private) ? /* @__PURE__ */ new Set([...exclude, ...PUBLISH_ONLY_CHECKS]) : exclude;
  console.log(chalk15.green(`Publint: ${String(pkg.name)}${Boolean(pkg.private) ? chalk15.gray(" (private)") : ""}`));
  console.log(chalk15.gray(pkgDir));
  let libraryErrors = 0;
  let libraryWarnings = 0;
  if (!effectiveExclude.has("publint")) {
    const library = await runPublintLibrary(pkgDir, pkg, strict, pack);
    libraryErrors = library.errors;
    libraryWarnings = library.warnings;
  }
  const [errorCount, warningCount, modified] = customPubLint(pkgDir, pkg, fix, effectiveExclude);
  if (modified) {
    const sorted = sortPackageJson(JSON.stringify(pkg, null, 2));
    await fs6.writeFile(`${pkgDir}/package.json`, sorted);
  }
  const compileTargets = effectiveExclude.has("compileTargets") ? { errors: 0, warnings: 0 } : checkCompileTargets(pkgDir, fix);
  return {
    errors: libraryErrors + errorCount + compileTargets.errors,
    warnings: libraryWarnings + warningCount + compileTargets.warnings
  };
};
var packagePublint = async (params = {}) => {
  const result = await packagePublintDetailed(params);
  return result.errors + (isStrictMode() ? result.warnings : 0);
};

// src/actions/package/recompile.ts
var packageRecompile = async (config2 = {}) => {
  return packageClean() || await packageCompile(config2);
};
export {
  ALL_PUBLINT_CHECKS,
  PUBLISH_ONLY_CHECKS,
  createEsbuildEntryPoints,
  createTypescriptConfig,
  esbuildOptions,
  packageClean,
  packageCleanOutputs,
  packageCleanTypescript,
  packageCompile,
  packageCompileEsbuild,
  packageCompileTsc,
  packageCompileTsup,
  packageCopyAssets,
  packageLint,
  packagePublint,
  packagePublintDetailed,
  packageRecompile,
  resolveCompilePlatforms,
  resolvePlatformEsbuildOptions,
  resolvePlatformRecord,
  tsupOptions,
  validateFullPackage,
  warnDeprecatedEmptyObjectTargets
};
//# sourceMappingURL=index.mjs.map
