Module: Licensed::Sources::Yarn

Included in:
Berry, V1
Defined in:
lib/licensed/sources/yarn.rb

Defined Under Namespace

Modules: ClassMethods Classes: Berry, V1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



12
13
14
# File 'lib/licensed/sources/yarn.rb', line 12

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#dependency_pathsObject

Returns a hash that maps all dependency names to their location on disk by parsing every package.json file under node_modules.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/licensed/sources/yarn.rb', line 29

def dependency_paths
  @dependency_paths ||= [
      *Dir.glob(config.pwd.join("**/node_modules/*/package.json")),
      *Dir.glob(config.pwd.join("**/node_modules/@*/*/package.json"))
    ].each_with_object({}) do |file, hsh|
    begin
      dirname = File.dirname(file)
      json = JSON.parse(File.read(file))
      hsh["#{json["name"]}@#{json["version"]}"] = dirname
    rescue JSON::ParserError
      # don't crash execution if there is a problem parsing a package.json file
      # if the bad package.json file relates to a package that licensed should be reporting on
      # then this will still result in an error about a missing package
    end
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/licensed/sources/yarn.rb', line 16

def enabled?
  return unless Licensed::Shell.tool_available?("yarn")
  return unless self.class.version_requirement.satisfied_by?(yarn_version)

  config.pwd.join("package.json").exist? && config.pwd.join("yarn.lock").exist?
end

#yarn_versionObject



23
24
25
# File 'lib/licensed/sources/yarn.rb', line 23

def yarn_version
  Gem::Version.new(Licensed::Shell.execute("yarn", "-v"))
end