Skip to content

Commit

Permalink
Fix cannot find module db error in JavaScript template (#3940)
Browse files Browse the repository at this point in the history
* use AST to parse the config and allow blitz server to be in `src`
  • Loading branch information
siddhsuresh authored Nov 2, 2022
1 parent 11b548e commit 8e5903c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/light-squids-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/generator": patch
---

Fix `cannot find module db error` in JavaScript template. Replace requiring the config using `esbuild` with parsing using `jscodeshift` to get the `cliConfig` values. Added logic to find the `blitz-server` file in `src` directory
16 changes: 4 additions & 12 deletions packages/blitz/src/cli/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
MutationsGenerator,
ModelGenerator,
QueryGenerator,
addCustomTemplatesBlitzConfig,
customTemplatesBlitzConfig,
} from "@blitzjs/generator"
import {log} from "../../logging"

Expand Down Expand Up @@ -65,7 +65,7 @@ const createCustomTemplates = async () => {
})
const templatesPathValue: string = templatesPath.value
const isTypeScript = await getIsTypeScript()
addCustomTemplatesBlitzConfig(templatesPathValue, isTypeScript)
await customTemplatesBlitzConfig(isTypeScript, templatesPathValue, true) // to run the codemod
log.success(`🚀 Custom templates path added/updated in app/blitz-server file`)
const customTemplatesPath = require("path").join(process.cwd(), templatesPathValue)
const fsExtra = await import("fs-extra")
Expand Down Expand Up @@ -275,20 +275,12 @@ const generate: CliCommand = async () => {
const generators = generatorMap[selectedType as keyof typeof generatorMap]

const isTypeScript = await getIsTypeScript()
const blitzServerPath = isTypeScript ? "app/blitz-server.ts" : "app/blitz-server.js"
const blitzServer = require("path").join(process.cwd(), blitzServerPath)
const {register} = require("esbuild-register/dist/node")
const {unregister} = register({
target: "es6",
})
const blitzConfig = require(blitzServer)
const {cliConfig} = blitzConfig
unregister()
const cliConfig = await customTemplatesBlitzConfig(isTypeScript)

for (const GeneratorClass of generators) {
const generator = new GeneratorClass({
destinationRoot: require("path").resolve(),
templateDir: cliConfig?.customTemplates,
templateDir: cliConfig,
extraArgs: args["_"].slice(3) as string[],
modelName: singularRootContext,
modelNames: modelNames(singularRootContext),
Expand Down
1 change: 1 addition & 0 deletions packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"diff": "5.0.0",
"enquirer": "2.3.6",
"fs-extra": "10.0.1",
"globby": "13.1.2",
"got": "^11.8.1",
"jscodeshift": "0.13.0",
"mem-fs": "1.2.0",
Expand Down
36 changes: 30 additions & 6 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,31 @@ import {readdirRecursive} from "./utils/readdir-recursive"
import prettier from "prettier"
const debug = require("debug")("blitz:generator")

export const addCustomTemplatesBlitzConfig = (
customTemplatesPath: string,
export function getProjectRootSync() {
return path.dirname(getConfigSrcPath())
}

export function getConfigSrcPath() {
const jsPath = path.resolve(path.join(process.cwd(), "next.config.js"))
return jsPath
}

export const customTemplatesBlitzConfig = async (
isTypeScript: boolean,
customTemplatesPath = "",
codemod = false,
) => {
const blitzServer = isTypeScript ? "app/blitz-server.ts" : "app/blitz-server.js"
const blitzServerPath = require("path").join(process.cwd(), blitzServer)
const {globby} = await import("globby")
const blitzServer = await globby(["{app,src}/**/blitz-server.{ts,js}"], {
cwd: getProjectRootSync(),
})
if (blitzServer.length === 0) {
throw new Error("Could not find blitz-server.js or blitz-server.ts in app or src folder")
}
if (blitzServer.length > 1) {
throw new Error("Found more than one blitz-server.js or blitz-server.ts in app or src folder")
}
const blitzServerPath = require("path").join(process.cwd(), blitzServer.at(0))
const userConfigModuleSource = fs.readFileSync(blitzServerPath, {encoding: "utf-8"})
const userConfigModule = j(userConfigModuleSource, {parser: customTsParser})
const program = userConfigModule.get()
Expand Down Expand Up @@ -86,6 +105,9 @@ export const addCustomTemplatesBlitzConfig = (
if (customTemplatesProperty.type === "ObjectProperty") {
const customValue = customTemplatesProperty.value
if (customValue.type === "StringLiteral") {
if (!codemod) {
return customValue.value
}
customValue.value = customTemplatesPath
}
}
Expand All @@ -94,8 +116,10 @@ export const addCustomTemplatesBlitzConfig = (
}
}
}
const newSource = userConfigModule.toSource()
fs.writeFileSync(blitzServerPath, newSource)
if (codemod) {
const newSource = userConfigModule.toSource()
fs.writeFileSync(blitzServerPath, newSource)
}
}

export const customTsParser = {
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e5903c

Please sign in to comment.