Local Community

WP-CLI global installation needs some improvement

Let me introduce myself, I am a big fan of VS Code integrated terminal and I use Windows 10 with Git Bash as a terminal shell. Thus during using the command line, I wouldn’t honestly use the “Open site shell” of Local. Therefore, the Global installation of PHP and WP-CLI is appreciated.

I will add ~/AppData/Local/Programs/Local/resources/extraResources/bin/wp-cli/bin/ in the environment variables. After that when accessing the wp --version common I receive that PHAR not found related error, debugged a lot and found the wrapper was not recognized. I followed this https://github.com/wp-cli/wp-cli/blob/master/bin/wp and will suggest instead of

../wp-cli.phar` please change it to
 `#!/usr/bin/env sh
"$(dirname -- "$0")/../wp-cli.phar" "[email protected]"

Next thing the connection when I hit command wp plugin list it throws error like:

Error: `No connection could be made because the target machine actively refused it.
`
Error establishing a database connection
This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could
mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?

If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums. This either means
that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at `localhost`. This could mean your host’s database server is down.

After editing constant DB_HOST with localhost:<port-number> this problem vanished.

Finally, I will like to suggest integrating an updated version of wp-cli with update command enabled, allow PHP, MYSQL and WP-CLI in the environment variable so everyone can have access to that resources because Local is for the developer. During the installation you can ask “Do you want to add resources to PATH?” :slight_smile:

1 Like

This snippet works great for me:

#!/usr/bin/env sh

dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../" && pwd)

if [ -d /proc/cygdrive ]; then
    case $(which php) in
        $(readlink -n /proc/cygdrive)/*)
            # We are in Cygwin using Windows php, so the path must be translated
            dir=$(cygpath -m "$dir");
            ;;
    esac
fi

"${dir}/wp-cli.phar" "[email protected]"
1 Like