Sha256: 7d53fcb9e480f088621839a1d6995fb73479e805771ff102cdebb392e4ac1b86

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module I18nJS
  class CLI
    class Command
      attr_reader :ui, :argv

      def self.command_name(name)
        define_method(:name) { name }
      end

      def self.description(description)
        define_method(:description) { description }
      end

      def self.parse(&block)
        define_method(:parse) do
          OptionParser
            .new {|opts| instance_exec(opts, &block) }
            .parse!(argv)
        end
      end

      def self.command(&block)
        define_method(:command, &block)
      end

      def initialize(argv:, ui:)
        @argv = argv.dup
        @ui = ui
      end

      def call
        parse
        command
      end

      def options
        @options ||= {}
      end

      private def load_config_file(config_file)
        config = Glob::SymbolizeKeys.call(YAML.load_file(config_file))

        if config.key?(:check)
          config[:lint_translations] ||= config.delete(:check)
        end

        config
      end

      private def load_require_file!(require_file)
        require_without_warnings(require_file)
      rescue Exception => error # rubocop:disable Lint/RescueException
        ui.stderr_print("=> ERROR: couldn't load",
                        options[:require_file].inspect)
        ui.fail_with(
          "\n#{error_description(error)}\n#{error.backtrace.join("\n")}"
        )
      end

      private def error_description(error)
        [
          error.class.name,
          error.message
        ].reject(&:empty?).join(" => ")
      end

      private def require_without_warnings(path)
        old_verbose = $VERBOSE
        $VERBOSE = nil

        load path
      ensure
        $VERBOSE = old_verbose
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
i18n-js-4.2.3 lib/i18n-js/cli/command.rb
i18n-js-4.2.2 lib/i18n-js/cli/command.rb
i18n-js-4.2.1 lib/i18n-js/cli/command.rb
i18n-js-4.2.0 lib/i18n-js/cli/command.rb
i18n-js-4.1.0 lib/i18n-js/cli/command.rb