"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readPatch = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fs_extra_1 = require("fs-extra");
const path_1 = require("../path");
const path_2 = require("path");
const parse_1 = require("./parse");
function readPatch({ patchFilePath, packageDetails, patchDir, }) {
    try {
        return parse_1.parsePatchFile(fs_extra_1.readFileSync(patchFilePath).toString());
    }
    catch (e) {
        const fixupSteps = [];
        const relativePatchFilePath = path_2.normalize(path_1.relative(process.cwd(), patchFilePath));
        const patchBaseDir = relativePatchFilePath.slice(0, relativePatchFilePath.indexOf(patchDir));
        if (patchBaseDir) {
            fixupSteps.push(`cd ${patchBaseDir}`);
        }
        fixupSteps.push(`patch -p1 -i ${relativePatchFilePath.slice(relativePatchFilePath.indexOf(patchDir))}`);
        fixupSteps.push(`npx patch-package ${packageDetails.pathSpecifier}`);
        if (patchBaseDir) {
            fixupSteps.push(`cd ${path_1.relative(path_1.resolve(process.cwd(), patchBaseDir), process.cwd())}`);
        }
        console.error(`
${chalk_1.default.red.bold("**ERROR**")} ${chalk_1.default.red(`Failed to apply patch for package ${chalk_1.default.bold(packageDetails.humanReadablePathSpecifier)}`)}
    
  This happened because the patch file ${relativePatchFilePath} could not be parsed.
   
  If you just upgraded patch-package, you can try running:
  
    ${fixupSteps.join("\n    ")}
    
  Otherwise, try manually creating the patch file again.
  
  If the problem persists, please submit a bug report:
  
    https://github.com/ds300/patch-package/issues/new?title=Patch+file+parse+error&body=%3CPlease+attach+the+patch+file+in+question%3E

`);
        process.exit(1);
    }
    return [];
}
exports.readPatch = readPatch;
//# sourceMappingURL=data:application/json;base64,[encoded data, 4348 characters]