Skip to content

Commit

Permalink
feat: track script level and log script name in nested scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Jan 23, 2024
1 parent a6213d8 commit b8d8c4a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ struct Cli {
pre_post: bool,
}

#[must_use]
pub fn get_level() -> usize {
std::env::var("NRR_LEVEL")
.ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1)
}

impl Cli {
#[allow(clippy::too_many_lines)]
async fn execute(&self) -> Result<()> {
Expand All @@ -60,7 +68,16 @@ impl Cli {
.map(|raw| serde_json::from_str::<PackageJson>(&raw))
{
if let Some(script_cmd) = package_data.scripts.get(self_script) {
eprint!("{}", make_package_prefix(&package_data));
eprint!(
"{}",
make_package_prefix(
&package_data,
match get_level() {
1 => None,
_ => Some(self_script),
}
)
);

if self.pre_post {
let pre_script_name = "pre".to_owned() + self_script;
Expand Down Expand Up @@ -150,7 +167,7 @@ impl Cli {
.await
.map(|raw| serde_json::from_str::<PackageJson>(&raw))
{
eprint!("{}", make_package_prefix(&package_data));
eprint!("{}", make_package_prefix(&package_data, None));

found_package = true;

Expand Down
9 changes: 8 additions & 1 deletion src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct PackageJson {
pub scripts: HashMap<String, String>,
}

#[must_use]
fn normalize_ser_path(path: &str) -> String {
path.replace(|ch: char| !ch.is_alphanumeric(), "_")
}
Expand Down Expand Up @@ -42,14 +43,15 @@ fn serialize_into(data: &Value, path: &str, to: &mut HashMap<String, String>) {
}
}

#[must_use]
pub fn serialize_package_json_env(data: &Value) -> HashMap<String, String> {
let mut ret: HashMap<String, String> = HashMap::new();
serialize_into(data, "npm_package", &mut ret);
ret
}

#[must_use]
pub fn make_package_prefix(package: &PackageJson) -> String {
pub fn make_package_prefix(package: &PackageJson, script_name: Option<&str>) -> String {
let mut prefix = String::new();

if let Some(name) = &package.name {
Expand All @@ -62,6 +64,11 @@ pub fn make_package_prefix(package: &PackageJson) -> String {
prefix.push_str(&version_str.magenta().dimmed().to_string());
}

if let Some(script_name) = script_name {
prefix.push(' ');
prefix.push_str(&script_name.cyan().to_string());
}

prefix.push('\n');
}

Expand Down
13 changes: 8 additions & 5 deletions src/run_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ pub async fn run_script(
}

let cmd_prefix = match script_type {
ScriptType::Normal => "$",
ScriptType::Pre => "pre$",
ScriptType::Post => "post$",
};
ScriptType::Normal => String::new(),
ScriptType::Pre => "pre".to_owned(),
ScriptType::Post => "post".to_owned(),
} + &"$".repeat(crate::get_level());

eprintln!("{} {}", cmd_prefix.cyan().dimmed(), full_cmd.dimmed());

Expand All @@ -97,10 +97,13 @@ pub async fn run_script(

subproc
.env("PATH", patched_path)
.env("NRR_COMPAT_MODE", "1")
.env("npm_execpath", &self_exe)
.env("npm_lifecycle_event", script_name);

subproc
.env("NRR_COMPAT_MODE", "1")
.env("NRR_LEVEL", format!("{}", crate::get_level() + 1));

if let Ok(node_path) = which(match compat_mode {
Some(CompatMode::Bun) => "bun",
_ => "node",
Expand Down

0 comments on commit b8d8c4a

Please sign in to comment.