import path from "path";
import fs from "fs";

export function renamePackageJsonName(appPath: string, projectName: string) {
  const packageJsonPath = path.join(appPath, "package.json");

  if (fs.existsSync(packageJsonPath)) {
    try {
      const packageData = JSON.parse(
        fs.readFileSync(packageJsonPath, "utf8")
      ) as { name: string };
      packageData.name = projectName;

      fs.writeFileSync(
        packageJsonPath,
        JSON.stringify(packageData, null, 2),
        "utf8"
      );

      console.log(`package.json name updated to "${projectName}".`);
    } catch (error) {
      console.error("Failed to update package.json:", error);
    }
  } else {
    console.warn("package.json not found, skipping rename.");
  }
}