|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Example usage: |
| 4 | +// ./patch-yarn.mjs < yarn-4.5.1.cjs > yarn-4.5.1-patched.cjs |
| 5 | +// chmod +x yarn-4.5.1-patched.cjs |
| 6 | + |
| 7 | +import process from "node:process"; |
| 8 | + |
| 9 | +process.stdin.setEncoding("utf8"); |
| 10 | + |
| 11 | +const chunks = []; |
| 12 | + |
| 13 | +process.stdin.on("data", (chunk) => { |
| 14 | + chunks.push(chunk); |
| 15 | +}); |
| 16 | + |
| 17 | +process.stdin.on("end", () => { |
| 18 | + const source = chunks.join(""); |
| 19 | + |
| 20 | + const versionMatch = source.match( |
| 21 | + /name:\s*"@yarnpkg\/cli",\s*version:\s*"([^"]+)"/, |
| 22 | + ); |
| 23 | + if (!versionMatch) { |
| 24 | + console.error("Couldn't parse the Yarn version from the source."); |
| 25 | + process.exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + const version = versionMatch[1]; |
| 29 | + if (version.endsWith("-patched")) { |
| 30 | + console.error( |
| 31 | + `Found Yarn version ${version}, which is already marked as patched.`, |
| 32 | + ); |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + console.log( |
| 37 | + source |
| 38 | + // Prevents the deletion of the "private" field from `package.json` |
| 39 | + // files. |
| 40 | + .replaceAll(/\bdelete [^\.]+\.private\b/g, "true") |
| 41 | + |
| 42 | + // Patches the `yarn workspaces foreach` command to identify itself via |
| 43 | + // the environment. |
| 44 | + .replaceAll( |
| 45 | + /\bProcess started[^;]*;/g, |
| 46 | + (match) => |
| 47 | + `${match}process.env.CODE_CHRONICLES_RUNNING_VIA_YARN_WORKSPACES_FOREACH="1";`, |
| 48 | + ) |
| 49 | + |
| 50 | + // Updates the Yarn version to indicate that we patched it. |
| 51 | + .replaceAll(`"${version}"`, `"${version}-patched"`), |
| 52 | + ); |
| 53 | +}); |
0 commit comments