Created
January 17, 2018 18:02
-
-
Save BrianMehrman/e095bcb88f99f1dd8ad5dea992ddea4e to your computer and use it in GitHub Desktop.
scans a JS file using TransisJS for prop dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pry' | |
class PropParser | |
HEX = %w(8 9 A B C D E F) | |
attr_reader :colors | |
def self.execute(source_file, out_file) | |
new(source_file, out_file).execute | |
end | |
def initialize(source_file, filepath) | |
@source_file = source_file | |
@filepath = filepath | |
end | |
def execute | |
create_graph(parse_file) | |
render_image | |
end | |
def parse_file | |
re = /this\.prop\('(.*)',[\s\S]*?on:\s(\[.*?\])[\s\S]*?}\);/ | |
text = File.read(@source_file) | |
results = text.scan(re) | |
results.each_with_object({}) { |value, object| object[value[0]] = eval(value[1]) } | |
end | |
def create_graph(hash) | |
data = write_graph(hash) | |
save_graph(data) | |
end | |
def write_graph(hash) | |
out = "" | |
out << "digraph G {\n" | |
hash.each do |key, values| | |
values.each do |value| | |
# out << "#{key} -> #{value.tr('.', '_')};\n" | |
out << "#{value.tr('.', '_')} -> #{key} [colol=\"#{new_color}\"];\n" | |
end | |
end | |
out << "}\n" | |
end | |
def save_graph(data) | |
File.open(graph_filepath, 'w') {|io| io.write(data) } | |
end | |
def render_image | |
cmd = "dot -T#{image_format} #{graph_filepath} -o #{@filepath}" | |
`#{cmd}` | |
end | |
private | |
def image_format | |
File.extname(@filepath).delete('.') | |
end | |
def graph_filepath | |
path, filename = File.split(@filepath) | |
ext = File.extname(filename) | |
base = File.basename(filename, ext) | |
File.join(path, "#{base}.gv") | |
end | |
def new_color | |
@colors ||= [] | |
color = nil | |
loop do | |
color = HEX.sample(6).join | |
unless @colors.include?(color) | |
@colors << color | |
break | |
end | |
end | |
color | |
end | |
end | |
# binding.pry | |
source = ARGV[0] #'components/ui/app/assets/javascripts/ui/models/flight.js' | |
destination = ARGV[1] # 'test.gv' | |
PropParser.execute(source, destination) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment