Skip to content

Commit e6aeab0

Browse files
committed
refactor(pm_shim): 优化二进制名称处理逻辑
增强了对路径输入的判断逻辑,当路径为绝对路径时,提取其文件名并去除后缀 '.cmd' 或 '.exe',从而确保在后续处理中获取正确的命令名称。此修改增加了代码可读性和鲁棒性。
1 parent 3d072ae commit e6aeab0

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

crates/snm_shim/src/pm_shim.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,20 @@ impl PmShim {
5656

5757
// 传进来的有可能是绝对路径, 如果是绝对路径的的话,取 file_name 判断一下。
5858
// 同时需要保证直取命令的名称,方便 后续的 json.get_bin_with_name(bin_name) 获取到对应 js 的真实路径
59-
let bin_name = Path::new(bin_name)
60-
.file_name()
61-
.and_then(|f| f.to_str())
62-
.map(|name| {
63-
name
64-
.strip_suffix(".cmd")
65-
.or_else(|| name.strip_suffix(".exe"))
66-
.unwrap_or(name)
67-
})
68-
.unwrap_or(bin_name);
59+
let bin_name = if Path::new(bin_name).is_absolute() {
60+
Path::new(bin_name)
61+
.file_name()
62+
.and_then(|f| f.to_str())
63+
.map(|name| {
64+
name
65+
.strip_suffix(".cmd")
66+
.or_else(|| name.strip_suffix(".exe"))
67+
.unwrap_or(name)
68+
})
69+
.unwrap_or(bin_name)
70+
} else {
71+
bin_name
72+
};
6973

7074
if bin_name != pm.name() && bin_name != "npx" {
7175
bail!(

0 commit comments

Comments
 (0)