Skip to content

Commit

Permalink
feat: add silent flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Mar 23, 2024
1 parent 6d187e6 commit b6d0d14
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
32 changes: 20 additions & 12 deletions src/cli/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,26 @@ pub fn handle(package_paths: impl Iterator<Item = PathBuf>, args: &ExecArgs) ->
if let Ok(raw) = fs::read_to_string(&package_path) {
if let Ok(package) = serde_json::from_str::<PackageJson>(&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;
Expand Down
17 changes: 15 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ struct RootArgs {
extra_args: Vec<String>,

/// 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)]
Expand All @@ -66,8 +70,12 @@ struct RunArgs {
extra_args: Vec<String>,

/// 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)]
Expand All @@ -78,6 +86,10 @@ struct ExecArgs {
/// Extra arguments to pass to the command
#[clap(allow_hyphen_values = true)]
extra_args: Vec<String>,

/// Disable printing package and command information
#[clap(short, long, env = "NRR_SILENT")]
silent: bool,
}

pub fn get_level() -> &'static usize {
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 20 additions & 13 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,59 @@ 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 {
let post_script_name = String::from("post") + script_name;

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,
)?;
}
}
Expand All @@ -85,6 +91,7 @@ pub fn handle(package_paths: impl Iterator<Item = PathBuf>, args: &RunArgs) -> R
script_cmd,
&args.extra_args,
args.no_pre_post,
args.silent,
)?;

executed_script = true;
Expand Down
39 changes: 21 additions & 18 deletions src/run/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
Expand Down Expand Up @@ -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);
}
Expand Down
33 changes: 19 additions & 14 deletions src/run/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit b6d0d14

Please sign in to comment.