forked from sds/scss-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_finder.rb
63 lines (52 loc) · 1.56 KB
/
file_finder.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
57
58
59
60
61
62
63
require 'find'
module SCSSLint
# Finds all SCSS files that should be linted given a set of paths, globs, and
# configuration.
class FileFinder
# List of extensions of files to include when only a directory is specified
# as a path.
VALID_EXTENSIONS = %w[.css .scss].freeze
# Create a {FileFinder}.
#
# @param config [SCSSLint::Config]
def initialize(config)
@config = config
end
# Find all files that match given the specified options.
#
# @param patterns [Array<String>] a list of file paths and glob patterns
def find(patterns)
if patterns.empty?
raise SCSSLint::Exceptions::NoFilesError,
'No files, paths, or patterns were specified'
end
matched_files = extract_files_from(patterns)
if matched_files.empty?
raise SCSSLint::Exceptions::NoFilesError,
"No SCSS files matched by the patterns: #{patterns.join(' ')}"
end
matched_files.reject { |file| @config.excluded_file?(file) }
end
private
# @param list [Array]
def extract_files_from(list)
files = []
list.each do |file|
if File.directory?(file)
Find.find(file) do |f|
files << f if scssish_file?(f)
end
else
files << file # Otherwise include file as-is
end
end
files.uniq.sort
end
# @param file [String]
# @return [true,false]
def scssish_file?(file)
return false unless FileTest.file?(file)
VALID_EXTENSIONS.include?(File.extname(file))
end
end
end