before starting – I have seen some SO similar questions, but tbh I got even more lost.
I am trying to run nvm use
and nvm -v
within the PHP shell_execute
, but am constantly getting the following error:
sh: nvm: command not found
It seems like PHP is using Bourne shell where nvm is not available.
I am mostly using Bash in Terminal where everything is fine (in other shells like ZSH I get the same error).
Questions:
How can I make PHP run commands with shell_exec
(if there is alternative I am open for considering it) I usually use in Bash?
Extra notes (maybe helpful):
-
This command is only used on a local environment so changing settings on dev machine is something I am open to consider. (would be good to support Mac/Win and Linux, but Mac is the main focus at the moment).
-
If I run
which nvm
in the bash I get nothing in the terminal. -
NVM was installed with Brew (don’t know if that has any impact)
The usecase:
I am building a WP CLI toolkit where one of the commands is wp custom install
. The command should switch to the right Node.js version of the project, install npm and php dependencies as well as some extra things.
// basic code sample
namespace Custom/CustomCli;
class CustomCli {
public static function init(){
if( !(defined('WP_CLI') && WP_CLI) ) return;
WP_CLI::add_command( 'custom', 'CustomCustomCli' );
}
/*
* If the Node.js version !== to .nvmrc try 'nvm use'
* after correct version is selected run 'npm install'
* also run 'composer install' and 'composer dump-autoload'
*/
public function install( $args ){
WP_CLI::log( shell_exec('nvm -v') ); # once I figure out this I guess I'll know how to fully handle installation
}
}
CustomCli::init();