package.dist.stateFile.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patch-package Show documentation
Show all versions of patch-package Show documentation
Fix broken node modules with no fuss
The newest version!
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyAppliedPatches = exports.clearPatchApplicationState = exports.savePatchApplicationState = exports.getPatchApplicationState = exports.STATE_FILE_NAME = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
const hash_1 = require("./hash");
const chalk_1 = __importDefault(require("chalk"));
const version = 1;
exports.STATE_FILE_NAME = ".patch-package.json";
function getPatchApplicationState(packageDetails) {
const fileName = path_1.join(packageDetails.path, exports.STATE_FILE_NAME);
let state = null;
try {
state = JSON.parse(fs_1.readFileSync(fileName, "utf8"));
}
catch (e) {
// noop
}
if (!state) {
return null;
}
if (state.version !== version) {
console.log(`You upgraded patch-package and need to fully reinstall node_modules to continue.`);
process.exit(1);
}
return state;
}
exports.getPatchApplicationState = getPatchApplicationState;
function savePatchApplicationState({ packageDetails, patches, isRebasing, }) {
const fileName = path_1.join(packageDetails.path, exports.STATE_FILE_NAME);
const state = {
patches,
version,
isRebasing,
};
fs_1.writeFileSync(fileName, json_stable_stringify_1.default(state, { space: 4 }), "utf8");
}
exports.savePatchApplicationState = savePatchApplicationState;
function clearPatchApplicationState(packageDetails) {
const fileName = path_1.join(packageDetails.path, exports.STATE_FILE_NAME);
try {
fs_1.unlinkSync(fileName);
}
catch (e) {
// noop
}
}
exports.clearPatchApplicationState = clearPatchApplicationState;
function verifyAppliedPatches({ appPath, patchDir, state, }) {
const patchesDirectory = path_1.join(appPath, patchDir);
for (const patch of state.patches) {
if (!patch.didApply) {
break;
}
const fullPatchPath = path_1.join(patchesDirectory, patch.patchFilename);
if (!fs_1.existsSync(fullPatchPath)) {
console.log(chalk_1.default.blueBright("Expected patch file"), fullPatchPath, "to exist but it is missing. Try removing and reinstalling node_modules first.");
process.exit(1);
}
if (patch.patchContentHash !== hash_1.hashFile(fullPatchPath)) {
console.log(chalk_1.default.blueBright("Patch file"), fullPatchPath, "has changed since it was applied. Try removing and reinstalling node_modules first.");
process.exit(1);
}
}
}
exports.verifyAppliedPatches = verifyAppliedPatches;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RhdGVGaWxlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3N0YXRlRmlsZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQSwyQkFBd0U7QUFDeEUsK0JBQTJCO0FBRTNCLGtGQUE2QztBQUM3QyxpQ0FBaUM7QUFDakMsa0RBQXlCO0FBT3pCLE1BQU0sT0FBTyxHQUFHLENBQUMsQ0FBQTtBQU9KLFFBQUEsZUFBZSxHQUFHLHFCQUFxQixDQUFBO0FBRXBELFNBQWdCLHdCQUF3QixDQUN0QyxjQUE4QjtJQUU5QixNQUFNLFFBQVEsR0FBRyxXQUFJLENBQUMsY0FBYyxDQUFDLElBQUksRUFBRSx1QkFBZSxDQUFDLENBQUE7SUFFM0QsSUFBSSxLQUFLLEdBQWlDLElBQUksQ0FBQTtJQUM5QyxJQUFJO1FBQ0YsS0FBSyxHQUFHLElBQUksQ0FBQyxLQUFLLENBQUMsaUJBQVksQ0FBQyxRQUFRLEVBQUUsTUFBTSxDQUFDLENBQUMsQ0FBQTtLQUNuRDtJQUFDLE9BQU8sQ0FBQyxFQUFFO1FBQ1YsT0FBTztLQUNSO0lBQ0QsSUFBSSxDQUFDLEtBQUssRUFBRTtRQUNWLE9BQU8sSUFBSSxDQUFBO0tBQ1o7SUFDRCxJQUFJLEtBQUssQ0FBQyxPQUFPLEtBQUssT0FBTyxFQUFFO1FBQzdCLE9BQU8sQ0FBQyxHQUFHLENBQ1Qsa0ZBQWtGLENBQ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