From f25aac08c094fecba541a9b558b0e40787cb4c2d Mon Sep 17 00:00:00 2001 From: Doc0x1 <19937034+Doc0x1@users.noreply.github.com> Date: Thu, 25 Jan 2024 02:48:34 -0600 Subject: [PATCH] Added codemod support for legacy projects with a /src folder (#4290) * Added support for legacy projects that have the pages folder located inside a /src directory * Fixed line 687 for path.resolve(`${findPagesDirectory}/api/rpc`) not actually calling the function and added path.resolve's to findPagesDirectory * Update for lines 56 and 1340 to add support for app and pages directories located in /src --- .changeset/serious-impalas-guess.md | 5 ++++ packages/codemod/src/upgrade-legacy.ts | 37 +++++++++++++++++++++----- 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 .changeset/serious-impalas-guess.md diff --git a/.changeset/serious-impalas-guess.md b/.changeset/serious-impalas-guess.md new file mode 100644 index 0000000000..ea69ea9192 --- /dev/null +++ b/.changeset/serious-impalas-guess.md @@ -0,0 +1,5 @@ +--- +"@blitzjs/codemod": patch +--- + +Added support to codemod upgrade-legacy for projects that have their pages folder nested in a src/ folder diff --git a/packages/codemod/src/upgrade-legacy.ts b/packages/codemod/src/upgrade-legacy.ts index 14b5c23d4a..acf034023e 100644 --- a/packages/codemod/src/upgrade-legacy.ts +++ b/packages/codemod/src/upgrade-legacy.ts @@ -32,6 +32,12 @@ class ExpectedError extends Error { } } +const findPagesDirectory = () => { + const srcPagesDir = path.join("src", "pages") + const pagesDir = path.resolve("pages") + return fs.existsSync(srcPagesDir) ? path.resolve(srcPagesDir) : pagesDir +} + const isInternalBlitzMonorepoDevelopment = fs.existsSync( path.join(__dirname, "../../../blitz-next"), ) @@ -46,7 +52,22 @@ const upgradeLegacy = async () => { if (blitzConfigFile === "") { throw new ExpectedError("Could not identify Legacy Blitz Config file") } - const appDir = path.resolve("app") + // Check if app directory exists in either app/ or src/app + const appDir = fs.existsSync(path.resolve("app")) + ? path.resolve("app") + : fs.existsSync(path.resolve("src")) + ? 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)", + ) + } + } catch (e) { + console.error(e) + } let failedAt = fs.existsSync(path.resolve(".migration.json")) && fs.readJSONSync("./.migration.json").failedAt let collectedErrors: {message: string; step: number}[] = [] @@ -678,7 +699,7 @@ const upgradeLegacy = async () => { steps.push({ name: "create pages/api/rpc directory and add [[...blitz]].ts wildecard API route", action: async () => { - const pagesDir = path.resolve("pages/api/rpc") + const pagesDir = path.resolve(`${findPagesDirectory()}/api/rpc`) const templatePath = path.join( require.resolve("@blitzjs/generator"), "..", @@ -929,7 +950,7 @@ const upgradeLegacy = async () => { name: "convert useRouterQuery to useRouter", action: async () => { //First check ./pages - const pagesDir = path.resolve("pages") + const pagesDir = findPagesDirectory() getAllFiles(pagesDir, [], [], [".ts", ".tsx", ".js", ".jsx"]).forEach((file) => { try { const filepath = path.resolve(pagesDir, file) @@ -1061,7 +1082,7 @@ const upgradeLegacy = async () => { steps.push({ name: "wrap App component with withBlitz HOC", action: async () => { - const pagesDir = path.resolve("pages") + const pagesDir = findPagesDirectory() const program = getCollectionFromSource( path.join(pagesDir, `_app.${isTypescript ? "tsx" : "jsx"}`), @@ -1111,7 +1132,7 @@ const upgradeLegacy = async () => { steps.push({ name: "update imports in the _document file", action: async () => { - const pagesDir = path.resolve("pages") + const pagesDir = findPagesDirectory() if (fs.existsSync(path.join(pagesDir, `_document.${isTypescript ? "tsx" : "jsx"}`))) { const program = getCollectionFromSource( @@ -1191,7 +1212,7 @@ const upgradeLegacy = async () => { steps.push({ name: "wrap getServerSideProps, getStaticProps and API handlers with gSSP, gSP, and api", action: async () => { - const pagesDir = path.resolve("pages") + const pagesDir = findPagesDirectory() getAllFiles(pagesDir, [], ["api"], [".ts", ".tsx", ".js", ".jsx"]).forEach((file) => { try { const program = getCollectionFromSource(file) @@ -1314,7 +1335,9 @@ const upgradeLegacy = async () => { steps.push({ name: "check for usages of invokeWithMiddleware", action: async () => { - getAllFiles(path.resolve("pages"), [], [], [".ts", ".tsx", ".js", ".jsx"]).forEach((file) => { + const srcPagesDir = path.resolve(path.join("src/pages")) + const pagesDir = fs.existsSync(srcPagesDir) ? srcPagesDir : path.resolve("pages") + getAllFiles(pagesDir, [], [], [".ts", ".tsx", ".js", ".jsx"]).forEach((file) => { const program = getCollectionFromSource(file) try { const invokeWithMiddlewarePath = findCallExpression(program, "invokeWithMiddleware")