Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 82 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ enum Commands {

/// pnpm commands with ultra-compact output
Pnpm {
/// pnpm filter arguments (can be repeated: --filter @app1 --filter @app2)
#[arg(long)]
filter: Vec<String>,

#[command(subcommand)]
command: PnpmCommands,
},
Expand Down Expand Up @@ -852,6 +856,24 @@ enum GoCommands {
Other(Vec<OsString>),
}

/// Merge pnpm global filters args with other ones
fn merge_pnpm_args(filters: &[String], args: &[String]) -> Vec<String> {
filters
.iter()
.map(|filter| format!("--filter={}", filter))
.chain(args.iter().map(|arg| arg.to_string()))
.collect()
}

/// Merge pnpm global filters args with other ones
fn merge_pnpm_args_os(filters: &[String], args: &[OsString]) -> Vec<OsString> {
filters
.iter()
.map(|filter| OsString::from(format!("--filter={}", filter)))
.chain(args.iter().map(|arg| arg.to_os_string()))
.collect()
}

fn main() -> Result<()> {
let cli = Cli::parse();

Expand Down Expand Up @@ -941,28 +963,38 @@ fn main() -> Result<()> {
gh_cmd::run(&subcommand, &args, cli.verbose, cli.ultra_compact)?;
}

Commands::Pnpm { command } => match command {
Commands::Pnpm { filter, command } => match command {
PnpmCommands::List { depth, args } => {
pnpm_cmd::run(pnpm_cmd::PnpmCommand::List { depth }, &args, cli.verbose)?;
pnpm_cmd::run(
pnpm_cmd::PnpmCommand::List { depth },
&merge_pnpm_args(&filter, &args),
cli.verbose,
)?;
}
PnpmCommands::Outdated { args } => {
pnpm_cmd::run(pnpm_cmd::PnpmCommand::Outdated, &args, cli.verbose)?;
pnpm_cmd::run(
pnpm_cmd::PnpmCommand::Outdated,
&merge_pnpm_args(&filter, &args),
cli.verbose,
)?;
}
PnpmCommands::Install { packages, args } => {
pnpm_cmd::run(
pnpm_cmd::PnpmCommand::Install { packages },
&args,
&merge_pnpm_args(&filter, &args),
cli.verbose,
)?;
}
PnpmCommands::Build { args } => {
// FIXME: if filters are present, we should find out wich workspaces are build before running build
next_cmd::run(&args, cli.verbose)?;
}
PnpmCommands::Typecheck { args } => {
// FIXME: if filters are present, we should find out wich workspaces are typechecked before running tsc
tsc_cmd::run(&args, cli.verbose)?;
}
PnpmCommands::Other(args) => {
pnpm_cmd::run_passthrough(&args, cli.verbose)?;
pnpm_cmd::run_passthrough(&merge_pnpm_args_os(&filter, &args), cli.verbose)?;
}
},

Expand Down Expand Up @@ -1546,4 +1578,49 @@ mod tests {
_ => panic!("Expected Git Commit command"),
}
}

#[test]
fn test_merge_filters_with_args() {
let filters = vec!["@app1".to_string(), "@app2".to_string()];
let args = vec![
"--filter=@app3".to_string(),
"--depth=0".to_string(),
"--no-verbose".to_string(),
];
let expected_args = vec![
"--filter=@app1",
"--filter=@app2",
"--filter=@app3",
"--depth=0",
"--no-verbose",
];
assert_eq!(merge_pnpm_args(&filters, &args), expected_args);
}

#[test]
fn test_merge_filters_with_args_os() {
let filters = vec!["@app1".to_string()];
let args = vec![OsString::from("--depth=0")];
let expected_args = vec![
OsString::from("--filter=@app1"),
OsString::from("--depth=0"),
];
assert_eq!(merge_pnpm_args_os(&filters, &args), expected_args);
}

#[test]
fn test_pnpm_subcommand_with_filter() {
let cli = Cli::try_parse_from(["rtk", "pnpm", "--filter", "@app1", "list"]).unwrap();
match cli.command {
Commands::Pnpm {
filter,
command: PnpmCommands::List { depth, args },
} => {
assert_eq!(depth, 0);
assert_eq!(filter, vec!["@app1"]);
assert!(args.is_empty());
}
_ => panic!("Expected Pnpm List command"),
}
}
}