diff --git a/Cargo.toml b/Cargo.toml index c8db05c..f412a72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/ryanccn/nrr.git" [dependencies] ahash = "0.8.9" -clap = { version = "4.5.1", features = ["derive"] } +clap = { version = "4.5.1", features = ["derive", "env"] } color-eyre = "0.6.2" ctrlc = "3.4.2" indexmap = { version = "2.2.3", features = ["serde"] } diff --git a/src/cli/exec.rs b/src/cli/exec.rs index 29bf14d..aaa6ca7 100644 --- a/src/cli/exec.rs +++ b/src/cli/exec.rs @@ -15,18 +15,26 @@ pub fn handle(package_paths: impl Iterator, args: &ExecArgs) -> if let Ok(raw) = fs::read_to_string(&package_path) { if let Ok(package) = serde_json::from_str::(&raw) { if has_exec(&package_path, &args.bin) { - eprint!( - "{}", - package.make_prefix( - match crate::get_level() { - 1 => None, - _ => Some(&args.bin), - }, - Stream::Stderr - ) - ); - - run_exec(&package_path, &package, &args.bin, &args.extra_args)?; + if !args.silent { + eprint!( + "{}", + package.make_prefix( + match crate::get_level() { + 1 => None, + _ => Some(&args.bin), + }, + Stream::Stderr + ) + ); + } + + run_exec( + &package_path, + &package, + &args.bin, + &args.extra_args, + args.silent, + )?; executed_exec = true; break; diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 1c31649..df82ccd 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -52,8 +52,12 @@ struct RootArgs { extra_args: Vec, /// Don't run pre- and post- scripts - #[arg(short, long)] + #[arg(short, long, env = "NRR_NO_PRE_POST")] no_pre_post: bool, + + /// Disable printing package and command information + #[clap(short, long, env = "NRR_SILENT")] + silent: bool, } #[derive(Args, Clone)] @@ -66,8 +70,12 @@ struct RunArgs { extra_args: Vec, /// Don't run pre- and post- scripts - #[arg(short, long)] + #[arg(short, long, env = "NRR_NO_PRE_POST")] no_pre_post: bool, + + /// Disable printing package and command information + #[clap(short, long, env = "NRR_SILENT")] + silent: bool, } #[derive(Args, Clone)] @@ -78,6 +86,10 @@ struct ExecArgs { /// Extra arguments to pass to the command #[clap(allow_hyphen_values = true)] extra_args: Vec, + + /// Disable printing package and command information + #[clap(short, long, env = "NRR_SILENT")] + silent: bool, } pub fn get_level() -> &'static usize { @@ -127,6 +139,7 @@ impl Cli { script: script_name.to_owned(), extra_args: self.root_args.extra_args.clone(), no_pre_post: self.root_args.no_pre_post, + silent: self.root_args.silent, }, )?; } else { diff --git a/src/cli/run.rs b/src/cli/run.rs index ec49526..74fc9a5 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -16,40 +16,45 @@ fn run_script_full( script_cmd: &str, extra_args: &[String], no_pre_post: bool, + silent: bool, ) -> Result<()> { - eprint!( - "{}", - package_data.make_prefix( - match crate::get_level() { - 1 => None, - _ => Some(script_name), - }, - Stream::Stderr - ) - ); + if !silent { + eprint!( + "{}", + package_data.make_prefix( + match crate::get_level() { + 1 => None, + _ => Some(script_name), + }, + Stream::Stderr + ) + ); + } if !no_pre_post { let pre_script_name = String::from("pre") + script_name; if let Some(pre_script_cmd) = package_data.scripts.get(&pre_script_name) { run_script( + ScriptType::Pre, package, package_data, &pre_script_name, pre_script_cmd, - ScriptType::Pre, &[], + silent, )?; } } run_script( + ScriptType::Normal, package, package_data, script_name, script_cmd, - ScriptType::Normal, extra_args, + silent, )?; if !no_pre_post { @@ -57,12 +62,13 @@ fn run_script_full( if let Some(post_script_cmd) = package_data.scripts.get(&post_script_name) { run_script( + ScriptType::Post, package, package_data, &post_script_name, post_script_cmd, - ScriptType::Post, &[], + silent, )?; } } @@ -85,6 +91,7 @@ pub fn handle(package_paths: impl Iterator, args: &RunArgs) -> R script_cmd, &args.extra_args, args.no_pre_post, + args.silent, )?; executed_script = true; diff --git a/src/run/exec.rs b/src/run/exec.rs index d8d21d3..4cbfab1 100644 --- a/src/run/exec.rs +++ b/src/run/exec.rs @@ -19,25 +19,26 @@ pub fn run_exec( package_data: &PackageJson, bin: &str, args: &[String], + silent: bool, ) -> Result<()> { let package_folder = package_path.parent().unwrap(); - let cmd_prefix = "$".repeat(*crate::get_level()); + if !silent { + let cmd_prefix = "$".repeat(*crate::get_level()); - eprintln!( - "{} {} {}", - cmd_prefix - .if_supports_color(Stream::Stderr, |text| text.cyan()) - .if_supports_color(Stream::Stderr, |text| text.dimmed()), - bin.if_supports_color(Stream::Stderr, |text| text.dimmed()), - args.join(" ") - .if_supports_color(Stream::Stderr, |text| text.dimmed()) - ); + eprintln!( + "{} {} {}", + cmd_prefix + .if_supports_color(Stream::Stderr, |text| text.cyan()) + .if_supports_color(Stream::Stderr, |text| text.dimmed()), + bin.if_supports_color(Stream::Stderr, |text| text.dimmed()), + args.join(" ") + .if_supports_color(Stream::Stderr, |text| text.dimmed()) + ); + } let mut subproc = Command::new(bin); - subproc.current_dir(package_folder); - - subproc.args(args.iter().map(|f| f.to_string())); + subproc.current_dir(package_folder).args(args); subproc .env("PATH", util::make_patched_path(package_path)?) @@ -70,11 +71,13 @@ pub fn run_exec( if !status.success() { let code = status.code().unwrap_or(1); - eprintln!( - "{} Exited with status {}!", - "error".if_supports_color(Stream::Stderr, |text| text.red()), - util::itoa(code).if_supports_color(Stream::Stderr, |text| text.bold()), - ); + if !silent { + eprintln!( + "{} Exited with status {}!", + "error".if_supports_color(Stream::Stderr, |text| text.red()), + util::itoa(code).if_supports_color(Stream::Stderr, |text| text.bold()), + ); + } std::process::exit(code); } diff --git a/src/run/script.rs b/src/run/script.rs index f29262f..74980fa 100644 --- a/src/run/script.rs +++ b/src/run/script.rs @@ -51,12 +51,13 @@ impl ScriptType { } pub fn run_script( + script_type: ScriptType, package_path: &Path, package_data: &PackageJson, script_name: &str, script_cmd: &str, - script_type: ScriptType, extra_args: &[String], + silent: bool, ) -> Result<()> { let package_folder = package_path.parent().unwrap(); @@ -72,15 +73,17 @@ pub fn run_script( full_cmd.push_str(arg.as_ref()); }); - let cmd_prefix = script_type.prefix() + &"$".repeat(*crate::get_level()); + if !silent { + let cmd_prefix = script_type.prefix() + &"$".repeat(*crate::get_level()); - eprintln!( - "{} {}", - cmd_prefix - .if_supports_color(Stream::Stderr, |text| text.cyan()) - .if_supports_color(Stream::Stderr, |text| text.dimmed()), - full_cmd.if_supports_color(Stream::Stderr, |text| text.dimmed()) - ); + eprintln!( + "{} {}", + cmd_prefix + .if_supports_color(Stream::Stderr, |text| text.cyan()) + .if_supports_color(Stream::Stderr, |text| text.dimmed()), + full_cmd.if_supports_color(Stream::Stderr, |text| text.dimmed()) + ); + } let mut subproc = make_shell_cmd()?; subproc.current_dir(package_folder).arg(&full_cmd); @@ -119,11 +122,13 @@ pub fn run_script( if !status.success() { let code = status.code().unwrap_or(1); - eprintln!( - "{} Exited with status {}!", - "error".if_supports_color(Stream::Stderr, |text| text.red()), - util::itoa(code).if_supports_color(Stream::Stderr, |text| text.bold()), - ); + if !silent { + eprintln!( + "{} Exited with status {}!", + "error".if_supports_color(Stream::Stderr, |text| text.red()), + util::itoa(code).if_supports_color(Stream::Stderr, |text| text.bold()), + ); + } std::process::exit(code); }