|
| 1 | +#!/usr/bin/bash |
| 2 | + |
| 3 | +# Copyright 2023 Adobe. All rights reserved. |
| 4 | +# This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. You may obtain a copy |
| 6 | +# of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 9 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 10 | +# OF ANY KIND, either express or implied. See the License for the specific language |
| 11 | +# governing permissions and limitations under the License. |
| 12 | + |
| 13 | +copy_env_file() { |
| 14 | + # Check if an input was provided |
| 15 | + [[ -z "$1" ]] && return |
| 16 | + |
| 17 | + example=$1/.env.example |
| 18 | + |
| 19 | + # Validate that the file exists |
| 20 | + [[ ! -f "$example" ]] && return |
| 21 | + |
| 22 | + # Create the local .env file if it doesn't exist |
| 23 | + [[ ! -f "$1/.env" ]] && touch $1/.env |
| 24 | + |
| 25 | + # Read in the local env example, line-by-line and look for variable names |
| 26 | + while IFS= read -r example_line; do |
| 27 | + if [[ "$example_line" = "#"* || "$example_line" = "" ]]; then |
| 28 | + echo "${example_line}" >> .env |
| 29 | + continue |
| 30 | + fi |
| 31 | + |
| 32 | + if [[ "$example_line" != *"="* ]]; then |
| 33 | + continue |
| 34 | + fi |
| 35 | + |
| 36 | + # Split the line into an array with 0=key, 1=value |
| 37 | + while IFS='=' read -ra example_var; do |
| 38 | + |
| 39 | + if [[ "${example_var[0]}" = "" || "${example_var[1]}" = "" ]]; then |
| 40 | + continue |
| 41 | + fi |
| 42 | + |
| 43 | + # If the variable does not exist in the home .env file, write it to the local .env file as-is |
| 44 | + if [[ -z $(grep "${example_var[0]}=" $HOME/.env) ]]; then |
| 45 | + echo "${example_var[0]}=${example_var[1]}" >> .env |
| 46 | + continue |
| 47 | + fi |
| 48 | + |
| 49 | + if ! test -f $HOME/.env; then |
| 50 | + continue |
| 51 | + fi |
| 52 | + |
| 53 | + # Look for that variable name in the root .env file |
| 54 | + while IFS= read -r root_line; do |
| 55 | + while IFS='=' read -ra root_var; do |
| 56 | + if [[ "${root_var[0]}" = "${example_var[0]}" ]]; then |
| 57 | + # Write the variable to the local .env file |
| 58 | + echo "${example_var[0]}=${root_var[1]}" >> .env |
| 59 | + continue |
| 60 | + fi |
| 61 | + done <<< "$root_line" |
| 62 | + done < <(grep -v '^#' $HOME/.env) |
| 63 | + done <<< "$example_line" |
| 64 | + done < $example |
| 65 | +} |
| 66 | + |
| 67 | +# Check the directory for an .env.example file (confirm first that we're not inside a node_modules folder) |
| 68 | +for example in $(find . -path \*.env.example -not -path \*/node_modules/\* -exec dirname {} \;); do |
| 69 | + # If the directory does not already have an .env file, copy relevant values from the user's root .env |
| 70 | + if [[ -f "$example/.env" ]]; then |
| 71 | + exit 0 |
| 72 | + else |
| 73 | + echo "🪄 Using .env.example to generate a local .env" |
| 74 | + copy_env_file $example |
| 75 | + fi |
| 76 | +done |
0 commit comments