@@ -4,6 +4,11 @@ import * as ContentstackTypes from "../types/schema";
44import * as _ from "lodash" ;
55import { CSLP_HELPERS } from "./shared/cslp-helpers" ;
66import { cliux } from "@contentstack/cli-utilities" ;
7+ import {
8+ isNumericIdentifier ,
9+ NUMERIC_IDENTIFIER_EXCLUSION_REASON ,
10+ checkNumericIdentifierExclusion ,
11+ } from "./shared/utils" ;
712
813export type TSGenOptions = {
914 docgen : DocumentationGenerator ;
@@ -87,11 +92,6 @@ export default function (userOptions: TSGenOptions) {
8792 const skippedBlocks : Array < { uid : string ; path : string ; reason : string } > =
8893 [ ] ;
8994
90- // Helper function to check if a key starts with a number
91- function isNumericKey ( key : string ) : boolean {
92- return / ^ \d / . test ( key ) ;
93- }
94-
9595 const typeMap : TypeMap = {
9696 text : { func : type_text , track : true , flag : TypeFlags . BuiltinJS } ,
9797 number : { func : type_number , track : true , flag : TypeFlags . BuiltinJS } ,
@@ -234,7 +234,7 @@ export default function (userOptions: TSGenOptions) {
234234 `Skipped field "${ field . uid } " with unknown type "${ field . data_type } ": ${ reason } ` ,
235235 { color : "yellow" }
236236 ) ;
237- type = "unknown" ; // Use unknown as fallback for better type safety
237+ type = "Record<string, unknown> " ; // Use Record<string, unknown> for balanced type safety
238238 }
239239 }
240240
@@ -243,12 +243,14 @@ export default function (userOptions: TSGenOptions) {
243243
244244 const handleGlobalField = ( field : ContentstackTypes . Field ) : string => {
245245 // Skip global field references with numeric UIDs
246- if ( isNumericKey ( field . reference_to ) ) {
247- const reason =
248- "TypeScript constraint: object keys cannot start with numbers" ;
249- skippedFields . push ( { uid : field . uid , path : field . uid , reason } ) ;
246+ const exclusionCheck = checkNumericIdentifierExclusion (
247+ field . reference_to ,
248+ field . uid
249+ ) ;
250+ if ( exclusionCheck . shouldExclude ) {
251+ skippedFields . push ( exclusionCheck . record ! ) ;
250252 cliux . print (
251- `Skipped global field reference "${ field . uid } " to "${ field . reference_to } ": ${ reason } ` ,
253+ `Skipped global field reference "${ field . uid } " to "${ field . reference_to } ": ${ NUMERIC_IDENTIFIER_EXCLUSION_REASON } ` ,
252254 { color : "yellow" }
253255 ) ;
254256 return "string" ; // Use string as fallback for global field references
@@ -295,17 +297,15 @@ export default function (userOptions: TSGenOptions) {
295297
296298 for ( const field of schema ) {
297299 // Skip fields with numeric UIDs
298- if ( isNumericKey ( field . uid ) ) {
299- const fieldPath = path ? `${ path } .${ field . uid } ` : field . uid ;
300- const reason =
301- "TypeScript constraint: object keys cannot start with numbers" ;
302- skippedFields . push ( {
303- uid : field . uid ,
304- path : fieldPath ,
305- reason,
306- } ) ;
300+ const fieldPath = path ? `${ path } .${ field . uid } ` : field . uid ;
301+ const exclusionCheck = checkNumericIdentifierExclusion (
302+ field . uid ,
303+ fieldPath
304+ ) ;
305+ if ( exclusionCheck . shouldExclude ) {
306+ skippedFields . push ( exclusionCheck . record ! ) ;
307307 cliux . print (
308- ` Skipped field "${ field . uid } " at path "${ fieldPath } ": ${ reason } ` ,
308+ `Skipped field "${ field . uid } " at path "${ fieldPath } ": ${ NUMERIC_IDENTIFIER_EXCLUSION_REASON } ` ,
309309 { color : "yellow" }
310310 ) ;
311311 continue ;
@@ -361,17 +361,15 @@ export default function (userOptions: TSGenOptions) {
361361 const modularBlockDefinitions = field . blocks
362362 . map ( ( block ) => {
363363 // Skip blocks with numeric UIDs
364- if ( isNumericKey ( block . uid ) ) {
365- const blockPath = `${ field . uid } .blocks.${ block . uid } ` ;
366- const reason =
367- "TypeScript constraint: object keys cannot start with numbers" ;
368- skippedBlocks . push ( {
369- uid : block . uid ,
370- path : blockPath ,
371- reason,
372- } ) ;
364+ const blockPath = `${ field . uid } .blocks.${ block . uid } ` ;
365+ const exclusionCheck = checkNumericIdentifierExclusion (
366+ block . uid ,
367+ blockPath
368+ ) ;
369+ if ( exclusionCheck . shouldExclude ) {
370+ skippedBlocks . push ( exclusionCheck . record ! ) ;
373371 cliux . print (
374- `Skipped block "${ block . uid } " at path "${ blockPath } ": ${ reason } ` ,
372+ `Skipped block "${ block . uid } " at path "${ blockPath } ": ${ NUMERIC_IDENTIFIER_EXCLUSION_REASON } ` ,
375373 { color : "yellow" }
376374 ) ;
377375 return null ; // Return null to filter out later
@@ -467,13 +465,18 @@ export default function (userOptions: TSGenOptions) {
467465
468466 function type_global_field ( field : ContentstackTypes . GlobalField ) {
469467 // Skip global fields with numeric UIDs
470- if ( isNumericKey ( field . uid ) ) {
471- const reason =
472- "TypeScript constraint: object keys cannot start with numbers" ;
473- skippedFields . push ( { uid : field . uid , path : field . uid , reason } ) ;
474- cliux . print ( `Skipped global field "${ field . uid } ": ${ reason } ` , {
475- color : "yellow" ,
476- } ) ;
468+ const exclusionCheck = checkNumericIdentifierExclusion (
469+ field . uid ,
470+ field . uid
471+ ) ;
472+ if ( exclusionCheck . shouldExclude ) {
473+ skippedFields . push ( exclusionCheck . record ! ) ;
474+ cliux . print (
475+ `Skipped global field "${ field . uid } ": ${ NUMERIC_IDENTIFIER_EXCLUSION_REASON } ` ,
476+ {
477+ color : "yellow" ,
478+ }
479+ ) ;
477480 return "string" ; // Use string as fallback for global fields
478481 }
479482
@@ -496,22 +499,22 @@ export default function (userOptions: TSGenOptions) {
496499 if ( Array . isArray ( field . reference_to ) ) {
497500 field . reference_to . forEach ( ( v ) => {
498501 // Skip references to content types with numeric names
499- if ( ! isNumericKey ( v ) ) {
502+ if ( ! isNumericIdentifier ( v ) ) {
500503 references . push ( name_type ( v ) ) ;
501504 } else {
502505 cliux . print (
503- `Skipped reference to content type "${ v } ": TypeScript constraint: object keys cannot start with numbers ` ,
506+ `Skipped reference to content type "${ v } ": ${ NUMERIC_IDENTIFIER_EXCLUSION_REASON } ` ,
504507 { color : "yellow" }
505508 ) ;
506509 }
507510 } ) ;
508511 } else {
509512 // Skip references to content types with numeric names
510- if ( ! isNumericKey ( field . reference_to ) ) {
513+ if ( ! isNumericIdentifier ( field . reference_to ) ) {
511514 references . push ( name_type ( field . reference_to ) ) ;
512515 } else {
513516 cliux . print (
514- `Skipped reference to content type "${ field . reference_to } ": TypeScript constraint: object keys cannot start with numbers ` ,
517+ `Skipped reference to content type "${ field . reference_to } ": ${ NUMERIC_IDENTIFIER_EXCLUSION_REASON } ` ,
515518 { color : "yellow" }
516519 ) ;
517520 }
@@ -558,7 +561,8 @@ export default function (userOptions: TSGenOptions) {
558561
559562 // Log summary table of skipped fields and blocks
560563 if ( skippedFields . length > 0 || skippedBlocks . length > 0 ) {
561- cliux . print ( "\n Summary of Skipped Items:" , {
564+ cliux . print ( "" ) ;
565+ cliux . print ( "Summary of Skipped Items:" , {
562566 color : "cyan" ,
563567 bold : true ,
564568 } ) ;
@@ -591,7 +595,8 @@ export default function (userOptions: TSGenOptions) {
591595 ) ;
592596
593597 const totalSkipped = skippedFields . length + skippedBlocks . length ;
594- cliux . print ( `\n Total skipped items: ${ totalSkipped } ` , {
598+ cliux . print ( "" , { } ) ;
599+ cliux . print ( `Total skipped items: ${ totalSkipped } ` , {
595600 color : "yellow" ,
596601 } ) ;
597602 cliux . success ( " Generation completed successfully with partial schema." ) ;
0 commit comments