Skip to content

Commit

Permalink
cleanup + test
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Oct 30, 2024
1 parent 1aa8f87 commit 0a75502
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 10 deletions.
7 changes: 1 addition & 6 deletions cli/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl ShellCommand for NpmCommand {
mut context: ShellCommandContext,
) -> LocalBoxFuture<'static, ExecuteResult> {
if context.args.first().map(|s| s.as_str()) == Some("run")
&& context.args.len() > 2
&& context.args.len() >= 2
// for now, don't run any npm scripts that have a flag because
// we don't handle stuff like `--workspaces` properly
&& !context.args.iter().any(|s| s.starts_with('-'))
Expand Down Expand Up @@ -375,11 +375,6 @@ impl ShellCommand for NpmPackageBinCommand {
},
];

context.state.apply_env_var(
"npm_config_user_agent",
&crate::npm::get_npm_config_user_agent(),
);

args.extend(context.args);
let executable_command = deno_task_shell::ExecutableCommand::new(
"deno".to_string(),
Expand Down
18 changes: 18 additions & 0 deletions cli/tools/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"npm_config_user_agent",
crate::npm::get_npm_config_user_agent(),
);
});
}

pub async fn run_script(
mode: WorkerExecutionMode,
flags: Arc<Flags>,
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 0 additions & 4 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,6 @@ impl CliMainWorkerFactory {
custom_extensions: Vec<Extension>,
stdio: deno_runtime::deno_io::Stdio,
) -> Result<CliMainWorker, AnyError> {
std::env::set_var(
"npm_config_user_agent",
crate::npm::get_npm_config_user_agent(),
);
let shared = &self.shared;
let ModuleLoaderAndSourceMapGetter { module_loader } = shared
.module_loader_factory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log(`npm_config_user_agent: ${process.env["npm_config_user_agent"]}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@denotest/print-npm-user-agent",
"version": "1.0.0",
"bin": {
"print-npm-user-agent": "index.js"
}
}
51 changes: 51 additions & 0 deletions tests/specs/npm/user_agent_env_var/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"tempDir": true,
"tests": {
"run_npm_package": {
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"args": "run -A npm:@denotest/print-npm-user-agent",
"output": "run.out"
}
]
},
"npx": {
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"if": "unix",
"args": "task run-with-npx",
"output": "npx.out"
}
]
},
"bin_command": {
"steps": [
{ "args": "install", "output": "[WILDCARD]" },
{
"args": "task run-via-bin",
"output": "Task run-via-bin print-npm-user-agent\nnpm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]\n"
}
]
},
"npm_run": {
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"args": "task run-via-other-task",
"output": "npm_run.out"
}
]
}
}
}
1 change: 1 addition & 0 deletions tests/specs/npm/user_agent_env_var/bin_command.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]
3 changes: 3 additions & 0 deletions tests/specs/npm/user_agent_env_var/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nodeModulesDir": "auto"
}
18 changes: 18 additions & 0 deletions tests/specs/npm/user_agent_env_var/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/specs/npm/user_agent_env_var/npm_run.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Task run-via-other-task npm run run-via-bin
Task run-via-bin print-npm-user-agent
npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]
2 changes: 2 additions & 0 deletions tests/specs/npm/user_agent_env_var/npx.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Task run-with-npx npx print-npm-user-agent
npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]
10 changes: 10 additions & 0 deletions tests/specs/npm/user_agent_env_var/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"run-with-npx": "npx print-npm-user-agent",
"run-via-other-task": "npm run run-via-bin",
"run-via-bin": "print-npm-user-agent"
},
"dependencies": {
"@denotest/print-npm-user-agent": "1.0.0"
}
}
1 change: 1 addition & 0 deletions tests/specs/npm/user_agent_env_var/run.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]
1 change: 1 addition & 0 deletions tests/specs/npm/user_agent_env_var/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(process.env.npm_config_user_agent);

0 comments on commit 0a75502

Please sign in to comment.