-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm.sh
49 lines (41 loc) · 1.2 KB
/
pm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function pm() {
local temp_file="/tmp/pm_last_dir"
local config_file="/tmp/pm_config_file"
local target_file="package.json"
if [[ -f "$config_file" ]]; then
target_file=$(cat "$config_file")
fi
if [[ "$1" == "--config" && -n "$2" ]]; then
target_file="$2"
echo "$target_file" > "$config_file"
echo "Configuration updated: target file is now '$target_file'"
return
fi
if [[ "$1" == "-" ]]; then
if [[ -f "$temp_file" ]]; then
local last_dir=$(cat "$temp_file")
cd "$last_dir" || return 1
echo "Returned to last directory: $last_dir"
else
echo "No previous directory saved."
fi
return
fi
local current_dir=$(pwd)
while [[ ! -f "$target_file" ]]; do
cd ..
if [[ $(pwd) == "/" ]]; then
echo "No $target_file file found in the current directory or its parents."
cd "$current_dir"
return 1
fi
done
echo "$current_dir" > "$temp_file"
if [[ $# -eq 0 ]]; then
echo "Project's root: $(pwd)"
else
echo "Command executed in $(pwd)"
"$@"
cd "$current_dir"
fi
}