From cc9fffe88b2ec06a1ee6f6e3669add43eca894c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sedlo=C5=88?= Date: Wed, 5 Jun 2024 12:56:27 +0200 Subject: [PATCH] feat: allow formatting to fail --- README.md | 1 + packages/generator/src/generator.ts | 39 +++++++++++++++++----------- packages/generator/src/lib/config.ts | 1 + 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index be766c9..86f5292 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ In addition to the Prisma features, you can also generate Drizzle-specific featu | relationalQuery | Flag to generate relational query | true | false | | moduleResolution | Specify the [module resolution](https://www.typescriptlang.org/tsconfig#moduleResolution) that will affect the import style | _*auto_ | nodenext | | verbose | Flag to enable verbose logging | - | true | +| abortOnFailedFormatting | Flag to throw exception when formatting fails | true | false | | **dateMode | Change the generated mode for date | "date" || _* It will find the closest tsconfig from the current working directory. Note that [extends](https://www.typescriptlang.org/tsconfig#extends) is not supported_ diff --git a/packages/generator/src/generator.ts b/packages/generator/src/generator.ts index c5e0137..385122c 100644 --- a/packages/generator/src/generator.ts +++ b/packages/generator/src/generator.ts @@ -85,21 +85,30 @@ generatorHandler({ function handleFormatting() { const generator = getGenerator() if (generator.config.formatter == null) return - switch (generator.config.formatter) { - case 'prettier': - execSync(`prettier --write ${generator.output.path}`, { - stdio: 'inherit', - }) - break - case 'biome': - execSync(`biome format --write ${generator.output.path}`, { - stdio: 'inherit', - }) - break - default: - execSync(`${generator.config.formatter} ${generator.output.path}`, { - stdio: 'inherit', - }) + + try { + switch (generator.config.formatter) { + case 'prettier': + execSync(`prettier --write ${generator.output.path}`, { + stdio: 'inherit', + }) + break + case 'biome': + execSync(`biome format --write ${generator.output.path}`, { + stdio: 'inherit', + }) + break + default: + execSync(`${generator.config.formatter} ${generator.output.path}`, { + stdio: 'inherit', + }) + } + } catch (e) { + if (generator.config.abortOnFailedFormatting) { + throw e + } + + logger.log('Failed to format the generated schema') } } diff --git a/packages/generator/src/lib/config.ts b/packages/generator/src/lib/config.ts index 3d154fc..6fa843a 100644 --- a/packages/generator/src/lib/config.ts +++ b/packages/generator/src/lib/config.ts @@ -17,6 +17,7 @@ const Config = object({ moduleResolution: optional(ModuleResolution), verbose: optional(BooleanInStr), formatter: optional(string()), + abortOnFailedFormatting: withDefault(optional(BooleanInStr), true), dateMode: optional(DateMode), }) export type Config = Output