Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/usr/local/bin/install-npm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ case "$TOOL_NAME" in
mkdir -p "${temp_folder}"

echo "Installing npm tool ${TOOL_NAME} v${TOOL_VERSION}"
npm install "${TOOL_NAME}@${TOOL_VERSION}" --global --unsafe --cache "${temp_folder}"
npm install "${TOOL_NAME}@${TOOL_VERSION}" --global --unsafe --no-audit --no-fund --cache "${temp_folder}"

# Clean download cache
npm cache clean --force
Expand Down
19 changes: 7 additions & 12 deletions src/usr/local/buildpack/tools/lerna.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ set -e

check_command node

# shellcheck source=/dev/null
. /usr/local/buildpack/utils/node.sh

tool_path=$(find_versioned_tool_path)

if [[ -z "${tool_path}" ]]; then
tool_path="$(create_versioned_tool_path)"

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "${TOOL_NAME}@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" "${temp_folder}"
npm_init
npm_install
tool_path=$(find_versioned_tool_path)
npm_clean
fi

link_wrapper "${TOOL_NAME}" "$tool_path/bin"
Expand Down
23 changes: 11 additions & 12 deletions src/usr/local/buildpack/tools/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then
exit 1
fi

# shellcheck source=/dev/null
. /usr/local/buildpack/utils/node.sh

