Skip to content

Commit

Permalink
Added codemod support for legacy projects with a /src folder (#4290)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Doc0x1 authored Jan 25, 2024
1 parent a09685a commit f25aac0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-impalas-guess.md
Original file line number Diff line number Diff line change
@@ -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
37 changes: 30 additions & 7 deletions packages/codemod/src/upgrade-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
Expand All @@ -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}[] = []
Expand Down Expand Up @@ -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"),
"..",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"}`),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f25aac0

Please sign in to comment.