forked from voxpupuli/puppet-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpm.pp
107 lines (99 loc) · 3.65 KB
/
npm.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# See README.md for usage information.
define nodejs::npm (
Stdlib::Absolutepath $target,
Pattern[/^[^< >= ]/] $ensure = 'present',
$cmd_exe_path = $nodejs::cmd_exe_path,
Array $install_options = [],
$npm_path = $nodejs::npm_path,
String $package = $title,
$source = 'registry',
Array $uninstall_options = [],
$home_dir = '/root',
$user = undef,
Boolean $use_package_json = false,
) {
$install_options_string = join($install_options, ' ')
$uninstall_options_string = join($uninstall_options, ' ')
# Note that install_check will always return false when a remote source is
# provided
if $source != 'registry' {
$install_check_package_string = $source
$package_string = $source
} elsif $ensure =~ /^(present|absent)$/ {
$install_check_package_string = $package
$package_string = $package
} else {
# ensure is either a tag, version or 'latest'
# Note that install_check will always return false when 'latest' or a tag is
# provided
# npm ls does not keep track of tags after install
$install_check_package_string = "${package}:${package}@${ensure}"
$package_string = "${package}@${ensure}"
}
$grep_command = $facts['os']['family'] ? {
'Windows' => "${cmd_exe_path} /c findstr /l",
default => 'grep',
}
$dirsep = $facts['os']['family'] ? {
'Windows' => '\\',
default => '/'
}
$list_command = "${npm_path} ls --long --parseable"
$install_check = "${list_command} | ${grep_command} \"${target}${dirsep}node_modules${dirsep}${install_check_package_string}\""
# set a sensible path on Unix
$exec_path = $facts['os']['family'] ? {
'Windows' => undef,
'Darwin' => ['/bin', '/usr/bin', '/opt/local/bin', '/usr/local/bin'],
default => ['/bin', '/usr/bin', '/usr/local/bin'],
}
if $ensure == 'absent' {
$npm_command = 'rm'
$options = $uninstall_options_string
if $use_package_json {
exec { "npm_${npm_command}_${name}":
command => "${npm_path} ${npm_command} * ${options}",
path => $exec_path,
onlyif => $list_command,
user => $user,
cwd => "${target}${dirsep}node_modules",
require => Class['nodejs'],
}
} else {
exec { "npm_${npm_command}_${name}":
command => "${npm_path} ${npm_command} ${package_string} ${options}",
path => $exec_path,
onlyif => $install_check,
user => $user,
cwd => $target,
require => Class['nodejs'],
}
}
} else {
$npm_command = 'install'
$options = $install_options_string
# Conditionally require proxy and https-proxy to be set first only if the resource exists.
Nodejs::Npm::Global_config_entry<| title == 'https-proxy' |> -> Exec["npm_install_${name}"]
Nodejs::Npm::Global_config_entry<| title == 'proxy' |> -> Exec["npm_install_${name}"]
if $use_package_json {
exec { "npm_${npm_command}_${name}":
command => "${npm_path} ${npm_command} ${options}",
path => $exec_path,
unless => $list_command,
user => $user,
cwd => $target,
environment => "HOME=${home_dir}",
require => Class['nodejs'],
}
} else {
exec { "npm_${npm_command}_${name}":
command => "${npm_path} ${npm_command} ${package_string} ${options}",
path => $exec_path,
unless => $install_check,
user => $user,
cwd => $target,
environment => "HOME=${home_dir}",
require => Class['nodejs'],
}
}
}
}