Here's how to turn long Shopify CLI commands like
shopify theme dev --nodelete --ignore 'templates/*.json' 'config/settings_data.json' 'locales/en.default.json' 'sections/*.json' --store "$STORE" --theme "$THEME_ID"into a simple alias like serve that's scoped to your project.
- zsh*
- Technically this will work for bash, fish, or any other shell – but I've written this guide specifically for zsh because that's what I use.
- direnv
- Shopify CLI
To avoid constantly updating .gitignore files to accommodate personal configuration files or custom scripts, you can use git config core.excludesfile "path/to/file" to specify additional ignore files to use.
- Create a
.gitignore_localfile in the root of your project and paste add the following:
.gitignore_local
.envrc
.scripts/*
.zshrc
- (optional) Add this function into your
~/.zshrcfile and use it quickly create and configure.gitignore_localfiles.
# set `git config -local core.excludesfile` to the `.gitignore_local` file the in working directory
localGitIgnore() {
current_dir=$(pwd -P)
if [[ ! -e ".gitignore_local" ]]; then
touch .gitignore_local
else
if ! grep -q ".gitignore_local" "${current_dir}/.gitignore_local"; then
echo "\n.gitignore_local" >>"${current_dir}/.gitignore_local"
fi
fi
git config core.excludesfile "${current_dir}/.gitignore_local"
}Environment variables are normally added to a .env file, but need to be loaded into the shell if you want to use them with the Shopify CLI. This is where direnv comes in. It takes care of loading and unloading environment variables for you.
- Install direnv
- Create a
.envrcfile in the root of your project, then define theTHEME_IDandSTOREvariables - Run
direnv allowto load your environment variables.- Verify it's working by running
echo $THEME_ID. It should output the theme id you just configured.
- Verify it's working by running
- Create a
.scripts/directory in the root of your project - Create a new shell script in the
.scripts/folder for each command you want to run. The most common scripts you'll need are: - Verify the scripts work by running
zsh .scripts/theme.sh
You'll want to use these scripts across lots of different project, so they'll need to be scoped to each project directory.
- Create a
.zshrcfile in the root of your project and past the following
#!/usr/bin/bash
alias serve="zsh .scripts/serve.sh"
alias pull="zsh .scripts/pull.sh"
alias push="zsh .scripts/push.sh"
alias theme="zsh .scripts/theme.sh"- Add the following code snippet to your global zsh file
~/.zshrc. This will automatically source any.zshrcfile anytime you change directories:
# Source .zshrc files in the current directory
function _source_local_zshrc_files() {
if [[ $PWD != $HOME ]]; then
if [[ -r "$PWD/.zshrc" ]]; then
source "$PWD/.zshrc"
return 0
else
return 1
fi
fi
}
# Search for new .zshrc files to source when changing directories
function chpwd() {
_source_local_zshrc_files
}
# Source .zshrc files on init
_source_local_zshrc_files