Skip to content

Commit 34da0d5

Browse files
committed
fix: properly handle custom resource file extensions
1 parent e03ecfa commit 34da0d5

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

lib/anchor/type_script/file_structure.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ def self.export_code(identifier)
2424
end
2525
end
2626

27-
def initialize(definition)
27+
def initialize(definition, extension: ".ts")
2828
@definition = definition
2929
@name = definition.name
3030
@object = definition.object
31+
@extension = extension
3132
end
3233

3334
def name
34-
"#{@definition.name}.ts"
35+
"#{@definition.name}#{@extension}"
3536
end
3637

3738
def to_code(manually_editable: false)
@@ -66,7 +67,7 @@ def shared_imports
6667
def relationship_imports
6768
relationships_to_import
6869
.reject { |type| type.anchor_schema_name == @name }
69-
.map { |type| Import.new(file_name: "#{type.anchor_schema_name}.ts", type:) }
70+
.map { |type| Import.new(file_name: "#{type.anchor_schema_name}#{@extension}", type:) }
7071
end
7172

7273
def relationships_to_import

lib/anchor/type_script/multifile_save_service.rb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ def self.call(...)
88
new(...).call
99
end
1010

11-
def initialize(generator:, folder_path:, force: false, resource_file_extension: ".ts")
11+
def initialize(generator:, folder_path:, force: false)
1212
@generator = generator
1313
@folder_path = folder_path
1414
@force = force
15-
@resource_file_extension = "." + resource_file_extension.sub(/^\./, "")
1615
end
1716

1817
def call
@@ -24,13 +23,7 @@ def call
2423
private
2524

2625
def save_result(result)
27-
filename = if result.type == MultifileSchemaGenerator::FileType::RESOURCE
28-
resource_file_name(result.name)
29-
else
30-
result.name
31-
end
32-
33-
path = Rails.root.join(@folder_path, filename)
26+
path = Rails.root.join(@folder_path, result.name)
3427

3528
if @force || !File.exist?(path)
3629
File.open(path, "w") { |f| f.write(result.text) }
@@ -51,10 +44,6 @@ def save_result(result)
5144
File.open(path, "w") { |f| f.write(new_content) }
5245
end
5346

54-
def resource_file_name(name)
55-
File.basename(name, ".ts") + @resource_file_extension
56-
end
57-
5847
def manually_editable?(text)
5948
!text.match(REGEX).nil?
6049
end

lib/anchor/type_script/multifile_schema_generator.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ module FileType
77
UTIL = "util"
88
end
99

10-
def initialize(register:, context: {}, include_all_fields: false, exclude_fields: nil, manually_editable: true) # rubocop:disable Lint/MissingSuper
10+
def initialize( # rubocop:disable Lint/MissingSuper
11+
register:,
12+
context: {},
13+
include_all_fields: false,
14+
exclude_fields: nil,
15+
manually_editable: true,
16+
resource_file_extension: ".ts"
17+
)
1118
@register = register
1219
@context = context
1320
@include_all_fields = include_all_fields
1421
@exclude_fields = exclude_fields
1522
@manually_editable = manually_editable
23+
@resource_file_extension = "." + resource_file_extension.sub(/^\./, "")
1624
end
1725

1826
def call
@@ -37,7 +45,7 @@ def resource_files
3745
exclude_fields: @exclude_fields.nil? ? [] : @exclude_fields[r.anchor_schema_name.to_sym],
3846
)
3947

40-
file_structure = ::Anchor::TypeScript::FileStructure.new(definition)
48+
file_structure = ::Anchor::TypeScript::FileStructure.new(definition, extension: @resource_file_extension)
4149
text = file_structure.to_code(manually_editable: @manually_editable)
4250
name = file_structure.name
4351
Result.new(name:, text:, type: FileType::RESOURCE)

0 commit comments

Comments
 (0)