Skip to content

Commit

Permalink
Require env vars in start/build scripts
Browse files Browse the repository at this point in the history
This makes sure that you can't start or build the front-end without
having the required env vars set. If they aren't set, things will break
in hard-to-understand ways.
  • Loading branch information
reidmit committed Dec 13, 2020
1 parent 09e5339 commit c80e1f3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ See the [deployment doc](docs/deployment.md) for detailed instructions.

You'll need Node, NPM, and Yarn locally. Clone this repo. Check the [`package.json`](package.json) `scripts` section for all scripts, but some useful ones are:

- `yarn start` to start the front-end locally
- `PAIRIST_FIREBASE_PROJECT_ID=... PAIRIST_FIREBASE_API_KEY=... yarn start` to start the front-end locally, using the specified Firebase project as the back-end

- `PAIRIST_FIREBASE_PROJECT_ID=... PAIRIST_FIREBASE_API_KEY=... yarn build` to compile the front-end for production, using the specified Firebase project as the back-end

- `yarn test` to run unit tests
16 changes: 16 additions & 0 deletions craco.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
if (!Boolean(process.env.REACT_APP_FIREBASE_PROJECT_ID)) {
throw 'REACT_APP_FIREBASE_PROJECT_ID is not set.';
}

if (!Boolean(process.env.REACT_APP_FIREBASE_API_KEY)) {
throw 'REACT_APP_FIREBASE_API_KEY is not set.';
}

if (!Boolean(process.env.REACT_APP_FIREBASE_AUTH_DOMAIN)) {
throw 'REACT_APP_FIREBASE_AUTH_DOMAIN is not set.';
}

if (!Boolean(process.env.REACT_APP_FIREBASE_URL)) {
throw 'REACT_APP_FIREBASE_URL is not set.';
}

module.exports = {
reactScriptsVersion: 'react-scripts',
webpack: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"typescript": "4.1.2"
},
"scripts": {
"start": "craco start",
"build": "craco build",
"start": "scripts/start",
"build": "scripts/build",
"test": "craco test",
"eject": "react-scripts eject",
"deploy": "scripts/deploy"
Expand Down
24 changes: 24 additions & 0 deletions scripts/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

if [ -z "$PAIRIST_FIREBASE_PROJECT_ID" ]; then
echo "PAIRIST_FIREBASE_PROJECT_ID is not set."
echo "This should be set to your Firebase project ID."
exit 1
fi

if [ -z "$PAIRIST_FIREBASE_API_KEY" ]; then
echo "PAIRIST_FIREBASE_API_KEY is not set."
echo "This should be set to your Firebase API key."
exit 1
fi

set -x

# Build project
REACT_APP_FIREBASE_PROJECT_ID=${PAIRIST_FIREBASE_PROJECT_ID} \
REACT_APP_FIREBASE_API_KEY=${PAIRIST_FIREBASE_API_KEY} \
REACT_APP_FIREBASE_AUTH_DOMAIN="${PAIRIST_FIREBASE_PROJECT_ID}.firebaseapp.com" \
REACT_APP_FIREBASE_URL="https://${PAIRIST_FIREBASE_PROJECT_ID}.firebaseio.com" \
yarn run craco build
24 changes: 24 additions & 0 deletions scripts/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

if [ -z "$PAIRIST_FIREBASE_PROJECT_ID" ]; then
echo "PAIRIST_FIREBASE_PROJECT_ID is not set."
echo "This should be set to your Firebase project ID."
exit 1
fi

if [ -z "$PAIRIST_FIREBASE_API_KEY" ]; then
echo "PAIRIST_FIREBASE_API_KEY is not set."
echo "This should be set to your Firebase API key."
exit 1
fi

set -x

# Start dev server
REACT_APP_FIREBASE_PROJECT_ID=${PAIRIST_FIREBASE_PROJECT_ID} \
REACT_APP_FIREBASE_API_KEY=${PAIRIST_FIREBASE_API_KEY} \
REACT_APP_FIREBASE_AUTH_DOMAIN="${PAIRIST_FIREBASE_PROJECT_ID}.firebaseapp.com" \
REACT_APP_FIREBASE_URL="https://${PAIRIST_FIREBASE_PROJECT_ID}.firebaseio.com" \
yarn run craco start

0 comments on commit c80e1f3

Please sign in to comment.