From caeffc185af1c14a18c1ce8a871582eb35514150 Mon Sep 17 00:00:00 2001 From: Deep Singhvi Date: Fri, 1 Mar 2024 08:04:56 -0500 Subject: [PATCH] (ts, feature): introduce custom config for `tolerateRepublish` to re publish npm versions (#3093) * (ts, feature): introduce custom config * fix compile --- .../csharp/model/src/ModelGeneratorCli.ts | 4 ++++ .../express/cli/src/ExpressGeneratorCli.ts | 4 ++++ generators/typescript/sdk/CHANGELOG.md | 22 ++++++++++++++++--- generators/typescript/sdk/VERSION | 2 +- .../typescript/sdk/cli/src/SdkGeneratorCli.ts | 10 +++++++-- .../cli/src/custom-config/SdkCustomConfig.ts | 1 + .../schema/SdkCustomConfigSchema.ts | 1 + .../sdk/generator/src/SdkGenerator.ts | 1 + .../src/AbstractGeneratorCli.ts | 4 +++- .../src/publishPackage.ts | 7 ++++-- .../PersistedTypescriptProject.ts | 7 +++++- 11 files changed, 53 insertions(+), 10 deletions(-) diff --git a/generators/csharp/model/src/ModelGeneratorCli.ts b/generators/csharp/model/src/ModelGeneratorCli.ts index c7919fa5ca7..c04bc9460ef 100644 --- a/generators/csharp/model/src/ModelGeneratorCli.ts +++ b/generators/csharp/model/src/ModelGeneratorCli.ts @@ -40,4 +40,8 @@ export class ModelGeneratorCLI extends AbstractGeneratorCli { includeContentHeadersOnFileDownloadResponse: parsed?.includeContentHeadersOnFileDownloadResponse ?? false, noSerdeLayer, noOptionalProperties: parsed?.noOptionalProperties ?? false, - includeApiReference: parsed?.includeApiReference ?? false + includeApiReference: parsed?.includeApiReference ?? false, + tolerateRepublish: parsed?.tolerateRepublish ?? false }; } @@ -99,7 +100,8 @@ export class SdkGeneratorCli extends AbstractGeneratorCli { includeContentHeadersOnFileDownloadResponse: customConfig.includeContentHeadersOnFileDownloadResponse, includeSerdeLayer: !customConfig.noSerdeLayer, noOptionalProperties: customConfig.noOptionalProperties, - includeApiReference: customConfig.includeApiReference ?? false + includeApiReference: customConfig.includeApiReference ?? false, + tolerateRepublish: customConfig.tolerateRepublish } }); const typescriptProject = await sdkGenerator.generate(); @@ -118,4 +120,8 @@ export class SdkGeneratorCli extends AbstractGeneratorCli { protected outputSourceFiles(customConfig: SdkCustomConfig): boolean { return customConfig.outputSourceFiles; } + + protected shouldTolerateRepublish(customConfig: SdkCustomConfig): boolean { + return customConfig.tolerateRepublish; + } } diff --git a/generators/typescript/sdk/cli/src/custom-config/SdkCustomConfig.ts b/generators/typescript/sdk/cli/src/custom-config/SdkCustomConfig.ts index afa04563b33..be0a29be931 100644 --- a/generators/typescript/sdk/cli/src/custom-config/SdkCustomConfig.ts +++ b/generators/typescript/sdk/cli/src/custom-config/SdkCustomConfig.ts @@ -22,4 +22,5 @@ export interface SdkCustomConfig { noSerdeLayer: boolean; noOptionalProperties: boolean; includeApiReference: boolean | undefined; + tolerateRepublish: boolean; } diff --git a/generators/typescript/sdk/cli/src/custom-config/schema/SdkCustomConfigSchema.ts b/generators/typescript/sdk/cli/src/custom-config/schema/SdkCustomConfigSchema.ts index ac674ad6044..03118060580 100644 --- a/generators/typescript/sdk/cli/src/custom-config/schema/SdkCustomConfigSchema.ts +++ b/generators/typescript/sdk/cli/src/custom-config/schema/SdkCustomConfigSchema.ts @@ -18,6 +18,7 @@ export const SdkCustomConfigSchema = z.strictObject({ treatUnknownAsAny: z.optional(z.boolean()), noSerdeLayer: z.optional(z.boolean()), noOptionalProperties: z.optional(z.boolean()), + tolerateRepublish: z.optional(z.boolean()), // beta (not in docs) includeContentHeadersOnFileDownloadResponse: z.optional(z.boolean()), diff --git a/generators/typescript/sdk/generator/src/SdkGenerator.ts b/generators/typescript/sdk/generator/src/SdkGenerator.ts index 907463129dc..b3217446e83 100644 --- a/generators/typescript/sdk/generator/src/SdkGenerator.ts +++ b/generators/typescript/sdk/generator/src/SdkGenerator.ts @@ -91,6 +91,7 @@ export declare namespace SdkGenerator { includeSerdeLayer: boolean; noOptionalProperties: boolean; includeApiReference: boolean; + tolerateRepublish: boolean; } } diff --git a/generators/typescript/utils/abstract-generator-cli/src/AbstractGeneratorCli.ts b/generators/typescript/utils/abstract-generator-cli/src/AbstractGeneratorCli.ts index d5e439d27e4..c35dc78fb88 100644 --- a/generators/typescript/utils/abstract-generator-cli/src/AbstractGeneratorCli.ts +++ b/generators/typescript/utils/abstract-generator-cli/src/AbstractGeneratorCli.ts @@ -97,7 +97,8 @@ export abstract class AbstractGeneratorCli { npmPackage, dryRun: config.dryRun, generatorNotificationService, - typescriptProject + typescriptProject, + shouldTolerateRepublish: this.shouldTolerateRepublish(customConfig) }); await typescriptProject.npmPackAsZipTo({ logger, @@ -170,6 +171,7 @@ export abstract class AbstractGeneratorCli { }): Promise; protected abstract isPackagePrivate(customConfig: CustomConfig): boolean; protected abstract outputSourceFiles(customConfig: CustomConfig): boolean; + protected abstract shouldTolerateRepublish(customConfig: CustomConfig): boolean; } class GeneratorContextImpl implements GeneratorContext { diff --git a/generators/typescript/utils/abstract-generator-cli/src/publishPackage.ts b/generators/typescript/utils/abstract-generator-cli/src/publishPackage.ts index 771bc1c187e..3e9d589a1c9 100644 --- a/generators/typescript/utils/abstract-generator-cli/src/publishPackage.ts +++ b/generators/typescript/utils/abstract-generator-cli/src/publishPackage.ts @@ -8,13 +8,15 @@ export async function publishPackage({ logger, npmPackage, dryRun, - typescriptProject + typescriptProject, + shouldTolerateRepublish }: { generatorNotificationService: GeneratorNotificationService | undefined; logger: Logger; npmPackage: NpmPackage | undefined; dryRun: boolean; typescriptProject: PersistedTypescriptProject; + shouldTolerateRepublish: boolean; }): Promise { if (npmPackage?.publishInfo == null) { throw new Error("npmPackage.publishInfo is not defined."); @@ -30,7 +32,8 @@ export async function publishPackage({ await typescriptProject.publish({ logger, dryRun, - publishInfo: npmPackage.publishInfo + publishInfo: npmPackage.publishInfo, + shouldTolerateRepublish }); await generatorNotificationService?.sendUpdate(FernGeneratorExec.GeneratorUpdate.published(packageCoordinate)); diff --git a/generators/typescript/utils/commons/src/typescript-project/PersistedTypescriptProject.ts b/generators/typescript/utils/commons/src/typescript-project/PersistedTypescriptProject.ts index 18caf50841f..a6d10dfe3fc 100644 --- a/generators/typescript/utils/commons/src/typescript-project/PersistedTypescriptProject.ts +++ b/generators/typescript/utils/commons/src/typescript-project/PersistedTypescriptProject.ts @@ -183,11 +183,13 @@ export class PersistedTypescriptProject { public async publish({ logger, publishInfo, - dryRun + dryRun, + shouldTolerateRepublish }: { logger: Logger; publishInfo: PublishInfo; dryRun: boolean; + shouldTolerateRepublish: boolean; }): Promise { await this.build(logger); @@ -212,6 +214,9 @@ export class PersistedTypescriptProject { if (dryRun) { publishCommand.push("--dry-run"); } + if (shouldTolerateRepublish) { + publishCommand.push("--tolerate-republish"); + } await npm(publishCommand); }