Last active
August 16, 2022 20:36
-
-
Save juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee to your computer and use it in GitHub Desktop.
nodejs, php, bash: Magento 1 / 2 baseDir autodetection
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
import { realpathSync, existsSync } from 'node:fs' | |
import { basename, dirname } from 'node:path' | |
export default (() => { | |
let mageRoot = dirname(realpathSync(import.meta.url.slice(7))) | |
while (!(existsSync(`${ mageRoot }/app/Mage.php`) || existsSync(`${ mageRoot }/bin/magento`))) { | |
if (mageRoot === '/') { | |
throw new Error(`${ basename(import.meta.url.slice(7)) } should be placed inside Magento project tree`) | |
} | |
mageRoot = dirname(mageRoot) | |
} | |
return mageRoot | |
})() |
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
<?php | |
$dirname = __DIR__; | |
while (!(file_exists($dirname . '/app/Mage.php') || file_exists($dirname . '/bin/magento'))) { | |
if ($mageRoot === '/') { | |
trigger_error(basename(__FILE__) . ' should be placed inside Magento project tree', E_USER_ERROR); | |
} | |
$dirname = dirname($dirname); | |
} | |
return $dirname; |
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
#!/usr/bin/env sh | |
mageRoot="$(dirname "$(realpath "${0}")")" | |
until [ -e "${mageRoot}/app/Mage.php" ] || [ -e "${mageRoot}/bin/magento" ] | |
do | |
if [ "${mageRoot}" = "/" ]; then | |
printf "%s should be placed inside Magento project tree\n" "$(basename "${0}")" >&2 | |
return 1 2>/dev/null || exit | |
fi | |
mageRoot="$(dirname "${mageRoot}")" | |
done | |
return 2>/dev/null || printf "%s" "${mageRoot}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment