// src/lib/updo/applyUpdates.ts
import { readFileSync, writeFileSync } from "fs";
import PATH from "path";
import chalk from "chalk";
function applyUpdates(cwd, workspaces, updates) {
  const updateMap = new Map(updates.map((u) => [u.name, u]));
  let modified = 0;
  for (const ws of workspaces) {
    const pkgPath = PATH.resolve(cwd, ws.location, "package.json");
    let content;
    try {
      content = readFileSync(pkgPath, "utf8");
    } catch {
      continue;
    }
    const pkg = JSON.parse(content);
    let changed = false;
    for (const depField of ["dependencies", "devDependencies", "peerDependencies"]) {
      const deps = pkg[depField];
      if (!deps) continue;
      for (const [name, currentRange] of Object.entries(deps)) {
        const update = updateMap.get(name);
        if (!update) continue;
        const prefix = /^([~^]|>=?|<=?)/.exec(currentRange)?.[0] ?? "~";
        const newRange = `${prefix}${update.targetVersion}`;
        if (deps[name] !== newRange) {
          deps[name] = newRange;
          changed = true;
        }
      }
    }
    if (changed) {
      writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}
`);
      modified++;
      console.log(chalk.gray(`  Updated ${ws.name}`));
    }
  }
  return modified;
}
export {
  applyUpdates
};
//# sourceMappingURL=applyUpdates.mjs.map