Skip to content

Commit

Permalink
[F] Add scaffold for craft / next proj
Browse files Browse the repository at this point in the history
  • Loading branch information
lthurston committed Jun 24, 2021
1 parent 8c435d7 commit 9de940b
Show file tree
Hide file tree
Showing 27 changed files with 602 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/brew-dockerize-craft-next
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

#GIT_IGNORE_LINE='docker-compose/mysql/data'
#GIT_IGNORE_FILE='.gitignore'
#if [ -f "$GIT_IGNORE_FILE" ]; then
# grep -qF -- "$GIT_IGNORE_LINE" "$GIT_IGNORE_FILE" || echo "$GIT_IGNORE_LINE" >> "$GIT_IGNORE_FILE"
#fi

brew scaffold docker_october


echo ""
echo "==> See the updated README for next steps!"
echo ""
6 changes: 6 additions & 0 deletions cmd/brew-scaffold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
end
end

if type == "docker_craft_next"
unless File.exists?("api/composer.json") && File.exists?("client/package.json")
abort "The current directory does not appear to be the root of a Craft/Next project"
end
end


source_dir = File.join(template_dir, type)
source_root_path = Pathname.new(source_dir)
Expand Down
55 changes: 55 additions & 0 deletions scaffold/docker_craft_next/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Development Environment

This project uses Docker and Docker Compose to setup a development environment. You'll need to install a current version of Docker, which you can get here: https://docs.docker.com/docker-for-mac/install/

To setup this project for the first time, begin by eyeballing the `Dockerfile`s in the repo. There is one in the `api/` directory for Craft, and another in the `client/` directory for Next.js. Does the Craft `Dockerfile` contain the correct PHP version on the base image? Are you using the correct version of Composer? Is the correct Node version referenced in the Next Dockerfile?

If you've just used `brew scaffold` to put required files in place, note that you will need to merge updated files and commit those changes
to version control. You can also delete the `config/dev` directory, and `Brewfile.*`.

Then, follow these steps:

```
# Start up the containers. This will take longer the first time because
# Docker will need to build the containers. Every time you run this command
# the postgres database will be dropped and recreated if it already exists.
script/setup
# Once the containers are built, fire them up! The first time, a new
# user with credentials `username` / `password` will be created, and Craft
# will be installed.
script/up
# Note: script/server does the same thing as script/up!
```

The Craft backend will be accessible here: http://localhost:8000/admin
The Next.js frontend will be accessible here: http://localhost:3000

#### Other useful commands:

```
# Shorthand for `docker compose down`. Shuts down and removes all containers.
script/down
# Run an Craft CLI command
script/craft/cli [[COMMAND]]
# Here's one you'll likely need to write project config from database structure
docker compose exec craft php craft project-config/write
# Add a node module
docker compose exec nextjs yarn add [[MODULE]]
# Update a node module
docker compose exec nextjs yarn upgrade [[MODULE]]
# Force rebuild of all container images
docker compose up --build --force-recreate --no-deps
# Sync a site from staging
bundle exec cap staging content:sync
```

If you need to SSH into any of the running containers, use the Docker dashboard,
locate the container, and click the terminal icon.
46 changes: 46 additions & 0 deletions scaffold/docker_craft_next/api/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# The environment Craft is currently running in ("dev", "staging", "production", etc.)
ENVIRONMENT="dev"

# The application ID used to to uniquely store session and cache data, mutex locks, and more
APP_ID=""

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY=

# The database driver that will be used ("mysql" or "pgsql")
DB_DRIVER="pgsql"

# The database server name or IP address
DB_SERVER="pg"

# The port to connect to the database with
DB_PORT="5432"

# The name of the database to select
DB_DATABASE="pg"

# The database username to connect with
DB_USER="pg"

# The database password to connect with
DB_PASSWORD="pg"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA="public"

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""

# Use these env var to reduce repetition, see HEROES example below
BASE_PATH=/var/www/html/web
PRIMARY_SITE_URL="http://localhost:8000"
DEFAULT_SITE_URL="http://localhost:8000"

