1
0
mirror of synced 2026-02-04 12:08:33 -05:00

Compare commits

...

1 Commits

Author SHA1 Message Date
Siddharth Suresh
b08c0ff49a use AST to parse the config and allow blitz server to be in src 2022-10-31 20:46:44 +05:30
4 changed files with 38 additions and 19 deletions

View File

@@ -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),

View File

@@ -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",

View File

@@ -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 = {

4
pnpm-lock.yaml generated
View File

@@ -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