Module: Lowendinsight::PackageDependencies

Defined in:
lib/lowendinsight/package_json_dependencies.rb

Class Method Summary collapse

Class Method Details

.get_all_dependencies_from_file(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lowendinsight/package_json_dependencies.rb', line 49

def self.get_all_dependencies_from_file(file)
  h = []
  return [{"message":"npm not found"}] if !npm?

  top_level = get_dependencies_from_file(file)
  top_level.each do |dep|
      deps = get_dependencies_from_npm(dep["name"],dep["specified"])
      return deps if deps[0]['message']
      h << {"name" => dep["name"], "specified" => dep["specified"], "available" => get_latest_version_of_package(dep["name"]), "dependencies" => deps}
  end
  return h
end

.get_dependencies_from_file(file) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lowendinsight/package_json_dependencies.rb', line 94

def self.get_dependencies_from_file(file)
  h = []
  begin
    json = JSON.parse(file)["dependencies"]
    if json.nil?
      return h
    else
      json.each do |k,v|
        h << {"name" => k, "specified" => v}
      end
      return h
    end
  rescue
    h << {"message" => "not a valid package.json"}
    return h
  end

end

.get_dependencies_from_npm(package, version) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lowendinsight/package_json_dependencies.rb', line 62

def self.get_dependencies_from_npm(package, version)
  j = {}
  d = []
  begin
    return d[{"message":"npm not found"}] if !npm?
    j = JSON.parse(`npm info #{package}@"#{version}" --json`)
  rescue
    d = [{"dependency_check_failed"=>{"package" => package, "version" => version}}]
  end
  if j["dependencies"]
    j["dependencies"].each do |package|
      version = version_clean(package[1].delete("~"))
      inner_deps = get_dependencies_from_npm(package[0], version)
      d << {"name" => package[0], "specified" => package[1], "dependencies" => inner_deps}
    end
  end
  return d
end

.get_dependencies_from_repo(url) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lowendinsight/package_json_dependencies.rb', line 25

def self.get_dependencies_from_repo(url)

  h = []

  begin
    @url = url
    paths = URI(@url).path.split("/")
    @name = paths[(paths.size)-1]

    g = Git.clone(@url, @name, :path => '/tmp/checkout')
    raise unless File.exist?("/tmp/checkout/#{@name}/package.json")
    json = JSON.parse(File.read("/tmp/checkout/#{@name}/package.json"))["dependencies"]

    FileUtils.rm_rf("/tmp/checkout/#{@name}")
    json.each do |k,v|
      h << {"name" => k, "specified" => v}
    end
  rescue Exception => e
    h << {"message" => "no package.json found"}
    return h
  end
  return h
end

.get_latest_version_of_package(package) ⇒ Object



90
91
92
# File 'lib/lowendinsight/package_json_dependencies.rb', line 90

def self.get_latest_version_of_package(package)
  JSON.parse(`npm info #{package} --json`)['dist-tags']['latest']
end

.npm?Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/lowendinsight/package_json_dependencies.rb', line 113

def self.npm?
  npm = Lowendinsight.which('npm')
  return npm
end

.version_clean(version) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/lowendinsight/package_json_dependencies.rb', line 81

def self.version_clean(version)
  if version.count('.') == 1
    version = version + ".0"
  elsif version.count('.') == 0
    version = version + ".0.0"
  end
  return version
end