Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3748 Add custom template option back to blitz generate command #3866

Merged
merged 25 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c139faa
Update generate.ts
siddhsuresh Sep 26, 2022
3131586
add custom template back to toolkit
siddhsuresh Sep 26, 2022
c85f85d
cleanup
siddhsuresh Sep 26, 2022
d260c09
update blitz-server.ts when user generates custom templates
siddhsuresh Sep 27, 2022
5388347
Merge branch 'add-custom-template' of https://github.com/siddhsuresh/…
siddhsuresh Sep 27, 2022
19d2ba6
add to help
siddhsuresh Sep 27, 2022
6985dd9
fix blitz-server loading in javascript
siddhsuresh Sep 27, 2022
78fcc2a
Create slow-impalas-tap.md
siddhsuresh Sep 27, 2022
1c20794
Merge branch 'main' into add-custom-template
Sep 28, 2022
5409cf1
convert flag to command, default export to named export cliConfig
siddhsuresh Sep 29, 2022
4189312
fix types
siddhsuresh Sep 29, 2022
f62d248
update changeset
siddhsuresh Sep 29, 2022
fea099e
update changeset
siddhsuresh Sep 29, 2022
3e996c9
Merge branch 'main' into add-custom-template
siddhsuresh Sep 29, 2022
9cdafa4
update type from BlitzServerConfig to BlitzCliConfig
siddhsuresh Sep 29, 2022
c5cdcc6
Merge branch 'main' into add-custom-template
siddhsuresh Sep 29, 2022
5de52e0
Merge branch 'main' into add-custom-template
siddhsuresh Sep 30, 2022
b59ab74
Merge branch 'main' into add-custom-template
siddhsuresh Oct 3, 2022
b719d8e
Merge branch 'main' into add-custom-template
Oct 3, 2022
6677040
Merge branch 'main' into add-custom-template
siddhsuresh Oct 4, 2022
a17db35
Merge branch 'main' into add-custom-template
siddhsuresh Oct 4, 2022
16ce7ad
Update .changeset/slow-impalas-tap.md
siddhsuresh Oct 11, 2022
b4db9d3
Merge branch 'main' into add-custom-template
siddhsuresh Oct 11, 2022
7690ad2
update pnpm-lock
siddhsuresh Oct 11, 2022
490ea3f
Merge branch 'main' into add-custom-template
siddhsuresh Oct 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/slow-impalas-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/generator": patch
---

#3748 Add custom template usage back to toolkit
beerose marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/blitz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dotenv-expand": "8.0.3",
"envinfo": "7.8.1",
"esbuild": "0.14.34",
"esbuild-register": "3.3.3",
"find-up": "4.1.0",
"fs-extra": "10.0.1",
"hasbin": "1.2.3",
Expand Down
55 changes: 55 additions & 0 deletions packages/blitz/src/cli/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
MutationsGenerator,
ModelGenerator,
QueryGenerator,
addCustomTemplatesBlitzConfig,
} from "@blitzjs/generator"
import {log} from "../../logging"

