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

[legacy-framework] Allow passing custom templates folder for blitz generate #3068

Merged
merged 7 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions examples/auth/blitz.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module.exports = withBundleAnalyzer({
cli: {
clearConsoleOnBlitzDev: false,
},
codegen: {
templateDir: "./my-templates",
},
log: {
// level: "trace",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module.exports = withBundleAnalyzer({
cli: {
clearConsoleOnBlitzDev: true
},
codegen: {
templateDir: \\"my-templates\\",
},
log: {
// level: \\"trace\\",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ describe('transformBlitzConfig transform', () => {
' cli: {',
' clearConsoleOnBlitzDev: false,',
' },',
' codegen: {',
' templateDir: "my-templates",',
' },',
' log: {',
' // level: "trace",',
' },',
Expand Down
3 changes: 3 additions & 0 deletions nextjs/packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export type NextConfig = { [key: string]: any } & {
httpsProxy?: string
noProxy?: string
}
codegen?: {
templateDir?: string
}
log?: {
level?: LogLevel
type?: LogType
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,14 @@ export class Generate extends Command {
const singularRootContext = modelName(model)
this.validateModelName(singularRootContext)

const {loadConfigProduction} = await import("next/dist/server/config-shared")
const blitzConfig = loadConfigProduction(process.cwd())

const generators = generatorMap[args.type]
for (const GeneratorClass of generators) {
const generator = new GeneratorClass({
destinationRoot: require("path").resolve(),
templateDir: blitzConfig.codegen?.templateDir,
extraArgs: argv.slice(2).filter((arg) => !arg.startsWith("-")),
modelName: singularRootContext,
modelNames: modelNames(singularRootContext),
Expand Down
6 changes: 4 additions & 2 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const customTsParser = {
export interface GeneratorOptions {
context?: string
destinationRoot?: string
templateDir?: string
dryRun?: boolean
useTs?: boolean
}
Expand Down Expand Up @@ -242,14 +243,15 @@ export abstract class Generator<

async write(): Promise<void> {
debug("Generator.write...")
const paths = await readdirRecursive(this.sourcePath(), (name) => {
const sourcePath = this.sourcePath()
const paths = await readdirRecursive(sourcePath, (name) => {
const additionalFilesToIgnore = this.filesToIgnore()
return ![...alwaysIgnoreFiles, ...additionalFilesToIgnore].includes(name)
})
try {
this.prettier = await import("prettier")
} catch {}
const prettierOptions = await this.prettier?.resolveConfig(this.sourcePath())
const prettierOptions = await this.prettier?.resolveConfig(sourcePath)

for (let filePath of paths) {
try {
Expand Down
8 changes: 7 additions & 1 deletion packages/generator/src/generators/form-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {getTemplateRoot} from "../utils/get-template-root"
import {camelCaseToKebabCase} from "../utils/inflector"

export interface FormGeneratorOptions extends GeneratorOptions {
Expand All @@ -13,8 +14,13 @@ export interface FormGeneratorOptions extends GeneratorOptions {
}

export class FormGenerator extends Generator<FormGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: FormGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "form"})
}

static subdirectory = "queries"
sourceRoot: SourceRootType = {type: "template", path: "form"}

private getId(input: string = "") {
if (!input) return input
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/generators/model-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import path from "path"
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {Field} from "../prisma/field"
import {Model} from "../prisma/model"
import {getTemplateRoot} from "../utils/get-template-root"

export interface ModelGeneratorOptions extends GeneratorOptions {
modelName: string
extraArgs: string[]
}

export class ModelGenerator extends Generator<ModelGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: ModelGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "absolute", path: ""})
}
// default subdirectory is /app/[name], we need to back out of there to generate the model
static subdirectory = "../.."
sourceRoot: SourceRootType = {type: "absolute", path: ""}
unsafe_disableConflictChecker = true

async getTemplateValues() {}
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/generators/mutation-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {getTemplateRoot} from "../utils/get-template-root"
import {camelCaseToKebabCase} from "../utils/inflector"

export interface MutationGeneratorOptions extends GeneratorOptions {
Expand All @@ -7,8 +8,12 @@ export interface MutationGeneratorOptions extends GeneratorOptions {
}

export class MutationGenerator extends Generator<MutationGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: MutationGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "mutation"})
}
static subdirectory = "mutation"
sourceRoot: SourceRootType = {type: "template", path: "mutation"}

// eslint-disable-next-line require-await
async getTemplateValues() {
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/generators/mutations-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {getTemplateRoot} from "../utils/get-template-root"
import {camelCaseToKebabCase} from "../utils/inflector"

export interface MutationsGeneratorOptions extends GeneratorOptions {
Expand All @@ -13,8 +14,12 @@ export interface MutationsGeneratorOptions extends GeneratorOptions {
}

export class MutationsGenerator extends Generator<MutationsGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: MutationsGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "mutations"})
}
static subdirectory = "mutations"
sourceRoot: SourceRootType = {type: "template", path: "mutations"}

private getId(input: string = "") {
if (!input) return input
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/generators/page-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {getTemplateRoot} from "../utils/get-template-root"
import {camelCaseToKebabCase} from "../utils/inflector"

export interface PageGeneratorOptions extends GeneratorOptions {
Expand All @@ -13,8 +14,12 @@ export interface PageGeneratorOptions extends GeneratorOptions {
}

export class PageGenerator extends Generator<PageGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: PageGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "page"})
}
static subdirectory = "pages"
sourceRoot: SourceRootType = {type: "template", path: "page"}

private getId(input: string = "") {
if (!input) return input
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/generators/queries-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {getTemplateRoot} from "../utils/get-template-root"
import {camelCaseToKebabCase} from "../utils/inflector"

export interface QueriesGeneratorOptions extends GeneratorOptions {
Expand All @@ -13,8 +14,12 @@ export interface QueriesGeneratorOptions extends GeneratorOptions {
}

export class QueriesGenerator extends Generator<QueriesGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: QueriesGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "queries"})
}
static subdirectory = "queries"
sourceRoot: SourceRootType = {type: "template", path: "queries"}

private getId(input: string = "") {
if (!input) return input
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/generators/query-generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
import {getTemplateRoot} from "../utils/get-template-root"
import {camelCaseToKebabCase} from "../utils/inflector"

export interface QueryGeneratorOptions extends GeneratorOptions {
Expand All @@ -7,8 +8,12 @@ export interface QueryGeneratorOptions extends GeneratorOptions {
}

export class QueryGenerator extends Generator<QueryGeneratorOptions> {
sourceRoot: SourceRootType
constructor(options: QueryGeneratorOptions) {
super(options)
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "query"})
}
static subdirectory = "query"
sourceRoot: SourceRootType = {type: "template", path: "query"}

// eslint-disable-next-line require-await
async getTemplateValues() {
Expand Down
22 changes: 22 additions & 0 deletions packages/generator/src/utils/get-template-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from "fs"
import {baseLogger} from "next/dist/server/lib/logging"
import * as path from "path"
import {SourceRootType} from "../generator.js"

export const getTemplateRoot = (
templateDir: string | undefined,
fallback: SourceRootType,
): SourceRootType => {
if (!templateDir) {
return fallback
}
const templatePath = path.join(templateDir, fallback.path)
if (!fs.existsSync(templatePath)) {
baseLogger({displayDateTime: false}).info(
`Template path "${templatePath}" does not exist. Falling back to the default template.`,
)
return fallback
}

return {path: templatePath, type: "absolute"}
}