Class: Js2coffee::PathHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/js2coffee/path_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PathHelper

Returns a new instance of PathHelper.



5
6
7
# File 'lib/js2coffee/path_helper.rb', line 5

def initialize(path)
  @path = Pathname(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/js2coffee/path_helper.rb', line 3

def path
  @path
end

Instance Method Details

#create_target_path!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/js2coffee/path_helper.rb', line 9

def create_target_path!
  # if find `coffee' in descend path, will create js directory at the same level.
  ext = path.extname[1..-1]
  target = ext == 'js' ? 'coffee' : 'js'

  path.descend do |x|
    if x.basename.to_s == ext
      target_path = path.sub("/#{ext}/", "/#{target}/").sub_ext(".#{target}")
      map_path = path.sub("/#{ext}/", '/.map/').sub_ext('.js.map')

      FileUtils.mkdir_p([target_path.dirname, map_path.dirname])

      return [target_path, map_path]
    end
  end

  # else, just save in the same path with new extname.
  target_path = path.sub_ext(".#{target}")
  map_path = path.sub_ext('.js.map').sub(%r{/([^/]+)$}, '/.\1')
  fail "filename extension must .#{ext}!" if path == target_path

  [target_path, map_path]
end