From 88bcd628f0f6cec9f13ba3ce3ba12b8ee250a9ba Mon Sep 17 00:00:00 2001 From: Jack Stevenson Date: Mon, 9 Dec 2024 21:02:16 +1030 Subject: [PATCH] feat(type-safe-api): add argument to exclude templates in generate command (#890) Allow excluding specific templates from code generation. --- packages/pdk/.projen/deps.json | 5 +++++ packages/pdk/package.json | 1 + packages/type-safe-api/.projen/deps.json | 5 +++++ packages/type-safe-api/package.json | 1 + .../type-safe-api/generators/generate-next.ts | 12 +++++++++++- pnpm-lock.yaml | 19 ++++++++++++++++--- projenrc/projects/type-safe-api-project.ts | 1 + 7 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/pdk/.projen/deps.json b/packages/pdk/.projen/deps.json index e017ad234..ef399cbdf 100644 --- a/packages/pdk/.projen/deps.json +++ b/packages/pdk/.projen/deps.json @@ -234,6 +234,11 @@ "name": "license-checker", "type": "build" }, + { + "name": "minimatch", + "version": "10.0.1", + "type": "build" + }, { "name": "node-fetch", "version": "^2.6.7", diff --git a/packages/pdk/package.json b/packages/pdk/package.json index c049abdf6..36bd2c6c5 100644 --- a/packages/pdk/package.json +++ b/packages/pdk/package.json @@ -89,6 +89,7 @@ "jsii-pacmak": "1.104.0", "jsii-rosetta": "1.104.0", "license-checker": "25.0.1", + "minimatch": "10.0.1", "node-fetch": "^2.6.7", "nx": "19", "parse-openapi": "0.0.1", diff --git a/packages/type-safe-api/.projen/deps.json b/packages/type-safe-api/.projen/deps.json index 93036eec1..063a3a9b9 100644 --- a/packages/type-safe-api/.projen/deps.json +++ b/packages/type-safe-api/.projen/deps.json @@ -130,6 +130,11 @@ "name": "jsii-rosetta", "type": "build" }, + { + "name": "minimatch", + "version": "10.0.1", + "type": "build" + }, { "name": "parse-openapi", "version": "0.0.1", diff --git a/packages/type-safe-api/package.json b/packages/type-safe-api/package.json index ad9349e80..31c15a1a5 100644 --- a/packages/type-safe-api/package.json +++ b/packages/type-safe-api/package.json @@ -62,6 +62,7 @@ "jsii-docgen": "8.0.56", "jsii-pacmak": "1.104.0", "jsii-rosetta": "1.104.0", + "minimatch": "10.0.1", "parse-openapi": "0.0.1", "prettier": "2.8.8", "projen": "0.82.8", diff --git a/packages/type-safe-api/scripts/type-safe-api/generators/generate-next.ts b/packages/type-safe-api/scripts/type-safe-api/generators/generate-next.ts index 81794a273..159a94906 100755 --- a/packages/type-safe-api/scripts/type-safe-api/generators/generate-next.ts +++ b/packages/type-safe-api/scripts/type-safe-api/generators/generate-next.ts @@ -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###"; @@ -42,6 +43,11 @@ interface Arguments { */ readonly templateDirs: string[]; + /** + * Glob patterns for templates to exclude + */ + readonly excludeTemplates?: string[]; + /** * JSON string containing metadata */ @@ -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 }); @@ -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); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e2097a964..05362b035 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1273,6 +1273,9 @@ importers: license-checker: specifier: 25.0.1 version: 25.0.1 + minimatch: + specifier: 10.0.1 + version: 10.0.1 node-fetch: specifier: ^2.6.7 version: 2.7.0 @@ -1672,6 +1675,9 @@ importers: jsii-rosetta: specifier: 1.104.0 version: 1.104.0 + minimatch: + specifier: 10.0.1 + version: 10.0.1 parse-openapi: specifier: 0.0.1 version: 0.0.1 @@ -7817,7 +7823,7 @@ packages: dependencies: semver: 7.6.2 shelljs: 0.8.5 - typescript: 5.7.0-dev.20241031 + typescript: 5.8.0-dev.20241127 dev: true /duplexer2@0.1.4: @@ -11249,6 +11255,13 @@ packages: engines: {node: '>=4'} dev: true + /minimatch@10.0.1: + resolution: {integrity: sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==} + engines: {node: 20 || >=22} + dependencies: + brace-expansion: 2.0.1 + dev: true + /minimatch@3.0.5: resolution: {integrity: sha512-tUpxzX0VAzJHjLu0xUfFv1gwVp9ba3IOuRAVH2EGuRW8a5emA2FlACLqiT/lDVtS1W+TGNwqz3sWaNyLgDJWuw==} dependencies: @@ -14244,8 +14257,8 @@ packages: engines: {node: '>=14.17'} hasBin: true - /typescript@5.7.0-dev.20241031: - resolution: {integrity: sha512-CR0uSMNCzLCXjptJ38ESiEpTMEYJiplIe9jmZLQuTp5nrxwOE2ljMg5z1XyPI+Uss1saZOdpwkPxXU3mDIASEg==} + /typescript@5.8.0-dev.20241127: + resolution: {integrity: sha512-TGb4/mmMCY5GOBuEry4xaOcDonyPwnxPc2zaR/8jgA6twK+iGEPsB6EeT+lSSrpOP906WPlFAn3XVQfQm4d7TQ==} engines: {node: '>=14.17'} hasBin: true dev: true diff --git a/projenrc/projects/type-safe-api-project.ts b/projenrc/projects/type-safe-api-project.ts index 12dc2b182..fc859b80b 100644 --- a/projenrc/projects/type-safe-api-project.ts +++ b/projenrc/projects/type-safe-api-project.ts @@ -47,6 +47,7 @@ export class TypeSafeApiProject extends PDKProject { "ejs@3.1.10", // Used by scripts "@types/ejs@3.1.5", // Used by scripts "parse-openapi@0.0.1", // Used by scripts + "minimatch@10.0.1", // Used by scripts "esbuild", ], deps: [