120 lines
3.9 KiB
Ruby
Raw Normal View History

2020-11-25 17:03:23 +01:00
# typed: true
# frozen_string_literal: true
module Language
2020-08-17 18:58:52 +02:00
# Helper functions for Node formulae.
#
# @api public
module Node
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def self.npm_cache_config
"cache=#{HOMEBREW_CACHE}/npm_cache"
end
def self.pack_for_installation
# Homebrew assumes the buildpath/testpath will always be disposable
# and from npm 5.0.0 the logic changed so that when a directory is
# fed to `npm install` only symlinks are created linking back to that
# directory, consequently breaking that assumption. We require a tarball
# because npm install creates a "real" installation when fed a tarball.
if (package = Pathname("package.json")) && package.exist?
begin
pkg_json = JSON.parse(package.read)
rescue JSON::ParserError
opoo "Could not parse package.json!"
raise
end
prepare_removed = pkg_json["scripts"]&.delete("prepare")
prepack_removed = pkg_json["scripts"]&.delete("prepack")
2022-06-14 08:56:04 +01:00
postpack_removed = pkg_json["scripts"]&.delete("postpack")
package.atomic_write(JSON.pretty_generate(pkg_json)) if prepare_removed || prepack_removed || postpack_removed
end
2021-03-17 15:34:20 +00:00
output = Utils.popen_read("npm", "pack", "--ignore-scripts")
raise "npm failed to pack #{Dir.pwd}" if !$CHILD_STATUS.exitstatus.zero? || output.lines.empty?
2018-09-17 02:45:00 +02:00
output.lines.last.chomp
end
def self.setup_npm_environment
# guard that this is only run once
return if @env_set
2018-09-17 02:45:00 +02:00
@env_set = true
# explicitly use our npm and node-gyp executables instead of the user
# managed ones in HOMEBREW_PREFIX/lib/node_modules which might be broken
begin
ENV.prepend_path "PATH", Formula["node"].opt_libexec/"bin"
rescue FormulaUnavailableError
nil
end
end
def self.std_npm_install_args(libexec)
setup_npm_environment
# tell npm to not install .brew_home by adding it to the .npmignore file
# (or creating a new one if no .npmignore file already exists)
2016-09-10 10:38:35 +01:00
open(".npmignore", "a") { |f| f.write("\n.brew_home\n") }
pack = pack_for_installation
# npm 7 requires that these dirs exist before install
(libexec/"lib").mkpath
# npm install args for global style module format installed into libexec
args = %W[
-ddd
--global
--build-from-source
--#{npm_cache_config}
--prefix=#{libexec}
#{Dir.pwd}/#{pack}
]
2020-02-28 13:36:29 +01:00
args << "--unsafe-perm" if Process.uid.zero?
args
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Array[String]) }
def self.local_npm_install_args
setup_npm_environment
# npm install args for local style module format
%W[
-ddd
--build-from-source
--#{npm_cache_config}
]
end
# Mixin module for {Formula} adding shebang rewrite features.
module Shebang
module_function
# A regex to match potential shebang permutations.
NODE_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?node( |$)}.freeze
# The length of the longest shebang matching `SHEBANG_REGEX`.
NODE_SHEBANG_MAX_LENGTH = "#! /usr/bin/env node ".length
# @private
sig { params(node_path: T.any(String, Pathname)).returns(Utils::Shebang::RewriteInfo) }
def node_shebang_rewrite_info(node_path)
Utils::Shebang::RewriteInfo.new(
NODE_SHEBANG_REGEX,
NODE_SHEBANG_MAX_LENGTH,
"#{node_path}\\1",
)
end
sig { params(formula: T.untyped).returns(Utils::Shebang::RewriteInfo) }
def detected_node_shebang(formula = self)
node_deps = formula.deps.map(&:name).grep(/^node(@.+)?$/)
raise ShebangDetectionError.new("Node", "formula does not depend on Node") if node_deps.empty?
raise ShebangDetectionError.new("Node", "formula has multiple Node dependencies") if node_deps.length > 1
node_shebang_rewrite_info(Formula[node_deps.first].opt_bin/"node")
end
end
end
end