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_coffee_path!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/js2coffee/path_helper.rb', line 30

def create_coffee_path!
  path.descend do |x|
    if x.basename.to_s == 'js'
      js_path = path.sub('/js/', '/coffee/').sub_ext('.coffee')
      map_path = path.sub('/js/', '/.map/').sub_ext('.js.map')

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

      return [js_path, map_path]
    end
  end

  js_path = path.sub_ext('.coffee')
  map_path = path.sub_ext('.js.map').sub(/\/([^\/]+)$/, '/.\1')
  fail 'filename extension must .js!' if path == js_path

  [js_path, map_path]
end

#create_js_path!Object



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

def create_js_path!
  # if find `coffee' in descend path, will create js directory at the same level.
  path.descend do |x|
    if x.basename.to_s == 'coffee'
      js_path = path.sub('/coffee/', '/js/').sub_ext('.js')
      map_path = path.sub('/coffee/', '/.map/').sub_ext('.js.map')

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

      return [js_path, map_path]
    end
  end

  # else, just save in the same path with new extname.
  js_path = path.sub_ext('.js')
  map_path = path.sub_ext('.js.map').sub(/\/([^\/]+)$/, '/.\1')
  fail 'filename extension must .coffee!' if path == js_path

  [js_path, map_path]
end