Module: CoffeeWithoutNodejs::CoffeeCompiler

Defined in:
lib/coffee_without_nodejs/compiler.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sourceObject



12
13
14
# File 'lib/coffee_without_nodejs/compiler.rb', line 12

def source
  @source ||= File.read(File.expand_path('../compiler/coffee-script.js', __FILE__))
end

Class Method Details

.compile(script, bare = true) ⇒ Object



36
37
38
39
40
# File 'lib/coffee_without_nodejs/compiler.rb', line 36

def compile(script, bare=true)
  compiler.call(wrapper, script, bare: bare)
rescue ExecJS::RuntimeError
  puts $!.message
end

.compile_file(file, bare = true, create_target_jsfile = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/coffee_without_nodejs/compiler.rb', line 42

def compile_file(file, bare=true, create_target_jsfile=false)
  root_dir = Pathname(Dir.pwd)
  file_path = Pathname(File.expand_path(file))
  source_code = file_path.read

  target_js_content = compiler.call(wrapper, source_code, bare: bare)

  if create_target_jsfile
    js_path, map_path = create_js_path_from(file_path)
    source_files = file_path.relative_path_from(js_path.dirname).to_s
    generated_file = js_path.relative_path_from(js_path.dirname).to_s
    relative_map_path = map_path.relative_path_from(js_path.dirname).to_s

    File.open(js_path, 'wb') do |f|
      f.print "#{ENV['JS_SHEBANG']}\n\n" if ENV['JS_SHEBANG']
      f.print target_js_content, "\n//# sourceMappingURL=#{relative_map_path}"
    end

    File.open(map_path, 'wb') do |f|
      f.print compiler.call(wrapper, source_code,
                            sourceMap: true,
                            sourceFiles: [source_files],
                            generatedFile: generated_file)['v3SourceMap']
    end

    puts "==> #{js_path.relative_path_from(root_dir)}"
    js_path
  else
    puts "==> #{file_path}" if $stdout.tty?
    target_js_content
  end
rescue ExecJS::RuntimeError
  error_msg = "[#{file_path.relative_path_from(root_dir)}]: #{$!.message}"
  `notify-send "#{error_msg}" -t 5000` if system 'which notify-send &>/dev/null'
  puts error_msg
end

.compilerObject



32
33
34
# File 'lib/coffee_without_nodejs/compiler.rb', line 32

def compiler
  @coffee ||= ExecJS.compile(source)
end

.create_js_path_from(path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/coffee_without_nodejs/compiler.rb', line 79

def create_js_path_from(path)
  # if in coffee directory, will create same level js directory.
  path.descend do |x|
    if x.basename.to_s == 'coffee'
      js_path = path.sub('/coffee/', '/js/').sub(/\.js\.coffee$|\.coffee$/, '.js')
      map_path = path.sub('/coffee/', '/.map/').sub(/\.js\.coffee$|\.coffee$/, '.js.map')

      js_dir = js_path.dirname
      map_dir = map_path.dirname

      js_dir.mkpath unless js_dir.exist?
      map_dir.mkpath unless map_dir.exist?

      return [js_path, map_path]
    end
  end
  # if not in coffee directory, rename it.
  js_path = path.sub(/\.js\.coffee$|\.coffee$/, '.js')
  map_path = js_path.sub_ext('.js.map').sub(/\/([^\/]+)$/, '/.\1')

  if path == js_path
    warn 'filename extension is .coffee or .js.coffee?'
    exit
  end
  [js_path, map_path]
end

.wrapperObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/coffee_without_nodejs/compiler.rb', line 16

def wrapper
  "(function(script, options) {\n    try {\n  return CoffeeScript.compile(script, options);\n    } catch (err) {\n  if (err instanceof SyntaxError && err.location) {\n      throw new SyntaxError([options.filename, err.location.first_line + 1, err.location.first_column + 1].join(\":\") + \": \" + err.message)\n  } else {\n      throw err;\n  }\n    }\n})\n"
end