Sha256: bb266756aa4bb398670f29d5d42be56490d3bbde8b04ecb48d31da93609b362d

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'opal/regexp_anchors'
require 'opal/hike'

module Opal
  class PathReader
    RELATIVE_PATH_REGEXP = /#{Opal::REGEXP_START}\.?\.#{Regexp.quote File::SEPARATOR}/
    DEFAULT_EXTENSIONS = ['.js', '.js.rb', '.rb', '.opalerb'].freeze

    def initialize(paths = Opal.paths, extensions = DEFAULT_EXTENSIONS)
      @file_finder = Hike::Trail.new
      @file_finder.append_paths(*paths)
      @file_finder.append_extensions(*extensions)
    end

    def read(path)
      full_path = expand(path)
      return nil if full_path.nil?
      File.open(full_path, 'rb:UTF-8', &:read)
    end

    def expand(path)
      if Pathname.new(path).absolute? || path =~ RELATIVE_PATH_REGEXP
        path
      else
        find_path(path)
      end
    end

    def paths
      file_finder.paths
    end

    def extensions
      file_finder.extensions
    end

    def append_paths(*paths)
      file_finder.append_paths(*paths)
    end

    private

    def find_path(path)
      pathname = Pathname(path)
      return path if pathname.absolute? && pathname.exist?
      file_finder.find(path)
    end

    attr_reader :file_finder
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-1.0.5 lib/opal/path_reader.rb
opal-1.0.4 lib/opal/path_reader.rb
opal-1.0.3 lib/opal/path_reader.rb
opal-1.0.2 lib/opal/path_reader.rb
opal-1.0.1 lib/opal/path_reader.rb
opal-1.0.0 lib/opal/path_reader.rb
opal-1.0.0.beta1 lib/opal/path_reader.rb