Skip to content

Commit 2726fe3

Browse files
authored
feat: uniform package manager default version logic (#35)
Both npm and pnpm now default to the detected version, when the version is omitted. In a case where pnpm is not detected, the latest version will be installed. This change also brings few minor improvements: - Will not try to reinstall requested version if already installed - Increase log points for easier debugging.
1 parent 2b8cf30 commit 2726fe3

File tree

4 files changed

+81
-16
lines changed

4 files changed

+81
-16
lines changed

.circleci/test-deploy.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ jobs:
4141
echo "pnpm version $PNPM_LATEST_VERSION not found"
4242
exit 1
4343
fi
44+
ensure-pnpm-fresh-install:
45+
docker:
46+
- image: node:lts-slim
47+
resource_class: medium
48+
steps:
49+
- core/ensure_pkg_manager:
50+
ref: pnpm
51+
- run:
52+
name: Validate version
53+
command: |
54+
PNPM_LATEST_VERSION=$(npm view pnpm version)
55+
if ! pnpm --version | grep -q "$PNPM_LATEST_VERSION"; then
56+
echo "pnpm version $PNPM_LATEST_VERSION not found"
57+
exit 1
58+
fi
4459
install-dependencies-npm-node-18:
4560
executor:
4661
name: core/node_secrets
@@ -70,7 +85,7 @@ jobs:
7085
- core/install_dependencies:
7186
pkg_manager: [email protected]
7287
pkg_json_dir: ~/project/sample
73-
install_command: npm install
88+
install_command: pnpm install
7489
cache_version: v10
7590
- run: cd ~/project/sample && npm run build
7691
run-script-npm:
@@ -105,6 +120,8 @@ workflows:
105120
filters: *filters
106121
- ensure-pnpm-version:
107122
filters: *filters
123+
- ensure-pnpm-fresh-install:
124+
filters: *filters
108125
- install-dependencies-npm-node-18:
109126
filters: *filters
110127
- install-dependencies-pnpm-node-18:
@@ -125,6 +142,7 @@ workflows:
125142
- orb-tools/pack
126143
- ensure-npm-version
127144
- ensure-pnpm-version
145+
- ensure-pnpm-fresh-install
128146
- install-dependencies-npm-node-18
129147
- install-dependencies-pnpm-node-18
130148
- install-dependencies-custom-command

src/commands/ensure_pkg_manager.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ parameters:
99
description: |
1010
Choose Node.js package manager to use. Supports npm and pnpm.
1111
The package manager must follow the format <name>[@<version|tag>].
12-
Omitting the version implies that the npm version is determined by the target Node.js environment,
13-
while pnpm will default to the latest version.
14-
12+
Omitting the version implies
13+
(for npm) use the version that comes with the target Node.js environment,
14+
(for pnpm) use the existing version if found, otherwise use the latest version.
1515
steps:
1616
- run:
1717
name: Check Node.js version

src/examples/pnpm_ensure.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
description: |
22
Ensure the desired package manager, including the version, is installed on the system.
3-
If desired the package manager can be set through the DEFAULT_PKG_MANAGER environment variable.
3+
If desired, the package manager can be set through the DEFAULT_PKG_MANAGER environment variable.
44
The npm is used to update itself and to install pnpm when required.
55
66
usage:

src/scripts/ensure-pkg-manager.sh

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ NAME="${PARAM_STR_REF}"
66
VERSION=""
77
SUDO=""
88

9+
if [[ ${EUID} -ne 0 ]]; then
10+
echo "Using sudo privileges to finish the process"
11+
12+
SUDO="sudo"
13+
fi
14+
915
check_installation () {
1016
local installed_version
1117
local required_version
@@ -31,6 +37,25 @@ check_installation () {
3137
fi
3238
}
3339

40+
change_pnpm_store_dir_and_exit () {
41+
local current_store_dir
42+
local target_store_dir
43+
44+
current_store_dir=$(pnpm store path)
45+
target_store_dir="${HOME}/.pnpm_store"
46+
47+
if [[ "${current_store_dir}" != "${target_store_dir}" ]]; then
48+
echo "Changing the pnpm store directory"
49+
50+
set -x
51+
pnpm config set store-dir "${target_store_dir}"
52+
${SUDO} rm -rf "${current_store_dir}"
53+
set +x
54+
fi
55+
56+
exit 0
57+
}
58+
3459
if [[ "${PARAM_STR_REF}" =~ ${PKG_MANAGER_WITH_VERSION_REGEX} ]]; then
3560
NAME="${BASH_REMATCH[1]}"
3661
VERSION="${BASH_REMATCH[2]}"
@@ -39,34 +64,56 @@ fi
3964
echo "export CURRENT_PKG_MANAGER='${NAME}'" >> "${BASH_ENV}"
4065
echo "Starting to ensure ${NAME} is set for usage"
4166

42-
if [[ ${EUID} -ne 0 ]]; then
43-
echo "Using sudo privileges to finish the process"
44-
45-
SUDO="sudo"
46-
fi
47-
67+
cd ~ || echo "Cannot navigate to home, possible version mismatch"
68+
4869
if [[ "${NAME}" == "npm" ]]; then
70+
echo "Detected npm $(npm --version)"
71+
4972
if [[ -n "${VERSION}" ]]; then
50-
${SUDO} npm i -g npm@"${VERSION}"
51-
check_installation "${NAME}" "${VERSION}"
52-
else
53-
echo "Detected npm version: $(npm --version)"
73+
if npm --version | grep "${VERSION}" > /dev/null 2>&1; then
74+
echo "Requested version of npm is already installed"
75+
else
76+
echo "Requested version of npm not found, updating detected version"
77+
78+
${SUDO} npm i -g npm@"${VERSION}"
79+
check_installation "${NAME}" "${VERSION}"
80+
fi
81+
82+
exit 0
5483
fi
5584

85+
echo "Using detected version of npm"
86+
5687
exit 0
5788
fi
5889

5990
if [[ "${NAME}" == "pnpm" ]]; then
6091
if command -v pnpm > /dev/null 2>&1; then
61-
echo "Found pnpm installation, removing it"
92+
echo "Detected pnpm $(pnpm --version)"
93+
94+
if [[ -z "${VERSION}" ]]; then
95+
echo "Using detected version of pnpm"
96+
97+
change_pnpm_store_dir_and_exit
98+
elif pnpm --version | grep "${VERSION}" > /dev/null 2>&1; then
99+
echo "Requested vesion of pnpm is already installed"
100+
101+
change_pnpm_store_dir_and_exit
102+
fi
103+
104+
echo "Requested version of pnpm not found, removing detected version"
62105

63106
${SUDO} rm -rf "$(pnpm store path)" > /dev/null 2>&1
64107
${SUDO} rm -rf "${PNPM_HOME}" > /dev/null 2>&1
65108
${SUDO} npm rm -g pnpm > /dev/null 2>&1
109+
else
110+
echo "Did not detect pnpm, proceeding with installation"
66111
fi
67112

68113
if [[ -z "${VERSION}" ]]; then
69114
VERSION=$(npm view pnpm version)
115+
116+
echo "Version not explicitly requested, opting for ${VERSION}"
70117
fi
71118

72119
${SUDO} npm i -g pnpm@"${VERSION}"

0 commit comments

Comments
 (0)