From b7a3aabf49164d67e05f754a3f53808cd1a3ed6e Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Mon, 28 Dec 2020 21:11:58 +0000 Subject: [PATCH] feat(zsh): add dynamic path support --- .../core/files/oh-my-zsh/custom/paths.zsh | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 site-modules/core/files/oh-my-zsh/custom/paths.zsh diff --git a/site-modules/core/files/oh-my-zsh/custom/paths.zsh b/site-modules/core/files/oh-my-zsh/custom/paths.zsh new file mode 100644 index 0000000..073a463 --- /dev/null +++ b/site-modules/core/files/oh-my-zsh/custom/paths.zsh @@ -0,0 +1,51 @@ +#!/bin/zsh +# +# Update global path variable when moving around the file system to include +# local project bin directories. When opening the shell in a project with a +# compatible bin directory i.e. `vendor/bin` for PHP composer packages, the +# directory will be added to the `PATH` variable so that the locally installed +# packages can be executed +# +# Compatible directories +# - vendor/bin +# - node_modules/.bin +# +# Author: Ade Attwood +# Updated: 2020-12-28 +# + +# +# Update paths function to be called when `cd` is called +# +update_path() { + if [[ -d ./vendor/bin ]]; then + export PATH=$PATH:./vendor/bin + else + path=( ${path[@]:#*./vendor/bin*} ) + fi + + if [[ -d ./node_modules/.bin ]]; then + export PATH=$PATH:./node_modules/.bin + else + path=( ${path[@]:#*./node_modules/.bin*} ) + fi +} + +# +# Call the update path function whenever the current working directory is +# changed +# +autoload -U add-zsh-hook +add-zsh-hook chpwd update_path + +# +# Call update path when zsh is loaded. If the shell is initialised in a +# directory when local bin directory the directory will not be added to the path +# because the `chpwd` hook has not been called +# +update_path + +# +# Remove duplicate entries from the path variable +# +typeset -U path