Module: CheckJS

Defined in:
lib/check_js.rb

Constant Summary collapse

COMPILER =
File.expand_path("../jar/js_compiler.jar", __FILE__)

Class Method Summary collapse

Class Method Details

.check_javaObject



8
9
10
# File 'lib/check_js.rb', line 8

def self.check_java
  system("java -h >/dev/null 2>/dev/null")
end

.check_syntax(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/check_js.rb', line 12

def self.check_syntax(file)
  cmd = "java -jar #{CheckJS::COMPILER} --js #{"'#{file}'"} >/dev/null"
  File.open(file, "r") do |f|
    count = 0
    f.each_line do |line|
      line = Iconv.new('UTF-8//IGNORE', 'UTF-8').iconv(line)
      if line =~ /console\.log\s*\(/
        $stderr.puts "#{file}:#{count}: WARNING - Suspicious code. console.log() only works when the inspector is enabled"
        $stderr.puts "      #{line.strip}"
      end
      count += 1
    end
  end
  system(cmd)
end

.hooks_pathObject



43
44
45
# File 'lib/check_js.rb', line 43

def self.hooks_path
  ".git/hooks"
end

.installObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/check_js.rb', line 28

def self.install
  if File.exist?(self.hooks_path)
    if File.exist?(self.pre_commit_path) && File.read(self.pre_commit_path) !~ /check_js/
      cp_path = self.pre_commit_path+".backup"
      puts "Creating a copy of your current hook on #{cp_path}"
      FileUtils.mv(self.pre_commit_path, cp_path)
    end

    File.open(self.pre_commit_path, "w") do |f|
      f << File.read(File.expand_path("../check_js/hook.rb", __FILE__))
    end
    FileUtils.chmod(0755, self.pre_commit_path)
  end
end

.pre_commit_pathObject



47
48
49
# File 'lib/check_js.rb', line 47

def self.pre_commit_path
  "#{self.hooks_path}/pre-commit"
end