|
| 1 | +import chalk from "chalk" |
| 2 | +import { existsSync } from "fs" |
| 3 | +import { join, resolve } from "path" |
| 4 | +import { applyPatch } from "./applyPatches" |
| 5 | +import { hashFile } from "./hash" |
| 6 | +import { PatchedPackageDetails } from "./PackageDetails" |
| 7 | +import { getGroupedPatches } from "./patchFs" |
| 8 | +import { |
| 9 | + getPatchApplicationState, |
| 10 | + savePatchApplicationState, |
| 11 | +} from "./stateFile" |
| 12 | + |
| 13 | +export function rebase({ |
| 14 | + appPath, |
| 15 | + patchDir, |
| 16 | + packagePathSpecifier, |
| 17 | + targetPatch, |
| 18 | +}: { |
| 19 | + appPath: string |
| 20 | + patchDir: string |
| 21 | + packagePathSpecifier: string |
| 22 | + targetPatch: string |
| 23 | +}): void { |
| 24 | + const patchesDirectory = join(appPath, patchDir) |
| 25 | + const groupedPatches = getGroupedPatches(patchesDirectory) |
| 26 | + |
| 27 | + if (groupedPatches.numPatchFiles === 0) { |
| 28 | + console.error(chalk.blueBright("No patch files found")) |
| 29 | + process.exit(1) |
| 30 | + } |
| 31 | + |
| 32 | + const packagePatches = |
| 33 | + groupedPatches.pathSpecifierToPatchFiles[packagePathSpecifier] |
| 34 | + if (!packagePatches) { |
| 35 | + console.error( |
| 36 | + chalk.blueBright("No patch files found for package"), |
| 37 | + packagePathSpecifier, |
| 38 | + ) |
| 39 | + process.exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + const state = getPatchApplicationState(packagePatches[0]) |
| 43 | + |
| 44 | + if (!state) { |
| 45 | + console.error( |
| 46 | + chalk.blueBright("No patch state found"), |
| 47 | + "Did you forget to run", |
| 48 | + chalk.bold("patch-package"), |
| 49 | + "(without arguments) first?", |
| 50 | + ) |
| 51 | + process.exit(1) |
| 52 | + } |
| 53 | + if (state.isRebasing) { |
| 54 | + console.error( |
| 55 | + chalk.blueBright("Already rebasing"), |
| 56 | + "Make changes to the files in", |
| 57 | + chalk.bold(packagePatches[0].path), |
| 58 | + "and then run `patch-package", |
| 59 | + packagePathSpecifier, |
| 60 | + "--continue` to", |
| 61 | + packagePatches.length === state.patches.length |
| 62 | + ? "append a patch file" |
| 63 | + : `update the ${ |
| 64 | + packagePatches[packagePatches.length - 1].patchFilename |
| 65 | + } file`, |
| 66 | + ) |
| 67 | + console.error( |
| 68 | + `💡 To remove a broken patch file, delete it and reinstall node_modules`, |
| 69 | + ) |
| 70 | + process.exit(1) |
| 71 | + } |
| 72 | + if (state.patches.length !== packagePatches.length) { |
| 73 | + console.error( |
| 74 | + chalk.blueBright("Some patches have not been applied."), |
| 75 | + "Reinstall node_modules and try again.", |
| 76 | + ) |
| 77 | + } |
| 78 | + // check hashes |
| 79 | + for (let i = 0; i < state.patches.length; i++) { |
| 80 | + const patch = state.patches[i] |
| 81 | + const fullPatchPath = join( |
| 82 | + patchesDirectory, |
| 83 | + packagePatches[i].patchFilename, |
| 84 | + ) |
| 85 | + if (!existsSync(fullPatchPath)) { |
| 86 | + console.error( |
| 87 | + chalk.blueBright("Expected patch file"), |
| 88 | + fullPatchPath, |
| 89 | + "to exist but it is missing. Try completely reinstalling node_modules first.", |
| 90 | + ) |
| 91 | + process.exit(1) |
| 92 | + } |
| 93 | + if (patch.patchContentHash !== hashFile(fullPatchPath)) { |
| 94 | + console.error( |
| 95 | + chalk.blueBright("Patch file"), |
| 96 | + fullPatchPath, |
| 97 | + "has changed since it was applied. Try completely reinstalling node_modules first.", |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (targetPatch === "0") { |
| 103 | + // unapply all |
| 104 | + unApplyPatches({ |
| 105 | + patches: packagePatches, |
| 106 | + appPath, |
| 107 | + patchDir, |
| 108 | + }) |
| 109 | + savePatchApplicationState({ |
| 110 | + packageDetails: packagePatches[0], |
| 111 | + isRebasing: true, |
| 112 | + patches: [], |
| 113 | + }) |
| 114 | + console.log(` |
| 115 | +Make any changes you need inside ${chalk.bold(packagePatches[0].path)} |
| 116 | +
|
| 117 | +When you are done, run |
| 118 | +
|
| 119 | + ${chalk.bold( |
| 120 | + `patch-package ${packagePathSpecifier} --append 'MyChangeDescription'`, |
| 121 | + )} |
| 122 | + |
| 123 | +to insert a new patch file. |
| 124 | +`) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + // find target patch |
| 129 | + const target = packagePatches.find((p) => { |
| 130 | + if (p.patchFilename === targetPatch) { |
| 131 | + return true |
| 132 | + } |
| 133 | + if ( |
| 134 | + resolve(process.cwd(), targetPatch) === |
| 135 | + join(patchesDirectory, p.patchFilename) |
| 136 | + ) { |
| 137 | + return true |
| 138 | + } |
| 139 | + |
| 140 | + if (targetPatch === p.sequenceName) { |
| 141 | + return true |
| 142 | + } |
| 143 | + const n = Number(targetPatch.replace(/^0+/g, "")) |
| 144 | + if (!isNaN(n) && n === p.sequenceNumber) { |
| 145 | + return true |
| 146 | + } |
| 147 | + return false |
| 148 | + }) |
| 149 | + |
| 150 | + if (!target) { |
| 151 | + console.error( |
| 152 | + chalk.red("Could not find target patch file"), |
| 153 | + chalk.bold(targetPatch), |
| 154 | + ) |
| 155 | + console.error() |
| 156 | + console.error("The list of available patch files is:") |
| 157 | + packagePatches.forEach((p) => { |
| 158 | + console.error(` - ${p.patchFilename}`) |
| 159 | + }) |
| 160 | + |
| 161 | + process.exit(1) |
| 162 | + } |
| 163 | + const currentHash = hashFile(join(patchesDirectory, target.patchFilename)) |
| 164 | + |
| 165 | + const prevApplication = state.patches.find( |
| 166 | + (p) => p.patchContentHash === currentHash, |
| 167 | + ) |
| 168 | + if (!prevApplication) { |
| 169 | + console.error( |
| 170 | + chalk.red("Could not find previous application of patch file"), |
| 171 | + chalk.bold(target.patchFilename), |
| 172 | + ) |
| 173 | + console.error() |
| 174 | + console.error("You should reinstall node_modules and try again.") |
| 175 | + process.exit(1) |
| 176 | + } |
| 177 | + |
| 178 | + // ok, we are good to start undoing all the patches that were applied up to but not including the target patch |
| 179 | + const targetIdx = state.patches.indexOf(prevApplication) |
| 180 | + |
| 181 | + unApplyPatches({ |
| 182 | + patches: packagePatches.slice(targetIdx + 1), |
| 183 | + appPath, |
| 184 | + patchDir, |
| 185 | + }) |
| 186 | + savePatchApplicationState({ |
| 187 | + packageDetails: packagePatches[0], |
| 188 | + isRebasing: true, |
| 189 | + patches: packagePatches.slice(0, targetIdx + 1).map((p) => ({ |
| 190 | + patchFilename: p.patchFilename, |
| 191 | + patchContentHash: hashFile(join(patchesDirectory, p.patchFilename)), |
| 192 | + didApply: true, |
| 193 | + })), |
| 194 | + }) |
| 195 | + |
| 196 | + console.log(` |
| 197 | +Make any changes you need inside ${chalk.bold(packagePatches[0].path)} |
| 198 | +
|
| 199 | +When you are done, do one of the following: |
| 200 | +
|
| 201 | + To update ${chalk.bold(packagePatches[targetIdx].patchFilename)} run |
| 202 | +
|
| 203 | + ${chalk.bold(`patch-package ${packagePathSpecifier}`)} |
| 204 | + |
| 205 | + To create a new patch file after ${chalk.bold( |
| 206 | + packagePatches[targetIdx].patchFilename, |
| 207 | + )} run |
| 208 | + |
| 209 | + ${chalk.bold( |
| 210 | + `patch-package ${packagePathSpecifier} --append 'MyChangeDescription'`, |
| 211 | + )} |
| 212 | +
|
| 213 | + `) |
| 214 | +} |
| 215 | + |
| 216 | +function unApplyPatches({ |
| 217 | + patches, |
| 218 | + appPath, |
| 219 | + patchDir, |
| 220 | +}: { |
| 221 | + patches: PatchedPackageDetails[] |
| 222 | + appPath: string |
| 223 | + patchDir: string |
| 224 | +}) { |
| 225 | + for (const patch of patches.slice().reverse()) { |
| 226 | + if ( |
| 227 | + !applyPatch({ |
| 228 | + patchFilePath: join(appPath, patchDir, patch.patchFilename) as string, |
| 229 | + reverse: true, |
| 230 | + patchDetails: patch, |
| 231 | + patchDir, |
| 232 | + cwd: process.cwd(), |
| 233 | + }) |
| 234 | + ) { |
| 235 | + console.error( |
| 236 | + chalk.red("Failed to un-apply patch file"), |
| 237 | + chalk.bold(patch.patchFilename), |
| 238 | + "Try completely reinstalling node_modules.", |
| 239 | + ) |
| 240 | + process.exit(1) |
| 241 | + } |
| 242 | + console.log(chalk.green("Un-applied patch file"), patch.patchFilename) |
| 243 | + } |
| 244 | +} |
0 commit comments