From 3611accf33ee27bb58ae87132840ce446148b672 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Mon, 26 Aug 2024 16:05:12 +0530 Subject: [PATCH 1/4] feat: generate separate interface for modular blocks --- MIGRATION.md | 51 ++++++++++++++++++++++++++ README.md | 57 +++++++++++++----------------- package-lock.json | 4 +-- package.json | 2 +- src/lib/tsgen/factory.ts | 35 ++++++++++++++---- tests/tsgen/modular.blocks.test.ts | 24 ++++++++----- 6 files changed, 123 insertions(+), 50 deletions(-) create mode 100644 MIGRATION.md diff --git a/MIGRATION.md b/MIGRATION.md new file mode 100644 index 0000000..66f5ec4 --- /dev/null +++ b/MIGRATION.md @@ -0,0 +1,51 @@ +## Migrating from v2 to v3 +This migration document details the changes needed to separate nested modular blocks into distinct interfaces. This update will impact how modular blocks are represented and utilized throughout the codebase. + +## Before Example +export interface Test { + /** Version */ + _version?: 2; + /** Title */ + title: string; + /** Modular Blocks */ + modular_blocks?: { + test: { + /** Multi Line Textbox */ + multi_line?: string; + /** Rich Text Editor */ + rich_text_editor?: string; + /** Modular Blocks1 */ + modular_blocks1?: { + test1: { + /** Multi Line Textbox */ + multi_line?: string; + }; + }[]; + }; + }[]; +} + + +## After +export interface Test { + /** Version */ + _version: 2; + /** Title */ + title: string; + /** Modular Blocks */ + modular_blocks?: ModularBlocks[]; +} + +export interface ModularBlocks { + /** Multi Line Textbox */ + multi_line?: string; + /** Rich Text Editor */ + rich_text_editor?: string; + /** Modular Blocks1 */ + modular_blocks1?: ModularBlocks1[]; +} + +export interface ModularBlocks1 { + /** Multi Line Textbox */ + multi_line?: string; +} diff --git a/README.md b/README.md index cb0e6a2..954ef44 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ This plugin generates TypeScript typings from Content Types. Interfaces and fiel $ csdx plugins:install contentstack-cli-tsgen ``` +## Migration +See the V3 [Migration Guide](https://github.com/contentstack/contentstack-cli-tsgen/blob/master/MIGRATION.md) if you are migrating from v2 or older. + ## How to use this plugin `$ csdx tsgen` @@ -92,38 +95,7 @@ interface BuiltinExample { /** Single Choice */ single_choice: "Choice 1" | "Choice 2" | "Choice 3"; /** Modular Blocks */ - modular_blocks?: ( - | { - block_1: { - /** Number */ - number?: number; - /** Single line textbox */ - single_line?: string; - }; - block_2: undefined; - seo_gf: undefined; - } - | { - block_2: { - /** Boolean */ - boolean?: boolean; - /** Date */ - date?: string; - }; - block_1: undefined; - seo_gf: undefined; - } - | { - seo_gf: { - /** Keywords */ - keywords?: string; - /** Description */ - description?: string; - }; - block_1: undefined; - block_2: undefined; - } - )[]; + modular_blocks?:ModularBlocks[]; /** Number */ number?: number; /** Link */ @@ -135,4 +107,25 @@ interface BuiltinExample { /** Date */ date?: string; } + +interface ModularBlocks { + block_1: { + /** Number */ + number?: number; + /** Single line textbox */ + single_line?: string; + }; + block_2: { + /** Boolean */ + boolean?: boolean; + /** Date */ + date?: string; + }; + seo_gf: { + /** Keywords */ + keywords?: string; + /** Description */ + description?: string; + }; +} ``` diff --git a/package-lock.json b/package-lock.json index 0c15fe1..63e575e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "contentstack-cli-tsgen", - "version": "2.3.2", + "version": "3.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "contentstack-cli-tsgen", - "version": "2.3.2", + "version": "3.0.0", "license": "MIT", "dependencies": { "@contentstack/cli-command": "^1.2.17", diff --git a/package.json b/package.json index b81dbdd..0f21ecc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "contentstack-cli-tsgen", "description": "Generate TypeScript typings from a Stack.", - "version": "2.3.2", + "version": "3.0.0", "author": "Michael Davis", "bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues", "dependencies": { diff --git a/src/lib/tsgen/factory.ts b/src/lib/tsgen/factory.ts index a48d65f..9784e56 100644 --- a/src/lib/tsgen/factory.ts +++ b/src/lib/tsgen/factory.ts @@ -65,6 +65,7 @@ export default function (userOptions: TSGenOptions) { const visitedGlobalFields = new Set() const visitedContentTypes = new Set() const cachedGlobalFields: GlobalFieldCache = {} + const modularBlockInterfaces = new Set() const typeMap: TypeMap = { text: {func: type_text, track: true, flag: TypeFlags.BuiltinJS}, @@ -215,6 +216,8 @@ export default function (userOptions: TSGenOptions) { if (field.multiple) { fieldType += "[]"; } + }else if (field.data_type === 'blocks') { + fieldType = type_modular_blocks(field); } return [ field.uid + op_required(field.mandatory) + ':', @@ -235,7 +238,7 @@ export default function (userOptions: TSGenOptions) { function visit_content_type( contentType: ContentstackTypes.ContentType | ContentstackTypes.GlobalField ) { - return [ + const contentTypeInterface = [ options.docgen.interface(contentType.description), define_interface(contentType, options.systemFields), '{', @@ -246,8 +249,10 @@ export default function (userOptions: TSGenOptions) { ] .filter(v => v) .join('\n') - } + return [...modularBlockInterfaces, contentTypeInterface].join('\n\n'); + } + function visit_modular_block( field: ContentstackTypes.Field, block: ContentstackTypes.Block @@ -260,11 +265,29 @@ export default function (userOptions: TSGenOptions) { ) } - function type_modular_blocks(field: ContentstackTypes.Field) { - return op_paren( - field.blocks.map(block => visit_modular_block(field, block)).join(' | ') - ) + function type_modular_blocks(field: ContentstackTypes.Field): string { + const blockInterfaceName = name_type(field.uid); + const blockInterfaces = field.blocks.map((block) => { + const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)] + ? name_type(block.reference_to) + : visit_fields(block.schema || []); + + const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`; + return `${block.uid}: ${schema}`; + }); + + const modularInterface = [ + `export interface ${blockInterfaceName} {`, + blockInterfaces.join('\n'), + '}', + ].join('\n'); + + // Store or track the generated block interface for later use + modularBlockInterfaces.add(modularInterface); + + return field.multiple ? `${blockInterfaceName}[]` : blockInterfaceName; } + function type_group(field: ContentstackTypes.Field) { return ['{', visit_fields(field.schema), '}'].filter(v => v).join('\n') diff --git a/tests/tsgen/modular.blocks.test.ts b/tests/tsgen/modular.blocks.test.ts index 06cc63b..dea74a7 100644 --- a/tests/tsgen/modular.blocks.test.ts +++ b/tests/tsgen/modular.blocks.test.ts @@ -18,20 +18,26 @@ describe("modular blocks", () => { test("definition", () => { expect(result.definition).toMatchInlineSnapshot(` - "export interface ModularBlocks + "export interface ModularBlocks { + string_block: { + single_line?: string ; + multi_line?: string ; + markdown?: string ; + rich_text_editor?: string ; } + string_block_with_options: { + single_line_textbox_required: string ; + single_line_textbox_multiple?: string[] ; } + boolean_block: { + boolean?: boolean ; } + } + + export interface ModularBlocks { /** Version */ _version: 2 ; title: string ; url: string ; - modular_blocks?: ({string_block: {single_line?: string ; - multi_line?: string ; - markdown?: string ; - rich_text_editor?: string ;};string_block_with_options: undefined; - boolean_block: undefined;} | {string_block_with_options: {single_line_textbox_required: string ; - single_line_textbox_multiple?: string[] ;};string_block: undefined; - boolean_block: undefined;} | {boolean_block: {boolean?: boolean ;};string_block: undefined; - string_block_with_options: undefined;})[] ; + modular_blocks?: ModularBlocks[] ; }" `); }); From 7dc3b30dddb8a94f07566865b4adae75091e98b5 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Mon, 26 Aug 2024 16:29:06 +0530 Subject: [PATCH 2/4] updated migration file --- MIGRATION.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/MIGRATION.md b/MIGRATION.md index 66f5ec4..3431a6f 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -1,7 +1,8 @@ ## Migrating from v2 to v3 This migration document details the changes needed to separate nested modular blocks into distinct interfaces. This update will impact how modular blocks are represented and utilized throughout the codebase. -## Before Example +## Before +```typescript export interface Test { /** Version */ _version?: 2; @@ -24,9 +25,11 @@ export interface Test { }; }[]; } +``` ## After +```typescript export interface Test { /** Version */ _version: 2; @@ -36,6 +39,7 @@ export interface Test { modular_blocks?: ModularBlocks[]; } + export interface ModularBlocks { /** Multi Line Textbox */ multi_line?: string; @@ -49,3 +53,4 @@ export interface ModularBlocks1 { /** Multi Line Textbox */ multi_line?: string; } +``` \ No newline at end of file From 4ee9ea4ab6052b35dbd436c9ecb54f29f5a82ded Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Tue, 27 Aug 2024 11:17:27 +0530 Subject: [PATCH 3/4] refactor: readme file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 954ef44..39d9e9b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ $ csdx plugins:install contentstack-cli-tsgen ``` ## Migration -See the V3 [Migration Guide](https://github.com/contentstack/contentstack-cli-tsgen/blob/master/MIGRATION.md) if you are migrating from v2 or older. +See the v3 [Migration Guide](https://github.com/contentstack/contentstack-cli-tsgen/blob/master/MIGRATION.md) if you are migrating from v2 or older. ## How to use this plugin From 07e1c6f18848e6d2dc1933d8b2978a3493ff98af Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Tue, 27 Aug 2024 15:15:14 +0530 Subject: [PATCH 4/4] refactor: migration & readme file --- MIGRATION.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 3431a6f..da8f57f 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -1,5 +1,5 @@ ## Migrating from v2 to v3 -This migration document details the changes needed to separate nested modular blocks into distinct interfaces. This update will impact how modular blocks are represented and utilized throughout the codebase. +This document outlines the necessary changes to separate nested modular blocks into distinct interfaces. This update will affect how modular blocks are structured and used throughout the codebase. ## Before ```typescript diff --git a/README.md b/README.md index 39d9e9b..fbaad89 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ $ csdx plugins:install contentstack-cli-tsgen ``` ## Migration -See the v3 [Migration Guide](https://github.com/contentstack/contentstack-cli-tsgen/blob/master/MIGRATION.md) if you are migrating from v2 or older. +Refer to the [Migration Guide](https://github.com/contentstack/contentstack-cli-tsgen/blob/master/MIGRATION.md) version 3 if you are migrating from version 2 or older. ## How to use this plugin