# HEROES_BASE_URL=${PRIMARY_SITE_URL}/assets/heroes
# HEROES_PATH=${BASE_PATH}/assets/heroes

# Used by the scripts/run script when site is first created
CRAFTCMS_SITENAME="EN"

# For generating previews
ALIAS_PREVIEW_URL_FORMAT=${DEFAULT_SITE_URL}/api/preview?entryUid={sourceUid}
46 changes: 46 additions & 0 deletions scaffold/docker_craft_next/api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# The environment Craft is currently running in ("dev", "staging", "production", etc.)
ENVIRONMENT="dev"

# The application ID used to to uniquely store session and cache data, mutex locks, and more
APP_ID=""

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY=

# The database driver that will be used ("mysql" or "pgsql")
DB_DRIVER="pgsql"

# The database server name or IP address
DB_SERVER="pg"

# The port to connect to the database with
DB_PORT="5432"

# The name of the database to select
DB_DATABASE="pg"

# The database username to connect with
DB_USER="pg"

# The database password to connect with
DB_PASSWORD="pg"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA="public"

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""

# Use these env var to reduce repetition, see HEROES example below
BASE_PATH=/var/www/html/web
PRIMARY_SITE_URL="http://localhost:8000"
DEFAULT_SITE_URL="http://localhost:8000"

# HEROES_BASE_URL=${PRIMARY_SITE_URL}/assets/heroes
# HEROES_PATH=${BASE_PATH}/assets/heroes

# Used by the scripts/run script when site is first created
CRAFTCMS_SITENAME="EN"

# For generating previews
ALIAS_PREVIEW_URL_FORMAT=${DEFAULT_SITE_URL}/api/preview?entryUid={sourceUid}
28 changes: 28 additions & 0 deletions scaffold/docker_craft_next/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM craftcms/php-fpm:7.4-dev

USER root

# These are used by our startup scripts
RUN apk add bash jq findutils php7-pecl-xdebug

# Came from https://github.com/urbantrout/craftcms. TODO: determine if this is necessary
RUN apk add gnu-libiconv --update-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Copy our scripts into the container and make them +X
COPY scripts/ /scripts/
RUN chown -R www-data:www-data /scripts && chmod -R +x /scripts

WORKDIR /var/www/html
RUN chown -R www-data:www-data .
USER www-data

# Install Craft CMS and save original dependencies in file
RUN composer create-project craftcms/craft . \
&& cp composer.json composer.base

ENTRYPOINT [ "/scripts/run.sh" ]

CMD [ "docker-php-entrypoint", "php-fpm"]
1 change: 1 addition & 0 deletions scaffold/docker_craft_next/api/scripts/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
These scripts came from https://github.com/urbantrout/craftcms.
4 changes: 4 additions & 0 deletions scaffold/docker_craft_next/api/scripts/composer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
install_composer_dependencies() {
cd /var/www/html
composer install
}
26 changes: 26 additions & 0 deletions scaffold/docker_craft_next/api/scripts/database.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
setup_database() {
declare zip_file
declare sql_file

# Save DB credentials
echo $DB_SERVER:$DB_PORT:$DB_DATABASE:$DB_USER:$DB_PASSWORD >~/.pgpass
chmod 600 ~/.pgpass

h2 "Setup Craft CMS (if necessary)"

while ! pg_isready -h $DB_SERVER; do
h2 "Waiting for PostreSQL server"
sleep 1
done

cd /var/www/html &&
./craft setup/security-key &&
./craft install \
--interactive=0 \
--email="${CRAFTCMS_EMAIL:-office@castironcoding.com}" \
--username="${CRAFTCMS_USERNAME:-admin}" \
--password="${CRAFTCMS_PASSWORD:-password}" \
--siteUrl="${CRAFTCMS_SITEURL:-@web}" \
--language="${CRAFTCMS_LANGUAGE:-en-US}"

}
3 changes: 3 additions & 0 deletions scaffold/docker_craft_next/api/scripts/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2() {
printf '\e[1;33m==>\e[37;1m %s\e[0m\n' "$*"
}
22 changes: 22 additions & 0 deletions scaffold/docker_craft_next/api/scripts/plugins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
activate_plugins() {
h2 'Activating Plugins.'

cd /var/www/html

dependencies=$(cat composer.json |
jq '.require' |
jq --compact-output 'keys' |
tr -d '[]"' | tr ',' '\n')

for package in ${dependencies}; do

vendor=$(awk -F '[\/:]+' '{print $1}' <<<$package)
packageName=$(awk -F '[\/:]+' '{print $2}' <<<$package)
isCraftPlugin=$(cat vendor/$vendor/$packageName/composer.json | jq '.type == "craft-plugin"')

if [ "$isCraftPlugin" = true ]; then
handle=$(cat vendor/$vendor/$packageName/composer.json | jq -r '.extra.handle')
./craft plugin/install $handle
fi
done
}
30 changes: 30 additions & 0 deletions scaffold/docker_craft_next/api/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

