ts_schema 0.1.3 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77d28bf3fa481c9cee1708d44202497b93168c5c41c1912fe3153e87594c2a85
4
- data.tar.gz: 4b5d1eb645ba546a5152b879481c897b735e6bd5a3f58cbf82913ff0160f8b86
3
+ metadata.gz: bd4c606265d13a68760b14f6a3e439e8b6e4bf0dbf5a82262a98d8ed30702107
4
+ data.tar.gz: bced11b47a17ed266bb1d04edbc0e9e962370e802455f8b01a5c314a63a5d069
5
5
  SHA512:
6
- metadata.gz: e4d295418b3c84c8b6c9c1c0e2ae80a4bbe5595fe8a34d223c3207d95083da7fb0a61ecf83878333f499d6493752bc0ebe818b775c110ad2c0746eda2dc05208
7
- data.tar.gz: a1ce2c28636f2a9e5071e11ea42de845d5d411602d661f792e303039fd89b31d5138d63a556744d4df15b72233d135f538e42dab1cbdfcc0fc2a61f9c4dd5f89
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.generate
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
- TsSchema.generate if TsSchema.configuration.auto_generate
16
+ auto_generate_and_save_file
13
17
  end
14
18
 
15
19
  task rollback: :environment do
16
- TsSchema.generate if TsSchema.configuration.auto_generate
20
+ auto_generate_and_save_file
17
21
  end
18
22
 
19
23
  task reset: :environment do
20
- TsSchema.generate if TsSchema.configuration.auto_generate
24
+ auto_generate_and_save_file
21
25
  end
22
26
 
23
27
  task setup: :environment do
24
- TsSchema.generate if TsSchema.configuration.auto_generate
28
+ auto_generate_and_save_file
25
29
  end
26
30
  end
@@ -13,6 +13,7 @@ module TsSchema
13
13
  namespace: :schema,
14
14
  indent: :tab,
15
15
  spaces: 2,
16
+ additional_models: []
16
17
  }
17
18
 
18
19
  attr_accessor(*DEFAULTS.keys)
@@ -12,7 +12,7 @@ boolean: boolean
12
12
  date: string
13
13
  datetime: string
14
14
  timestamp: string
15
- datetime_with_timezone: :string
15
+ datetime_with_timezone: string
16
16
  inet: string
17
17
  cidr: string
18
18
  macaddr: string
@@ -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
- ts = generate_typescript
13
- output_file(ts)
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.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
@@ -1,3 +1,3 @@
1
1
  module TsSchema
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.6"
3
3
  end
data/lib/ts_schema.rb CHANGED
@@ -17,7 +17,12 @@ module TsSchema
17
17
  def generate
18
18
  SchemaGenerator.new.generate
19
19
  end
20
+
21
+ def output_file
22
+ SchemaGenerator.new.output_file
23
+ end
20
24
  end
21
25
  end
22
26
 
23
27
  require "generators/install_generator"
28
+ require "generators/generate_generator"
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.3
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