From 29c67b4ee25afd9b9b1553399c79014ab2345d99 Mon Sep 17 00:00:00 2001 From: Vandivier Date: Thu, 9 May 2024 22:01:53 -0400 Subject: [PATCH 1/4] fix: typo --- packages/codemod/src/upgrade-legacy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/codemod/src/upgrade-legacy.ts b/packages/codemod/src/upgrade-legacy.ts index acf034023e..5ce7322e34 100644 --- a/packages/codemod/src/upgrade-legacy.ts +++ b/packages/codemod/src/upgrade-legacy.ts @@ -582,7 +582,7 @@ const upgradeLegacy = async () => { replaceTemplateValues(blitzClient), ) } else { - throw new ExpectedError("App directory doesn't exit") + throw new ExpectedError("App directory doesn't exist") } }, }) From e3c4b875aef48d693bdcfc4751f3298eda7ecec0 Mon Sep 17 00:00:00 2001 From: Vandivier Date: Thu, 9 May 2024 22:29:05 -0400 Subject: [PATCH 2/4] feat: use const when appropriate and rm unused import and rm misleading comment and add space for readability --- packages/codemod/src/upgrade-legacy.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/codemod/src/upgrade-legacy.ts b/packages/codemod/src/upgrade-legacy.ts index 5ce7322e34..ba190f4a66 100644 --- a/packages/codemod/src/upgrade-legacy.ts +++ b/packages/codemod/src/upgrade-legacy.ts @@ -1,9 +1,4 @@ -import j, { - Expression, - ImportDeclaration, - ImportDefaultSpecifier, - ImportSpecifier, -} from "jscodeshift" +import j, {ImportDeclaration, ImportDefaultSpecifier, ImportSpecifier} from "jscodeshift" import * as fs from "fs-extra" import path from "path" import { @@ -41,17 +36,20 @@ const findPagesDirectory = () => { const isInternalBlitzMonorepoDevelopment = fs.existsSync( path.join(__dirname, "../../../blitz-next"), ) + type Step = {name: string; action: (stepIndex: number) => Promise} + const upgradeLegacy = async () => { - let isTypescript = fs.existsSync(path.resolve("tsconfig.json")) - let existingBlitzConfigFiles = ["ts", "js"] + const isTypescript = fs.existsSync(path.resolve("tsconfig.json")) + const existingBlitzConfigFiles = ["ts", "js"] .map((e) => `blitz.config.${e}`) .map((e) => path.resolve(e)) .filter(fs.existsSync) - let blitzConfigFile = existingBlitzConfigFiles.reduce((_prev, current) => current, "") + const blitzConfigFile = existingBlitzConfigFiles.reduce((_prev, current) => current, "") if (blitzConfigFile === "") { - throw new ExpectedError("Could not identify Legacy Blitz Config file") + throw new ExpectedError("Could not identify Legacy Blitz Config file.") } + // Check if app directory exists in either app/ or src/app const appDir = fs.existsSync(path.resolve("app")) ? path.resolve("app") @@ -59,7 +57,6 @@ const upgradeLegacy = async () => { ? path.resolve(path.join("src", "app")) : "" try { - // Throw Error if appDir empty if (appDir === "") { throw new ExpectedError( "Could not identify Legacy Blitz App directory in project (no app/ or src/ directory found)", From ad1b2db72209a6f49cbdaac6bc4486ff435fa750 Mon Sep 17 00:00:00 2001 From: Vandivier Date: Thu, 9 May 2024 22:31:22 -0400 Subject: [PATCH 3/4] feat: allow next config to be considered an existing config file --- packages/codemod/src/upgrade-legacy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/codemod/src/upgrade-legacy.ts b/packages/codemod/src/upgrade-legacy.ts index ba190f4a66..5c06cf3962 100644 --- a/packages/codemod/src/upgrade-legacy.ts +++ b/packages/codemod/src/upgrade-legacy.ts @@ -42,7 +42,7 @@ type Step = {name: string; action: (stepIndex: number) => Promise} const upgradeLegacy = async () => { const isTypescript = fs.existsSync(path.resolve("tsconfig.json")) const existingBlitzConfigFiles = ["ts", "js"] - .map((e) => `blitz.config.${e}`) + .map((e) => `blitz.config.${e}` || `next.config.js`) .map((e) => path.resolve(e)) .filter(fs.existsSync) const blitzConfigFile = existingBlitzConfigFiles.reduce((_prev, current) => current, "") From 534de33c48ffac878517eb8f95a69c6ee863346f Mon Sep 17 00:00:00 2001 From: Vandivier Date: Thu, 9 May 2024 23:04:12 -0400 Subject: [PATCH 4/4] feat: don't insert dup import --- packages/codemod/src/upgrade-legacy.ts | 30 ++++++++++++++++++-------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/codemod/src/upgrade-legacy.ts b/packages/codemod/src/upgrade-legacy.ts index 5c06cf3962..e3c86e1de2 100644 --- a/packages/codemod/src/upgrade-legacy.ts +++ b/packages/codemod/src/upgrade-legacy.ts @@ -1336,18 +1336,29 @@ const upgradeLegacy = async () => { const pagesDir = fs.existsSync(srcPagesDir) ? srcPagesDir : path.resolve("pages") getAllFiles(pagesDir, [], [], [".ts", ".tsx", ".js", ".jsx"]).forEach((file) => { const program = getCollectionFromSource(file) + const identifiers = new Set() + + // Collect all identifiers in the scope + program.find(j.Identifier).forEach((path) => { + identifiers.add(path.value.name) + }) + try { - const invokeWithMiddlewarePath = findCallExpression(program, "invokeWithMiddleware") - if (invokeWithMiddlewarePath?.length) { - invokeWithMiddlewarePath.forEach((path) => { + const invokeWithMiddlewarePaths = findCallExpression(program, "invokeWithMiddleware") + if (invokeWithMiddlewarePaths?.length) { + invokeWithMiddlewarePaths.forEach((path) => { const resolverName = path.value.arguments.at(0) if (resolverName?.type === "Identifier") { - const resolverExpression = j.callExpression( - j.identifier(resolverName.name), - path.value.arguments.slice(1), - ) - const resolverStatement = j.expressionStatement(resolverExpression) - j(path).replaceWith(resolverStatement) + if (!identifiers.has(resolverName.name)) { + // Check if identifier exists + // We skip insertion if the identifier already exists + const resolverExpression = j.callExpression( + j.identifier(resolverName.name), + path.value.arguments.slice(1), + ) + const resolverStatement = j.expressionStatement(resolverExpression) + j(path).replaceWith(resolverStatement) + } } else { throw new Error( `invokeWithMiddleware can only be used with a resolver as the first argument \nError at Line ${path?.value?.loc?.start.line}`, @@ -1365,6 +1376,7 @@ const upgradeLegacy = async () => { }) }, }) + // Loop through steps and run the action if ((failedAt && failedAt < steps.length) || failedAt !== "SUCCESS") { for (let [index, step] of steps.entries()) {