Module: Sharekit::Cli::FileLister

Defined in:
lib/sharekit/cli/file_lister.rb

Overview

Picks which files a scan should look at. Inside a git repo, defers to git ls-files (tracked + untracked-but-not-ignored) so .gitignore is respected for free instead of reimplementing its match semantics. Outside a git repo, falls back to a plain glob with noise directories excluded.

Constant Summary collapse

DEFAULT_EXCLUDED_DIRS =
%w[.git node_modules vendor .bundle].freeze

Class Method Summary collapse

Class Method Details

.excluded?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sharekit/cli/file_lister.rb', line 37

def excluded?(path)
  DEFAULT_EXCLUDED_DIRS.any? { |dir_name| path.split(File::SEPARATOR).include?(dir_name) }
end

.git_tracked_files(dir) ⇒ Object

Returns nil (not an empty array) when dir isn't inside a git repo or git isn't installed, so the caller falls back to glob_files instead of silently scanning nothing.



24
25
26
27
28
29
30
31
# File 'lib/sharekit/cli/file_lister.rb', line 24

def git_tracked_files(dir)
  out, status = Open3.capture2("git", "-C", dir, "ls-files", "--cached", "--others", "--exclude-standard")
  return nil unless status.success?

  out.lines.map { |line| File.join(dir, line.chomp) }
rescue Errno::ENOENT
  nil
end

.glob_files(dir) ⇒ Object



33
34
35
# File 'lib/sharekit/cli/file_lister.rb', line 33

def glob_files(dir)
  Dir.glob("#{dir}/**/*").select { |path| File.file?(path) && !excluded?(path) }
end

.list(dir) ⇒ Object



17
18
19
# File 'lib/sharekit/cli/file_lister.rb', line 17

def list(dir)
  git_tracked_files(dir) || glob_files(dir)
end