更改 NPM 版本以进行测试的工具 - 通过将不同版本符号链接到全局空间 [英] Tool to change NPM version for testing - by symlinking different versions to global space

查看:38
本文介绍了更改 NPM 版本以进行测试的工具 - 通过将不同版本符号链接到全局空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NVM 是一个很棒的工具,可以让我们随意更改 Node 版本.我正在尝试开发一些好的东西,让用户可以随意更改 NPM 版本,主要是为了在发布之前测试库代码.

NVM is a great tool to allow us to change Node versions at will. I am trying to develop something good that will allow users to change NPM versions at will, mostly for the purposes of testing library code, before release.

所以我正在开发@oresoftware/npm.version",它有一个名为 npmv 的可执行文件,它将更改 $PATH 中的当前 npm 版本.

So I am working on '@oresoftware/npm.version', which has an executable called npmv which will change out the current npm version in the $PATH.

用户运行:

npmv use 5.3

如果未安装 5.3,它会将其安装到用户 home 所在的目录中,然后将该版本符号链接到全局空间.

and if 5.3 is not installed, it will install it to a directory with the user home, then it will symlink that version to the global space.

这是我所拥有的:

#!/usr/bin/env bash

set -e;
desired_npm_version="$1"

if [ -z "$desired_npm_version" ]; then
  echo >&2 "No desired npm version provided.";
  exit 1;
fi

desired_v="$npmvv/$desired_npm_version"

if [ ! -d "$desired_v" ]; then

  mkdir -p "$desired_v";
  cd "$desired_v";
  npm init -f --silent;
  npm install "npm@$desired_npm_version" -f --silent

fi


cd "$npmvv/$desired_npm_version";

npm_root="$(npm root -g)";
npm_bin="$(npm bin -g)";

rm -rf "$npm_root/npm";
rm -rf "$npm_bin/npm";
rm -rf "$npm_bin/npx";

ln -sf node_modules/npm "$npm_root"
ln -sf node_modules/.bin/npm  "$npm_bin/npm"
ln -sf node_modules/.bin/npx  "$npm_bin/npx"
# end

不要运行它,因为它可能会破坏您的 NPM 安装.我不知道为什么它不起作用,但至少它不能很好地与 NVM(节点版本管理器)配合使用.

don't run it, because it will probably break your NPM installation. I cannot figure out why it doesn't work, but it doesn't play well with NVM (Node Version Manager) at the very least.

请注意,NPM 仅附带两个可执行文件(npm、npx).

Note that NPM only comes with two executables (npm, npx).

如果您知道自己在做什么并且看到脚本中可能缺少的内容,请告诉我.我唯一能想到的是,也许运行 ln -s ;<target-link> 针对本身就是符号链接的 a,不起作用?

If you know what you are doing and see something that might be missing in the script, please let me know. The only thing I can think of, is that perhaps running ln -s <source-file> <target-link> against a that is itself a symlink, doesn't work?

推荐答案

我现在有一些工作得非常好,只需使用符号链接.需要注意的一件重要事情是,无法保证 npm <---> 节点兼容性.许多 npm 版本与较旧的节点版本或较新的节点版本不兼容.

I have something that works pretty damn well now, just using symlinks. One important thing to note is that npm <---> node compatibility is not guaranteed. Many npm versions are not compatible with older node versions or newer node versions.

https://github.com/ORESoftware/npm.version

主要思想是,我们在用户主页中安装新需要的 NPM 版本,然后使用符号链接符号链接到该 NPM 版本:

the main idea, is that we install the newly desired NPM version in user home, and then use symlinks to symlink to that NPM version:

npm_bin="$(npm bin -g)"
npm_root="$(npm root -g)"
cd "$HOME/.npmv_stash/versions/6.1.0";
ln -s "$PWD/node_modules/npm" "$npm_root/npm";
npm_source="$(readlink -f "$PWD/node_modules/.bin/npm")";
npx_source="$(readlink -f "$PWD/node_modules/.bin/npx")";
ln -sf  "$npm_source" "$npm_bin/npm"
ln -sf  "$npx_source" "$npm_bin/npx" 

这篇关于更改 NPM 版本以进行测试的工具 - 通过将不同版本符号链接到全局空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