Skip to content

Commit

Permalink
feat(type-safe-api): add argument to exclude templates in generate co…
Browse files Browse the repository at this point in the history
…mmand (#890)

Allow excluding specific templates from code generation.
  • Loading branch information
cogwirrel authored Dec 9, 2024
1 parent c8cfeec commit 88bcd62
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/pdk/.projen/deps.json

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

1 change: 1 addition & 0 deletions packages/pdk/package.json

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

5 changes: 5 additions & 0 deletions packages/type-safe-api/.projen/deps.json

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

1 change: 1 addition & 0 deletions packages/type-safe-api/package.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getOperationResponses } from "parse-openapi/dist/parser/getOperationRes
import { getOperationResponse } from "parse-openapi/dist/parser/getOperationResponse";
import { generateMockDataForSchema } from "../custom/mock-data/generate-mock-data";
import { allFakers, Faker } from "@faker-js/faker";
import { minimatch } from "minimatch";

const TSAPI_WRITE_FILE_START = "###TSAPI_WRITE_FILE###";
const TSAPI_WRITE_FILE_END = "###/TSAPI_WRITE_FILE###";
Expand All @@ -42,6 +43,11 @@ interface Arguments {
*/
readonly templateDirs: string[];

/**
* Glob patterns for templates to exclude
*/
readonly excludeTemplates?: string[];

/**
* JSON string containing metadata
*/
Expand Down Expand Up @@ -1049,6 +1055,7 @@ export default async (argv: string[], rootScriptDir: string) => {
specPath: { type: String },
metadata: { type: String, optional: true },
templateDirs: { type: String, multiple: true },
excludeTemplates: { type: String, multiple: true, optional: true },
outputPath: { type: String },
printData: { type: Boolean, optional: true },
}, { argv });
Expand All @@ -1063,9 +1070,12 @@ export default async (argv: string[], rootScriptDir: string) => {
}

// Read all .ejs files in each template directory
const templates = args.templateDirs.flatMap(t => listFilesInDirRecursive(resolveTemplateDir(rootScriptDir, t))
const candidateTemplates = args.templateDirs.flatMap(t => listFilesInDirRecursive(resolveTemplateDir(rootScriptDir, t))
.filter(f => f.endsWith('.ejs') && !f.endsWith('.partial.ejs')));

// Filter out any excluded templates
const templates = candidateTemplates.filter((t => !(args.excludeTemplates ?? []).some(pattern => minimatch(t, pattern))));

// Render the templates with the data from the spec
const renderedFiles = await Promise.all(templates.map(async (template) => {
return await ejs.renderFile(template, data);
Expand Down
19 changes: 16 additions & 3 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions projenrc/projects/type-safe-api-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class TypeSafeApiProject extends PDKProject {
"[email protected]", // Used by scripts
"@types/[email protected]", // Used by scripts
"[email protected]", // Used by scripts
"[email protected]", // Used by scripts
"esbuild",
],
deps: [
Expand Down

0 comments on commit 88bcd62

Please sign in to comment.