Class: Npmdc::Checker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Errors
Defined in:
lib/npmdc/checker.rb

Constant Summary collapse

DEPENDENCIES =
%w(dependencies devDependencies).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Checker

Returns a new instance of Checker.



17
18
19
20
21
22
# File 'lib/npmdc/checker.rb', line 17

def initialize(options)
  @path = options['path'] || Dir.pwd
  @formatter = Npmdc::Formatter.(options)
  @dependencies_count = 0
  @missed_dependencies = []
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



13
14
15
# File 'lib/npmdc/checker.rb', line 13

def formatter
  @formatter
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/npmdc/checker.rb', line 13

def path
  @path
end

Instance Method Details

#callObject



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
64
65
# File 'lib/npmdc/checker.rb', line 26

def call
  begin
    success = false
    package_json_data = package_json(@path)
    DEPENDENCIES.each do |dep|
      if package_json_data[dep]
        begin
          check_dependencies(package_json_data[dep], dep)
        end
      end
    end

  rescue NoNodeModulesError => e
    output("Failed! Can't find `node_modules` folder inside '#{e.message}' directory!")
    output("\nRun `npm install` to install missing packages.", :warn)

  rescue WrongPathError => e
    output("There is no '#{e.message}' directory.")

  rescue MissedPackageError => e
    output("There is no `package.json` file inside '#{e.message}' directory.")

  rescue JsonParseError => e
    output("Can't parse JSON file #{e.message}")
  else
    success = true unless !@missed_dependencies.empty?
  ensure
    if !@missed_dependencies.empty?
      output("Following dependencies required by your package.json file are missing or not installed properly:")
      @missed_dependencies.uniq.each do |dep|
        output("  * #{dep}")
      end
      output("\nRun `npm install` to install #{@missed_dependencies.uniq.count} missed packages.", :warn)
    elsif success
      output("#{pluralize(@dependencies_count, 'package')} checked. Everything is ok.", :success)
    end

    return success
  end
end