language/node: set cache config via argument

instead of writing a .npmrc file, which simplifies the code.
npm_cache_config is still preserved for backwarts compatiblility and
usage int he kibana@n formulas in core.
This commit is contained in:
Christian Moritz 2017-06-26 21:47:47 +02:00
parent 466fe9841a
commit 6baea2543a
2 changed files with 16 additions and 19 deletions

View File

@ -1,7 +1,7 @@
module Language module Language
module Node module Node
def self.npm_cache_config def self.npm_cache_config
"cache=#{HOMEBREW_CACHE}/npm_cache\n" "cache=#{HOMEBREW_CACHE}/npm_cache"
end end
def self.pack_for_installation def self.pack_for_installation
@ -20,13 +20,9 @@ module Language
end end
def self.setup_npm_environment def self.setup_npm_environment
npmrc = Pathname.new("#{ENV["HOME"]}/.npmrc") # guard that this is only run once
# only run setup_npm_environment once per formula return if @env_set
return if npmrc.exist? @env_set = true
# explicitly set npm's cache path to HOMEBREW_CACHE/npm_cache to fix
# issues caused by overriding $HOME (long build times, high disk usage)
# https://github.com/Homebrew/brew/pull/37#issuecomment-208840366
npmrc.write npm_cache_config
# explicitly use our npm and node-gyp executables instead of the user # explicitly use our npm and node-gyp executables instead of the user
# managed ones in HOMEBREW_PREFIX/lib/node_modules which might be broken # managed ones in HOMEBREW_PREFIX/lib/node_modules which might be broken
begin begin
@ -49,6 +45,7 @@ module Language
-ddd -ddd
--global --global
--build-from-source --build-from-source
--#{npm_cache_config}
--prefix=#{libexec} --prefix=#{libexec}
#{Dir.pwd}/#{pack} #{Dir.pwd}/#{pack}
] ]
@ -57,9 +54,10 @@ module Language
def self.local_npm_install_args def self.local_npm_install_args
setup_npm_environment setup_npm_environment
# npm install args for local style module format # npm install args for local style module format
%w[ %W[
-ddd -ddd
--build-from-source --build-from-source
--#{npm_cache_config}
] ]
end end
end end

View File

@ -2,22 +2,21 @@ require "language/node"
describe Language::Node do describe Language::Node do
describe "#setup_npm_environment" do describe "#setup_npm_environment" do
it "does nothing when npmrc exists" do it "calls prepend_path when node formula exists only during the first call" do
expect(subject.setup_npm_environment).to be_nil
end
it "calls prepend_path when node formula exists and npmrc does not exist" do
node = formula "node" do node = formula "node" do
url "node-test" url "node-test"
end end
stub_formula_loader(node) stub_formula_loader(node)
allow_any_instance_of(Pathname).to receive(:exist?).and_return(false)
expect(ENV).to receive(:prepend_path) expect(ENV).to receive(:prepend_path)
subject.setup_npm_environment subject.instance_variable_set(:@env_set, false)
expect(subject.setup_npm_environment).to be_nil
expect(subject.instance_variable_get(:@env_set)).to eq(true)
expect(ENV).not_to receive(:prepend_path)
expect(subject.setup_npm_environment).to be_nil
end end
it "does not call prepend_path when node formula does not exist but npmrc exists" do it "does not call prepend_path when node formula does not exist" do
allow_any_instance_of(Pathname).to receive(:exist?).and_return(false)
expect(subject.setup_npm_environment).to be_nil expect(subject.setup_npm_environment).to be_nil
end end
end end
@ -51,6 +50,6 @@ describe Language::Node do
specify "#local_npm_install_args" do specify "#local_npm_install_args" do
resp = subject.local_npm_install_args resp = subject.local_npm_install_args
expect(resp).to include("-ddd", "--build-from-source") expect(resp).to include("-ddd", "--build-from-source", "--cache=#{HOMEBREW_CACHE}/npm_cache")
end end
end end