Skip to content

Commit

Permalink
Replace ~ with homedir
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 6, 2024
1 parent 31d3c73 commit 8e7f28e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions crates/deno_task_shell/src/shell/commands/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,11 @@ fn execute_cd(cwd: &Path, args: Vec<String>) -> Result<PathBuf> {
// create a new vector to avoid modifying the original
let mut args = args;
if args.is_empty() {
// append `~` to args
// append homedir to args
args.push("~".to_string());
}
let path = parse_args(args.clone())?;
let new_dir = if path == "~" {
dirs::home_dir()
.ok_or_else(|| anyhow::anyhow!("Home directory not found"))?
} else {
cwd.join(&path)
};
let new_dir = cwd.join(&path);
let new_dir = match new_dir.parse_dot() {
Ok(path) => path.to_path_buf(),
// fallback to canonicalize path just in case
Expand All @@ -68,7 +63,13 @@ fn parse_args(args: Vec<String>) -> Result<String> {
for arg in args {
match arg {
ArgKind::Arg(arg) => {
paths.push(arg);
if arg.contains('~') {
let home_dir = dirs::home_dir().unwrap();
let arg = arg.replacen("~", home_dir.to_string_lossy().as_ref(), 1);
paths.push(arg);
} else {
paths.push(arg.to_string());
}
}
_ => arg.bail_unsupported()?,
}
Expand Down

0 comments on commit 8e7f28e

Please sign in to comment.