From 051bfd4116643b28173d91e4476798283514f504 Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Sun, 21 Apr 2024 11:02:08 +0200 Subject: [PATCH] fix(upgrade): Download yarn patches (#10491) --- packages/cli/src/commands/upgrade.js | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/packages/cli/src/commands/upgrade.js b/packages/cli/src/commands/upgrade.js index 2b52f4fd087b..01b25fb77311 100644 --- a/packages/cli/src/commands/upgrade.js +++ b/packages/cli/src/commands/upgrade.js @@ -111,6 +111,11 @@ export const handler = async ({ dryRun, tag, verbose, dedupe }) => { updatePackageVersionsFromTemplate(ctx, { dryRun, verbose }), enabled: (ctx) => ctx.versionToUpgradeTo?.includes('canary'), }, + { + title: 'Downloading yarn patches', + task: (ctx) => downloadYarnPatches(ctx, { dryRun, verbose }), + enabled: (ctx) => ctx.versionToUpgradeTo?.includes('canary'), + }, { title: 'Running yarn install', task: (ctx) => yarnInstall(ctx, { dryRun, verbose }), @@ -355,6 +360,58 @@ async function updatePackageVersionsFromTemplate(ctx, { dryRun, verbose }) { ) } +async function downloadYarnPatches(ctx, { dryRun, verbose }) { + if (!ctx.versionToUpgradeTo) { + throw new Error('Failed to upgrade') + } + + const res = await fetch( + 'https://api.github.com/repos/redwoodjs/redwood/git/trees/main?recursive=1', + ) + const json = await res.json() + const patches = json.tree.filter((patchInfo) => + patchInfo.path.startsWith( + 'packages/create-redwood-app/templates/ts/.yarn/patches/', + ), + ) + + const patchDir = path.join(getPaths().base, '.yarn', 'patches') + + if (verbose) { + console.log('Creating patch directory', patchDir) + } + + if (!dryRun) { + fs.mkdirSync(patchDir, { recursive: true }) + } + + return new Listr( + patches.map((patch) => { + return { + title: `Downloading ${patch.path}`, + task: async () => { + const res = await fetch(patch.url) + const patchMeta = await res.json() + const patchPath = path.join( + getPaths().base, + '.yarn', + 'patches', + path.basename(patch.path), + ) + + if (verbose) { + console.log('Writing patch', patchPath) + } + + if (!dryRun) { + await fs.writeFile(patchPath, patchMeta.content, 'base64') + } + }, + } + }), + ) +} + async function refreshPrismaClient(task, { verbose }) { /** Relates to prisma/client issue, @see: https://github.com/redwoodjs/redwood/issues/1083 */ try {