blob: 61d1b0a5682c6ffc307113c4be1e74647b847150 [file] [log] [blame]
# Non-predefined variables must be escaped with "$$" (e.g. "$$FOO"); see
# https://bazel.build/reference/be/make-variables#predefined_label_variables.
#
# We cannot use single quotes here because of the "echo '%s' > $@" command in the below genrule.
_SCRIPT = """
# The "realpath" comand gives us an absolute path where any symlinks are resolved.
NPM_BIN=$$(realpath $(rootpath @nodejs_linux_amd64//:bin/nodejs/bin/npm))
# We need to make sure the Bazel-downloaded "node" binary comes first in the PATH environment
# variable, or "npm" might fail if it picks up the system "node" binary and its version is too
# old.
NODE_BIN_DIR=$$(realpath $$(dirname $(rootpath @nodejs_linux_amd64//:bin/nodejs/bin/node)))
export PATH=$$NODE_BIN_DIR:$$PATH
# Change into the directory where Bazel was invoked to ensure that "npm" finds the correct
# package.json and package-lock.json files. See
# https://bazel.build/docs/user-manual#running-executables.
cd $$BUILD_WORKING_DIRECTORY
$$NPM_BIN $$@
"""
genrule(
name = "gen_script",
outs = ["npm.sh"],
cmd = "echo '%s' > $@" % _SCRIPT,
exec_tools = [
"@nodejs_linux_amd64//:bin/nodejs/bin/npm",
"@nodejs_linux_amd64//:bin/nodejs/bin/node",
],
)
# Wrapper script around the Bazel-downloaded "npm" binary.
#
# This script ensures "npm" always uses the Bazel-downloaded "node" binary rather than the system's
# "node" binary.
#
# Reference: https://bazel.build/reference/be/shell#sh_binary.
sh_binary(
name = "npm",
srcs = ["npm.sh"],
data = [
"@nodejs_linux_amd64//:bin/nodejs/bin/node",
"@nodejs_linux_amd64//:bin/nodejs/bin/npm",
],
visibility = ["//visibility:public"],
)