Class: Licensed::Sources::NPM
- Defined in:
- lib/licensed/sources/npm.rb
Defined Under Namespace
Classes: Dependency
Instance Attribute Summary
Attributes inherited from Source
Class Method Summary collapse
Instance Method Summary collapse
- #enabled? ⇒ Boolean
- #enumerate_dependencies ⇒ Object
-
#include_non_production? ⇒ Boolean
Returns whether to include non production dependencies based on the licensed configuration settings.
-
#npm_version ⇒ Object
Returns the currently installed version of npm as a Gem::Version object.
-
#package_metadata ⇒ Object
Returns parsed package metadata returned from ‘npm list`.
-
#package_metadata_args ⇒ Object
Returns an array of arguments that should be used for all ‘npm list` calls, regardless of how the output is formatted.
-
#package_metadata_command ⇒ Object
Returns the output from running ‘npm list` to get package metadata.
-
#package_metadata_error ⇒ Object
Returns an error, if one exists, from running ‘npm list` to get package metadata.
- #packages ⇒ Object
-
#recursive_dependencies(dependencies, result = {}) ⇒ Object
Recursively parse dependency JSON data.
-
#yarn_lock_present ⇒ Object
Returns true if a yarn.lock file exists in the current directory.
Methods inherited from Source
#dependencies, #ignored?, inherited, #initialize
Constructor Details
This class inherits a constructor from Licensed::Sources::Source
Class Method Details
.type ⇒ Object
26 27 28 |
# File 'lib/licensed/sources/npm.rb', line 26 def self.type "npm" end |
Instance Method Details
#enabled? ⇒ Boolean
30 31 32 |
# File 'lib/licensed/sources/npm.rb', line 30 def enabled? Licensed::Shell.tool_available?("npm") && File.exist?(config.pwd.join("package.json")) end |
#enumerate_dependencies ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/licensed/sources/npm.rb', line 34 def enumerate_dependencies packages.map do |name, package| path = package["path"] Dependency.new( name: name, version: package["version"], path: path, metadata: { "type" => NPM.type, "name" => package["name"], "summary" => package["description"], "homepage" => package["homepage"] } ) end end |
#include_non_production? ⇒ Boolean
Returns whether to include non production dependencies based on the licensed configuration settings
132 133 134 |
# File 'lib/licensed/sources/npm.rb', line 132 def include_non_production? config.dig("npm", "production_only") == false end |
#npm_version ⇒ Object
Returns the currently installed version of npm as a Gem::Version object
120 121 122 123 124 |
# File 'lib/licensed/sources/npm.rb', line 120 def npm_version @npm_version ||= begin Gem::Version.new(Licensed::Shell.execute("npm", "-v").strip) end end |
#package_metadata ⇒ Object
Returns parsed package metadata returned from ‘npm list`
80 81 82 83 84 85 86 87 88 |
# File 'lib/licensed/sources/npm.rb', line 80 def return @package_metadata if defined?(@package_metadata) @package_metadata = JSON.parse() rescue JSON::ParserError => e = "Licensed was unable to parse the output from 'npm list'. JSON Error: #{e.}" npm_error = = "#{}. npm Error: #{npm_error}" if npm_error raise Licensed::Sources::Source::Error, end |
#package_metadata_args ⇒ Object
Returns an array of arguments that should be used for all ‘npm list` calls, regardless of how the output is formatted
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/licensed/sources/npm.rb', line 108 def args = [] args << "--production" unless include_non_production? # on npm 7+, the --all argument is necessary to evaluate the project's # full dependency tree args << "--all" if npm_version >= Gem::Version.new("7.0.0") return args end |
#package_metadata_command ⇒ Object
Returns the output from running ‘npm list` to get package metadata
99 100 101 102 103 104 |
# File 'lib/licensed/sources/npm.rb', line 99 def args = %w(--json --long) args.concat() Licensed::Shell.execute("npm", "list", *args, allow_failure: true) end |
#package_metadata_error ⇒ Object
Returns an error, if one exists, from running ‘npm list` to get package metadata
91 92 93 94 95 96 |
# File 'lib/licensed/sources/npm.rb', line 91 def Licensed::Shell.execute("npm", "list", *) return "" rescue Licensed::Shell::Error => e return e. end |
#packages ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/licensed/sources/npm.rb', line 51 def packages root_dependencies = ["dependencies"] recursive_dependencies(root_dependencies).each_with_object({}) do |(name, results), hsh| results.uniq! { |package| package["version"] } if results.size == 1 hsh[name] = results[0] else results.each do |package| name_with_version = "#{name}-#{package["version"]}" hsh[name_with_version] = package end end end end |
#recursive_dependencies(dependencies, result = {}) ⇒ Object
Recursively parse dependency JSON data. Returns a hash mapping the package name to it’s metadata
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/licensed/sources/npm.rb', line 68 def recursive_dependencies(dependencies, result = {}) dependencies.each do |name, dependency| next if dependency["peerMissing"] next if yarn_lock_present && dependency["missing"] dependency["name"] = name (result[name] ||= []) << dependency recursive_dependencies(dependency["dependencies"] || {}, result) end result end |
#yarn_lock_present ⇒ Object
Returns true if a yarn.lock file exists in the current directory
127 128 129 |
# File 'lib/licensed/sources/npm.rb', line 127 def yarn_lock_present @yarn_lock_present ||= File.exist?(config.pwd.join("yarn.lock")) end |