2 # Cookbook Name:: nodejs
 
   5 # Copyright 2013, OpenStreetMap Foundation
 
   7 # Licensed under the Apache License, Version 2.0 (the "License");
 
   8 # you may not use this file except in compliance with the License.
 
   9 # You may obtain a copy of the License at
 
  11 #     http://www.apache.org/licenses/LICENSE-2.0
 
  13 # Unless required by applicable law or agreed to in writing, software
 
  14 # distributed under the License is distributed on an "AS IS" BASIS,
 
  15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16 # See the License for the specific language governing permissions and
 
  17 # limitations under the License.
 
  20 require "chef/mixin/shell_out"
 
  23 include Chef::Mixin::ShellOut
 
  27 def load_current_resource
 
  28   @packages = JSON.parse(shell_out("npm list --global --json").stdout)["dependencies"] || {}
 
  30   @current_resource = Chef::Resource::NodejsPackage.new(new_resource.name)
 
  31   @current_resource.package_name(new_resource.package_name)
 
  32   if (package = @packages[@current_resource.package_name])
 
  33     @current_resource.version(package["version"])
 
  39   package_name = if new_resource.version
 
  40                    "#{new_resource.package_name}@#{new_resource.version}"
 
  42                    new_resource.package_name
 
  45   if !@packages.include?(new_resource.package_name)
 
  46     shell_out!("npm install --global #{package_name}")
 
  47     new_resource.updated_by_last_action(true)
 
  48   elsif new_resource.version &&
 
  49         new_resource.version != @current_resource.version
 
  50     shell_out!("npm install --global #{package_name}")
 
  51     new_resource.updated_by_last_action(true)
 
  56   if @packages.include?(new_resource.package_name)
 
  57     shell_out!("npm update --global #{new_resource.package_name}")
 
  58     new_resource.updated_by_last_action(true)
 
  63   if @packages.include?(new_resource.package_name)
 
  64     shell_out!("npm remove --global #{new_resource.package_name}")
 
  65     new_resource.updated_by_last_action(true)