Sha256: 1999f99454c909558fb880c47f410329da5dde1da57e4907adbe246fd382b68a

Contents?: true

Size: 1.17 KB

Versions: 33

Compression:

Stored size: 1.17 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}/.freeze
    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) if File.exist?(full_path)
    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

33 entries across 33 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 lib/opal/path_reader.rb
opal-1.8.2 lib/opal/path_reader.rb
opal-1.8.1 lib/opal/path_reader.rb
opal-1.8.0 lib/opal/path_reader.rb
opal-1.8.0.beta1 lib/opal/path_reader.rb
opal-1.7.4 lib/opal/path_reader.rb
opal-1.8.0.alpha1 lib/opal/path_reader.rb
opal-1.7.3 lib/opal/path_reader.rb
opal-1.7.2 lib/opal/path_reader.rb
opal-1.7.1 lib/opal/path_reader.rb
opal-1.7.0 lib/opal/path_reader.rb
opal-1.7.0.rc1 lib/opal/path_reader.rb
opal-1.6.1 lib/opal/path_reader.rb
opal-1.6.0 lib/opal/path_reader.rb
opal-1.6.0.rc1 lib/opal/path_reader.rb
opal-1.6.0.alpha1 lib/opal/path_reader.rb
opal-1.5.1 lib/opal/path_reader.rb
opal-1.5.0 lib/opal/path_reader.rb
opal-1.5.0.rc1 lib/opal/path_reader.rb
opal-1.4.1 lib/opal/path_reader.rb