import { Command } from "commander";
import { loadPackageJson, Package } from "./lib/utils.js";
import chalk from "chalk";
import { oneoffContext } from "./lib/context.js";

export const update = new Command("update")
  .description("Print instructions to update the convex package")
  .action(async () => {
    const ctx = oneoffContext;
    let updateInstructions = "npm install convex@latest\n";
    const oldPackages = (await loadPackageJson(ctx)).filter((elt: Package) =>
      elt.name.startsWith("@convex-dev")
    );
    for (const pkg of oldPackages) {
      updateInstructions += `npm uninstall ${pkg.name}\n`;
    }

    console.log(
      chalk.green(
        `To view the Convex changelog, go to https://blog.convex.dev/tag/releases/\nWhen you are ready to upgrade, run the following commands:\n${updateInstructions}`
      )
    );
  });
