keith_shannon

npm.pp

Sep 14th, 2016
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Puppet 1.29 KB | None | 0 0
  1. # Define: nodejs::npm
  2. #
  3. # Parameters:
  4. #
  5. # Actions:
  6. #
  7. # Requires:
  8. #
  9. # Usage:
  10. #
  11. define nodejs::npm (
  12.   $ensure      = present,
  13.   $version     = undef,
  14.   $source      = undef,
  15.   $install_opt = undef,
  16.   $remove_opt  = undef
  17. ) {
  18.   include nodejs
  19.  
  20.   $npm = split($name, ':')
  21.   $npm_dir = $npm[0]
  22.   $npm_pkg = $npm[1]
  23.  
  24.   if $source {
  25.     $install_pkg = $source
  26.   } elsif $version {
  27.     $install_pkg = "${npm_pkg}@${version}"
  28.   } else {
  29.     $install_pkg = $npm_pkg
  30.   }
  31.  
  32.   if $version {
  33.     $validate = "${npm_dir}/node_modules/${npm_pkg}:${npm_pkg}@${version}"
  34.   } else {
  35.     $validate = "${npm_dir}/node_modules/${npm_pkg}"
  36.   }
  37.  
  38.   if $ensure == present {
  39.     exec { "npm_install_${name}":
  40.       command => "npm install ${install_opt} ${install_pkg}",
  41.       unless  => "npm list -p -l | grep '${validate}'",
  42.       cwd     => $npm_dir,
  43.       path    => $::path,
  44.       require => Class['nodejs'],
  45.     }
  46.  
  47.     # Conditionally require npm_proxy only if resource exists.
  48.     Exec<| title=='npm_proxy' |> -> Exec["npm_install_${name}"]
  49.   } else {
  50.     exec { "npm_remove_${name}":
  51.       command => "npm remove ${npm_pkg}",
  52.       onlyif  => "npm list -p -l | grep '${validate}'",
  53.       cwd     => $npm_dir,
  54.       path    => $::path,
  55.       require => Class['nodejs'],
  56.     }
  57.   }
  58. }
Add Comment
Please, Sign In to add comment