|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off -- Maintains the pinned SDK patch without loading a provider. |
| 2 | +import * as NodeCrypto from "node:crypto"; |
| 3 | +import * as NodeFSP from "node:fs/promises"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | + |
| 6 | +const originalGrepOutput = |
| 7 | + "matches:e.result.value.matches?.map((e=>({file:e.file,...void 0!==e.lineNumber?{lineNumber:e.lineNumber}:{},line:e.line,...e.beforeContext?{beforeContext:e.beforeContext}:{},...e.afterContext?{afterContext:e.afterContext}:{}})))??[],totalMatches:e.result.value.totalMatches??0"; |
| 8 | +const patchedGrepOutput = [ |
| 9 | + "matches:e.result.value.matches?.flatMap((e=>{", |
| 10 | + "if(!Array.isArray(e.matches))return[{file:e.file,...void 0!==e.lineNumber?{lineNumber:e.lineNumber}:{},line:e.line,...e.beforeContext?{beforeContext:e.beforeContext}:{},...e.afterContext?{afterContext:e.afterContext}:{}}];", |
| 11 | + "return e.matches.flatMap(((t,r,n)=>{if(t.isContextLine)return[];let s=r,i=r;for(;s>0&&n[s-1].isContextLine;)s--;for(;i+1<n.length&&n[i+1].isContextLine;)i++;", |
| 12 | + "const o=n.slice(s,r).map((e=>e.content)),a=n.slice(r+1,i+1).map((e=>e.content));", |
| 13 | + "return[{file:e.file,...void 0!==t.lineNumber?{lineNumber:t.lineNumber}:{},line:t.content,...o.length?{beforeContext:o}:{},...a.length?{afterContext:a}:{}}]}))", |
| 14 | + "}))??[],totalMatches:e.result.value.totalMatchedLines??e.result.value.totalMatches??0", |
| 15 | +].join(""); |
| 16 | + |
| 17 | +const originalSha256 = "c57599bbc8c9e18c55d8635c6c1621d746022254308b544ad04469bb9cf274ef"; |
| 18 | +const patchedSha256 = "43e958124dea446e7e00d15f7736276e923634a4c7e3a13f92fd1582f241d806"; |
| 19 | + |
| 20 | +function sha256(source: string): string { |
| 21 | + return NodeCrypto.createHash("sha256").update(source).digest("hex"); |
| 22 | +} |
| 23 | + |
| 24 | +function cursorGrepSource(source: string, target: "original" | "patched"): string { |
| 25 | + const hash = sha256(source); |
| 26 | + if (hash !== originalSha256 && hash !== patchedSha256) { |
| 27 | + throw new Error(`Unexpected @cursor/sdk 1.0.35 689.js hash: ${hash}`); |
| 28 | + } |
| 29 | + const expected = target === "original" ? originalSha256 : patchedSha256; |
| 30 | + if (hash === expected) return source; |
| 31 | + const [before, after] = |
| 32 | + target === "original" |
| 33 | + ? [patchedGrepOutput, originalGrepOutput] |
| 34 | + : [originalGrepOutput, patchedGrepOutput]; |
| 35 | + if (source.split(before).length !== 2) throw new Error("Ambiguous SDK grep patch anchor"); |
| 36 | + const result = source.replace(before, () => after); |
| 37 | + if (sha256(result) !== expected) throw new Error("Unexpected generated SDK grep hash"); |
| 38 | + return result; |
| 39 | +} |
| 40 | + |
| 41 | +// Regenerate with: node scripts/lib/cursor-grep-patch.ts generate <sdk-dir> <patch-file>. |
| 42 | +const [command, sdkDirectory, patchFile] = process.argv.slice(2); |
| 43 | +if (!sdkDirectory || (command !== "generate" && command !== "verify")) { |
| 44 | + throw new Error("Usage: cursor-grep-patch.ts generate <sdk-dir> <patch-file> | verify <sdk-dir>"); |
| 45 | +} |
| 46 | +const source = await NodeFSP.readFile(NodePath.join(sdkDirectory, "dist/cjs/689.js"), "utf8"); |
| 47 | +if (command === "verify") { |
| 48 | + if (patchFile !== undefined || sha256(source) !== patchedSha256) { |
| 49 | + throw new Error("The installed @cursor/sdk 1.0.35 does not contain the expected grep patch"); |
| 50 | + } |
| 51 | +} else { |
| 52 | + if (!patchFile) throw new Error("A destination patch file is required"); |
| 53 | + const original = cursorGrepSource(source, "original"); |
| 54 | + const patched = cursorGrepSource(source, "patched"); |
| 55 | + if (original.includes("\n") || patched.includes("\n")) { |
| 56 | + throw new Error("Expected one minified line in 689.js"); |
| 57 | + } |
| 58 | + await NodeFSP.writeFile( |
| 59 | + patchFile, |
| 60 | + "diff --git a/dist/cjs/689.js b/dist/cjs/689.js\n" + |
| 61 | + "--- a/dist/cjs/689.js\n+++ b/dist/cjs/689.js\n@@ -1 +1 @@\n" + |
| 62 | + `-${original}\n\\ No newline at end of file\n` + |
| 63 | + `+${patched}\n\\ No newline at end of file\n`, |
| 64 | + ); |
| 65 | +} |
0 commit comments