Skip to content

Commit 88bcd62

Browse files
authored
feat(type-safe-api): add argument to exclude templates in generate command (#890)
Allow excluding specific templates from code generation.
1 parent c8cfeec commit 88bcd62

File tree

7 files changed

+40
-4
lines changed

7 files changed

+40
-4
lines changed

packages/pdk/.projen/deps.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/pdk/package.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/type-safe-api/.projen/deps.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/type-safe-api/package.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/type-safe-api/scripts/type-safe-api/generators/generate-next.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { getOperationResponses } from "parse-openapi/dist/parser/getOperationRes
2424
import { getOperationResponse } from "parse-openapi/dist/parser/getOperationResponse";
2525
import { generateMockDataForSchema } from "../custom/mock-data/generate-mock-data";
2626
import { allFakers, Faker } from "@faker-js/faker";
27+
import { minimatch } from "minimatch";
2728

2829
const TSAPI_WRITE_FILE_START = "###TSAPI_WRITE_FILE###";
2930
const TSAPI_WRITE_FILE_END = "###/TSAPI_WRITE_FILE###";
@@ -42,6 +43,11 @@ interface Arguments {
4243
*/
4344
readonly templateDirs: string[];
4445

46+
/**
47+
* Glob patterns for templates to exclude
48+
*/
49+
readonly excludeTemplates?: string[];
50+
4551
/**
4652
* JSON string containing metadata
4753
*/
@@ -1049,6 +1055,7 @@ export default async (argv: string[], rootScriptDir: string) => {
10491055
specPath: { type: String },
10501056
metadata: { type: String, optional: true },
10511057
templateDirs: { type: String, multiple: true },
1058+
excludeTemplates: { type: String, multiple: true, optional: true },
10521059
outputPath: { type: String },
10531060
printData: { type: Boolean, optional: true },
10541061
}, { argv });
@@ -1063,9 +1070,12 @@ export default async (argv: string[], rootScriptDir: string) => {
10631070
}
10641071

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

1076+
// Filter out any excluded templates
1077+
const templates = candidateTemplates.filter((t => !(args.excludeTemplates ?? []).some(pattern => minimatch(t, pattern))));
1078+
10691079
// Render the templates with the data from the spec
10701080
const renderedFiles = await Promise.all(templates.map(async (template) => {
10711081
return await ejs.renderFile(template, data);

pnpm-lock.yaml

+16-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projenrc/projects/type-safe-api-project.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class TypeSafeApiProject extends PDKProject {
4747
"[email protected]", // Used by scripts
4848
"@types/[email protected]", // Used by scripts
4949
"[email protected]", // Used by scripts
50+
"[email protected]", // Used by scripts
5051
"esbuild",
5152
],
5253
deps: [

0 commit comments

Comments
 (0)