Skip to content

Instantly share code, notes, and snippets.

@mockdeep
Created February 11, 2017 05:03
Show Gist options
  • Save mockdeep/028cd48c8465cf721f3f2dd36b9a6c1b to your computer and use it in GitHub Desktop.
Save mockdeep/028cd48c8465cf721f3f2dd36b9a6c1b to your computer and use it in GitHub Desktop.
require 'active_support/all'
class JsFile
attr_accessor :path, :file_contents
def initialize(path)
@path = path
@file_contents = File.read(@path)
end
def contains_sprockets?
file_contents.match(/^\/\/= require/)
end
def contains_chk?
file_contents.include?('Chk.')
end
def contains_one_chk?
file_contents.scan('Chk.').count == 1
end
def contains_one_chk_eqs?
file_contents.scan(/Chk\.\S+\s=/).count == 1
end
def contains_module_exports?
file_contents.include?('module.exports')
end
def contains_component_name?(name)
file_contents.include?(name)
end
def contains_component_file_name?(file_name)
file_contents.include?(file_name)
end
end
def star_break
puts '*' * 60
puts
end
def find_js_files(match_path)
js_files = []
Dir[match_path].each do |path|
file = JsFile.new(path)
js_files.push(file)
end
js_files
end
def component_name(target)
target[:file].file_contents.scan(/Chk\.\S+/).first.split('.').last
end
def relative_path(target)
split_path = target[:file].path.split('/')
last = split_path.length - 2
path_items = split_path[3..last]
path_items << target[:file_name]
path_items.join('/')
end
star_break
app_js_files = find_js_files('app/assets/javascripts/**/*.*')
puts "#{app_js_files.length} - TOTAL APP JS FILES"
files = app_js_files.reject(&:contains_sprockets?)
puts " #{files.length} - WITHOUT SPROCKETS"
files = files.select(&:contains_chk?)
puts " #{files.length} - WITH CHK.*"
files = files.select(&:contains_one_chk?)
puts " #{files.length} - WITH ONLY ONE CHK.*"
files = files.select(&:contains_one_chk_eqs?)
puts " #{files.length} - WITH ONE CHK.* ="
files_no_module_exports = files.reject(&:contains_module_exports?)
puts " #{files_no_module_exports.length} - WITHOUT MODULE EXPORTS"
puts
target = { file: files_no_module_exports[0] }
target[:component_name] = component_name(target)
target[:file_name] = target[:component_name].underscore
target[:relative_path] = relative_path(target)
total_no_exports = app_js_files.reject(&:contains_module_exports?)
puts "#{total_no_exports.length} - TOTAL FILES WITHOUT MODULE EXPORTS"
puts
spec_js_files = find_js_files('spec/javascripts/**/*.*')
puts "#{spec_js_files.length} - TOTAL SPEC JS FILES"
puts
all_js_files = app_js_files + spec_js_files
star_break
def other_files_with_component(all_js_files, target)
files = all_js_files.select do |f|
f.contains_component_name?(target[:component_name])
end
other_files = files - [target[:file]]
other_files.each { |f| puts " #{f.path}" }
end
def component_requires_line?(line, file_name)
if line =~ /^\/\/= require/
name = line.split('/')[-1].delete("\n/")
return true if name == file_name
end
end
def search_files_for_requires(files, target)
files_with_component_requires = []
files.each do |f|
f.file_contents.each_line do |line|
if component_requires_line?(line, target[:file_name])
puts " #{f.path}"
files_with_component_requires.push(f)
end
end
end
files_with_component_requires
end
puts 'TARGET COMPONENT FILE:'
puts " #{target[:file].path}\n\n"
puts "OTHER FILES WITH COMPONENT: '#{target[:component_name]}'"
other_files = other_files_with_component(all_js_files, target)
puts
puts "FILES WITH SPROCKET: '#{target[:file_name]}'"
files_with_component_requires = search_files_for_requires(all_js_files, target)
puts
star_break
def commit_file(output, file)
output = output.join
file.file_contents = output
File.write(file.path, output)
end
def update_to_module_exports(target)
output = []
f = target[:file]
f.file_contents.each_line do |line|
if line =~ /Chk./
line_to_update = line.split(' ')
line_to_update[0] = 'module.exports'
leading_spaces = line[/\A */]
line = line_to_update.join(' ')
line = "#{leading_spaces + line}\n"
end
output << line
end
commit_file(output, f)
end
def requires_line(line, leading_spaces, target)
text = "var #{target[:component_name]} = require('#{target[:relative_path]}');"
if line =~ /^\s*var /
"#{leading_spaces + text}\n"
else
"#{leading_spaces + text}\n\n"
end
end
def add_node_style_requires(files, target)
files.each do |f|
output = []
insert_at = nil
leading_spaces = nil
f.file_contents.each_line.with_index do |line, index|
if line.include?('use strict')
insert_at = index + 2
leading_spaces = line[/\A */]
end
if index == insert_at
output << requires_line(line, leading_spaces, target)
end
output << line
end
commit_file(output, f)
end
end
def remove_chk_dot(files, target)
files.each do |f|
output = []
f.file_contents.each_line do |line|
if line.include?(target[:component_name].to_s)
pattern = /(Chk\..*#{target[:component_name]})/
line = line.gsub(pattern, target[:component_name])
end
output << line
end
commit_file(output, f)
end
end
def remove_sprockets(files, target)
files.each do |f|
output = []
f.file_contents.each_line do |line|
output << line unless component_requires_line?(line, target[:file_name])
end
output.delete_at(0) if output[0] == "\n"
commit_file(output, f)
end
end
update_to_module_exports(target)
puts "! UPDATED COMPONENT FILE TO MODULE EXPORTS\n\n"
add_node_style_requires(other_files, target)
puts "! ADDED NODE-STYLE REQUIRES\n\n"
remove_chk_dot(other_files, target)
puts "! REMOVED 'Chk.*.#{target[:component_name]}'\n\n"
remove_sprockets(files_with_component_requires, target)
puts "! REMOVED '#{target[:file_name]}' SPROCKETS\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment