Sha256: 9580fec3f43ef9b077ae981cd050754e93b1eacf32755fd664ff44a60877aac9

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

gem "guard"
gem "guard-compat"
require "guard/compat/plugin"

require "i18n-js"

module Guard
  class I18njs < Plugin
    attr_reader :config_file, :require_file, :current_thread

    def initialize(options = {})
      @config_file = options.delete(:config_file)
      @require_file = options.delete(:require_file)
      super
    end

    def start
      export_files
    end

    def stop
      current_thread&.exit
    end

    def reload
      export_files
    end

    def run_all
      export_files
    end

    def run_on_additions(paths)
      export_files(paths)
    end

    def run_on_modifications(paths)
      export_files(paths)
    end

    def run_on_removals(paths)
      export_files(paths)
    end

    def export_files(changes = nil)
      return unless validate_file(:config_file, config_file)
      return unless validate_file(:require_file, require_file)

      current_thread&.exit

      info("Changes detected: #{changes.join(', ')}") if changes

      @current_thread = Thread.new do
        require @require_file
        ::I18nJS.call(config_file: @config_file)
      end

      current_thread.join
    end

    def validate_file(key, file)
      return true if file && File.file?(file)

      error("#{key.inspect} must be a file")
      false
    end

    def error(message)
      ::Guard::UI.error "[guard-i18n-js] #{message}"
    end

    def info(message)
      ::Guard::UI.info "[guard-i18n-js] #{message}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
i18n-js-4.0.0 lib/guard/i18n-js.rb
i18n-js-4.0.0.alpha5 lib/guard/i18n-js.rb
i18n-js-4.0.0.alpha4 lib/guard/i18n-js.rb
i18n-js-4.0.0.alpha3 lib/guard/i18n-js.rb
i18n-js-4.0.0.alpha2 lib/guard/i18n-js.rb
i18n-js-4.0.0.alpha1 lib/guard/i18n-js.rb