const getIsTypeScript = async () =>
require("fs").existsSync(require("path").join(process.cwd(), "tsconfig.json"))
Expand Down Expand Up @@ -72,6 +74,7 @@ const args = arg(
"--parent": String,
"--dry-run": Boolean,
"--env": String,
"--generate-custom-templates": Boolean,
siddhsuresh marked this conversation as resolved.
Show resolved Hide resolved

// Aliases
"-e": "--env",
Expand Down Expand Up @@ -198,6 +201,11 @@ const getHelp = async () => {
For example, this command generates pages under pages/projects/[projectId]/tasks/

> blitz generate all tasks --parent=projects

# The '--generate-custom-templates' flag will copy the default templates to your app.
This is useful if you want to customize the templates for your app

> blitz generate --generate-custom-templates

# Database models can also be generated directly from the CLI.
Model fields can be specified with any generator that generates a database model ("all", "model", "resource").
Expand All @@ -213,8 +221,44 @@ const getHelp = async () => {
}
}

const createCustomTemplates = async () => {
if (args["--generate-custom-templates"]) {
const continuePrompt = await prompts({
type: "confirm",
name: "value",
message: `This will copy the default templates to your app/templates folder. Do you want to continue?`,
})
if (!continuePrompt.value) {
process.exit(0)
}
const templatesPath = await prompts({
type: "text",
name: "value",
message: `Enter the path to save the custom templates folder`,
initial: "app/templates",
})
const templatesPathValue: string = templatesPath.value
const isTypeScript = await getIsTypeScript()
addCustomTemplatesBlitzConfig(templatesPathValue, isTypeScript)
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")
const blitzGeneratorPath = require.resolve("@blitzjs/generator")
const templateFolder = ["form", "page", "query", "mutation", "queries", "mutations"]
for (const template of templateFolder) {
await fsExtra.copy(
require("path").join(blitzGeneratorPath, "..", "templates", template),
require("path").join(customTemplatesPath, template),
)
}
log.success(`🚀 Custom templates created in ${templatesPathValue} directory`)
process.exit(0)
}
}

const generate: CliCommand = async () => {
await getHelp()
await createCustomTemplates()
await determineType()
if (!selectedModelName) {
await determineName()
Expand All @@ -227,9 +271,20 @@ 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).default
itsdillon marked this conversation as resolved.
Show resolved Hide resolved
unregister()

for (const GeneratorClass of generators) {
const generator = new GeneratorClass({
destinationRoot: require("path").resolve(),
templateDir: blitzConfig?.customTemplates,
extraArgs: args["_"].slice(3) as string[],
modelName: singularRootContext,
modelNames: modelNames(singularRootContext),
Expand Down
4 changes: 4 additions & 0 deletions packages/blitz/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
pathname: string
}

export type BlitzServerConfig = {
customTemplates?: string
}

export const isRouteUrlObject = (x: any): x is RouteUrlObject => {
return typeof x === "object" && "pathname" in x && typeof x.pathname === "string"
}
Expand Down
63 changes: 63 additions & 0 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,69 @@ import {readdirRecursive} from "./utils/readdir-recursive"
import prettier from "prettier"
const debug = require("debug")("blitz:generator")

export const addCustomTemplatesBlitzConfig = (
customTemplatesPath: string,
isTypeScript: boolean,
) => {
const blitzServer = isTypeScript ? "app/blitz-server.ts" : "app/blitz-server.js"
const blitzServerPath = require("path").join(process.cwd(), blitzServer)
const userConfigModuleSource = fs.readFileSync(blitzServerPath, {encoding: "utf-8"})
const userConfigModule = j(userConfigModuleSource, {parser: customTsParser})
const program = userConfigModule.get()
const defaultDeclaration = userConfigModule.find(j.ExportDefaultDeclaration)
const defaultConfig = defaultDeclaration.paths()[0]?.value
if (!defaultConfig) {
const config = j.identifier("config")
const configVariable = j.variableDeclaration("const", [
j.variableDeclarator(
config,
j.objectExpression([
j.objectProperty(j.identifier("customTemplates"), j.literal(customTemplatesPath)),
]),
),
])
if (isTypeScript) {
const type = j.tsTypeAnnotation(j.tsTypeReference(j.identifier("BlitzServerConfig")))
const declaration: any = configVariable?.declarations
declaration[0].id.typeAnnotation = type
const typeImport = j.importDeclaration(
[j.importSpecifier(j.identifier("BlitzServerConfig"))],
j.literal("blitz"),
)
typeImport.importKind = "type"
program.node.program.body.unshift(typeImport)
}
const defaultConfig = j.exportDefaultDeclaration(j.identifier("config"))
program.node.program.body.push(configVariable)
program.node.program.body.push(defaultConfig)
} else {
const config = defaultConfig.declaration
if (config.type === "Identifier") {
const configVariable = userConfigModule.find(j.VariableDeclarator, {id: {name: config.name}})
const customTemplates = configVariable.find(j.ObjectProperty, {
key: {name: "customTemplates"},
})
customTemplates.remove()
configVariable
.find(j.ObjectExpression)
.get()
.node.properties.push(
j.objectProperty(j.identifier("customTemplates"), j.literal(customTemplatesPath)),
)
} else if (config.type === "ObjectExpression") {
const customTemplates = userConfigModule.find(j.ObjectProperty, {
key: {name: "customTemplates"},
})
customTemplates.remove()
config.properties.push(
j.objectProperty(j.identifier("customTemplates"), j.literal(customTemplatesPath)),
)
}
}
const newSource = userConfigModule.toSource()
fs.writeFileSync(blitzServerPath, newSource)
}

export const customTsParser = {
parse(source: string, options?: Overrides) {
const babelOptions = getBabelOptions(options)
Expand Down
Loading