Skip to content

Instantly share code, notes, and snippets.

@juliyvchirkov
Last active August 16, 2022 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee to your computer and use it in GitHub Desktop.
Save juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee to your computer and use it in GitHub Desktop.
nodejs, php, bash: Magento 1 / 2 baseDir autodetection
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
})()
<?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;
#!/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