1+ #!/usr/bin/env node
2+ const fs = require ( 'fs' ) ;
3+ const path = require ( 'path' ) ;
4+
5+ // Function to extract imported types from the file
6+ function extractImportedTypes ( content ) {
7+ const typeImports = [ ] ;
8+ const importRegex = / i m p o r t t y p e \{ ( \w + ) \} f r o m ' \. \/ \w + ' ; / g;
9+ let match ;
10+
11+ while ( ( match = importRegex . exec ( content ) ) !== null ) {
12+ typeImports . push ( match [ 1 ] ) ;
13+ }
14+
15+ return typeImports ;
16+ }
17+
18+ function fixJsonValueBug ( content ) {
19+ // Fix the specific pattern in ToJSONTyped functions where 'value' is the parameter
20+ content = content . replace (
21+ / e x p o r t f u n c t i o n ( \w + ) T o J S O N T y p e d \( v a l u e \? : ( [ ^ , ] + ) \| n u l l , i g n o r e D i s c r i m i n a t o r : b o o l e a n = f a l s e \) : a n y \{ [ \s \S ] * ?r e t u r n j s o n ; / g,
22+ ( match ) => match . replace ( 'return json;' , 'return value;' )
23+ ) ;
24+
25+ return content ;
26+ }
27+
28+ // Update both functions to use this more precise fix:
29+
30+ // Function to extract imported types from the file
31+ function extractImportedTypes ( content ) {
32+ const typeImports = [ ] ;
33+ // Match both single-line and multi-line imports
34+ const importRegex = / i m p o r t t y p e \{ ( \w + ) \} f r o m [ ' " ] \. \/ \w + [ ' " ] ; / g;
35+ let match ;
36+
37+ while ( ( match = importRegex . exec ( content ) ) !== null ) {
38+ typeImports . push ( match [ 1 ] ) ;
39+ }
40+
41+ console . log ( ` Found imported types: ${ typeImports . join ( ', ' ) } ` ) ;
42+ return typeImports ;
43+ }
44+
45+ // Function to fix union type definitions
46+ function fixUnionTypes ( filePath ) {
47+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
48+ const originalContent = content ;
49+
50+ // Pattern to match empty type definitions
51+ const emptyTypePattern = / e x p o r t t y p e ( \w + ) = ; / ;
52+
53+ const match = emptyTypePattern . exec ( content ) ;
54+ if ( match ) {
55+ const typeName = match [ 1 ] ;
56+ console . log ( `Fixing empty type: ${ typeName } ` ) ;
57+
58+ // Extract imported types from this file
59+ const importedTypes = extractImportedTypes ( content ) ;
60+
61+ // Filter imported types that are likely part of this union
62+ const unionTypes = importedTypes . filter ( t => {
63+ const isUsed = content . includes ( `instanceof${ t } ` ) ||
64+ content . includes ( `${ t } FromJSON` ) ||
65+ content . includes ( `${ t } ToJSON` ) ;
66+ if ( isUsed ) {
67+ console . log ( ` Type ${ t } is part of the union` ) ;
68+ }
69+ return isUsed ;
70+ } ) ;
71+
72+ if ( unionTypes . length > 0 ) {
73+ // Create the union type
74+ const unionTypeDefinition = `export type ${ typeName } = ${ unionTypes . join ( ' | ' ) } ;` ;
75+
76+ // Replace the empty type definition
77+ content = content . replace (
78+ `export type ${ typeName } = ;` ,
79+ unionTypeDefinition
80+ ) ;
81+
82+ console . log ( ` Replaced with: ${ unionTypeDefinition } ` ) ;
83+ } else {
84+ console . log ( ` WARNING: No union types found for ${ typeName } ` ) ;
85+ }
86+ }
87+
88+ // Only write if we made changes
89+ if ( content !== originalContent ) {
90+ fs . writeFileSync ( filePath , content , 'utf8' ) ;
91+ console . log ( `✅ Fixed ${ filePath } ` ) ;
92+ }
93+ }
94+
95+ function fixAnyTypeImports ( filePath ) {
96+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
97+ const originalContent = content ;
98+
99+ // Remove AnyType imports
100+ content = content . replace ( / i m p o r t t y p e \{ A n y T y p e \} f r o m ' \. \/ A n y T y p e ' ; \n / g, '' ) ;
101+ content = content . replace ( / i m p o r t \{ \s * [ ^ } ] * \s * \} f r o m ' \. \/ A n y T y p e ' ; \n / g, '' ) ;
102+
103+ // Replace AnyType with any
104+ content = content . replace ( / : A n y T y p e / g, ': any' ) ;
105+
106+ // Fix the json/value bug with the more precise function
107+ content = fixJsonValueBug ( content ) ;
108+
109+ // Only write if we made changes
110+ if ( content !== originalContent ) {
111+ fs . writeFileSync ( filePath , content , 'utf8' ) ;
112+ }
113+ }
114+
115+ // Main function to process all TypeScript files
116+ function processTypeScriptFiles ( directory ) {
117+ const modelsDir = path . join ( directory , 'src' , 'models' ) ;
118+
119+ if ( ! fs . existsSync ( modelsDir ) ) {
120+ console . error ( `Models directory not found: ${ modelsDir } ` ) ;
121+ process . exit ( 1 ) ;
122+ }
123+
124+ // Files that typically have union type issues
125+ const filesToFix = [
126+ 'CreateAIPlatformConnectorRequest.ts' ,
127+ 'CreateSourceConnectorRequest.ts' ,
128+ 'CreateDestinationConnectorRequest.ts'
129+ ] ;
130+
131+ filesToFix . forEach ( fileName => {
132+ const filePath = path . join ( modelsDir , fileName ) ;
133+ if ( fs . existsSync ( filePath ) ) {
134+ fixUnionTypes ( filePath ) ;
135+ } else {
136+ console . log ( `⚠️ File not found: ${ fileName } ` ) ;
137+ }
138+ } ) ;
139+
140+ const files = fs . readdirSync ( modelsDir ) ;
141+ files . forEach ( file => {
142+ if ( file . endsWith ( '.ts' ) ) {
143+ const filePath = path . join ( modelsDir , file ) ;
144+ fixAnyTypeImports ( filePath ) ;
145+ }
146+ } ) ;
147+ }
148+
149+ // Get the directory from command line argument
150+ const tsDirectory = process . argv [ 2 ] ;
151+
152+ if ( ! tsDirectory ) {
153+ console . error ( 'Usage: node fix-ts-unions.js <typescript-output-directory>' ) ;
154+ process . exit ( 1 ) ;
155+ }
156+
157+ console . log ( '🔧 Fixing TypeScript union types...' ) ;
158+ processTypeScriptFiles ( tsDirectory ) ;
159+ console . log ( '✨ Done!' ) ;
0 commit comments