Class: Anticuado::JavaScript::Npm

Inherits:
Base
  • Object
show all
Defined in:
lib/anticuado/javascript/npm.rb

Class Method Summary collapse

Class Method Details

.format(outdated) ⇒ Array

Returns Array include outdated data. If target project have no outdated data, then return blank array such as ‘[]`.

Parameters:

  • outdated (String)

    The result of command ‘npm outdated`

Returns:

  • (Array)

    Array include outdated data. If target project have no outdated data, then return blank array such as ‘[]`



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/anticuado/javascript/npm.rb', line 24

def self.format(outdated)
  array = outdated.split(/\R/).map(&:strip)
  index = array.find_index { |line| line.scan(/\APackage\s+Current\s+Wanted\s+Latest\s+Location\z/) != [] }

  return [] if index.nil?

  array[index + 1...array.size].map do |library|
    versions = library.split(/\s+/) # e.g. ["babel-brunch", "6.0.2", "6.0.6", "6.0.6"]
    {
        library_name: versions[0],
        current_version: versions[1],
        available_version: versions[2],
        latest_version: versions[3]
    }
  end
end

.outdated(project = nil) ⇒ String

Returns The result of command ‘npm outdated`.

Parameters:

  • project (String) (defaults to: nil)

    Path to project directory.

Returns:

  • (String)

    The result of command ‘npm outdated`.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/anticuado/javascript/npm.rb', line 6

def self.outdated(project = nil)
  return puts "have no npm command" if `which npm`.empty?

  if project
    current_dir = Anticuado.current_dir
    Dir.chdir Anticuado.project_dir(project)
    `npm install`
    outdated_str = `npm outdated`
    Dir.chdir current_dir
  else
    outdated_str = `npm outdated`
  end
  outdated_str
end