// src/lib/generateIgnoreFiles.ts
import chalk from "chalk";

// src/pm/workspaces.ts
import { existsSync, readFileSync } from "node:fs";
import PATH from "node:path";
import { globSync } from "glob";
function readJsonFile(path) {
  if (!existsSync(path)) return void 0;
  try {
    return JSON.parse(readFileSync(path, "utf8"));
  } catch {
    return void 0;
  }
}
function readRootPackageJson(cwd = process.cwd()) {
  return readJsonFile(PATH.resolve(cwd, "package.json"));
}
function detectPackageManagerFromPackageJson(cwd = process.cwd()) {
  const packageManager = readRootPackageJson(cwd)?.packageManager;
  if (!packageManager) return void 0;
  return packageManager.split("@")[0];
}

// src/pm/detectPackageManager.ts
import { existsSync as existsSync2 } from "node:fs";
var knownPackageManagers = /* @__PURE__ */ new Set(["bun", "npm", "pnpm", "yarn"]);
function detectPackageManager() {
  const packageManager = detectPackageManagerFromPackageJson();
  if (packageManager && knownPackageManagers.has(packageManager)) {
    return packageManager;
  }
  if (existsSync2("pnpm-lock.yaml") || existsSync2("pnpm-workspace.yaml")) return "pnpm";
  if (existsSync2("bun.lock") || existsSync2("bun.lockb")) return "bun";
  if (existsSync2("yarn.lock") || existsSync2(".yarnrc.yml")) return "yarn";
  if (existsSync2("package-lock.json") || existsSync2("npm-shrinkwrap.json")) return "npm";
  return "yarn";
}

// src/pm/registry.ts
var implementations = /* @__PURE__ */ new Map();
function getPackageManager(name) {
  const pmName = name ?? detectPackageManager();
  const pm = implementations.get(pmName);
  if (!pm) {
    throw new Error(
      `No package manager implementation registered for "${pmName}". Ensure registerPackageManager() has been called before getPackageManager().`
    );
  }
  return pm;
}

// src/lib/file/constants.ts
var WINDOWS_NEWLINE_REGEX = /\r\n/g;
var CROSS_PLATFORM_NEWLINE = "\n";

// src/lib/file/fileLines.ts
import {
  existsSync as existsSync3,
  readFileSync as readFileSync2,
  writeFileSync
} from "node:fs";

// src/lib/string/empty.ts
var empty = (value) => value?.trim().length === 0;
var notEmpty = (value) => !empty(value);

// src/lib/string/union.ts
var union = (a, b) => /* @__PURE__ */ new Set([...new Set(a), ...new Set(b)]);

// src/lib/file/ReadFileSyncOptions.ts
var defaultReadFileSyncOptions = { encoding: "utf8" };

// src/lib/file/fileLines.ts
var readLines = (uri, options = defaultReadFileSyncOptions) => existsSync3(uri) ? readFileSync2(uri, options).replace(WINDOWS_NEWLINE_REGEX, CROSS_PLATFORM_NEWLINE).split(CROSS_PLATFORM_NEWLINE) : [];
var readNonEmptyLines = (uri, options = defaultReadFileSyncOptions) => readLines(uri, options).filter(notEmpty);
var writeLines = (uri, lines, options = defaultReadFileSyncOptions) => {
  const existing = existsSync3(uri) ? readFileSync2(uri, options) : void 0;
  const desired = lines.join(CROSS_PLATFORM_NEWLINE);
  if (existing !== desired) writeFileSync(uri, desired, options);
};

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

// src/lib/generateIgnoreFiles.ts
var localeCompare = (a, b) => a.localeCompare(b);
var mergeEntries = (a, b) => [...union(a, b)].toSorted(localeCompare);
var generateIgnoreFiles = (filename, pkg) => {
  console.log(chalk.green(`Generate ${filename} Files`));
  const cwd = INIT_CWD();
  const pm = getPackageManager();
  const singleWorkspace = pkg ? pm.findWorkspace(pkg) : void 0;
  const workspaces = singleWorkspace ? [singleWorkspace] : pm.listWorkspaces();
  const readEntries = (location) => readNonEmptyLines(`${location}/${filename}`);
  const writeEntries = (location, entries) => writeLines(`${location}/${filename}`, entries);
  const results = workspaces.map(({ location, name }) => {
    try {
      writeEntries(location, mergeEntries(readEntries(cwd), readEntries(location)));
      return 0;
    } catch (ex) {
      const error = ex;
      console.error(`Generate ${filename} Files [${name}] [${error.message}]`);
      return 1;
    }
  });
  const succeeded = results.every((result) => result === 0);
  return succeeded ? 0 : 1;
};
export {
  generateIgnoreFiles
};
//# sourceMappingURL=generateIgnoreFiles.mjs.map
