]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nodejs/resources/package.rb
Merge branch 'patch-2' of https://github.com/Tigerfell/chef into pr257
[chef.git] / cookbooks / nodejs / resources / package.rb
1 #
2 # Cookbook:: nodejs
3 # Resource:: 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 # https://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 "json"
21
22 default_action :install
23
24 property :package, :kind_of => String, :name_property => true
25 property :version, :kind_of => String
26
27 action :install do
28   qualified_name = if new_resource.version
29                      "#{new_resource.package}@#{new_resource.version}"
30                    else
31                      new_resource.package
32                    end
33
34   if current_version.nil?
35     converge_by "install #{qualified_name}" do
36       shell_out!("npm install --global #{qualified_name}")
37     end
38   elsif new_resource.version &&
39         new_resource.version != current_version
40     converge_by "update #{qualified_name}" do
41       shell_out!("npm install --global #{qualified_name}")
42     end
43   end
44 end
45
46 action :upgrade do
47   unless current_version.nil?
48     converge_by "update #{new_resource.package}" do
49       shell_out!("npm update --global #{new_resource.package}")
50     end
51   end
52 end
53
54 action :remove do
55   unless current_version.nil?
56     converge_by "remove #{new_resource.package}" do
57       shell_out!("npm remove --global #{new_resource.package}")
58     end
59   end
60 end
61
62 action_class do
63   def current_version
64     @current_version ||= JSON.parse(shell_out("npm list --global --json").stdout)
65                              .dig("dependencies", new_resource.package, "version")
66   end
67 end