set -e

source /scripts/helpers.sh
source /scripts/database.sh
source /scripts/composer.sh
source /scripts/plugins.sh

# This should only run the first time a project is set up and dependencies aren't in place yet. In all other cases
# the developer will be managing dependencies.
[ ! -d "/var/www/html/vendor" ] && install_composer_dependencies

setup_database &
SETUP_PID=$!
DEPENDENDIES_PID=$!

wait $SETUP_PID
wait $DEPENDENCIES_PID
activate_plugins

wait

h2 "✅ Visit http://localhost:8000 to start using Craft CMS."

# Make sure PHP FPM has a log dir
[ ! -d "/var/www/html/log" ] && mkdir -p /var/www/html/log

# Start php-fpm
exec "$@"
13 changes: 13 additions & 0 deletions scaffold/docker_craft_next/client/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Prepend "NEXT_PUBLIC_" to Next.js env vars to expose them as process.env.NEXT_PUBLIC_ANALYTICS_ID, for example
#NEXT_PUBLIC_ANALYTICS_ID=

# API requests coming from server vs browser means different request domains.
# Make sure your code respects both.
NEXT_PUBLIC_PRIVATE_BASE_URL="http://nginx"
NEXT_PUBLIC_BASE_URL="http://localhost:8000"
NEXT_PUBLIC_API_URL_PREPEND="api/"

NEXT_PUBLIC_SHARETHIS_PROPERTY=

# This should not be present in Production, but it's OK for dev.
NODE_TLS_REJECT_UNAUTHORIZED=0
13 changes: 13 additions & 0 deletions scaffold/docker_craft_next/client/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Prepend "NEXT_PUBLIC_" to Next.js env vars to expose them as process.env.NEXT_PUBLIC_ANALYTICS_ID, for example
#NEXT_PUBLIC_ANALYTICS_ID=

# API requests coming from server vs browser means different request domains.
# Make sure your code respects both.
NEXT_PUBLIC_PRIVATE_BASE_URL="http://nginx"
NEXT_PUBLIC_BASE_URL="http://localhost:8000"
NEXT_PUBLIC_API_URL_PREPEND="api/"

NEXT_PUBLIC_SHARETHIS_PROPERTY=

# This should not be present in Production, but it's OK for dev.
NODE_TLS_REJECT_UNAUTHORIZED=0
20 changes: 20 additions & 0 deletions scaffold/docker_craft_next/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:15.5.1-alpine3.10

ENV PORT 3000
ENV PATH /usr/src/node_modules/.bin:$PATH

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Installing dependencies
COPY package*.json yarn.lock /usr/src/
RUN yarn

# Copying source files
COPY . /usr/src/app

EXPOSE 3000

# Running the app
CMD ["yarn", "dev"]
Empty file.
Loading

0 comments on commit 9de940b

Please sign in to comment.