include Chef::Mixin::ShellOut
+use_inline_resources
+
def load_current_resource
@packages = JSON.parse(shell_out("npm list --global --json").stdout)["dependencies"] || {}
@current_resource = Chef::Resource::NodejsPackage.new(new_resource.name)
@current_resource.package_name(new_resource.package_name)
- if package = @packages[@current_resource.package_name]
+ if (package = @packages[@current_resource.package_name])
@current_resource.version(package["version"])
end
@current_resource
end
action :install do
- if new_resource.version
- package_name = "#{new_resource.package_name}@#{new_resource.version}"
- else
- package_name = new_resource.package_name
- end
+ package_name = if new_resource.version
+ "#{new_resource.package_name}@#{new_resource.version}"
+ else
+ new_resource.package_name
+ end
- unless @packages.include?(new_resource.package_name)
- shell_out!("npm install --global #{package_name}")
- new_resource.updated_by_last_action(true)
- else
- if new_resource.version &&
- new_resource.version != @current_resource.version
+ if !@packages.include?(new_resource.package_name)
+ converge_by "install #{package_name}" do
+ shell_out!("npm install --global #{package_name}")
+ end
+ elsif new_resource.version &&
+ new_resource.version != @current_resource.version
+ converge_by "update #{package_name}" do
shell_out!("npm install --global #{package_name}")
- new_resource.updated_by_last_action(true)
end
end
end
action :upgrade do
if @packages.include?(new_resource.package_name)
- shell_out!("npm update --global #{new_resource.package_name}")
- new_resource.updated_by_last_action(true)
+ converge_by "update #{new_resource.package_name}" do
+ shell_out!("npm update --global #{new_resource.package_name}")
+ end
end
end
action :remove do
if @packages.include?(new_resource.package_name)
- shell_out!("npm remove --global #{new_resource.package_name}")
- new_resource.updated_by_last_action(true)
+ converge_by "remove #{new_resource.package_name}" do
+ shell_out!("npm remove --global #{new_resource.package_name}")
+ end
end
end