diff --git a/.changeset/light-squids-draw.md b/.changeset/light-squids-draw.md new file mode 100644 index 0000000000..7bc86f7e67 --- /dev/null +++ b/.changeset/light-squids-draw.md @@ -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 diff --git a/packages/blitz/src/cli/commands/generate.ts b/packages/blitz/src/cli/commands/generate.ts index 6758eb3c2c..e633780751 100644 --- a/packages/blitz/src/cli/commands/generate.ts +++ b/packages/blitz/src/cli/commands/generate.ts @@ -15,7 +15,7 @@ import { MutationsGenerator, ModelGenerator, QueryGenerator, - addCustomTemplatesBlitzConfig, + customTemplatesBlitzConfig, } from "@blitzjs/generator" import {log} from "../../logging" @@ -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") @@ -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), diff --git a/packages/generator/package.json b/packages/generator/package.json index 8f2a47d03e..4a7edc71d1 100644 --- a/packages/generator/package.json +++ b/packages/generator/package.json @@ -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", diff --git a/packages/generator/src/generator.ts b/packages/generator/src/generator.ts index 518662e6aa..b4b9489853 100644 --- a/packages/generator/src/generator.ts +++ b/packages/generator/src/generator.ts @@ -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() @@ -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 } } @@ -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 = { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 703640eaf8..b14879fc50 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1065,6 +1065,7 @@ importers: enquirer: 2.3.6 eslint: 7.32.0 fs-extra: 10.0.1 + globby: 13.1.2 got: ^11.8.1 jscodeshift: 0.13.0 mem-fs: 1.2.0 @@ -1094,6 +1095,7 @@ importers: 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_slgjdbbopna4ebnpdn2nkn3v2a mem-fs: 1.2.0 @@ -2606,7 +2608,7 @@ packages: peerDependencies: "@babel/core": ^7.0.0-0 dependencies: - "@babel/core": 7.12.10 + "@babel/core": 7.12.10_supports-color@8.1.1 "@babel/helper-plugin-utils": 7.17.12 dev: false