Snippets Collections
#!/usr/bin/env bash
#
# Returns Magento document root if sourced or prints if launched as standalone
#

mageRoot="$(dirname $(realpath "${0}"))"

while [[ ! -e ${mageRoot}/app/Mage.php && ! -e ${mageRoot}/bin/magento ]]
do
    mageRoot="$(dirname $(realpath ${mageRoot}))"
done

return "${mageRoot}" 2>/dev/null || printf '%s' "${mageRoot}" && exit
import { realpathSync as realpath, accessSync as access, existsSync as exists } from 'fs'
import { dirname } from 'path'

/**
 * IIFE to resolve Magento document root
 * @returns <string> Returns Magento document root
 */
export const mageRoot = (() => {
    let mageRoot = dirname(realpath(import.meta.url.slice(7)))

    while (!(exists(`${ mageRoot }/app/Mage.php`) || exists(`${ mageRoot }/bin/magento`))) {
        mageRoot = dirname(mageRoot)
    }

    return mageRoot
})()
<?php

$mageRoot = __DIR__;

while (!(file_exists($mageRoot . '/app/Mage.php') || file_exists($mageRoot . '/bin/magento'))) {
    $mageRoot = dirname($mageRoot);
}

return $mageRoot;
star

Sat Jan 08 2022 06:07:58 GMT+0000 (Coordinated Universal Time) https://gist.github.com/juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee#file-magentodocumentroot-sh

#bash #magento #document_root #resolve #shell
star

Sat Jan 08 2022 06:00:50 GMT+0000 (Coordinated Universal Time) https://gist.github.com/juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee#file-magentodocumentroot-mjs

#javascript #nodejs #magento #document_root #resolve #module #mjs
star

Sat Jan 08 2022 05:57:18 GMT+0000 (Coordinated Universal Time) https://gist.github.com/juliyvchirkov/2c5c8d54b182528e39d4565d1632f8ee#file-magentodocumentroot-php

#php #magento #document_root #resolve #module #include

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension