-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_js_regexp.rb
executable file
·56 lines (45 loc) · 1.34 KB
/
find_js_regexp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env ruby
require 'json'
require 'pathname'
ROOT_PATH = Pathname.new(ENV['ROOT_PATH'] || `pwd -P`.chomp)
BLAKLIST = /(\.min\.js$|lodash\.js$|hammer\.js$|nicEdit\.js$|semantic\.js$|semantic-form\.js$|webcam\.js$|Chart\.js$|jquery_lib)/
class JsFile
attr_reader :filename, :error, :reg_expr_list
def initialize(filename)
@filename = filename
@reg_expr_list = JSON.load(`ts-node ext_re.ts #{ROOT_PATH.join(filename)}`)
rescue => e
@error = e
end
def empty?
reg_expr_list.empty?
end
def as_json(*)
return { error: error.to_s } if error
{ path: filename, list: reg_expr_list }
end
end
def main
filenames = `git --git-dir="#{ROOT_PATH}/.git" --work-tree="#{ROOT_PATH}" ls-files | rg "^(app/).+\\.(js|vue)\$"`.strip.split("\n")
filenames.reject! { |fn| fn =~ BLAKLIST }
STDERR.puts "#{filenames.size} files found"
report = { total: 0, items: [] }
filenames.each_with_index do |filename, index|
STDERR.puts "Parsing #{index+1} of #{filenames.size}: #{filename}"
begin
file = JsFile.new(filename)
if file.error
STDERR.puts file.error
next
end
next if file.empty?
report[:total] += file.reg_expr_list.size
report[:items] << file.as_json
rescue => e
STDERR.puts "#{filename} #{e}"
end
end
ensure
puts JSON.pretty_generate(report)
end
main