Module: JsDependency::PathnameUtility

Defined in:
lib/js_dependency/pathname_utility.rb

Class Method Summary collapse

Class Method Details

.complement_extname(pathname) ⇒ Pathname

Parameters:

  • pathname (Pathname)

Returns:

  • (Pathname)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/js_dependency/pathname_utility.rb', line 7

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

.filter_js_files(paths) ⇒ Array<String>

Parameters:

  • paths (Array<String>)

Returns:

  • (Array<String>)


52
53
54
55
56
57
# File 'lib/js_dependency/pathname_utility.rb', line 52

def self.filter_js_files(paths)
  return paths if paths.nil? || paths.empty?

  permitted_extnames = %w[.js .vue .jsx]
  paths.filter { |path| permitted_extnames.include?(Pathname.new(path).extname) }
end

.parse(pathname, level = -1)) ⇒ Array

Parameters:

  • pathname (Pathname)
  • level (Integer) (defaults to: -1))

Returns:

  • (Array)


32
33
34
# File 'lib/js_dependency/pathname_utility.rb', line 32

def self.parse(pathname, level = -1)
  pathname.each_filename.with_object([]) { |filename, array| array << filename }.reverse[0..level].reverse
end

.relative_path_or_external_path(path, src_path) ⇒ String

Parameters:

  • path (String)
  • src_path (String)

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
# File 'lib/js_dependency/pathname_utility.rb', line 39

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

.to_target_pathname(target_path) ⇒ Pathname

Parameters:

  • target_path (String)

Returns:

  • (Pathname)


21
22
23
24
25
26
27
# File 'lib/js_dependency/pathname_utility.rb', line 21

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