Last active
March 13, 2025 19:26
-
-
Save 4DRIAN0RTIZ/534b3552c759e9de604d2ed321916349 to your computer and use it in GitHub Desktop.
dfx-reset - Cross-platform ICP dapp deployment helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
checkOs() { | |
if [ "$(uname)" == "Darwin" ]; then | |
echo "macOS" | |
elif [ "$(uname -s)" == "Linux" ]; then | |
if grep -qi microsoft /proc/version; then | |
echo "WSL" | |
else | |
echo "Linux" | |
fi | |
elif [ "$(uname -o)" == "Msys" ]; then | |
echo "Windows" | |
else | |
echo "Unsupported OS" | |
return 1 | |
fi | |
} | |
checkPath() { | |
if [ ! -f dfx.json ]; then | |
echo "Error: No dfx.json file found in current directory" >&2 | |
dirs=$(find . -maxdepth 3 -not -path "*/\.*" -not -path "*/node_modules/*" -type f -name "dfx.json" -exec dirname {} \; | sort) | |
# | |
if [ ! -z "$dirs" ]; then | |
echo -e "\nFound directories with dfx.json files:" | |
echo "$dirs" | |
fi | |
return 1 | |
elif [ ! -r dfx.json ]; then | |
echo "Error: dfx.json file exists but is not readable" >&2 | |
return 1 | |
elif [ ! -s dfx.json ]; then | |
echo "Error: dfx.json file is empty" >&2 | |
return 1 | |
fi | |
return 0 | |
} | |
dfx_reset() { | |
os=$(checkOs) || return 1 | |
checkPath || return 1 | |
case "$os" in | |
"WSL") | |
browser="wslview" | |
;; | |
"Linux") | |
browser="${1:-xdg-open}" | |
;; | |
"Darwin") | |
browser="${1:-open}" | |
;; | |
*) | |
echo "Unsupported OS" | |
return 1 | |
;; | |
esac | |
dfx stop | |
dfx start --background --clean | |
temp_file=$(mktemp) | |
dfx deploy 2> "$temp_file" | |
link=$(cat "$temp_file" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tail -n 1 | awk '{print $2}') | |
echo "$link" | xsel --clipboard | |
echo "Opening with $browser" | |
echo "Deployed at: $link" | |
"$browser" "$link" | |
rm "$temp_file" | |
} | |
dfx_reset "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DFX-RESET
This shell script born as an alternative to keep typing "dfx stop, dfx start and dfx deploy".
How its work?
Install
1. Download Gist
2. Give execution permission
3. Create a simbolic link in
/usr/local/bin
(recommended) or/usr/bin
sudo ln -s "$(pwd)/dfx-reset.sh" /usr/local/bin/dfx-reset
NOTE
Make sure to
.zshrc
or.bashrc
are exporting the directory what you are using into the PATHNow you can executes from anywhere typing