Sha256: 4a16f6e1c0c40204b68e5e17d126b9c310a17503b54358b664016280f068bccd

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require_relative "../i18n-js"
require_relative "cli/command"
require_relative "cli/ui"
require_relative "cli/init_command"
require_relative "cli/version_command"
require_relative "cli/export_command"
require_relative "cli/plugins_command"
require_relative "cli/lint_translations_command"
require_relative "cli/lint_scripts_command"
require_relative "cli/check_command"

module I18nJS
  class CLI
    attr_reader :ui

    def initialize(argv:, stdout:, stderr:, colored: stdout.tty?)
      @argv = argv.dup
      @ui = UI.new(stdout: stdout, stderr: stderr, colored: colored)
    end

    def call
      command_name = @argv.shift
      command = commands.find {|cmd| cmd.name == command_name }

      ui.fail_with(root_help) unless command

      command.call
    end

    private def command_classes
      [
        InitCommand,
        ExportCommand,
        VersionCommand,
        PluginsCommand,
        LintTranslationsCommand,
        LintScriptsCommand,
        CheckCommand
      ]
    end

    private def commands
      command_classes.map do |command_class|
        command_class.new(argv: @argv, ui: ui)
      end
    end

    private def root_help
      commands_list = commands
                      .map {|cmd| "- #{cmd.name}: #{cmd.description}" }
                      .join("\n")

      <<~TEXT
        Usage: i18n COMMAND FLAGS

        Commands:

        #{commands_list}

        Run `i18n COMMAND --help` for more information on specific commands.
      TEXT
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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