Skip to main content

Auto linking project toolchains

If you use direnv you can automatically setup your project toolchain requirements. In the case of WordPress projects this typically includes composer, PHP, node, npm.

Simply add the required php and composer version to the project's .envrc file:

#example project requirements
use php 7.4
use composer 2

# Set node version
nvmrc=~/.nvm/nvm.sh
if [ -e $nvmrc ]; then
  source $nvmrc
  nvm use
fi
PATH_add node_modules/.bin

To make this happen, you must add support for the switching by adding the following to ~/.direnvrc:

# Usage: use php <version>
#
# Loads the specified php version into the environent
#
use_php() {
  php --version | grep -q "PHP $1" || (brew unlink php@$1 &&  brew link php@$1 --force)
}

# Usage: use composer <version>
#
# Loads the specified composer version into the environent
#
use_composer() {
  composer --version | grep -q "version $1" || composer self-update --$1
}