Skip to content

Commit

Permalink
(nodejs plugin) add support for automatically shimming package manage…
Browse files Browse the repository at this point in the history
…rs with `corepack enable` (#1118)

* feat: support RTX_NODE_COREPACK_ENABLE

* chore: lint e2e/test_nodejs

* refactor(node): RTX_NODE_COREPACK_ENABLE -> RTX_NODE_COREPACK

* refactor(node): remove unnecessary `test_corepack`

---------

Co-authored-by: jdx <[email protected]>
  • Loading branch information
jasisk and jdx authored Dec 8, 2023
1 parent 1c6adf8 commit f0c1b8f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ required system dependencies.
- `RTX_NODE_CONFIGURE_OPTS` [string]: Additional `./configure` options.
- `RTX_NODE_MAKE_OPTS` [string]: Additional `make` options.
- `RTX_NODE_MAKE_INSTALL_OPTS` [string]: Additional `make install` options.
- `RTX_NODE_COREPACK` [bool]: Installs the default corepack shims after installing any node version that ships with [corepack](https://github.com/nodejs/corepack).

## Default node packages

Expand Down
5 changes: 5 additions & 0 deletions e2e/test_nodejs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ assert "rtx x -- node --version" "v20.0.0"
# rtx uninstall node
# RTX_NODE_COMPILE=1 rtx i node
# assert "rtx x -- node --version" "v20.0.0"

export RTX_NODE_COREPACK=1
rtx uninstall node
rtx i node
assert_contains "rtx x node -- which yarn" "yarn"
1 change: 1 addition & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub static RTX_NODE_DEFAULT_PACKAGES_FILE: Lazy<PathBuf> = Lazy::new(|| {
HOME.join(".default-npm-packages")
})
});
pub static RTX_NODE_COREPACK: Lazy<bool> = Lazy::new(|| var_is_true("RTX_NODE_COREPACK"));
pub static NVM_DIR: Lazy<PathBuf> =
Lazy::new(|| var_path("NVM_DIR").unwrap_or_else(|| HOME.join(".nvm")));
pub static NODENV_ROOT: Lazy<PathBuf> =
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ impl NodePlugin {
tv.install_path().join("bin/npm")
}

fn corepack_path(&self, tv: &ToolVersion) -> PathBuf {
tv.install_path().join("bin/corepack")
}

fn install_default_packages(
&self,
config: &Config,
Expand Down Expand Up @@ -204,6 +208,22 @@ impl NodePlugin {
Ok(())
}

fn enable_default_corepack_shims(
&self,
config: &Config,
tv: &ToolVersion,
pr: &ProgressReport,
) -> Result<()> {
pr.set_message("enabling corepack shims");
let corepack = self.corepack_path(tv);
CmdLineRunner::new(&config.settings, corepack)
.with_pr(pr)
.arg("enable")
.env("PATH", CorePlugin::path_env_with_tv_path(tv)?)
.execute()?;
Ok(())
}

fn test_node(&self, config: &Config, tv: &ToolVersion, pr: &ProgressReport) -> Result<()> {
pr.set_message("node -v");
CmdLineRunner::new(&config.settings, self.node_path(tv))
Expand Down Expand Up @@ -295,6 +315,10 @@ impl Plugin for NodePlugin {
self.install_npm_shim(&ctx.tv)?;
self.test_npm(ctx.config, &ctx.tv, &ctx.pr)?;
self.install_default_packages(ctx.config, &ctx.tv, &ctx.pr)?;
if *env::RTX_NODE_COREPACK && self.corepack_path(&ctx.tv).exists() {
self.enable_default_corepack_shims(ctx.config, &ctx.tv, &ctx.pr)?;
}

Ok(())
}
}
Expand Down

0 comments on commit f0c1b8f

Please sign in to comment.