ts_schema 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/templates/ts_schema.rb +63 -31
- data/lib/ts_schema/configuration.rb +2 -0
- data/lib/ts_schema/schema_generator.rb +20 -20
- data/lib/ts_schema/version.rb +1 -1
- metadata +1 -2
- data/lib/generators/generate_generator.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8602c4a7ca10962ac35fd7c64a18d0669ed3f7d1ecdfdf7bc9c29a258bff754a
|
4
|
+
data.tar.gz: 322d779a986f2edf18d53829a6a8669326732be7aa4080567d2f4a1ecb1ebac0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88fd048bbffe0eb93ac910bdcaf456a35e4ded1bf934dab9330d5ad1dba4d57ad3106c07a549a92555f312fe4d713d1b1b4b8c7d29d3909d85a7062df96e6426
|
7
|
+
data.tar.gz: 4c778296ba8de708bf9539bdcd163d79370c0477600d2bdbfd98a0e5c0355d3e02872728452be964922032cc880b3ac92ee270f40269e251734fb48ebfb74bcb
|
@@ -1,57 +1,89 @@
|
|
1
|
+
# Default type mappings:
|
2
|
+
#
|
3
|
+
# string: string
|
4
|
+
# text: string
|
5
|
+
# integer: number
|
6
|
+
# enum: number
|
7
|
+
# bigint: number
|
8
|
+
# float: number
|
9
|
+
# decimal: number
|
10
|
+
# json: Record<string, any>
|
11
|
+
# jsonb: Record<string, any>
|
12
|
+
# binary: string
|
13
|
+
# boolean: boolean
|
14
|
+
# date: string
|
15
|
+
# datetime: string
|
16
|
+
# timestamp: string
|
17
|
+
# datetime_with_timezone: string
|
18
|
+
# inet: string
|
19
|
+
# cidr: string
|
20
|
+
# macaddr: string
|
21
|
+
|
1
22
|
TsSchema.setup do |config|
|
2
|
-
#
|
23
|
+
# Case options: camel|snake|pascal
|
24
|
+
#
|
3
25
|
# config.case = :camel
|
4
26
|
|
27
|
+
|
5
28
|
# Customize output path and file name
|
29
|
+
#
|
6
30
|
# config.output = Rails.root.join('app', 'assets', 'javascripts', 'schema.d.ts')
|
7
31
|
|
8
|
-
|
32
|
+
|
33
|
+
# Whether to generate the schema file after running migrations
|
34
|
+
#
|
9
35
|
# config.auto_generate = true
|
10
36
|
|
37
|
+
|
11
38
|
# Add custom type mappings or overrides
|
12
|
-
|
13
|
-
# Default type mappings:
|
14
|
-
#
|
15
|
-
# string: string
|
16
|
-
# text: string
|
17
|
-
# integer: number
|
18
|
-
# enum: number
|
19
|
-
# bigint: number
|
20
|
-
# float: number
|
21
|
-
# decimal: number
|
22
|
-
# json: Record<string, any>
|
23
|
-
# jsonb: Record<string, any>
|
24
|
-
# binary: string
|
25
|
-
# boolean: boolean
|
26
|
-
# date: string
|
27
|
-
# datetime: string
|
28
|
-
# timestamp: string
|
29
|
-
# datetime_with_timezone: string
|
30
|
-
# inet: string
|
31
|
-
# cidr: string
|
32
|
-
# macaddr: string
|
33
|
-
#
|
39
|
+
#
|
34
40
|
# config.custom_types = {
|
35
41
|
#
|
36
42
|
# }
|
37
43
|
|
44
|
+
|
38
45
|
# Default type for unrecognized types
|
46
|
+
#
|
39
47
|
# config.default_type = :string
|
40
48
|
|
41
|
-
|
49
|
+
|
50
|
+
# Whether to generate types for associated models
|
51
|
+
#
|
42
52
|
# config.include_associated = true
|
43
53
|
|
44
|
-
# Additional models to map which don't have a model file
|
45
|
-
# config.additional_models = [
|
46
|
-
#
|
47
|
-
# ]
|
48
54
|
|
49
|
-
#
|
55
|
+
# Parent classes of models to generate (as strings or symbols)
|
56
|
+
# Only classes inheriting from those in this list will have types generated
|
57
|
+
#
|
58
|
+
# config.parent_classes = [
|
59
|
+
# "ApplicationRecord",
|
60
|
+
# ]
|
61
|
+
|
62
|
+
|
63
|
+
# Additional models to generate schema from, such as those added by other gems
|
64
|
+
# which don't have a model file. (as strings or symbols)
|
65
|
+
#
|
66
|
+
# config.additional_models = [
|
67
|
+
#
|
68
|
+
# ]
|
69
|
+
|
70
|
+
|
71
|
+
# Namespace for generated types
|
72
|
+
#
|
50
73
|
# config.namespace = :schema
|
51
74
|
|
52
|
-
|
75
|
+
|
76
|
+
# Output schema as types or interfaces: type|interface
|
77
|
+
#
|
78
|
+
# config.schema_type = :interface
|
79
|
+
|
80
|
+
|
81
|
+
# Indentation options: tab|space
|
82
|
+
#
|
53
83
|
# config.indent = :tab
|
54
84
|
|
85
|
+
|
55
86
|
# If indent is spaces, specify how many
|
87
|
+
#
|
56
88
|
# config.spaces = 2
|
57
89
|
end
|
@@ -7,10 +7,12 @@ module TsSchema
|
|
7
7
|
|
8
8
|
def initialize(config = nil)
|
9
9
|
@config = config || TsSchema::Configuration.new
|
10
|
+
@models = []
|
10
11
|
|
11
12
|
Rails.application.eager_load!
|
12
|
-
|
13
|
-
|
13
|
+
@config.parent_classes.each do |parent|
|
14
|
+
@models.concat(get_subclasses(parent.to_s.constantize))
|
15
|
+
end
|
14
16
|
unless @config.additional_models.empty?
|
15
17
|
@models.concat(@config.additional_models.map do |m|
|
16
18
|
m.to_s.constantize
|
@@ -30,33 +32,19 @@ module TsSchema
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def generate
|
33
|
-
generate_typescript
|
34
|
-
end
|
35
|
-
|
36
|
-
def output_file
|
37
|
-
path = @config.output
|
38
|
-
FileUtils.mkdir_p(File.dirname(path))
|
39
|
-
|
40
|
-
content = generate
|
41
|
-
return if File.exist?(path) && File.read(path) == content
|
42
|
-
|
43
|
-
File.open(path, 'w') do |f|
|
44
|
-
f.write content
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def generate_typescript
|
49
35
|
type_template = ""
|
36
|
+
|
50
37
|
@models.each do |model|
|
51
38
|
columns = map_column_types(model)
|
52
39
|
columns.concat(map_associations(model)) if @config.include_associated
|
53
40
|
|
54
41
|
type_template += <<~TYPESCRIPT
|
55
|
-
|
42
|
+
#{@config.schema_type} #{model.model_name.param_key.camelize} #{@config.schema_type.to_sym == :type ? "= " : ""}{
|
56
43
|
#{columns.map { |column| "#{indent_as_str}#{column_name_cased(column[:name])}: #{column[:ts_type]};" }.join("\n")}
|
57
44
|
}\n
|
58
45
|
TYPESCRIPT
|
59
46
|
end
|
47
|
+
|
60
48
|
type_template = <<~TPL
|
61
49
|
declare namespace #{@config.namespace} {
|
62
50
|
#{indent_wrapper(type_template)}
|
@@ -64,12 +52,24 @@ module TsSchema
|
|
64
52
|
TPL
|
65
53
|
end
|
66
54
|
|
55
|
+
def output_file
|
56
|
+
path = @config.output
|
57
|
+
FileUtils.mkdir_p(File.dirname(path))
|
58
|
+
|
59
|
+
content = generate
|
60
|
+
return if File.exist?(path) && File.read(path) == content
|
61
|
+
|
62
|
+
File.open(path, 'w') do |f|
|
63
|
+
f.write content
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
67
|
def map_column_types(model)
|
68
68
|
model.columns.map { |i|
|
69
69
|
type = @types[i.type.to_s] || @config.default_type
|
70
70
|
|
71
71
|
if(enum = model.defined_enums[i.name])
|
72
|
-
type = enum.keys.map { |k| "'#{k}'" }.join("
|
72
|
+
type = enum.keys.map { |k| "'#{k}'" }.join("|")
|
73
73
|
end
|
74
74
|
|
75
75
|
{
|
data/lib/ts_schema/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ts_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Avram Walden
|
@@ -48,7 +48,6 @@ files:
|
|
48
48
|
- MIT-LICENSE
|
49
49
|
- README.md
|
50
50
|
- Rakefile
|
51
|
-
- lib/generators/generate_generator.rb
|
52
51
|
- lib/generators/install_generator.rb
|
53
52
|
- lib/generators/templates/ts_schema.rb
|
54
53
|
- lib/tasks/ts_schema_tasks.rake
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rails/generators'
|
4
|
-
require 'rails/generators/base'
|
5
|
-
|
6
|
-
module TsSchema
|
7
|
-
module Generators
|
8
|
-
class GenerateGenerator < Rails::Generators::Base
|
9
|
-
|
10
|
-
desc "Generates a schema.d.ts file"
|
11
|
-
|
12
|
-
def generate_schema
|
13
|
-
create_file TsSchema.configuration.output, TsSchema.generate
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|