Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "contentstack-cli-tsgen",
"description": "Generate TypeScript typings from a Stack.",
"version": "3.0.0",
"version": "3.0.1",
"author": "Michael Davis",
"bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues",
"dependencies": {
Expand Down
51 changes: 22 additions & 29 deletions src/lib/tsgen/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default function (userOptions: TSGenOptions) {
const cachedGlobalFields: GlobalFieldCache = {}
const cachedModularBlocks: ModularBlockCache = {}
const modularBlockInterfaces = new Set<string>()
const uniqueBlockInterfaces = new Set<string>()
let counter = 1

const typeMap: TypeMap = {
text: {func: type_text, track: true, flag: TypeFlags.BuiltinJS},
Expand Down Expand Up @@ -178,21 +180,6 @@ export default function (userOptions: TSGenOptions) {
return op_paren(choices.map(v => get_value(v)).join(' | '))
}

function visit_block_names(
field: ContentstackTypes.Field,
except: ContentstackTypes.Block
) {
const uids: string[] = []

field.blocks.forEach(block => {
if (block.uid !== except.uid) {
uids.push(`${block.uid}: undefined;`)
}
})

return uids.join('\n')
}

function visit_field_type(field: ContentstackTypes.Field) {
let type = 'any'

Expand Down Expand Up @@ -260,28 +247,34 @@ export default function (userOptions: TSGenOptions) {
}

function type_modular_blocks(field: ContentstackTypes.Field): string {
const blockInterfaceName = name_type(field.uid);
if(!cachedModularBlocks[blockInterfaceName]){
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}`;
});

let 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 blockInterfacesKey = blockInterfaces.join(';');

if(!uniqueBlockInterfaces.has(blockInterfacesKey)) {
uniqueBlockInterfaces.add(blockInterfacesKey);
// Keep appending a counter until a unique name is found
while (cachedModularBlocks[blockInterfaceName]) {
blockInterfaceName = `${blockInterfaceName}${counter}`;
counter++;
}
const modularInterface = [
`export interface ${blockInterfaceName} {`,
blockInterfaces.join('\n'),
'}',
].join('\n');

// Store or track the generated block interface for later use
modularBlockInterfaces.add(modularInterface);
cachedModularBlocks[blockInterfaceName] = blockInterfaceName;
}

return field.multiple ? `${blockInterfaceName}[]` : blockInterfaceName;
}

Expand Down
Loading