Skip to content

Commit

Permalink
Add N_PRESERVE_COREPACK (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jul 16, 2022
1 parent 36a85d1 commit e20a854
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,24 @@ Or execute a command with `PATH` modified so `node` and `npm` will be from the d

## Preserving npm

A Node.js install normally includes `npm` as well, but you may wish to preserve an updated `npm` and `npx` leaving them out of the install using `--preserve`:
A Node.js install normally also includes `npm`, `npx`, and `corepack`, but you may wish to preserve your current (especially newer) versions using `--preserve`:

$ npm install -g npm@latest
...
$ npm --version
6.13.7
# Node.js 8.17.0 includes (older) npm 6.13.4
$ n -p 8
installed : v8.17.0
$ npm --version
6.13.7

You can make this the default by setting `N_PRESERVE_NPM` to a non-empty string.
You can make this the default by setting the environment variable to a non-empty string. There are separate environment variables for `npm` and `corepack`:

export N_PRESERVE_NPM=1
export N_PRESERVE_COREPACK=1

You can be explicit to get the desired behaviour whatever the environment variable:
You can be explicit to get the desired behaviour whatever the environment variables:

n --preserve nightly
n --no-preserve latest
Expand Down Expand Up @@ -276,6 +278,7 @@ In brief:
- support for [NO_COLOR](https://no-color.org) and [CLICOLOR=0](https://bixense.com/clicolors) for controlling use of ANSI color codes
- `N_MAX_REMOTE_MATCHES` to change the default `ls-remote` maximum of 20 matching versions
- `N_PRESERVE_NPM`: See [Preserving npm](#preserving-npm)
- `N_PRESERVE_COREPACK`: See [Preserving npm](#preserving-npm)

## How It Works

Expand Down
11 changes: 7 additions & 4 deletions bin/n
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ activate() {
clean_copy_folder "$dir/lib/node_modules/npm" "$N_PREFIX/lib/node_modules/npm"
fi
# Takes same steps for corepack (experimental in node 16.9.0) as for npm, to avoid version problems.
if [[ -e "$dir/lib/node_modules/corepack" ]]; then
if [[ -e "$dir/lib/node_modules/corepack" && -z "${N_PRESERVE_COREPACK}" ]]; then
mkdir -p "$N_PREFIX/lib/node_modules"
clean_copy_folder "$dir/lib/node_modules/corepack" "$N_PREFIX/lib/node_modules/corepack"
fi
Expand All @@ -707,7 +707,9 @@ activate() {
# Copy bin items by hand, in case user has installed global npm modules into cache.
cp -f "$dir/bin/node" "$N_PREFIX/bin"
[[ -e "$dir/bin/node-waf" ]] && cp -f "$dir/bin/node-waf" "$N_PREFIX/bin" # v0.8.x
[[ -e "$dir/bin/corepack" ]] && cp -fR "$dir/bin/corepack" "$N_PREFIX/bin" # from 16.9.0
if [[ -z "${N_PRESERVE_COREPACK}" ]]; then
[[ -e "$dir/bin/corepack" ]] && cp -fR "$dir/bin/corepack" "$N_PREFIX/bin" # from 16.9.0
fi
if [[ -z "${N_PRESERVE_NPM}" ]]; then
[[ -e "$dir/bin/npm" ]] && cp -fR "$dir/bin/npm" "$N_PREFIX/bin"
[[ -e "$dir/bin/npx" ]] && cp -fR "$dir/bin/npx" "$N_PREFIX/bin"
Expand Down Expand Up @@ -1422,6 +1424,7 @@ function show_diagnostics() {
[[ -n "${N_PREFIX}" ]] && echo "PATH: ${PATH}"
echo "ls-remote max matches: ${N_MAX_REMOTE_MATCHES}"
[[ -n "${N_PRESERVE_NPM}" ]] && echo "installs preserve npm by default"
[[ -n "${N_PRESERVE_COREPACK}" ]] && echo "installs preserve corepack by default"

printf "\nProxy\n"
# disable "var is referenced but not assigned": https://github.com/koalaman/shellcheck/wiki/SC2154
Expand Down Expand Up @@ -1561,8 +1564,8 @@ while [[ $# -ne 0 ]]; do
-q|--quiet) set_quiet ;;
-d|--download) DOWNLOAD="true" ;;
--insecure) set_insecure ;;
-p|--preserve) N_PRESERVE_NPM="true" ;;
--no-preserve) N_PRESERVE_NPM="" ;;
-p|--preserve) N_PRESERVE_NPM="true" N_PRESERVE_COREPACK="true" ;;
--no-preserve) N_PRESERVE_NPM="" N_PRESERVE_COREPACK="" ;;
--use-xz) N_USE_XZ="true" ;;
--no-use-xz) N_USE_XZ="false" ;;
--latest) display_remote_versions latest; exit ;;
Expand Down
1 change: 1 addition & 0 deletions test/tests/shared-functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function unset_n_env(){
unset N_NODE_DOWNLOAD_MIRROR
unset N_MAX_REMOTE_MATCHES
unset N_PRESERVE_NPM
unset N_PRESERVE_COREPACK
unset HTTP_USER
unset HTTP_PASSWORD
unset GREP_OPTIONS
Expand Down

0 comments on commit e20a854

Please sign in to comment.