jsonapi-resources-anchor 1.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f39f4e1690cf63ea4437c270480ddb44170135ca973132c28be67a095add422
|
4
|
+
data.tar.gz: 1af7acc60184d15bb7f2887ce3f906318f5a9ee05cf2fec6f2124dedb5fd9c61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f80462d3f298454a77222182d2fbb3d4a5c58ac3d441dc4108734c609f7630f80a466b32a2f7c729c0df1e15948e02a39b9c6c43be10908a9d38dd17d5f2d6c0
|
7
|
+
data.tar.gz: bcaf6345537c377a37304caa122faa4bff143919ffcbe284b759c694ef4afc33d2f37ab0d31baa9c3e531063d5faf509fa9a95d9450796084cb1980b1775eacf
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Anchor::TypeScript
|
2
|
+
class MultifileSaveService
|
3
|
+
START_MARKER = "// START AUTOGEN\n"
|
4
|
+
END_MARKER = "\n// END AUTOGEN\n"
|
5
|
+
REGEX = /#{Regexp.escape(START_MARKER)}(.*?)#{Regexp.escape(END_MARKER)}/m
|
6
|
+
|
7
|
+
def self.call(...)
|
8
|
+
new(...).call
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(generator:, folder_path:, force: false, resource_file_extension: ".ts")
|
12
|
+
@generator = generator
|
13
|
+
@folder_path = folder_path
|
14
|
+
@force = force
|
15
|
+
@resource_file_extension = "." + resource_file_extension.sub(/^\./, "")
|
16
|
+
end
|
17
|
+
|
18
|
+
def call
|
19
|
+
FileUtils.mkdir_p(@folder_path)
|
20
|
+
results = @generator.call
|
21
|
+
results.each { |result| save_result(result) }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
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)
|
34
|
+
|
35
|
+
if @force || !File.exist?(path)
|
36
|
+
File.open(path, "w") { |f| f.write(result.text) }
|
37
|
+
return
|
38
|
+
end
|
39
|
+
|
40
|
+
existing_content = File.read(path)
|
41
|
+
|
42
|
+
new_content = if manually_editable?(existing_content)
|
43
|
+
replace_between(
|
44
|
+
existing: existing_content,
|
45
|
+
new: result.text,
|
46
|
+
)
|
47
|
+
else
|
48
|
+
result.text
|
49
|
+
end
|
50
|
+
|
51
|
+
File.open(path, "w") { |f| f.write(new_content) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def resource_file_name(name)
|
55
|
+
File.basename(name, ".ts") + @resource_file_extension
|
56
|
+
end
|
57
|
+
|
58
|
+
def manually_editable?(text)
|
59
|
+
!text.match(REGEX).nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
def replace_between(existing:, new:)
|
63
|
+
new_content = extract_between(new)
|
64
|
+
raise "Was @generator initialized with manually_editable: false?" unless new_content
|
65
|
+
|
66
|
+
existing.gsub(REGEX, "#{START_MARKER}\n#{new_content}\n#{END_MARKER}")
|
67
|
+
end
|
68
|
+
|
69
|
+
def extract_between(text)
|
70
|
+
match = text.match(REGEX)
|
71
|
+
match[1].strip
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Anchor::TypeScript
|
2
|
+
class MultifileSchemaGenerator < Anchor::SchemaGenerator
|
3
|
+
Result = Struct.new(:name, :text, :type, keyword_init: true)
|
4
|
+
|
5
|
+
module FileType
|
6
|
+
RESOURCE = "resource"
|
7
|
+
UTIL = "util"
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(register:, context: {}, include_all_fields: false, exclude_fields: nil, manually_editable: true) # rubocop:disable Lint/MissingSuper
|
11
|
+
@register = register
|
12
|
+
@context = context
|
13
|
+
@include_all_fields = include_all_fields
|
14
|
+
@exclude_fields = exclude_fields
|
15
|
+
@manually_editable = manually_editable
|
16
|
+
end
|
17
|
+
|
18
|
+
def call
|
19
|
+
[shared_file] + resource_files
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def shared_file
|
25
|
+
maybe_type = "export type Maybe<T> = T | null;"
|
26
|
+
|
27
|
+
enum_expressions = enums.map(&:express)
|
28
|
+
text = ([maybe_type] + enum_expressions).join("\n\n") + "\n"
|
29
|
+
Result.new(name: "shared.ts", text:, type: FileType::UTIL)
|
30
|
+
end
|
31
|
+
|
32
|
+
def resource_files
|
33
|
+
resources.map do |r|
|
34
|
+
definition = r.definition(
|
35
|
+
context: @context,
|
36
|
+
include_all_fields: @include_all_fields,
|
37
|
+
exclude_fields: @exclude_fields.nil? ? [] : @exclude_fields[r.anchor_schema_name.to_sym],
|
38
|
+
)
|
39
|
+
|
40
|
+
file_structure = ::Anchor::TypeScript::FileStructure.new(definition)
|
41
|
+
text = file_structure.to_code(manually_editable: @manually_editable)
|
42
|
+
name = file_structure.name
|
43
|
+
Result.new(name:, text:, type: FileType::RESOURCE)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def resources
|
48
|
+
@resources ||= @register.resources.map { |r| Anchor::TypeScript::Resource.new(r) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def enums
|
52
|
+
@enums ||= @register.enums.map { |e| Anchor::TypeScript::Types::Enum.new(e) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -32,51 +32,4 @@ module Anchor::TypeScript
|
|
32
32
|
@enums ||= @register.enums.map { |e| Anchor::TypeScript::Types::Enum.new(e) }
|
33
33
|
end
|
34
34
|
end
|
35
|
-
|
36
|
-
class MultifileSchemaGenerator < Anchor::SchemaGenerator
|
37
|
-
def initialize(register:, context: {}, include_all_fields: false, exclude_fields: nil, manually_editable: true) # rubocop:disable Lint/MissingSuper
|
38
|
-
@register = register
|
39
|
-
@context = context
|
40
|
-
@include_all_fields = include_all_fields
|
41
|
-
@exclude_fields = exclude_fields
|
42
|
-
@manually_editable = manually_editable
|
43
|
-
end
|
44
|
-
|
45
|
-
def call
|
46
|
-
[shared_file] + resource_files
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def shared_file
|
52
|
-
maybe_type = "export type Maybe<T> = T | null;"
|
53
|
-
|
54
|
-
enum_expressions = enums.map(&:express)
|
55
|
-
content = ([maybe_type] + enum_expressions).join("\n\n") + "\n"
|
56
|
-
{ name: "shared.ts", content: }
|
57
|
-
end
|
58
|
-
|
59
|
-
def resource_files
|
60
|
-
resources.map do |r|
|
61
|
-
definition = r.definition(
|
62
|
-
context: @context,
|
63
|
-
include_all_fields: @include_all_fields,
|
64
|
-
exclude_fields: @exclude_fields.nil? ? [] : @exclude_fields[r.anchor_schema_name.to_sym],
|
65
|
-
)
|
66
|
-
|
67
|
-
file_structure = ::Anchor::TypeScript::FileStructure.new(definition)
|
68
|
-
content = file_structure.to_code(manually_editable: @manually_editable)
|
69
|
-
name = file_structure.name
|
70
|
-
{ name:, content: }
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def resources
|
75
|
-
@resources ||= @register.resources.map { |r| Anchor::TypeScript::Resource.new(r) }
|
76
|
-
end
|
77
|
-
|
78
|
-
def enums
|
79
|
-
@enums ||= @register.enums.map { |e| Anchor::TypeScript::Types::Enum.new(e) }
|
80
|
-
end
|
81
|
-
end
|
82
35
|
end
|
data/lib/anchor/version.rb
CHANGED
@@ -15,6 +15,8 @@ require "anchor/types/inference/active_record"
|
|
15
15
|
require "anchor/type_script/file_structure"
|
16
16
|
require "anchor/type_script/types"
|
17
17
|
require "anchor/type_script/schema_generator"
|
18
|
+
require "anchor/type_script/multifile_save_service"
|
19
|
+
require "anchor/type_script/multifile_schema_generator"
|
18
20
|
require "anchor/type_script/serializer"
|
19
21
|
require "anchor/type_script/resource"
|
20
22
|
require "anchor/json_schema/serializer"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-resources-anchor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt
|
@@ -101,6 +101,8 @@ files:
|
|
101
101
|
- lib/anchor/schema.rb
|
102
102
|
- lib/anchor/schema_generator.rb
|
103
103
|
- lib/anchor/type_script/file_structure.rb
|
104
|
+
- lib/anchor/type_script/multifile_save_service.rb
|
105
|
+
- lib/anchor/type_script/multifile_schema_generator.rb
|
104
106
|
- lib/anchor/type_script/resource.rb
|
105
107
|
- lib/anchor/type_script/schema_generator.rb
|
106
108
|
- lib/anchor/type_script/serializer.rb
|
@@ -117,6 +119,7 @@ metadata:
|
|
117
119
|
changelog_uri: https://github.com/mattkhan/jsonapi-resources-anchor/blob/main/CHANGELOG.md
|
118
120
|
documentation_uri: https://anchor-gem.vercel.app/docs
|
119
121
|
source_code_uri: https://github.com/mattkhan/jsonapi-resources-anchor
|
122
|
+
github_repo: ssh://github.com/mattkhan/jsonapi-resources-anchor
|
120
123
|
post_install_message:
|
121
124
|
rdoc_options: []
|
122
125
|
require_paths:
|
@@ -132,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
135
|
- !ruby/object:Gem::Version
|
133
136
|
version: '0'
|
134
137
|
requirements: []
|
135
|
-
rubygems_version: 3.
|
138
|
+
rubygems_version: 3.4.20
|
136
139
|
signing_key:
|
137
140
|
specification_version: 4
|
138
141
|
summary: jsonapi-resources type annotation, inference, and schema/docs generation
|