Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#20] include project config, Mailpit setup #22

Merged
merged 9 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .ddev/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ database:
version: '8.0'
use_dns_when_possible: true
composer_version: '2'
web_environment: []
web_environment:
# Use Mailpit
- SYSTEM_EMAIL_HOSTNAME=localhost
- SYSTEM_EMAIL_PORT=1025
- SYSTEM_EMAIL_USERNAME=abc
- SYSTEM_EMAIL_PASSWORD=123
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard coding these values into the DDEV config makes it harder to accidentally override these with production credentials.

nodejs_version: '18'
hooks:
post-start:
Expand Down
8 changes: 8 additions & 0 deletions .env.example.dev
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ CRAFT_APP_ID=
# The environment Craft is currently running in (dev, staging, production, etc.)
CRAFT_ENVIRONMENT=dev

# Email
[email protected]
SYSTEM_EMAIL_SENDER="Craft CMS"
SYSTEM_EMAIL_HOSTNAME=
SYSTEM_EMAIL_PORT=
SYSTEM_EMAIL_USERNAME=
SYSTEM_EMAIL_PASSWORD=

# Database connection settings
CRAFT_DB_DRIVER=mysql
CRAFT_DB_SERVER=127.0.0.1
Expand Down
7 changes: 7 additions & 0 deletions .env.example.production
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ CRAFT_APP_ID=
# The environment Craft is currently running in (dev, staging, production, etc.)
CRAFT_ENVIRONMENT=production

# Email
[email protected]
SYSTEM_EMAIL_HOSTNAME=
SYSTEM_EMAIL_PORT=
SYSTEM_EMAIL_USERNAME=
SYSTEM_EMAIL_PASSWORD=

# Database connection settings
CRAFT_DB_DRIVER=mysql
CRAFT_DB_SERVER=127.0.0.1
Expand Down
7 changes: 7 additions & 0 deletions .env.example.staging
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ CRAFT_APP_ID=
# The environment Craft is currently running in (dev, staging, production, etc.)
CRAFT_ENVIRONMENT=staging

# Email
[email protected]
SYSTEM_EMAIL_HOSTNAME=
SYSTEM_EMAIL_PORT=
SYSTEM_EMAIL_USERNAME=
SYSTEM_EMAIL_PASSWORD=

# Database connection settings
CRAFT_DB_DRIVER=mysql
CRAFT_DB_SERVER=127.0.0.1
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ node_modules
/web/dist
/.vite
php-cs-fixer.cache

# BEGIN-STARTER-ONLY
config/license.key
# END-STARTER-ONLY
Comment on lines +10 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is relatively simple to Regex replace everything within the tags

18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
# Getting Started
# Viget's Craft CMS Starter

This repo is a Composer "project" intended for use with the `composer create-project` command.

Our starter uses DDEV for local development. Install it before doing any of the following steps.

# Features

