Sha256: 26cfacf240ec72212af16a643db00f4a307b1db1b49c1ccbdd0b324e1df6edce

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module JsDependency
  module PathnameUtility
    # @param [Pathname] pathname
    # @return [Pathname]
    def self.complement_extname(pathname)
      return pathname if pathname.exist? || pathname.extname != ""

      %w[.js .jsx .vue].each do |ext|
        next unless pathname.sub_ext(ext).file?

        return pathname.sub_ext(ext)
      end

      pathname
    end

    # @param [String] target_path
    # @return [Pathname]
    def self.to_target_pathname(target_path)
      if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
        Pathname.new(target_path).realpath
      else
        Pathname.new(target_path)
      end
    end

    # @param [Pathname] pathname
    # @param [Integer] level
    # @return [Array]
    def self.parse(pathname, level = -1)
      pathname.each_filename.with_object([]) { |filename, array| array << filename }.reverse[0..level].reverse
    end

    # @param [String] path
    # @param [String] src_path
    # @return [String]
    def self.relative_path_or_external_path(path, src_path)
      pathname = Pathname.new(path)
      src_pathname = Pathname.new(src_path)

      if pathname.exist?
        pathname.realpath.relative_path_from(src_pathname.realpath.to_s).to_s
      else
        pathname.to_s
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
js_dependency-0.3.14 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.13 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.12 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.11 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.10 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.9 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.8 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.7 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.6 lib/js_dependency/pathname_utility.rb
js_dependency-0.3.5 lib/js_dependency/pathname_utility.rb