This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
npm_support.rb
81 lines (67 loc) · 2.68 KB
/
npm_support.rb
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
require "logger"
require "fileutils"
require File.expand_path("../npm_cache", __FILE__)
require File.expand_path("../npm_package", __FILE__)
require File.expand_path("../npm_helper", __FILE__)
require File.expand_path("../../../git_cache", __FILE__)
module NpmSupport
# If there is no npm-shwrinkwrap.json file don't do anything
# If user has node_modules folder and config option "ignoreNodeModules"
# in cloudfoundry.json is not set don't do anything
# Otherwise, install node modules according to shwrinkwrap.json tree
#
# For each dependency in shrinkwrap tree recursively:
# - Get from cache by name & version
# - If not in cache, fetch it, run npm rebuild, put in cache
def compile_node_modules
# npm provided?
return unless runtime[:npm]
@dependencies = get_dependencies
return unless should_install_packages?
@npm_helper = NpmHelper.new(runtime[:executable], runtime[:version], runtime[:npm],
@secure_uid, @secure_gid)
return unless @npm_helper.npm_version
cache_base_dir = StagingPlugin.platform_config["cache"]
FileUtils.mkdir_p File.join(cache_base_dir, "node_modules")
cache_version_dir = File.join(cache_base_dir, "node_modules", library_version)
@cache = NpmCache.new(cache_version_dir, logger)
@git_cache = GitCache.new(File.join(cache_base_dir, "git_cache"),
File.join(cache_version_dir, "git_cache"), logger)
logger.info("Installing dependencies. Node version #{runtime[:version]}")
install_packages(@dependencies, app_directory)
end
def should_install_packages?
return unless @dependencies
return true if @vcap_config["ignoreNodeModules"]
user_packages_dir = File.join(app_directory, "node_modules")
!File.exists?(user_packages_dir)
end
def install_packages(dependencies, where)
dependencies.each do |name, props|
package = NpmPackage.new(name, props, where, @staging_uid,
@staging_gid, @npm_helper, logger, @cache, @git_cache)
installed_dir = package.install
if installed_dir && props["dependencies"].is_a?(Hash)
install_packages(props["dependencies"], installed_dir)
end
end
end
def get_dependencies
shrinkwrap_file = File.join(app_directory, "npm-shrinkwrap.json")
return unless File.exists?(shrinkwrap_file)
shrinkwrap_config = Yajl::Parser.parse(File.new(shrinkwrap_file, "r"))
if shrinkwrap_config.is_a?(Hash) && shrinkwrap_config["dependencies"].is_a?(Hash)
shrinkwrap_config["dependencies"]
end
end
def library_version
case runtime[:name]
when "node08"
"08"
when "node06"
"06"
else
"04"
end
end
end