Created
February 20, 2014 14:25
-
-
Save RSO/9114767 to your computer and use it in GitHub Desktop.
Find scss files that you haven't imported
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 | |
def find_imports_from root_file | |
imports = [] | |
path = resolve_path root_file | |
return [] unless File.exists? root_file | |
open root_file do |f| | |
import_lines = f.grep(/\@import/) | |
import_lines.map do |line| | |
match = /\@import (?:"|')(.*)(?:"|')/.match(line) | |
imported_file_path = "#{path}#{match[1]}.scss" | |
if imported_file_path.index(/\*/) | |
imports.concat Dir[imported_file_path] | |
else | |
imports.push imported_file_path | |
end | |
end | |
end | |
imports | |
end | |
def find_all_imports_from root_file | |
imports = find_imports_from root_file | |
imports.each do |import| | |
imports.concat find_all_imports_from import | |
end | |
end | |
def find_unimported_files | |
all_files = Dir["app/assets/stylesheets/**/**/**/**/*.scss"] | |
all_imports = [] | |
root_files = [ | |
"app/assets/stylesheets/application.scss", | |
"app/assets/stylesheets/email.scss" | |
] | |
root_files.each do |root_file| | |
all_imports.concat find_all_imports_from root_file | |
end | |
all_files - all_imports - root_files | |
end | |
def resolve_path file | |
parts = file.split(/\//)[0...-1] | |
path = parts.join '/' | |
if path.length > 0 | |
"#{path}/" | |
else | |
"" | |
end | |
end | |
p find_unimported_files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment