Intro: This bash script provides functionality to check and sync environment files in your project.
File:
check_env_files.sh
- Checks
.env
files against a template (.env.template
) - Identifies missing variables in
.env
files - Offers to sync
.env
files with the template - Skips syncing content for specified files
- Place the script (
check_env_files.sh
) in your project root directory. - Ensure you have a
.env.template
file with all required environment variables. - Make the script executable:
chmod +x check_env_files.sh
- Run the script:
./check_env_files.sh
- (optional) Add to
package.json
to check every start yarn{ ... scripts: { "prestart": "sh check_env_files.sh", } }
You can change sh
to bash
if you want to use bash instead of sh, especially on Ubuntu.
- (optional) Add parameter to specify the package manager
{ ... scripts: { "prestart": "sh check_env_files.sh <package-manager>", } }
Supported package managers: npm
, yarn
, pnpm
-
Checking Environment Files:
- The script checks all
.env*
files in the current directory against.env.template
. - It displays missing variables in each file.
- A summary of errors (if any) is shown for each file.
- The script checks all
-
Syncing Environment Files:
- If errors are found, the script offers to sync
.env
files with.env.template
. - If you choose to sync (by entering 'y'), the script will:
- Comment out variables not present in the template
- Add new variables from the template
- keep existing variables: preserving the current value and adding a comment with the template value
- If errors are found, the script offers to sync
- The script uses
.env.template
as the reference. Ensure this file contains all required variables for your project.
After syncing, the script cleans the yarn cache. You may want to adjust or remove this step based on your project needs.