jsonapi-resources-anchor 1.0.1 → 2.0.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: 5d8d6d47667a4695d9170713bd551579f39719ad329ae4d735f9c2578363f056
|
4
|
+
data.tar.gz: 65d7202de3222920a5da8a97b9e506f7ca7fa955b2a7bb9f117120f186217533
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca66b196dceabe0c3093c506e9080172a89071415fc05f5f8bd51ac1d72f0a54153f85ff8886e96329d466a58c29f8a1f2f5cacdbf10b08d04dfeefb41e33d3f
|
7
|
+
data.tar.gz: f9cb30452f5b90fc8c4cd76519102d538e27aa3e1c75d4acaabc5fadedf396b50105216acb855edc7a819c9a30ec106803053d0ea76c06aad55a99cf81164864
|
@@ -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:
|
4
|
+
version: 2.0.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
|
@@ -113,7 +115,10 @@ files:
|
|
113
115
|
homepage:
|
114
116
|
licenses:
|
115
117
|
- MIT
|
116
|
-
metadata:
|
118
|
+
metadata:
|
119
|
+
changelog_uri: https://github.com/mattkhan/jsonapi-resources-anchor/blob/main/CHANGELOG.md
|
120
|
+
documentation_uri: https://anchor-gem.vercel.app/docs
|
121
|
+
source_code_uri: https://github.com/mattkhan/jsonapi-resources-anchor
|
117
122
|
post_install_message:
|
118
123
|
rdoc_options: []
|
119
124
|
require_paths:
|
@@ -129,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
134
|
- !ruby/object:Gem::Version
|
130
135
|
version: '0'
|
131
136
|
requirements: []
|
132
|
-
rubygems_version: 3.
|
137
|
+
rubygems_version: 3.5.9
|
133
138
|
signing_key:
|
134
139
|
specification_version: 4
|
135
140
|
summary: jsonapi-resources type annotation, inference, and schema/docs generation
|