]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nodejs/providers/package.rb
ec20d157d023b9aa382aa1dee6aeb065e87b8471
[chef.git] / cookbooks / nodejs / providers / package.rb
1 #
2 # Cookbook Name:: nodejs
3 # Provider:: package
4 #
5 # Copyright 2013, OpenStreetMap Foundation
6 #
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
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 require "chef/mixin/shell_out"
21 require "json"
22
23 include Chef::Mixin::ShellOut
24
25 def load_current_resource
26   @packages = JSON.parse(shell_out("npm list --global --json").stdout)["dependencies"] || {}
27
28   @current_resource = Chef::Resource::NodejsPackage.new(new_resource.name)
29   @current_resource.package_name(new_resource.package_name)
30   if package = @packages[@current_resource.package_name]
31     @current_resource.version(package["version"])
32   end
33   @current_resource
34 end
35
36 action :install do
37   if new_resource.version
38     package_name = "#{new_resource.package_name}@#{new_resource.version}"
39   else
40     package_name = new_resource.package_name
41   end
42
43   unless @packages.include?(new_resource.package_name)
44     shell_out!("npm install --global #{package_name}")
45     new_resource.updated_by_last_action(true)
46   else
47     if new_resource.version &&
48        new_resource.version != @current_resource.version
49       shell_out!("npm install --global #{package_name}")
50       new_resource.updated_by_last_action(true)
51     end
52   end
53 end
54
55 action :upgrade do
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)
59   end
60 end
61
62 action :remove do
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)
66   end
67 end