@@ -28,6 +28,9 @@ def initialize(output_ts_file = DEFAULT_OUTPUT_TS_FILE, log_struct_classes = nil
2828 # Public method to export TypeScript definitions and JSON key mappings to files
2929 sig { void }
3030 def export
31+ # Get the data once and reuse for all exports
32+ data = generate_data
33+
3134 # Export TypeScript definitions
3235 puts "Exporting LogStruct types to TypeScript..."
3336 puts "Output file: #{ @output_ts_file } "
@@ -36,7 +39,7 @@ def export
3639 FileUtils . mkdir_p ( File . dirname ( @output_ts_file ) )
3740
3841 # Generate the TypeScript content
39- content = generate_typescript_definitions
42+ content = generate_typescript ( data )
4043
4144 # Write to file
4245 File . write ( @output_ts_file , content )
@@ -45,6 +48,9 @@ def export
4548
4649 # Export LOG_KEYS mapping to JSON
4750 export_keys_to_json
51+
52+ # Export enums and log structs to JSON
53+ export_data_to_json ( data )
4854 end
4955
5056 # Export LOG_KEYS mapping to a JSON file
@@ -70,15 +76,108 @@ def export_keys_to_json(output_json_file = nil)
7076 puts "Exported key mappings to #{ output_json_file } "
7177 end
7278
79+ # Export both enums and log structs to JSON files
80+ sig { params ( data : T ::Hash [ Symbol , T . untyped ] ) . void }
81+ def export_data_to_json ( data )
82+ # Export enums to JSON
83+ export_enums_to_json ( data [ :enums ] )
84+
85+ # Export log structs to JSON
86+ export_log_structs_to_json ( data [ :logs ] )
87+ end
88+
89+ # Export Sorbet enums to a JSON file
90+ sig { params ( enums_data : T ::Hash [ Symbol , T ::Array [ String ] ] , output_json_file : T . nilable ( String ) ) . void }
91+ def export_enums_to_json ( enums_data , output_json_file = nil )
92+ # Default to the same directory as the TypeScript file
93+ output_json_file ||= File . join ( File . dirname ( @output_ts_file ) , "sorbet-enums.json" )
94+
95+ puts "Exporting Sorbet enums to JSON..."
96+ puts "Output file: #{ output_json_file } "
97+
98+ # Create output directory if needed
99+ FileUtils . mkdir_p ( File . dirname ( output_json_file ) )
100+
101+ # Format enum data for JSON
102+ json_enum_data = { }
103+
104+ # For each enum, get the full class name and values
105+ T ::Enum . subclasses
106+ . select { |klass | klass . name . to_s . start_with? ( "LogStruct::" ) }
107+ . each do |enum_class |
108+ # Get the full enum name (e.g., "LogStruct::LogLevel")
109+ full_name = enum_class . name . to_s
110+
111+ # Get the simple name (e.g., "LogLevel")
112+ simple_name = full_name . split ( "::" ) . last
113+
114+ # Skip if we don't have data for this enum
115+ next unless enums_data . key? ( simple_name . to_sym )
116+
117+ # Map enum values to their constant names
118+ values_with_names = enum_class . values . map do |value |
119+ constant_name = enum_class . constants . find { |const_name | enum_class . const_get ( const_name ) == value } &.to_s
120+ serialized = value . serialize
121+
122+ # Return a hash with name and value
123+ {
124+ name : constant_name ,
125+ value : serialized
126+ }
127+ end
128+
129+ # Add to the JSON data
130+ json_enum_data [ full_name ] = values_with_names
131+ end
132+
133+ # Write to file with pretty formatting
134+ File . write ( output_json_file , JSON . pretty_generate ( json_enum_data ) )
135+
136+ puts "Exported Sorbet enums to #{ output_json_file } "
137+ end
138+
139+ # Export LogStruct log structs to a JSON file
140+ sig { params ( logs_data : T ::Hash [ String , T ::Hash [ Symbol , T . untyped ] ] , output_json_file : T . nilable ( String ) ) . void }
141+ def export_log_structs_to_json ( logs_data , output_json_file = nil )
142+ # Default to the same directory as the TypeScript file
143+ output_json_file ||= File . join ( File . dirname ( @output_ts_file ) , "sorbet-log-structs.json" )
144+
145+ puts "Exporting LogStruct log structs to JSON..."
146+ puts "Output file: #{ output_json_file } "
147+
148+ # Create output directory if needed
149+ FileUtils . mkdir_p ( File . dirname ( output_json_file ) )
150+
151+ # Format structs data for JSON
152+ json_structs_data = { }
153+
154+ # Process each log struct class
155+ logs_data . each do |struct_name , struct_info |
156+ # Get the full class name
157+ full_name = "LogStruct::Log::#{ struct_name } "
158+
159+ # Add to the structs data
160+ json_structs_data [ full_name ] = {
161+ name : struct_name ,
162+ fields : struct_info [ :fields ] . transform_keys ( &:to_s )
163+ }
164+ end
165+
166+ # Write to file with pretty formatting
167+ File . write ( output_json_file , JSON . pretty_generate ( json_structs_data ) )
168+
169+ puts "Exported LogStruct log structs to #{ output_json_file } "
170+ end
171+
73172 # Public method to generate TypeScript definitions as a string
74173 # This is the method we can test easily without file I/O
75174 sig { returns ( String ) }
76175 def generate_typescript_definitions
77176 # Get the data
78- log_types_data = generate_data
177+ data = generate_data
79178
80179 # Transform data to TypeScript
81- generate_typescript ( log_types_data )
180+ generate_typescript ( data )
82181 end
83182
84183 sig { returns ( T ::Hash [ Symbol , T . untyped ] ) }
0 commit comments