Skip to content

Commit d396ba9

Browse files
committed
refactor(command): extract resolve_program to dedupe ps1-rewrite plumbing
Both `run_command` and `run_command_with_fspy` were doing the same resolve_bin + rewrite_cmd_to_powershell match. Pull that into a private helper so each call site is one line.
1 parent 2da60ef commit d396ba9

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

crates/vite_command/src/lib.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ pub fn resolve_bin(
4545
AbsolutePathBuf::new(path).ok_or_else(|| Error::CannotFindBinaryPath(bin_name.into()))
4646
}
4747

48+
/// Resolve `bin_name` to a path and apply the Windows `.cmd` → PowerShell
49+
/// rewrite. Returns the program to spawn and the arg prefix to prepend
50+
/// before the user args (empty when no rewrite applies).
51+
fn resolve_program(
52+
bin_name: &str,
53+
envs: &HashMap<String, String>,
54+
cwd: &AbsolutePath,
55+
) -> Result<(AbsolutePathBuf, Vec<std::ffi::OsString>), Error> {
56+
let bin_path = resolve_bin(bin_name, envs.get("PATH").map(|p| OsStr::new(p.as_str())), cwd)?;
57+
Ok(match ps1_shim::rewrite_cmd_to_powershell(&bin_path) {
58+
Some(rewritten) => rewritten,
59+
None => (bin_path, Vec::new()),
60+
})
61+
}
62+
4863
/// Build a `tokio::process::Command` for a pre-resolved binary path.
4964
/// Sets inherited stdio and `fix_stdio_streams` (Unix pre_exec).
5065
/// Callers can further customize (add args, envs, override stdio, etc.).
@@ -142,12 +157,7 @@ where
142157
S: AsRef<OsStr>,
143158
{
144159
let cwd = cwd.as_ref();
145-
let paths = envs.get("PATH");
146-
let bin_path = resolve_bin(bin_name, paths.map(|p| OsStr::new(p.as_str())), cwd)?;
147-
let (program, prefix_args) = match ps1_shim::rewrite_cmd_to_powershell(&bin_path) {
148-
Some(rewritten) => rewritten,
149-
None => (bin_path, Vec::new()),
150-
};
160+
let (program, prefix_args) = resolve_program(bin_name, envs, cwd)?;
151161
let mut cmd = build_command(&program, cwd);
152162
cmd.args(&prefix_args).args(args).envs(envs);
153163
let status = cmd.status().await?;
@@ -177,11 +187,7 @@ where
177187
S: AsRef<OsStr>,
178188
{
179189
let cwd = cwd.as_ref();
180-
let bin_path = resolve_bin(bin_name, envs.get("PATH").map(|p| OsStr::new(p.as_str())), cwd)?;
181-
let (program, prefix_args) = match ps1_shim::rewrite_cmd_to_powershell(&bin_path) {
182-
Some(rewritten) => rewritten,
183-
None => (bin_path, Vec::new()),
184-
};
190+
let (program, prefix_args) = resolve_program(bin_name, envs, cwd)?;
185191

186192
let mut cmd = fspy::Command::new(program.as_path());
187193
cmd.args(&prefix_args)

0 commit comments

Comments
 (0)