NODE_DISTRO=linux-x64
tool_path=$(find_versioned_tool_path)
PREFIX="${USER_HOME}/.npm-global"
Expand All @@ -24,6 +27,8 @@ function update_env () {
link_wrapper npx "$tool_path/bin"

export_tool_path "${PREFIX}/bin"
export_tool_env NO_UPDATE_NOTIFIER 1
export_tool_env NPM_CONFIG_FUND false

tool_env=$(find_tool_env)

Expand Down Expand Up @@ -56,18 +61,17 @@ function prepare_user_config () {

prepare_prefix "${prefix}"
echo "prefix = \"${prefix}\"" >> "${USER_HOME}/.npmrc"
chown -R "${USER_ID}" "${prefix}" "${USER_HOME}/.npmrc"
chmod -R g+w "${prefix}" "${USER_HOME}/.npmrc"
mkdir -p "${USER_HOME}/.npm/_logs"
chown -R "${USER_ID}" "${prefix}" "${USER_HOME}/.npmrc" "${USER_HOME}/.npm"
chmod -R g+w "${prefix}" "${USER_HOME}/.npmrc" "${USER_HOME}/.npm"
}

if [[ -z "${tool_path}" ]]; then
npm_init

tool_path="$(create_versioned_tool_path)"
npm=${tool_path}/bin/npm

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

file=/tmp/${TOOL_NAME}.tar.xz

curl -sLo "${file}" "https://nodejs.org/dist/v${TOOL_VERSION}/node-v${TOOL_VERSION}-${NODE_DISTRO}.tar.xz"
Expand All @@ -90,15 +94,10 @@ if [[ -z "${tool_path}" ]]; then

if [[ ${MAJOR} -lt 15 ]]; then
# update to latest node-gyp to fully support python3
NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- "$npm" install --cache "${temp_folder}" node-gyp@latest
rm -rf "${temp_folder}"
$npm explore npm --global --prefix "$tool_path" -- "$npm" install node-gyp@latest --no-audit --cache "${NPM_CONFIG_CACHE}"
fi

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path $npm cache clean --force

# Clean node-gyp cache
rm -rf "$HOME/.cache"
npm_clean
fi

update_env "${tool_path}"
Expand Down
29 changes: 8 additions & 21 deletions src/usr/local/buildpack/tools/npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,20 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then
exit 1
fi

# shellcheck source=/dev/null
. /usr/local/buildpack/utils/node.sh

tool_path=$(find_versioned_tool_path)
npm=$(command -v npm)

if [[ -z "${tool_path}" ]]; then
tool_path="$(create_versioned_tool_path)"

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

$npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path $npm install --cache "${temp_folder}" -g "npm@${TOOL_VERSION}"

if [[ ${MAJOR} -lt 7 ]]; then
# update to latest node-gyp to fully support python3
NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- npm install --cache "${temp_folder}" node-gyp@latest
rm -rf "${temp_folder}"
fi

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path $npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" "${temp_folder}"
chmod -R 775 "${HOME}/.npm" || true
npm_init
npm_install
tool_path=$(find_versioned_tool_path)
npm_clean
fi

link_wrapper "${TOOL_NAME}" "$tool_path/bin"
link_wrapper npx "$tool_path/bin"
hash -d "${TOOL_NAME}" npx 2>/dev/null || true
hash -d npm npx 2>/dev/null || true

npm --version
19 changes: 7 additions & 12 deletions src/usr/local/buildpack/tools/pnpm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ set -e

check_command node

# shellcheck source=/dev/null
. /usr/local/buildpack/utils/node.sh

tool_path=$(find_versioned_tool_path)

if [[ -z "${tool_path}" ]]; then
tool_path="$(create_versioned_tool_path)"

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "${TOOL_NAME}@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" "${temp_folder}"
npm_init
npm_install
tool_path=$(find_versioned_tool_path)
npm_clean
fi

link_wrapper "${TOOL_NAME}" "$tool_path/bin"
Expand Down
19 changes: 6 additions & 13 deletions src/usr/local/buildpack/tools/yarn-slim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ set -e

check_command node

# shellcheck source=/dev/null
. /usr/local/buildpack/utils/node.sh

tool_path=$(find_versioned_tool_path)

if [[ -z "${tool_path}" ]]; then
npm_init
tool_path="$(create_versioned_tool_path)"

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

mkdir -p "${tool_path}"

npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "yarn@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" "${temp_folder}"
npm install "yarn@${TOOL_VERSION}" --global --no-audit --prefix "$tool_path" --cache "${NPM_CONFIG_CACHE}"
npm_clean

# patch yarn
sed -i 's/ steps,/ steps.slice(0,1),/' "$tool_path/lib/node_modules/yarn/lib/cli.js"
Expand Down
20 changes: 7 additions & 13 deletions src/usr/local/buildpack/tools/yarn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ set -e

check_command node

# shellcheck source=/dev/null
. /usr/local/buildpack/utils/node.sh

tool_path=$(find_versioned_tool_path)

if [[ -z "${tool_path}" ]]; then
tool_path="$(create_versioned_tool_path)"

temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"

npm cache clean --force
NPM_CONFIG_PREFIX=$tool_path npm install --cache "${temp_folder}" -g "${TOOL_NAME}@${TOOL_VERSION}"

# Clean download cache
NPM_CONFIG_PREFIX=$tool_path npm cache clean --force
# Clean node-gyp cache
rm -rf "$HOME/.cache" "${temp_folder}"
chmod -R 775 "${HOME}/.npm" || true
npm_init
npm_install
tool_path=$(find_versioned_tool_path)
npm_clean
fi

link_wrapper "${TOOL_NAME}" "${tool_path}/bin"
Expand Down
24 changes: 24 additions & 0 deletions src/usr/local/buildpack/utils/node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

function npm_init () {
temp_folder=$(mktemp -u)
mkdir -p "${temp_folder}"
export NPM_CONFIG_CACHE="${temp_folder}" NO_UPDATE_NOTIFIER=1 NPM_CONFIG_FUND=false
}

function npm_install () {
local tool_path=
tool_path="$(create_versioned_tool_path)"

npm install "${TOOL_NAME}@${TOOL_VERSION}" --global --no-audit --prefix "$tool_path" --cache "${NPM_CONFIG_CACHE}"

if [[ "${TOOL_NAME}" == "npm" && ${MAJOR} -lt 7 ]]; then
# update to latest node-gyp to fully support python3
"$tool_path/bin/npm" explore npm --global --prefix "$tool_path" -- npm install node-gyp@latest --no-audit --cache "${NPM_CONFIG_CACHE}"
fi
}

function npm_clean () {
# Clean npm stuff
rm -rf "$HOME/.cache" "${NPM_CONFIG_CACHE}" "$HOME/.npm/_logs"/*
}
17 changes: 17 additions & 0 deletions test/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ RUN install-tool yarn 1.22.15
#--------------------------------------
FROM build as testk

ARG BUILDPACK_DEBUG
# ENV NPM_CONFIG_LOGLEVEL=verbose

USER 1111:0

SHELL [ "/bin/sh", "-c" ]
Expand All @@ -304,6 +307,20 @@ RUN node --version
RUN npm --version
RUN npm --version | grep '7.24.2'

# don't update!!
ENV NPM_VERSION=6.14.16

RUN install-tool npm ${NPM_VERSION}

RUN node --version
RUN npm --version
RUN npm --version | grep "${NPM_VERSION}"

# TODO: use bats test
RUN set -ex; \
[ "$(cat /home/user/npm/${NPM_VERSION}/lib/node_modules/npm/node_modules/node-gyp/package.json | jq -r .version)" != "5.1.0" ] \
&& echo node-gyp works || exit 1;

#--------------------------------------
# final
#--------------------------------------
Expand Down