- Local development [powered by DDEV](https://ddev.com/)
- [Vite](https://vitejs.dev/) based front-end build tooling.
- Automatic linting, formatting and typechecking
- Runs on git pre-commit hook with [Husky](https://typicode.github.io/husky/)
- Only processes staged files using [lint-staged](https://github.com/lint-staged/lint-staged)
- [Prettier](https://prettier.io/), [eslint](https://eslint.org/), [PHPStan](https://github.com/craftcms/phpstan), [PHP
Easy Coding Standard](https://github.com/craftcms/ecs)
- Common plugins come pre-installed
- Local email is routed
through [Mailpit](https://ddev.readthedocs.io/en/stable/users/usage/developer-tools/#email-capture-and-review-mailpit) (
never worry about emailing a client or user)

# Getting Started

## Create Project

1. [Install DDEV](https://ddev.readthedocs.io/en/stable/users/install/ddev-installation/)
Expand Down
11 changes: 11 additions & 0 deletions composer-scripts/ScriptHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class ScriptHelpers
{
public static function replaceFileText(string $filePath, string $pattern, string $replacement): void
{
$fileContent = file_get_contents($filePath);
$fileContent = preg_replace($pattern, $replacement, $fileContent);
file_put_contents($filePath, $fileContent);
}
}
94 changes: 94 additions & 0 deletions composer-scripts/post-create-project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

use craft\helpers\Console;
use craft\helpers\StringHelper;
Comment on lines +3 to +4
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realized I could use these Craft helpers, which makes ScriptHelpers super minimal (only need it for file text replacements).


require_once 'ScriptHelpers.php';
require_once 'vendor/autoload.php';

$cwd = getcwd();

/**
* Prompt the user for input
*/
$projectName = Console::prompt('What is the name of your project (Example: My Client Name)? ', [
'required' => true,
]);

Console::output("Great! We'll use the name: $projectName");

$suggestedProjectSlug = StringHelper::toKebabCase($projectName);

$projectSlugPrompt = Console::prompt("Customize the project slug? This controls the DDEV URL, etc.", [
'default' => $suggestedProjectSlug,
]);

$projectSlug = !empty(trim($projectSlugPrompt)) ? StringHelper::toKebabCase($projectSlugPrompt) : $suggestedProjectSlug;

Console::output("Great! We'll use $projectSlug");

/**
* Update DDEV config
*/

ScriptHelpers::replaceFileText(
filePath: "$cwd/.ddev/config.yaml",
pattern: "/name:\s+viget-craft-starter/",
replacement: "name: $projectSlug",
);

/**
* Update package.json
*/

ScriptHelpers::replaceFileText(
filePath: "$cwd/package.json",
pattern: "/\"name\": \"viget-craft-starter\"/",
replacement: "\"name\": \"$projectSlug\"",
);

ScriptHelpers::replaceFileText(
filePath: "$cwd/package-lock.json",
pattern: "/\"name\": \"viget-craft-starter\"/",
replacement: "\"name\": \"$projectSlug\"",
);

/**
* Update project config
*/

ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/project.yaml",
pattern: "/Viget Craft Starter/",
replacement: "$projectName",
);

// Replace plugin license keys.
// These are regenerated when viewing the Control Panel
ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/project.yaml",
pattern: "/ licenseKey: REPLACE[\r\n|\r|\n]/", // Make sure to remove new line too
replacement: "",
);

ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/siteGroups/805d8826-faed-4186-9b88-f509eb9b07e6.yaml",
pattern: "/Viget Craft Starter/",
replacement: "$projectName",
);

ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/sites/default--35b563a0-4662-40b9-b885-a8450a2868d9.yaml",
pattern: "/Viget Craft Starter/",
replacement: "$projectName",
);

/**
* .gitignore
*/

ScriptHelpers::replaceFileText(
filePath: "$cwd/.gitignore",
pattern: "/# BEGIN-STARTER-ONLY\X*# END-STARTER-ONLY/m",
replacement: '',
);
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@
"scripts": {
"post-create-project-cmd": [
"@php -r \"file_exists('.env') || copy('.env.example.dev', '.env');\"",
"@php install-scripts/rename-ddev.php",
"@php composer-scripts/post-create-project.php",
"echo 'Cleaning composer.json'",
"@composer config --unset scripts.post-create-project-cmd",
"@composer config --unset name",
"@composer config --unset license",
"@composer config --unset type",
"@composer update --ignore-platform-reqs",
"@composer dump-autoload -o",
"rm -rf install-scripts "
"rm -rf composer-scripts"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example.dev', '.env');\""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
headingLevels:
- 1
- 2
- 3
- 4
- 5
- 6
name: Simple
toolbar:
- heading
- '|'
- bold
- italic
- link
76 changes: 76 additions & 0 deletions config/project/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
dateModified: 1716932096
email:
fromEmail: $SYSTEM_EMAIL_FROM
fromName: 'Viget Craft Starter'
replyToEmail: null
template: null
transportSettings:
host: $SYSTEM_EMAIL_HOSTNAME
password: $SYSTEM_EMAIL_PASSWORD
port: $SYSTEM_EMAIL_PORT
useAuthentication: '1'
username: $SYSTEM_EMAIL_USERNAME
transportType: craft\mail\transportadapters\Smtp
Comment on lines +3 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes we'll use some kind of SMTP service on our projects.

I've found it to be necessary to get password reset emails to work, even for small clients.

It's super affordable: https://aws.amazon.com/ses/pricing/

meta:
__names__:
35b563a0-4662-40b9-b885-a8450a2868d9: 'Viget Craft Starter' # Viget Craft Starter
805d8826-faed-4186-9b88-f509eb9b07e6: 'Viget Craft Starter' # Viget Craft Starter
b7e66782-af96-4012-9e17-914134073ced: Simple # Simple
Comment on lines +16 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a little funny that these UUIDs will be repeated across projects... but it would be impossible for two Craft projects to be installed using the exact same DB tables, so it's not really an issue.

plugins:
aws-s3:
edition: standard
enabled: true
schemaVersion: '2.0'
ckeditor:
edition: standard
enabled: true
schemaVersion: 3.0.0.0
classnames:
edition: standard
enabled: true
schemaVersion: 3.0.0
cp-field-inspect:
edition: standard
enabled: true
schemaVersion: 1.0.0
empty-coalesce:
edition: standard
enabled: true
schemaVersion: 1.0.0
imager-x:
edition: lite
enabled: true
licenseKey: REPLACE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We remove these licenseKey lines. They auto-regenerate with new numbers when you create a new project and visit the Craft admin for the first time.

schemaVersion: 4.0.0
navigation:
edition: standard
enabled: true
licenseKey: REPLACE
schemaVersion: 2.1.0
retour:
edition: standard
enabled: true
licenseKey: REPLACE
schemaVersion: 3.0.12
seomatic:
edition: standard
enabled: true
licenseKey: REPLACE
schemaVersion: 3.0.12
vite:
edition: standard
enabled: true
schemaVersion: 1.0.0
system:
edition: solo
live: true
name: 'Viget Craft Starter'
schemaVersion: 5.0.0.20
timeZone: America/Los_Angeles
users:
allowPublicRegistration: false
defaultGroup: null
photoSubpath: null
photoVolumeUid: null
require2fa: false
requireEmailVerification: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: 'Viget Craft Starter'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
baseUrl: $PRIMARY_SITE_URL
enabled: true
handle: default
hasUrls: true
language: en-US
name: 'Viget Craft Starter'
primary: true
siteGroup: 805d8826-faed-4186-9b88-f509eb9b07e6 # Viget Craft Starter
sortOrder: 1
Loading