-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat(cli): Setup command for mailer #9335
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4d1d2b2
setup command
Josh-Walker-GM 8044e80
restore auto test/dev handlers
Josh-Walker-GM 8fbbadf
Merge branch 'main' into jgmw-cli/mailer-setup
Josh-Walker-GM a784087
Merge branch 'main' into jgmw-cli/mailer-setup
Josh-Walker-GM eff012f
rename example component
Josh-Walker-GM 874c413
Add setup cli docs
Josh-Walker-GM 01adde0
Apply suggestions from code review
Josh-Walker-GM 5e9d6cf
Merge branch 'main' into jgmw-cli/mailer-setup
Josh-Walker-GM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers' | ||
|
||
export const command = 'mailer' | ||
|
||
export const description = | ||
'Setup the redwood mailer. This will install the required packages and add the required initial configuration to your redwood app.' | ||
|
||
export const builder = (yargs) => { | ||
yargs | ||
.option('force', { | ||
alias: 'f', | ||
default: false, | ||
description: 'Overwrite existing configuration', | ||
type: 'boolean', | ||
}) | ||
.option('skip-examples', { | ||
default: false, | ||
description: 'Only include required files and exclude any examples', | ||
type: 'boolean', | ||
}) | ||
} | ||
|
||
export const handler = async (options) => { | ||
recordTelemetryAttributes({ | ||
command: 'setup mailer', | ||
force: options.force, | ||
skipExamples: options.skipExamples, | ||
}) | ||
const { handler } = await import('./mailerHandler.js') | ||
return handler(options) | ||
} |
119 changes: 119 additions & 0 deletions
119
packages/cli/src/commands/setup/mailer/mailerHandler.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import { Listr } from 'listr2' | ||
|
||
import { addApiPackages } from '@redwoodjs/cli-helpers' | ||
import { errorTelemetry } from '@redwoodjs/telemetry' | ||
|
||
import { getPaths, transformTSToJS, writeFile } from '../../../lib' | ||
import c from '../../../lib/colors' | ||
import { isTypeScriptProject } from '../../../lib/project' | ||
|
||
export const handler = async ({ force, skipExamples }) => { | ||
const projectIsTypescript = isTypeScriptProject() | ||
const redwoodVersion = | ||
require(path.join(getPaths().base, 'package.json')).devDependencies[ | ||
'@redwoodjs/core' | ||
] ?? 'latest' | ||
|
||
const tasks = new Listr( | ||
[ | ||
{ | ||
title: `Adding api/src/lib/mailer.${ | ||
projectIsTypescript ? 'ts' : 'js' | ||
}...`, | ||
task: () => { | ||
const templatePath = path.resolve( | ||
__dirname, | ||
'templates', | ||
'mailer.ts.template' | ||
) | ||
const templateContent = fs.readFileSync(templatePath, { | ||
encoding: 'utf8', | ||
flag: 'r', | ||
}) | ||
|
||
const mailerPath = path.join( | ||
getPaths().api.lib, | ||
`mailer.${projectIsTypescript ? 'ts' : 'js'}` | ||
) | ||
const mailerContent = projectIsTypescript | ||
? templateContent | ||
: transformTSToJS(mailerPath, templateContent) | ||
|
||
return writeFile(mailerPath, mailerContent, { | ||
overwriteExisting: force, | ||
}) | ||
}, | ||
}, | ||
{ | ||
title: 'Adding api/src/mail directory...', | ||
task: () => { | ||
const mailDir = path.join(getPaths().api.mail) | ||
if (!fs.existsSync(mailDir)) { | ||
fs.mkdirSync(mailDir) | ||
} | ||
}, | ||
}, | ||
{ | ||
title: `Adding example ReactEmail mail template`, | ||
skip: () => skipExamples, | ||
task: () => { | ||
const templatePath = path.resolve( | ||
__dirname, | ||
'templates', | ||
're-example.tsx.template' | ||
) | ||
const templateContent = fs.readFileSync(templatePath, { | ||
encoding: 'utf8', | ||
flag: 'r', | ||
}) | ||
|
||
const exampleTemplatePath = path.join( | ||
getPaths().api.mail, | ||
'Example', | ||
`Example.${projectIsTypescript ? 'tsx' : 'jsx'}` | ||
) | ||
const exampleTemplateContent = projectIsTypescript | ||
? templateContent | ||
: transformTSToJS(exampleTemplatePath, templateContent) | ||
|
||
return writeFile(exampleTemplatePath, exampleTemplateContent, { | ||
overwriteExisting: force, | ||
}) | ||
}, | ||
}, | ||
{ | ||
// Add production dependencies | ||
...addApiPackages([ | ||
`@redwoodjs/mailer-core@${redwoodVersion}`, | ||
`@redwoodjs/mailer-handler-nodemailer@${redwoodVersion}`, | ||
`@redwoodjs/mailer-renderer-react-email@${redwoodVersion}`, | ||
`@react-email/components`, // NOTE: Unpinned dependency here | ||
]), | ||
title: 'Adding production dependencies to your api side...', | ||
}, | ||
{ | ||
// Add development dependencies | ||
...addApiPackages([ | ||
'-D', | ||
`@redwoodjs/mailer-handler-in-memory@${redwoodVersion}`, | ||
`@redwoodjs/mailer-handler-studio@${redwoodVersion}`, | ||
]), | ||
title: 'Adding development dependencies to your api side...', | ||
}, | ||
], | ||
{ | ||
rendererOptions: { collapseSubtasks: false }, | ||
} | ||
) | ||
|
||
try { | ||
await tasks.run() | ||
} catch (e) { | ||
errorTelemetry(process.argv, e.message) | ||
console.error(c.error(e.message)) | ||
process.exit(e?.exitCode || 1) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/cli/src/commands/setup/mailer/templates/mailer.ts.template
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Mailer } from '@redwoodjs/mailer-core' | ||
import { NodemailerMailHandler } from '@redwoodjs/mailer-handler-nodemailer' | ||
import { ReactEmailRenderer } from '@redwoodjs/mailer-renderer-react-email' | ||
|
||
import { logger } from './logger' | ||
Josh-Walker-GM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export const mailer = new Mailer({ | ||
handling: { | ||
handlers: { | ||
// TODO: Update this handler config or switch it out for a different handler completely | ||
nodemailer: new NodemailerMailHandler({ | ||
transport: { | ||
host: 'localhost', | ||
port: 4319, | ||
secure: false, | ||
}, | ||
}), | ||
}, | ||
default: 'nodemailer', | ||
}, | ||
|
||
rendering: { | ||
renderers: { | ||
reactEmail: new ReactEmailRenderer(), | ||
}, | ||
default: 'reactEmail', | ||
}, | ||
|
||
logger, | ||
}) |
40 changes: 40 additions & 0 deletions
40
packages/cli/src/commands/setup/mailer/templates/re-example.tsx.template
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react' | ||
|
||
import { | ||
Html, | ||
Text, | ||
Hr, | ||
Body, | ||
Head, | ||
Tailwind, | ||
Preview, | ||
Container, | ||
Heading, | ||
} from '@react-email/components' | ||
|
||
export function ExampleEmail( | ||
{ when }: { when: string } = { when: new Date().toLocaleString() } | ||
) { | ||
return ( | ||
<Html lang="en"> | ||
<Head /> | ||
<Preview>An example email</Preview> | ||
<Tailwind> | ||
<Body className="mx-auto my-auto bg-white font-sans"> | ||
<Container className="mx-auto my-[40px] rounded border border-solid border-gray-200 p-[20px]"> | ||
<Heading className="mx-0 my-[30px] p-0 text-center text-[24px] font-normal text-black"> | ||
Example Email | ||
</Heading> | ||
<Text className="text-[14px] leading-[24px] text-black"> | ||
This is an example email which you can customise to your needs. | ||
</Text> | ||
<Hr className="mx-0 my-[26px] w-full border border-solid border-[#eaeaea]" /> | ||
<Text className="text-[12px] leading-[24px] text-[#666666]"> | ||
Message was sent on {when} | ||
</Text> | ||
</Container> | ||
</Body> | ||
</Tailwind> | ||
</Html> | ||
) | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like many setup needs, getting the RW version feels like it could be a utility function rather than repeated the logic here. I know I had to have it for realtime setup and I think I checked a different package inside the setup/internal package rather than the project package -- which seems more sensible and can be consistent across all setups.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'll note down that this should be extracted out into a new utility. I'll do that in a separate PR