Skip to content

Commit 00254ec

Browse files
authored
chore: support .env template at root (#2255)
A utility that copies the .env settings from a user's root to the project to ease the following workflow: git clean -dfx yarn install Checking out a clean repo is a critical part of the workflow but in doing so, it removes any uncommitted utilities such as the .env file with the Chromatic project token. Any time a yarn install is run, this utility script tries to copy variables listed in the example file from root.
1 parent c3244d2 commit 00254ec

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

.env.example

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CHROMATIC_PROJECT_TOKEN=1234abcd
2+
3+
# NX settings
4+
NX_PREFER_TS_NODE=true
5+
NX_INTERACTIVE=true
6+
7+
# Default dev environment settings
8+
NODE_ENV=development
9+
STORYBOOK_URL="/preview/"
10+
WATCH_MODE=true
11+
12+
# Optional NX settings
13+
NX_VERBOSE_LOGGING=false
14+
NX_SKIP_NX_CACHE=false
15+
NX_PERF_LOGGING=false

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
"mv:preview": "rimraf dist/preview && mv tools/preview/storybook-static dist/preview",
4646
"new": "nx run @spectrum-css/generator:new",
4747
"precommit": "lint-staged",
48-
"prepare": "husky install && yarn migration:clean",
48+
"prepare": "husky install && run-p refresh:env migration:clean",
4949
"prepare:site": "run-s clean:docs build:all",
50+
"refresh:env": "test -n \"${BASH_VERSION}\" && bash ./tasks/copy-env-from-root.sh || exit 0",
5051
"prerelease": "yarn version:build",
5152
"release": "lerna publish",
5253
"release:beta-from-package": "yarn release from-package --conventional-prerelease --preid beta --pre-dist-tag beta --no-private",

tasks/copy-env-from-root.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)