0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00
ghost/.devcontainer/createLocalConfig.js
Chris Raible d0bf80b718
Added from address to Dev Container's auto configuration (#21442)
refs
https://linear.app/ghost/issue/ENG-1686/mail-auto-configuration-doesnt-work

- Previously the `mail` configuration that is auto-generated didn't work
because the `from` address wasn't being set, and requests were
consequently failing due to some Mailgun internal validation.
- This change requires the `MAILGUN_FROM_ADDRESS` environment variable
to be set for it to automatically configure the `mail` block, and if it
is, it adds it as `mail.from`. Now with all three of
`MAILGUN_SMTP_AUTH`, `MAILGUN_SMTP_PASS` and `MAILGUN_FROM_ADDRESS`
setup, transactional emails work out of the box in the Dev Container.
2024-10-28 16:14:04 -07:00

93 lines
No EOL
3.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const assert = require('node:assert/strict');
// Reads the config.local.json file and updates it with environments variables for devcontainer setup
const configBasePath = path.join(__dirname, '..', 'ghost', 'core');
const configFile = path.join(configBasePath, 'config.local.json');
let originalConfig = {};
if (fs.existsSync(configFile)) {
try {
// Backup the user's config.local.json file just in case
// This won't be used by Ghost but can be useful to switch back to local development
const backupFile = path.join(configBasePath, 'config.local-backup.json');
fs.copyFileSync(configFile, backupFile);
// Read the current config.local.json file into memory
const fileContent = fs.readFileSync(configFile, 'utf8');
originalConfig = JSON.parse(fileContent);
} catch (error) {
console.error('Error reading or parsing config file:', error);
process.exit(1);
}
} else {
console.log('Config file does not exist. Creating a new one.');
}
let newConfig = {};
// Change the url if we're in a codespace
if (process.env.CODESPACES === 'true') {
assert.ok(process.env.CODESPACE_NAME, 'CODESPACE_NAME is not defined');
assert.ok(process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN, 'GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN is not defined');
const url = `https://${process.env.CODESPACE_NAME}-2368.${process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}`;
newConfig.url = url;
}
newConfig.database = {
client: 'mysql2',
connection: {
host: 'mysql',
user: 'root',
password: 'root',
database: 'ghost'
}
}
newConfig.adapters = {
Redis: {
host: 'redis',
port: 6379
}
}
// Only update the mail settings if they aren't already set
if (!originalConfig.mail && process.env.MAILGUN_SMTP_PASS && process.env.MAILGUN_SMTP_USER && process.env.MAILGUN_FROM_ADDRESS) {
newConfig.mail = {
transport: 'SMTP',
from: process.env.MAILGUN_FROM_ADDRESS,
options: {
service: 'Mailgun',
host: 'smtp.mailgun.org',
secure: true,
port: 465,
auth: {
user: process.env.MAILGUN_SMTP_USER,
pass: process.env.MAILGUN_SMTP_PASS
}
}
}
}
// Only update the bulk email settings if they aren't already set
if (!originalConfig.bulkEmail && process.env.MAILGUN_API_KEY && process.env.MAILGUN_DOMAIN) {
newConfig.bulkEmail = {
mailgun: {
baseUrl: 'https://api.mailgun.net/v3',
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN,
tag: 'bulk-email'
}
}
}
// Merge the original config with the new config
const config = {...originalConfig, ...newConfig};
// Write the updated config.local.json file
try {
fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
console.log('Config file updated successfully.');
} catch (error) {
console.error('Error writing config file:', error);
process.exit(1);
}