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.0.1 lib/souls/cli/update/type_rbs.rb
souls-1.0.0 lib/souls/cli/update/type_rbs.rb
souls-0.71.7 lib/souls/cli/update/type_rbs.rb