const fs = require("fs");
const { bold, green } = require("kolorist");

module.exports = {
  parseCommand(packageManager, scriptName) {
    if (scriptName === "install") {
      return packageManager === "yarn" ? "yarn" : `${packageManager} install`;
    }

    return packageManager === "npm"
      ? `npm run ${scriptName}`
      : `${packageManager} ${scriptName}`;
  },

  printHelpContent() {
    console.log(`${bold(green("Command options"))}:`);
    console.log(
      `${bold(green("-n"))}, ${bold(
        green("--Folder-name") + "=_folderName_",
      )}\tProject folder name`,
    );
    console.log(
      `${bold(green("-p"))}, ${bold(
        green("--plugin-name") + "=_pluginName_",
      )}\tPlugin name`,
    );
    console.log(
      `${bold(green("-f"))}, ${bold(
        green("--force-overwrite"),
      )}\t\tOverwrite folder content (if exists)`,
    );
    console.log(
        `${bold(green("-s"))}, ${bold(
            green("--sample"),
        )}\t\t\tCopy all sample code`,
    );
    console.log(
      `${bold(green("-h"))}, ${bold(
        green("--help"),
      )}\t\t\tPrint node command line options`,
    );
  },

  patchPackageFile(packageName, sourceFolder, targetFolder) {
    const oldContent = JSON.parse(fs.readFileSync(sourceFolder, "utf8"));
    oldContent.name = packageName;
    fs.writeFileSync(targetFolder, JSON.stringify(oldContent, undefined, 2));
  },
};