Repository: denoland/deno
File: cli/task_runner.rs (Rust)

Code before:

    // fallback to running the real npm command
    let npm_path = match context.state.resolve_command_path(OsStr::new("npm")) {
      Ok(path) => path,
      Err(err) => {
        let _ = context.stderr.write_line(&format!("{}", err));
        return Box::pin(std::future::ready(ExecuteResult::from_exit_code(
          err.exit_code(),
        )));
      }
    };
    ExecutableCommand::new("npm".to_string(), npm_path).execute(context)
  }
}

pub struct NodeCommand;

impl ShellCommand for NodeCommand {
  fn execute(
    &self,
    context: ShellCommandContext,
  ) -> LocalBoxFuture<'static, ExecuteResult> {
    // run with deno if it's a simple invocation, fall back to node
    // if there are extra flags
    let mut args: Vec<OsString> = Vec::with_capacity(context.args.len());
    if context.args.len() > 1
      && ({
        let first_arg = context.args[0].to_string_lossy();
        first_arg.starts_with('-') // has a flag
        || !matches!(
          MediaType::from_str(&first_arg),
          MediaType::Cjs | MediaType::Mjs | MediaType::JavaScript
        )
      })
    {
      return ExecutableCommand::new(
        "node".to_string(),
        "node".to_string().into(),
      )
      .execute(context);
    }

    args.extend(["run".into(), "-A".into()]);
    args.extend(context.args.iter().cloned());

    let mut state = context.state;

    state.apply_env_var(
      OsStr::new(USE_PKG_JSON_HIDDEN_ENV_VAR_NAME),
      OsStr::new("1"),
    );

Review comment (bug):
Side note: This seems wrong. Shouldn't this be `> 0` because it's only checking the first argument?

Code after:

    // fallback to running the real npm command
    let npm_path = match context.state.resolve_command_path(OsStr::new("npm")) {
      Ok(path) => path,
      Err(err) => {
        let _ = context.stderr.write_line(&format!("{}", err));
        return Box::pin(std::future::ready(ExecuteResult::from_exit_code(
          err.exit_code(),
        )));
      }
    };
    ExecutableCommand::new("npm".to_string(), npm_path).execute(context)
  }
}

pub struct NodeCommand;

impl ShellCommand for NodeCommand {
  fn execute(
    &self,
    context: ShellCommandContext,
  ) -> LocalBoxFuture<'static, ExecuteResult> {
    // run with deno if it's a simple invocation, fall back to node
    // if there are extra flags
    let mut args: Vec<OsString> = Vec::with_capacity(context.args.len());
    if context.args.len() > 1
      && ({
        let first_arg = context.args[0].to_string_lossy();
        first_arg.starts_with('-') // has a flag
        || !matches!(
          MediaType::from_str(&first_arg),
          MediaType::Cjs | MediaType::Mjs | MediaType::JavaScript
        )
      })
    {
      return ExecutableCommand::new("node".to_string(), PathBuf::from("node"))
        .execute(context);
    }

    args.extend(["run".into(), "-A".into()]);
    args.extend(context.args);

    let mut state = context.state;

    state.apply_env_var(
      OsStr::new(USE_PKG_JSON_HIDDEN_ENV_VAR_NAME),
      OsStr::new("1"),
    );
    ExecutableCommand::new("deno".to_string(), std::env::current_exe().unwrap())
      .execute(ShellCommandContext {
        args,