summaryrefslogtreecommitdiff
path: root/cli/tools
diff options
context:
space:
mode:
authorNathan Whitaker <17734409+nathanwhit@users.noreply.github.com>2024-10-31 22:19:19 -0700
committerGitHub <noreply@github.com>2024-10-31 22:19:19 -0700
commit04ae1a551726dd6e0047a942b459d18e1dcb1935 (patch)
tree22bd67ae0f7d7caee2da4e701817907e769ed24c /cli/tools
parent6c6bbeb97495e8c3e8eac7bea27bf836f02b575f (diff)
fix(cli): set `npm_config_user_agent` when running npm packages or tasks (#26639)
Fixes #25342. Still not sure on the exact user agent to set (should it include `node`?). After this PR, here's the state of running some `create-*` packages (just ones I could think of off the top of my head): | package | prints/runs/suggests deno install | notes | | ---------------- | ------------- | ------ | | `create-next-app` | ❌ | falls back to npm, needs a PR ([code](https://github.com/vercel/next.js/blob/c32e2802097c03fd9f95b1dae228d6e0257569c0/packages/create-next-app/helpers/get-pkg-manager.ts#L3)) | `sv create` | ❌ | uses `package-manager-detector`, needs a PR ([code](https://github.com/antfu-collective/package-manager-detector/tree/main)) | `create-qwik` | ✅ | runs `deno install` but suggests `deno start` which doesn't work (should be `deno task start` or `deno run start`) | `create-astro` | ✅ | runs `deno install` but suggests `npm run dev` later in output, probably needs a PR | `nuxi init` | ❌ | deno not an option in dialog, needs a PR ([code](https://github.com/nuxt/cli/blob/f04e2e894446f597da9d971b7eb03191d5a0da7e/src/commands/init.ts#L96-L102)) | `create-react-app` | ❌ | uses npm | `ng new` (`@angular/cli`) | ❌ | uses npm | `create-vite` | ✅ | suggests working deno commands 🎉 | `create-solid` | ❌ | suggests npm commands, needs PR It's possible that fixing `package-manager-detector` or other packages might make some of these just work, but haven't looked too carefully at each
Diffstat (limited to 'cli/tools')
-rw-r--r--cli/tools/run/mod.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/cli/tools/run/mod.rs b/cli/tools/run/mod.rs
index 152e2650b..bebb3f588 100644
--- a/cli/tools/run/mod.rs
+++ b/cli/tools/run/mod.rs
@@ -30,6 +30,16 @@ To grant permissions, set them before the script argument. For example:
}
}
+fn set_npm_user_agent() {
+ static ONCE: std::sync::Once = std::sync::Once::new();
+ ONCE.call_once(|| {
+ std::env::set_var(
+ crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR,
+ crate::npm::get_npm_config_user_agent(),
+ );
+ });
+}
+
pub async fn run_script(
mode: WorkerExecutionMode,
flags: Arc<Flags>,
@@ -58,6 +68,10 @@ pub async fn run_script(
let main_module = cli_options.resolve_main_module()?;
+ if main_module.scheme() == "npm" {
+ set_npm_user_agent();
+ }
+
maybe_npm_install(&factory).await?;
let worker_factory = factory.create_cli_main_worker_factory().await?;
@@ -119,6 +133,10 @@ async fn run_with_watch(
let cli_options = factory.cli_options()?;
let main_module = cli_options.resolve_main_module()?;
+ if main_module.scheme() == "npm" {
+ set_npm_user_agent();
+ }
+
maybe_npm_install(&factory).await?;
let _ = watcher_communicator.watch_paths(cli_options.watch_paths());