Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Attempt to fix failng Rubocop lint checks #190

Merged
merged 1 commit into from
Jan 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/puppet/provider/package/npm.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
require 'puppet/provider/package'

Puppet::Type.type(:package).provide :npm, :parent => Puppet::Provider::Package do
desc "npm is the package manager for Node.js. This provider only handles global packages."
desc 'npm is the package manager for Node.js. This provider only handles global packages.'

has_feature :versionable

if Puppet::Util::Package.versioncmp(Puppet.version, '3.0') >= 0
has_command(:npm, 'npm') do
is_optional
environment :HOME => "/root"
environment :HOME => '/root'
end
else
optional_commands :npm => 'npm'
end

def self.npmlist
# Ignore non-zero exit codes as they can be minor, just try and parse JSON
output = execute([command(:npm), 'list', '--json', '--global'], {:combine => false})
output = execute([command(:npm), 'list', '--json', '--global'], :combine => false)
Puppet.debug("Warning: npm list --json exited with code #{$CHILD_STATUS.exitstatus}") unless $CHILD_STATUS.success?
begin
# ignore any npm output lines to be a bit more robust
output = PSON.parse(output.lines.select{ |l| l =~ /^((?!^npm).*)$/}.join("\n"), {:max_nesting => 100} )
output = PSON.parse(output.lines.select { |l| l =~ /^((?!^npm).*)$/ }.join("\n"), :max_nesting => 100)
@npmlist = output['dependencies'] || {}
rescue PSON::ParserError => e
Puppet.debug("Error: npm list --json command error #{e.message}")
Expand All @@ -34,15 +34,15 @@ def npmlist

def self.instances
@npmlist ||= npmlist
@npmlist.collect do |k,v|
new({:name=>k, :ensure=>v['version'], :provider=>'npm'})
@npmlist.collect do |k, v|
new(:name => k, :ensure => v['version'], :provider => 'npm')
end
end

def query
list = npmlist

if list.has_key?(resource[:name]) and list[resource[:name]].has_key?('version')
if list.key?(resource[:name]) && list[resource[:name]].key?('version')
version = list[resource[:name]]['version']
{ :ensure => version, :name => resource[:name] }
else
Expand All @@ -51,16 +51,16 @@ def query
end

def latest
if /#{resource[:name]}@([\d\.]+)/ =~ npm('outdated', '--global', resource[:name])
@latest = $1
if /#{resource[:name]}@([\d\.]+)/ =~ npm('outdated', '--global', resource[:name])
@latest = Regexp.last_match(1)
else
@property_hash[:ensure] unless @property_hash[:ensure].is_a? Symbol
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole function feels very unruby… not sure ow to make it better
or simplify it for that matter
perhaps split is all we need

so, basically,

def latest
  outdated = npm('outdated', '--global', resource[:name]).split('@', 1) # no idea what i'm doing
  return outdated if outdated =~ /^(\d\.)+$/ # n.b.: this doesn't match builds, alpha's rcs, etc…
  return @property_hash[:ensure] unless @property_hash[:ensure].is_a? Symbol # why? we could just to_s
  # cuz otherwise this returns nil
end

end

def update
resource[:ensure] = @latest
self.install
install
end

def install
Expand Down
Loading