-
Notifications
You must be signed in to change notification settings - Fork 5
add mage destroy <NAME> to fully remove a project
#44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Fully remove a Magento project: folder, database, and all Valet links/certs. | ||
| # Run from the PARENT directory of the project, e.g.: | ||
| # cd ~/Developer/magento && mage destroy my-project | ||
| function mage_destroy_project() { | ||
| local name="$1" | ||
|
|
||
| if [[ -z "$name" ]]; then | ||
| echo "Usage: mage destroy <PROJECT-NAME>" | ||
| return 1 | ||
| fi | ||
|
|
||
| if [[ ! -d "$name" ]]; then | ||
| echo "Error: Directory '${name}' not found in the current folder ($(pwd))." | ||
| echo "Run this command from the parent directory of the project." | ||
| return 1 | ||
| fi | ||
|
|
||
| # Gather Valet domains by reading .valet-env.php — only top-level array keys | ||
| # (the ones with array values) are domain names; string-value keys are skipped. | ||
| local valet_env="${name}/.valet-env.php" | ||
| local domains=() | ||
| if [[ -f "$valet_env" ]]; then | ||
| while IFS= read -r _d; do | ||
| [[ -n "$_d" ]] && domains+=("$_d") | ||
| done < <(python3 -c " | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convert to php |
||
| import re, sys | ||
| content = open(sys.argv[1]).read() | ||
| for k in re.findall(r\"'([^']+)'\\s*=>\\s*\\[\", content): | ||
| print(k) | ||
| " "$valet_env") | ||
| fi | ||
|
|
||
| echo "" | ||
| echo -e "${RED}WARNING: This will permanently destroy:${RESET}" | ||
| echo " • Project folder : $(pwd)/${name}" | ||
| echo " • Database : ${name}" | ||
| if [[ ${#domains[@]} -gt 0 ]]; then | ||
| echo " • Valet domains : ${domains[*]}" | ||
| fi | ||
| echo "" | ||
| read -p "Type the project name to confirm: " _confirm && echo "" | ||
|
|
||
| if [[ "$_confirm" != "$name" ]]; then | ||
| echo -e "Aborted — '${_confirm}' does not match '${name}'." | ||
| return 1 | ||
| fi | ||
|
|
||
| # Valet cleanup — delete cert files, nginx configs, and site symlinks directly | ||
| # so we can do a single valet restart instead of one nginx restart per domain. | ||
| if [[ $VALET == 1 && ${#domains[@]} -gt 0 ]]; then | ||
| local _valet_cfg="$HOME/.config/valet" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize |
||
| echo "Removing Valet certificates, nginx configs and site links..." | ||
| for d in "${domains[@]}"; do | ||
| rm -f "${_valet_cfg}/Certificates/${d}.test.crt" \ | ||
| "${_valet_cfg}/Certificates/${d}.test.csr" \ | ||
| "${_valet_cfg}/Certificates/${d}.test.key" \ | ||
| "${_valet_cfg}/Certificates/${d}.test.conf" | ||
| rm -f "${_valet_cfg}/Nginx/${d}.test" | ||
| rm -f "${_valet_cfg}/Sites/${d}" | ||
| sudo security delete-certificate -c "${d}.test" \ | ||
| /Library/Keychains/System.keychain 2>/dev/null || true | ||
| done | ||
| echo "Restarting nginx once..." | ||
| valet restart | ||
| fi | ||
|
|
||
| # Drop database | ||
| echo "Dropping database '${name}'..." | ||
| if command -v mysql &>/dev/null; then | ||
| mysql -uroot -proot -e "DROP DATABASE IF EXISTS \`${name}\`;" 2>/dev/null \ | ||
| || echo " Warning: could not drop database (check credentials)." | ||
| else | ||
| echo " Warning: mysql not found — skipping database removal." | ||
| fi | ||
|
|
||
| # Remove project folder | ||
| echo "Removing project folder '${name}'..." | ||
| rm -rf "${name}" | ||
|
|
||
| echo "" | ||
| echo -e "${GREEN}✓ Project '${name}' has been fully removed.${RESET}" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,14 @@ case "${@}" in | |
| echo "No name was given for the magento project, aborting.." | ||
| ;; | ||
|
|
||
| "destroy") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add alias nuke |
||
| echo "No project name given. Usage: mage destroy <PROJECT-NAME>" | ||
| ;; | ||
|
|
||
| "destroy "*) | ||
| mage_destroy_project "$2" | ||
| ;; | ||
|
|
||
| "create "*) | ||
| mage_install $2 | ||
| mage_setup | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rework to work directly from the project folder