Sha256: 7fa032ff9b4d7b8a7366db44de3d76f1e136146674030f04a1b25cfc49eb4543

Contents?: true

Size: 1.71 KB

Versions: 103

Compression:

Stored size: 1.71 KB

Contents

module Souls
  class Update < Thor
    desc "type_rbs [CLASS_NAME]", "Update GraphQL Type from schema.rb"
    def type_rbs(class_name)
      singularized_class_name = class_name.singularize.underscore
      new_cols = ""
      Dir.chdir(Souls.get_api_path.to_s) do
        new_cols = Souls.get_columns_num(class_name: singularized_class_name)
      end
      dir_name = "./sig/api/app/graphql/types"
      new_file_path = "config/create_type.rbs"
      file_path = "#{dir_name}/#{singularized_class_name}_type.rbs"
      argument = false
      File.open(file_path) do |f|
        File.open(new_file_path, "w") do |new_line|
          f.each_line do |line|
            next if line.include?("| (:") && argument

            if line.include?("    def self.edge_type:")
              new_line.write(line)
              argument = false
            elsif line.include?("def self.field:") && !argument
              new_cols.each_with_index do |col, i|
                type = Souls.get_type(col[:type])
                type = "[#{type}]" if col[:array]
                if i.zero?
                  new_line.write("    def self.field: (:#{col[:column_name]}, #{type}, null: true) -> #{type}\n")
                else
                  new_line.write("                  | (:#{col[:column_name]}, #{type}, null: true) -> #{type}\n")
                end
              end
              argument = true
            else
              new_line.write(line)
            end
          end
        end
      end
      FileUtils.rm(file_path)
      FileUtils.mv(new_file_path, file_path)
      puts(Paint % ["Updated file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
    rescue Thor::Error => e
      raise(Thor::Error, e)
    end
  end
end

Version data entries

103 entries across 103 versions & 1 rubygems

Version Path
souls-1.7.19 lib/souls/cli/update/type_rbs.rb
souls-1.7.18 lib/souls/cli/update/type_rbs.rb
souls-1.7.17 lib/souls/cli/update/type_rbs.rb
souls-1.7.16 lib/souls/cli/update/type_rbs.rb
souls-1.7.15 lib/souls/cli/update/type_rbs.rb
souls-1.7.14 lib/souls/cli/update/type_rbs.rb
souls-1.7.13 lib/souls/cli/update/type_rbs.rb
souls-1.7.12 lib/souls/cli/update/type_rbs.rb
souls-1.7.11 lib/souls/cli/update/type_rbs.rb
souls-1.7.10 lib/souls/cli/update/type_rbs.rb
souls-1.7.9 lib/souls/cli/update/type_rbs.rb
souls-1.7.8 lib/souls/cli/update/type_rbs.rb
souls-1.7.7 lib/souls/cli/update/type_rbs.rb
souls-1.7.6 lib/souls/cli/update/type_rbs.rb
souls-1.7.5 lib/souls/cli/update/type_rbs.rb
souls-1.7.4 lib/souls/cli/update/type_rbs.rb
souls-1.7.3 lib/souls/cli/update/type_rbs.rb
souls-1.7.2 lib/souls/cli/update/type_rbs.rb
souls-1.7.1 lib/souls/cli/update/type_rbs.rb
souls-1.7.0 lib/souls/cli/update/type_rbs.rb