Sha256: 955a592ab8b934e9ba07055c0030af5fbd51567e897719acca19f7539cfe33a7

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
require 'opal/regexp_anchors'
require 'hike'

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

    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'){|f| f.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? and pathname.exist?
      file_finder.find(path)
    end

    attr_reader :file_finder
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
opal-0.11.4 lib/opal/path_reader.rb
opal-0.11.3 lib/opal/path_reader.rb
opal-0.11.2 lib/opal/path_reader.rb
opal-0.11.1 lib/opal/path_reader.rb
opal-0.11.1.pre lib/opal/path_reader.rb
opal-0.11.0 lib/opal/path_reader.rb