Last active
August 27, 2024 02:42
-
-
Save jacmkno/3f07a834b0a8bc21532121ea3e1060ca to your computer and use it in GitHub Desktop.
Simple Docker React Bundler from package.json only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Put this file in your react project root. | |
IMAGE="node:lts-alpine" | |
OUTPUT_DIR="$(pwd)/../public/" | |
if [ "$1" == "help" ]; then | |
echo "Usage: $0 [command] [options]" | |
echo | |
echo "Commands:" | |
echo " none build once and shutdown the container." | |
echo " clear Stop and remove the Docker container." | |
echo " pkg Run npm commands inside the container (e.g. $0 pkg install)." | |
echo " keep Preserve the container after the build." | |
echo " watch Continuously watch for file changes and rebuild automatically." | |
echo " help Show this help message." | |
echo | |
echo "Options:" | |
echo " All additional arguments after 'pkg' will be passed directly to npm." | |
echo | |
echo "Example usage:" | |
echo " $0 pkg install Install npm packages." | |
echo " $0 watch Watch for changes and automatically rebuild." | |
echo " $0 keep Keep the container running after the build." | |
echo " $0 clear Stop and remove the container." | |
exit 0 | |
fi | |
# Set the container name | |
CONTAINER_NAME="node-build-instance" | |
if [ "$1" == "clear" ]; then | |
echo "Stopping and removing the container..." | |
docker stop $CONTAINER_NAME && docker rm $CONTAINER_NAME | |
echo "Container removed." | |
exit 0 | |
fi | |
if [ "$1" == "pkg" ]; then | |
shift | |
docker exec -it $CONTAINER_NAME /bin/sh -c " | |
npm $@ && cp package.json original | |
" | |
exit 0 | |
fi | |
# Define the output directory | |
# Function to run the build inside Docker | |
run_build() { | |
echo "Running build..." | |
docker exec -it $CONTAINER_NAME /bin/sh -c " | |
cp -r /original/. /app && | |
npm install && | |
npm run build | |
" | |
} | |
# Create and start the container if it doesn't exist | |
if [ "$(docker ps -aq -f name=$CONTAINER_NAME)" ]; then | |
echo "Container exists." | |
if [ "$1" != "clear" ]; then | |
docker start $CONTAINER_NAME | |
run_build | |
fi | |
else | |
echo "Creating a new container..." | |
docker run -d --name $CONTAINER_NAME \ | |
-v "$(pwd)":/original \ | |
-v "$OUTPUT_DIR":/app/dist \ | |
-w /app $IMAGE tail -f /dev/null | |
run_build | |
fi | |
# Handle additional arguments | |
if [ "$1" == "keep" ]; then | |
echo "Keeping the container running..." | |
elif [ "$1" == "watch" ]; then | |
echo "Watching for changes build..." | |
while inotifywait -r -e modify,create,delete,move .; do | |
sleep 1 | |
run_build | |
done | |
else | |
# Stop the container after build by default | |
echo "Stopping and removing the container... Use keep to preserve. clear to remove" | |
docker stop $CONTAINER_NAME && docker rm $CONTAINER_NAME | |
fi | |
echo "Build complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment