ts_schema 0.1.3 → 0.1.6
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 +4 -4
- data/lib/generators/generate_generator.rb +17 -0
- data/lib/generators/templates/ts_schema.rb +27 -0
- data/lib/tasks/ts_schema_tasks.rake +9 -5
- data/lib/ts_schema/configuration.rb +1 -0
- data/lib/ts_schema/conversion_table.yml +1 -1
- data/lib/ts_schema/schema_generator.rb +15 -14
- data/lib/ts_schema/version.rb +1 -1
- data/lib/ts_schema.rb +5 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd4c606265d13a68760b14f6a3e439e8b6e4bf0dbf5a82262a98d8ed30702107
|
4
|
+
data.tar.gz: bced11b47a17ed266bb1d04edbc0e9e962370e802455f8b01a5c314a63a5d069
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd921fb8519068436415f72f03307cb9ed4442e5f90e0119063426a0949ebba3ffc019e1803bd30d0c2cc1a975978ca1ee2c4abdd87d97aafae1519d27c1024c
|
7
|
+
data.tar.gz: 2e0e87c7355b7b42baac898aaff6c4e22aa04122ecc75b28f88d50b31be94577e96506c3d011c5ffae87d205429f5352aa2570e3902a3ddd475589e89d6302ec
|
@@ -0,0 +1,17 @@
|
|
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
|
@@ -9,6 +9,28 @@ TsSchema.setup do |config|
|
|
9
9
|
# config.auto_generate = true
|
10
10
|
|
11
11
|
# 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
|
+
#
|
12
34
|
# config.custom_types = {
|
13
35
|
#
|
14
36
|
# }
|
@@ -19,6 +41,11 @@ TsSchema.setup do |config|
|
|
19
41
|
# Whether to generate types for associations
|
20
42
|
# config.include_associated = true
|
21
43
|
|
44
|
+
# Additional models to map which don't have a model file
|
45
|
+
# config.additional_models = [
|
46
|
+
#
|
47
|
+
# ]
|
48
|
+
|
22
49
|
# Namespace
|
23
50
|
# config.namespace = :schema
|
24
51
|
|
@@ -3,24 +3,28 @@ require "rake"
|
|
3
3
|
namespace :ts_schema do
|
4
4
|
desc "Generates a schema file in the default javascripts location, or the location specified in the ts_config initializer options"
|
5
5
|
task :generate do
|
6
|
-
TsSchema.
|
6
|
+
TsSchema.output_file
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
namespace :db do
|
11
|
+
def auto_generate_and_save_file
|
12
|
+
TsSchema.output_file if TsSchema.configuration.auto_generate
|
13
|
+
end
|
14
|
+
|
11
15
|
task migrate: :environment do
|
12
|
-
|
16
|
+
auto_generate_and_save_file
|
13
17
|
end
|
14
18
|
|
15
19
|
task rollback: :environment do
|
16
|
-
|
20
|
+
auto_generate_and_save_file
|
17
21
|
end
|
18
22
|
|
19
23
|
task reset: :environment do
|
20
|
-
|
24
|
+
auto_generate_and_save_file
|
21
25
|
end
|
22
26
|
|
23
27
|
task setup: :environment do
|
24
|
-
|
28
|
+
auto_generate_and_save_file
|
25
29
|
end
|
26
30
|
end
|
@@ -5,12 +5,24 @@ module TsSchema
|
|
5
5
|
def initialize
|
6
6
|
Rails.application.eager_load!
|
7
7
|
@models = ApplicationRecord.send(:subclasses)
|
8
|
+
@models.merge(TsSchema.configuration.additional_models) unless TsSchema.configuration.additional_models.empty?
|
8
9
|
@types = TsSchema.configuration.types.merge(TsSchema.configuration.custom_types || {})
|
9
10
|
end
|
10
11
|
|
11
12
|
def generate
|
12
|
-
|
13
|
-
|
13
|
+
generate_typescript
|
14
|
+
end
|
15
|
+
|
16
|
+
def output_file
|
17
|
+
path = TsSchema.configuration.output
|
18
|
+
FileUtils.mkdir_p(File.dirname(path))
|
19
|
+
|
20
|
+
content = generate
|
21
|
+
return if File.exist?(path) && File.read(path) == content
|
22
|
+
|
23
|
+
File.open(path, 'w') do |f|
|
24
|
+
f.write content
|
25
|
+
end
|
14
26
|
end
|
15
27
|
|
16
28
|
private
|
@@ -22,7 +34,7 @@ module TsSchema
|
|
22
34
|
columns.concat(map_associations(model)) if TsSchema.configuration.include_associated
|
23
35
|
|
24
36
|
type_template += <<~TYPESCRIPT
|
25
|
-
interface #{model.model_name.
|
37
|
+
interface #{model.model_name.param_key.camelize} {
|
26
38
|
#{columns.map { |column| "#{indent_as_str}#{column_name_cased(column[:name])}: #{column[:ts_type]};" }.join("\n")}
|
27
39
|
}\n
|
28
40
|
TYPESCRIPT
|
@@ -77,17 +89,6 @@ module TsSchema
|
|
77
89
|
end
|
78
90
|
end
|
79
91
|
|
80
|
-
def output_file(content)
|
81
|
-
path = TsSchema.configuration.output
|
82
|
-
FileUtils.mkdir_p(File.dirname(path))
|
83
|
-
|
84
|
-
return if File.exist?(path) && File.read(path) == content
|
85
|
-
|
86
|
-
File.open(path, 'w') do |f|
|
87
|
-
f.write content
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
92
|
def indent_as_str
|
92
93
|
case TsSchema.configuration.indent.to_sym
|
93
94
|
when :space || :spaces
|
data/lib/ts_schema/version.rb
CHANGED
data/lib/ts_schema.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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Avram Walden
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- MIT-LICENSE
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
|
+
- lib/generators/generate_generator.rb
|
43
44
|
- lib/generators/install_generator.rb
|
44
45
|
- lib/generators/templates/ts_schema.rb
|
45
46
|
- lib/tasks/ts_schema_tasks.rake
|