diff --git a/src/makePatch.ts b/src/makePatch.ts index 40349f7e..f6bf2ba8 100644 --- a/src/makePatch.ts +++ b/src/makePatch.ts @@ -40,6 +40,7 @@ import { PatchState, savePatchApplicationState, STATE_FILE_NAME, + verifyAppliedPatches, } from "./stateFile" function printNoPackageFoundError( @@ -92,8 +93,10 @@ export function makePatch({ mode = { type: "append", name: "initial" } } - // TODO: verify applied patch hashes - // TODO: handle case where rebase appending and the name is the same as the next one in the sequence + if (isRebasing && state) { + verifyAppliedPatches({ appPath, patchDir, state }) + } + if ( mode.type === "overwrite_last" && isRebasing && @@ -424,12 +427,8 @@ export function makePatch({ // scoped package mkdirSync(dirname(patchPath)) } - writeFileSync(patchPath, diffResult.stdout) - console.log( - `${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}\n`, - ) - // if we inserted a new patch into a sequence we may need to update the sequence numbers + // if we are inserting a new patch into a sequence we most likely need to update the sequence numbers if (isRebasing && mode.type === "append") { const patchesToNudge = existingPatches.slice(state!.patches.length) if (sequenceNumber === undefined) { @@ -460,6 +459,11 @@ export function makePatch({ } } + writeFileSync(patchPath, diffResult.stdout) + console.log( + `${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}\n`, + ) + const prevState: PatchState[] = patchesToApplyBeforeDiffing.map( (p): PatchState => ({ patchFilename: p.patchFilename, diff --git a/src/rebase.ts b/src/rebase.ts index 8dc78303..75ae56b7 100644 --- a/src/rebase.ts +++ b/src/rebase.ts @@ -1,5 +1,4 @@ import chalk from "chalk" -import { existsSync } from "fs" import { join, resolve } from "path" import { applyPatch } from "./applyPatches" import { hashFile } from "./hash" @@ -8,6 +7,7 @@ import { getGroupedPatches } from "./patchFs" import { getPatchApplicationState, savePatchApplicationState, + verifyAppliedPatches, } from "./stateFile" export function rebase({ @@ -76,28 +76,7 @@ export function rebase({ ) } // check hashes - for (let i = 0; i < state.patches.length; i++) { - const patch = state.patches[i] - const fullPatchPath = join( - patchesDirectory, - packagePatches[i].patchFilename, - ) - if (!existsSync(fullPatchPath)) { - console.log( - chalk.blueBright("Expected patch file"), - fullPatchPath, - "to exist but it is missing. Try completely reinstalling node_modules first.", - ) - process.exit(1) - } - if (patch.patchContentHash !== hashFile(fullPatchPath)) { - console.log( - chalk.blueBright("Patch file"), - fullPatchPath, - "has changed since it was applied. Try completely reinstalling node_modules first.", - ) - } - } + verifyAppliedPatches({ appPath, patchDir, state }) if (targetPatch === "0") { // unapply all diff --git a/src/stateFile.ts b/src/stateFile.ts index 422cdf80..7aea576d 100644 --- a/src/stateFile.ts +++ b/src/stateFile.ts @@ -1,7 +1,9 @@ -import { readFileSync, unlinkSync, writeFileSync } from "fs" +import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs" import { join } from "path" import { PackageDetails } from "./PackageDetails" import stringify from "json-stable-stringify" +import { hashFile } from "./hash" +import chalk from "chalk" export interface PatchState { patchFilename: string patchContentHash: string @@ -69,3 +71,37 @@ export function clearPatchApplicationState(packageDetails: PackageDetails) { // noop } } + +export function verifyAppliedPatches({ + appPath, + patchDir, + state, +}: { + appPath: string + patchDir: string + state: PatchApplicationState +}) { + const patchesDirectory = join(appPath, patchDir) + for (const patch of state.patches) { + if (!patch.didApply) { + break + } + const fullPatchPath = join(patchesDirectory, patch.patchFilename) + if (!existsSync(fullPatchPath)) { + console.log( + chalk.blueBright("Expected patch file"), + fullPatchPath, + "to exist but it is missing. Try removing and reinstalling node_modules first.", + ) + process.exit(1) + } + if (patch.patchContentHash !== hashFile(fullPatchPath)) { + console.log( + chalk.blueBright("Patch file"), + fullPatchPath, + "has changed since it was applied. Try removing and reinstalling node_modules first.", + ) + process.exit(1) + } + } +}