Last active
October 22, 2015 17:32
-
-
Save jeffpeterson/9feff9bfdc0fd3c54ae8 to your computer and use it in GitHub Desktop.
Find imports of files
This file contains hidden or 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 | |
if !ARGV[0] | |
puts "USAGE: #{$0} <file>" | |
exit 1 | |
end | |
at_exit { run! } | |
def run! | |
needle = norm_path(ARGV[0]) | |
results = `ag -G 'jsx?|less' "^import .+from +['\\"][./]|require\\(['\\"][./]|@import +['\\"]"`.split("\n") | |
for result in results | |
source_file, linenum, line = result.split(':', 3) | |
source_dir = File.dirname(source_file) | |
imported_file = line.match(/(['"])([^\1]+)\1/)[2] | |
imported_abs = norm_path(imported_file, source_dir) | |
if needle == imported_abs | |
puts "\n#{source_file}:#{linenum}\n #{line}" | |
end | |
end | |
end | |
def norm_path p, dir = nil | |
File.absolute_path(p, dir).sub(/\.[a-z]+$/, '') | |
end |
Author
jeffpeterson
commented
May 22, 2015
requires brew install ag
Added support for require
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment