Class: StudFinder::FileCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/stud_finder/file_collector.rb

Defined Under Namespace

Classes: Error, Result

Constant Summary collapse

DEFAULT_EXCLUDES =
[
  'db/schema.rb',
  'db/migrate/**',
  'node_modules/**',
  '**/node_modules/**',
  'vendor/**',
  '**/*.min.js',
  'tmp/**',
  'log/**',
  'spec/**',
  'test/**',
  '__tests__/**',
  '**/__tests__/**',
  '**/*.test.js',
  '**/*.test.ts',
  '**/*.test.jsx',
  '**/*.test.tsx',
  '**/*.spec.js',
  '**/*.spec.ts',
  '**/*.spec.jsx',
  '**/*.spec.tsx'
].freeze
EXTENSIONS =
%w[.rb .js .ts .jsx .tsx].freeze
LANGUAGES =
{
  '.rb' => :ruby,
  '.js' => :javascript,
  '.jsx' => :javascript,
  '.ts' => :typescript,
  '.tsx' => :typescript
}.freeze
FNM_FLAGS =
File::FNM_PATHNAME | File::FNM_DOTMATCH

Instance Method Summary collapse

Constructor Details

#initialize(path:, excludes: [], min_files: 20, stderr: $stderr) ⇒ FileCollector

Returns a new instance of FileCollector.



46
47
48
49
50
51
# File 'lib/stud_finder/file_collector.rb', line 46

def initialize(path:, excludes: [], min_files: 20, stderr: $stderr)
  @path = File.expand_path(path)
  @excludes = excludes
  @min_files = min_files
  @stderr = stderr
end

Instance Method Details

#collectObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stud_finder/file_collector.rb', line 53

def collect
  validate!

  default_excluded = 0
  custom_excluded = 0
  languages = {}
  files = Dir.glob(File.join(@path, '**', '*'), File::FNM_DOTMATCH)
             .select { |file| File.file?(file) }
             .sort
             .filter_map do |file|
    extension = File.extname(file)
    next unless EXTENSIONS.include?(extension)

    relative = relative_path(file)

    if default_excluded?(relative, file)
      default_excluded += 1
      next
    end

    if excluded_by_patterns?(relative, @excludes)
      custom_excluded += 1
      next
    end

    languages[relative] = LANGUAGES.fetch(extension)
    relative
  end

  if files.length < 5
    raise Error,
          "Error: only #{files.length} supported files found after excludes. Too few for meaningful analysis."
  end

  if files.length < @min_files
    @stderr.puts "Warning: only #{files.length} files found. Percentile ranks are unreliable at this scale. " \
                 'Results are advisory only.'
  end

  Result.new(files: files, languages: languages, default_excluded_count: default_excluded,
             custom_excluded_count: custom_excluded)
end