Class: Rhino::Commands::ExportTypesCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/rhino/commands/export_types_command.rb

Overview

Generate TypeScript interfaces from registered Rhino models via OpenAPI intermediate format + npx openapi-typescript.

Mirrors the Laravel ‘php artisan rhino:export-types` command exactly.

Usage:

rails rhino:export_types
rails rhino:export_types -- --output=path/to/types.d.ts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExportTypesCommand

Returns a new instance of ExportTypesCommand.



22
23
24
25
# File 'lib/rhino/commands/export_types_command.rb', line 22

def initialize
  super
  @options = { output: nil }
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/rhino/commands/export_types_command.rb', line 20

def options
  @options
end

Instance Method Details

#performObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rhino/commands/export_types_command.rb', line 27

def perform
  models = Rhino.config.models

  if models.empty?
    say "No models registered in Rhino configuration.", :yellow
    return true
  end

  output_paths = resolve_output_paths

  if output_paths.empty?
    say "No output paths configured. Set RHINO_CLIENT_PATH and/or RHINO_MOBILE_PATH in .env, or use --output flag.", :red
    return false
  end

  schemas = {}

  models.each do |slug, model_class_name|
    model_class = begin
      model_class_name.constantize
    rescue NameError
      say "Model class does not exist: #{model_class_name}", :red
      next
    end

    interface_name = slug_to_interface_name(slug)
    properties = introspect_columns(model_class)

    if properties.empty?
      say "No columns found for model: #{slug} (#{model_class_name})", :yellow
      next
    end

    schemas[interface_name] = {
      type: "object",
      properties: properties
    }
  end

  if schemas.empty?
    say "No schemas generated.", :yellow
    return true
  end

  openapi_spec = build_openapi_spec(schemas)

  temp_file = Tempfile.new(["rhino_openapi_", ".json"])
  begin
    temp_file.write(JSON.pretty_generate(openapi_spec))
    temp_file.flush

    output_paths.each do |output_path|
      dir = File.dirname(output_path)
      FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

      exit_code = run_openapi_typescript(temp_file.path, output_path)

      if exit_code != 0
        say "Failed to generate types at #{output_path}. Is openapi-typescript installed? Run: npm install -g openapi-typescript", :red
        return false
      end

      say "Generated TypeScript types at: #{output_path}", :green
    end
  ensure
    temp_file.close
    temp_file.unlink
  end

  true
end