@depup/react-email 5.2.10-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +652 -0
- package/README.md +43 -0
- package/changes.json +58 -0
- package/dev/CHANGELOG.md +13 -0
- package/dev/index.js +46 -0
- package/dev/package.json +10 -0
- package/dist/index.mjs +7326 -0
- package/license.md +7 -0
- package/package.json +128 -0
- package/readme.md +59 -0
- package/src/commands/build.ts +269 -0
- package/src/commands/dev.ts +27 -0
- package/src/commands/export.ts +204 -0
- package/src/commands/resend/reset.ts +8 -0
- package/src/commands/resend/setup.ts +29 -0
- package/src/commands/start.ts +38 -0
- package/src/index.ts +110 -0
- package/src/utils/conf.ts +9 -0
- package/src/utils/esbuild/escape-string-for-regex.ts +3 -0
- package/src/utils/esbuild/renderring-utilities-exporter.ts +63 -0
- package/src/utils/get-emails-directory-metadata.spec.ts +82 -0
- package/src/utils/get-emails-directory-metadata.ts +140 -0
- package/src/utils/get-preview-server-location.ts +50 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/packageJson.ts +4 -0
- package/src/utils/preview/get-env-variables-for-preview-app.ts +20 -0
- package/src/utils/preview/hot-reloading/create-dependency-graph.spec.ts +226 -0
- package/src/utils/preview/hot-reloading/create-dependency-graph.ts +343 -0
- package/src/utils/preview/hot-reloading/get-imported-modules.spec.ts +151 -0
- package/src/utils/preview/hot-reloading/get-imported-modules.ts +49 -0
- package/src/utils/preview/hot-reloading/resolve-path-aliases.spec.ts +11 -0
- package/src/utils/preview/hot-reloading/resolve-path-aliases.ts +32 -0
- package/src/utils/preview/hot-reloading/setup-hot-reloading.ts +121 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/inner/data-to-import.json +1 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/inner/file-a.ts +5 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/inner/file-b.ts +5 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/inner/general-importing-file.ts +9 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/inner/outer-dependency.ts +3 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/inner/path-aliases.ts +1 -0
- package/src/utils/preview/hot-reloading/test/dependency-graph/outer.ts +5 -0
- package/src/utils/preview/hot-reloading/test/some-file.ts +0 -0
- package/src/utils/preview/hot-reloading/test/tsconfig.json +8 -0
- package/src/utils/preview/index.ts +2 -0
- package/src/utils/preview/serve-static-file.ts +51 -0
- package/src/utils/preview/start-dev-server.ts +252 -0
- package/src/utils/register-spinner-autostopping.ts +28 -0
- package/src/utils/style-text.ts +11 -0
- package/src/utils/tree.spec.ts +29 -0
- package/src/utils/tree.ts +76 -0
- package/src/utils/types/hot-reload-change.ts +6 -0
- package/src/utils/types/hot-reload-event.ts +3 -0
- package/tsconfig.json +39 -0
- package/tsdown.config.ts +8 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import logSymbols from 'log-symbols';
|
|
2
|
+
import prompts from 'prompts';
|
|
3
|
+
import { conf } from '../../utils/conf.js';
|
|
4
|
+
import { styleText } from '../../utils/style-text.js';
|
|
5
|
+
|
|
6
|
+
export async function resendSetup() {
|
|
7
|
+
const previousValue = conf.get('resendApiKey');
|
|
8
|
+
if (typeof previousValue === 'string' && previousValue.length > 0) {
|
|
9
|
+
console.info(
|
|
10
|
+
`You already have a Resend API Key configured (${styleText('grey', previousValue.slice(0, 11))}...), continuing will replace it.`,
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { apiKey } = await prompts({
|
|
15
|
+
type: 'password',
|
|
16
|
+
name: 'apiKey',
|
|
17
|
+
message: 'Enter your API Key (make sure it has "Full Access")',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
if (apiKey?.trim().length > 0) {
|
|
21
|
+
conf.set('resendApiKey', apiKey);
|
|
22
|
+
console.info(
|
|
23
|
+
`${logSymbols.success} Resend integration successfully set up`,
|
|
24
|
+
);
|
|
25
|
+
console.info(
|
|
26
|
+
`You can always remove it with ${styleText('green', 'npx react-email@latest resend reset')}`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { getPreviewServerLocation } from '../utils/get-preview-server-location.js';
|
|
5
|
+
|
|
6
|
+
export const start = async () => {
|
|
7
|
+
try {
|
|
8
|
+
const previewServerLocation = await getPreviewServerLocation();
|
|
9
|
+
|
|
10
|
+
const usersProjectLocation = process.cwd();
|
|
11
|
+
const builtPreviewPath = path.resolve(
|
|
12
|
+
usersProjectLocation,
|
|
13
|
+
'./.react-email',
|
|
14
|
+
);
|
|
15
|
+
if (!fs.existsSync(builtPreviewPath)) {
|
|
16
|
+
console.error(
|
|
17
|
+
"Could not find .react-email, maybe you haven't ran email build?",
|
|
18
|
+
);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const nextStart = spawn('npx', ['next', 'start', builtPreviewPath], {
|
|
23
|
+
cwd: previewServerLocation,
|
|
24
|
+
stdio: 'inherit',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
process.on('SIGINT', () => {
|
|
28
|
+
nextStart.kill('SIGINT');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
nextStart.on('exit', (code) => {
|
|
32
|
+
process.exit(code ?? 0);
|
|
33
|
+
});
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.log(error);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { program } from 'commander';
|
|
4
|
+
import { build } from './commands/build.js';
|
|
5
|
+
import { dev } from './commands/dev.js';
|
|
6
|
+
import { exportTemplates } from './commands/export.js';
|
|
7
|
+
import { resendReset } from './commands/resend/reset.js';
|
|
8
|
+
import { resendSetup } from './commands/resend/setup.js';
|
|
9
|
+
import { start } from './commands/start.js';
|
|
10
|
+
import { packageJson } from './utils/packageJson.js';
|
|
11
|
+
|
|
12
|
+
const requiredFlags = [
|
|
13
|
+
'--experimental-vm-modules',
|
|
14
|
+
'--disable-warning=ExperimentalWarning',
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const hasRequiredFlags = requiredFlags.every((flag) =>
|
|
18
|
+
process.execArgv.includes(flag),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
if (!hasRequiredFlags) {
|
|
22
|
+
const child = spawn(
|
|
23
|
+
process.execPath,
|
|
24
|
+
[
|
|
25
|
+
...requiredFlags,
|
|
26
|
+
...process.execArgv,
|
|
27
|
+
process.argv[1] ?? '',
|
|
28
|
+
...process.argv.slice(2),
|
|
29
|
+
],
|
|
30
|
+
{ stdio: 'inherit' },
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
child.on('exit', (code) => {
|
|
34
|
+
process.exit(code ?? 0);
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
const PACKAGE_NAME = 'react-email';
|
|
38
|
+
|
|
39
|
+
program
|
|
40
|
+
.name(PACKAGE_NAME)
|
|
41
|
+
.description('A live preview of your emails right in your browser')
|
|
42
|
+
.version(packageJson.version);
|
|
43
|
+
|
|
44
|
+
program
|
|
45
|
+
.command('dev')
|
|
46
|
+
.description('Starts the preview email development app')
|
|
47
|
+
.option(
|
|
48
|
+
'-d, --dir <path>',
|
|
49
|
+
'Directory with your email templates',
|
|
50
|
+
'./emails',
|
|
51
|
+
)
|
|
52
|
+
.option('-p --port <port>', 'Port to run dev server on', '3000')
|
|
53
|
+
.action(dev);
|
|
54
|
+
|
|
55
|
+
program
|
|
56
|
+
.command('build')
|
|
57
|
+
.description('Copies the preview app for onto .react-email and builds it')
|
|
58
|
+
.option(
|
|
59
|
+
'-d, --dir <path>',
|
|
60
|
+
'Directory with your email templates',
|
|
61
|
+
'./emails',
|
|
62
|
+
)
|
|
63
|
+
.option(
|
|
64
|
+
'-p --packageManager <name>',
|
|
65
|
+
'Package name to use on installation on `.react-email`',
|
|
66
|
+
'npm',
|
|
67
|
+
)
|
|
68
|
+
.action(build);
|
|
69
|
+
|
|
70
|
+
program
|
|
71
|
+
.command('start')
|
|
72
|
+
.description('Runs the built preview app that is inside of ".react-email"')
|
|
73
|
+
.action(start);
|
|
74
|
+
|
|
75
|
+
program
|
|
76
|
+
.command('export')
|
|
77
|
+
.description('Build the templates to the `out` directory')
|
|
78
|
+
.option('--outDir <path>', 'Output directory', 'out')
|
|
79
|
+
.option('-p, --pretty', 'Pretty print the output', false)
|
|
80
|
+
.option('-t, --plainText', 'Set output format as plain text', false)
|
|
81
|
+
.option(
|
|
82
|
+
'-d, --dir <path>',
|
|
83
|
+
'Directory with your email templates',
|
|
84
|
+
'./emails',
|
|
85
|
+
)
|
|
86
|
+
.option(
|
|
87
|
+
'-s, --silent',
|
|
88
|
+
'To, or not to show a spinner with process information',
|
|
89
|
+
false,
|
|
90
|
+
)
|
|
91
|
+
.action(({ outDir, pretty, plainText, silent, dir: srcDir }) =>
|
|
92
|
+
exportTemplates(outDir, srcDir, { silent, plainText, pretty }),
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const resend = program.command('resend');
|
|
96
|
+
|
|
97
|
+
resend
|
|
98
|
+
.command('setup')
|
|
99
|
+
.description(
|
|
100
|
+
'Sets up the integration between the React Email CLI, and your Resend account through an API Key',
|
|
101
|
+
)
|
|
102
|
+
.action(resendSetup);
|
|
103
|
+
|
|
104
|
+
resend
|
|
105
|
+
.command('reset')
|
|
106
|
+
.description('Deletes your API Key from the React Email configuration')
|
|
107
|
+
.action(resendReset);
|
|
108
|
+
|
|
109
|
+
program.parse();
|
|
110
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import Conf from 'conf';
|
|
2
|
+
|
|
3
|
+
// Just simple encryption. This isn't completely safe
|
|
4
|
+
// because anyone can find this key here
|
|
5
|
+
const encryptionKey = 'h2#x658}1#qY(@!:7,BD1J)q12$[tM25';
|
|
6
|
+
|
|
7
|
+
export const conf = new Conf<{
|
|
8
|
+
resendApiKey?: string;
|
|
9
|
+
}>({ projectName: 'react-email', encryptionKey });
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import type { Loader, PluginBuild, ResolveOptions } from 'esbuild';
|
|
4
|
+
import { escapeStringForRegex } from './escape-string-for-regex.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Made to export the `render` function out of the user's email template
|
|
8
|
+
* so that issues like https://github.com/resend/react-email/issues/649 don't
|
|
9
|
+
* happen.
|
|
10
|
+
*
|
|
11
|
+
* This also exports the `createElement` from the user's React version as well
|
|
12
|
+
* to avoid mismatches.
|
|
13
|
+
*
|
|
14
|
+
* This avoids multiple versions of React being involved, i.e., the version
|
|
15
|
+
* in the CLI vs. the version the user has on their emails.
|
|
16
|
+
*/
|
|
17
|
+
export const renderingUtilitiesExporter = (emailTemplates: string[]) => ({
|
|
18
|
+
name: 'rendering-utilities-exporter',
|
|
19
|
+
setup: (b: PluginBuild) => {
|
|
20
|
+
b.onLoad(
|
|
21
|
+
{
|
|
22
|
+
filter: new RegExp(
|
|
23
|
+
emailTemplates
|
|
24
|
+
.map((emailPath) => escapeStringForRegex(emailPath))
|
|
25
|
+
.join('|'),
|
|
26
|
+
),
|
|
27
|
+
},
|
|
28
|
+
async ({ path: pathToFile }) => {
|
|
29
|
+
return {
|
|
30
|
+
contents: `${await fs.readFile(pathToFile, 'utf8')};
|
|
31
|
+
export { render } from 'react-email-module-that-will-export-render'
|
|
32
|
+
export { createElement as reactEmailCreateReactElement } from 'react';
|
|
33
|
+
`,
|
|
34
|
+
loader: path.extname(pathToFile).slice(1) as Loader,
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
b.onResolve(
|
|
40
|
+
{ filter: /^react-email-module-that-will-export-render$/ },
|
|
41
|
+
async (args) => {
|
|
42
|
+
const options: ResolveOptions = {
|
|
43
|
+
kind: 'import-statement',
|
|
44
|
+
importer: args.importer,
|
|
45
|
+
resolveDir: args.resolveDir,
|
|
46
|
+
namespace: args.namespace,
|
|
47
|
+
};
|
|
48
|
+
let result = await b.resolve('@react-email/render', options);
|
|
49
|
+
if (result.errors.length === 0) {
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// If @react-email/render does not exist, resolve to @react-email/components
|
|
54
|
+
result = await b.resolve('@react-email/components', options);
|
|
55
|
+
if (result.errors.length > 0 && result.errors[0]) {
|
|
56
|
+
result.errors[0].text =
|
|
57
|
+
"Failed trying to import `render` from either `@react-email/render` or `@react-email/components` to be able to render your email template.\n Maybe you don't have either of them installed?";
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { getEmailsDirectoryMetadata } from './get-emails-directory-metadata';
|
|
3
|
+
|
|
4
|
+
test('getEmailsDirectoryMetadata on demo emails', async () => {
|
|
5
|
+
const emailsDirectoryPath = path.resolve(
|
|
6
|
+
__dirname,
|
|
7
|
+
'../../../../apps/demo/emails',
|
|
8
|
+
);
|
|
9
|
+
expect(await getEmailsDirectoryMetadata(emailsDirectoryPath)).toEqual({
|
|
10
|
+
absolutePath: emailsDirectoryPath,
|
|
11
|
+
directoryName: 'emails',
|
|
12
|
+
relativePath: '',
|
|
13
|
+
emailFilenames: [],
|
|
14
|
+
subDirectories: [
|
|
15
|
+
{
|
|
16
|
+
absolutePath: path.join(emailsDirectoryPath, 'magic-links'),
|
|
17
|
+
directoryName: 'magic-links',
|
|
18
|
+
relativePath: 'magic-links',
|
|
19
|
+
emailFilenames: [
|
|
20
|
+
'aws-verify-email',
|
|
21
|
+
'linear-login-code',
|
|
22
|
+
'notion-magic-link',
|
|
23
|
+
'plaid-verify-identity',
|
|
24
|
+
'raycast-magic-link',
|
|
25
|
+
'slack-confirm',
|
|
26
|
+
],
|
|
27
|
+
subDirectories: [],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
absolutePath: path.join(emailsDirectoryPath, 'newsletters'),
|
|
31
|
+
directoryName: 'newsletters',
|
|
32
|
+
relativePath: 'newsletters',
|
|
33
|
+
emailFilenames: [
|
|
34
|
+
'codepen-challengers',
|
|
35
|
+
'google-play-policy-update',
|
|
36
|
+
'stack-overflow-tips',
|
|
37
|
+
],
|
|
38
|
+
subDirectories: [],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
absolutePath: path.join(emailsDirectoryPath, 'notifications'),
|
|
42
|
+
directoryName: 'notifications',
|
|
43
|
+
relativePath: 'notifications',
|
|
44
|
+
emailFilenames: [
|
|
45
|
+
'github-access-token',
|
|
46
|
+
'papermark-year-in-review',
|
|
47
|
+
'vercel-invite-user',
|
|
48
|
+
'yelp-recent-login',
|
|
49
|
+
],
|
|
50
|
+
subDirectories: [],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
absolutePath: path.join(emailsDirectoryPath, 'receipts'),
|
|
54
|
+
directoryName: 'receipts',
|
|
55
|
+
relativePath: 'receipts',
|
|
56
|
+
emailFilenames: ['apple-receipt', 'nike-receipt'],
|
|
57
|
+
subDirectories: [],
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
absolutePath: path.join(emailsDirectoryPath, 'reset-password'),
|
|
61
|
+
directoryName: 'reset-password',
|
|
62
|
+
relativePath: 'reset-password',
|
|
63
|
+
emailFilenames: ['dropbox-reset-password', 'twitch-reset-password'],
|
|
64
|
+
subDirectories: [],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
absolutePath: path.join(emailsDirectoryPath, 'reviews'),
|
|
68
|
+
directoryName: 'reviews',
|
|
69
|
+
relativePath: 'reviews',
|
|
70
|
+
emailFilenames: ['airbnb-review', 'amazon-review'],
|
|
71
|
+
subDirectories: [],
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
absolutePath: path.join(emailsDirectoryPath, 'welcome'),
|
|
75
|
+
directoryName: 'welcome',
|
|
76
|
+
relativePath: 'welcome',
|
|
77
|
+
emailFilenames: ['koala-welcome', 'netlify-welcome', 'stripe-welcome'],
|
|
78
|
+
subDirectories: [],
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const isFileAnEmail = async (fullPath: string): Promise<boolean> => {
|
|
5
|
+
let fileHandle: fs.promises.FileHandle;
|
|
6
|
+
try {
|
|
7
|
+
fileHandle = await fs.promises.open(fullPath, 'r');
|
|
8
|
+
} catch (exception) {
|
|
9
|
+
console.warn(exception);
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const stat = await fileHandle.stat();
|
|
13
|
+
|
|
14
|
+
if (stat.isDirectory()) {
|
|
15
|
+
await fileHandle.close();
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { ext } = path.parse(fullPath);
|
|
20
|
+
|
|
21
|
+
if (!['.js', '.tsx', '.jsx'].includes(ext)) {
|
|
22
|
+
await fileHandle.close();
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// check with a heuristic to see if the file has at least
|
|
27
|
+
// a default export (ES6) or module.exports (CommonJS) or named exports (MDX)
|
|
28
|
+
const fileContents = await fileHandle.readFile('utf8');
|
|
29
|
+
|
|
30
|
+
await fileHandle.close();
|
|
31
|
+
|
|
32
|
+
// Check for ES6 export default syntax
|
|
33
|
+
const hasES6DefaultExport = /\bexport\s+default\b/gm.test(fileContents);
|
|
34
|
+
|
|
35
|
+
// Check for CommonJS module.exports syntax
|
|
36
|
+
const hasCommonJSExport = /\bmodule\.exports\s*=/gm.test(fileContents);
|
|
37
|
+
|
|
38
|
+
// Check for named exports (used in MDX files) and ensure at least one is marked as default
|
|
39
|
+
const hasNamedExport = /\bexport\s+\{[^}]*\bdefault\b[^}]*\}/gm.test(
|
|
40
|
+
fileContents,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return hasES6DefaultExport || hasCommonJSExport || hasNamedExport;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export interface EmailsDirectory {
|
|
47
|
+
absolutePath: string;
|
|
48
|
+
relativePath: string;
|
|
49
|
+
directoryName: string;
|
|
50
|
+
emailFilenames: string[];
|
|
51
|
+
subDirectories: EmailsDirectory[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const mergeDirectoriesWithSubDirectories = (
|
|
55
|
+
emailsDirectoryMetadata: EmailsDirectory,
|
|
56
|
+
): EmailsDirectory => {
|
|
57
|
+
let currentResultingMergedDirectory: EmailsDirectory =
|
|
58
|
+
emailsDirectoryMetadata;
|
|
59
|
+
|
|
60
|
+
while (
|
|
61
|
+
currentResultingMergedDirectory.emailFilenames.length === 0 &&
|
|
62
|
+
currentResultingMergedDirectory.subDirectories.length === 1
|
|
63
|
+
) {
|
|
64
|
+
const onlySubDirectory = currentResultingMergedDirectory.subDirectories[0]!;
|
|
65
|
+
currentResultingMergedDirectory = {
|
|
66
|
+
...onlySubDirectory,
|
|
67
|
+
directoryName: path.join(
|
|
68
|
+
currentResultingMergedDirectory.directoryName,
|
|
69
|
+
onlySubDirectory.directoryName,
|
|
70
|
+
),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return currentResultingMergedDirectory;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const getEmailsDirectoryMetadata = async (
|
|
78
|
+
absolutePathToEmailsDirectory: string,
|
|
79
|
+
keepFileExtensions = false,
|
|
80
|
+
isSubDirectory = false,
|
|
81
|
+
|
|
82
|
+
baseDirectoryPath = absolutePathToEmailsDirectory,
|
|
83
|
+
): Promise<EmailsDirectory | undefined> => {
|
|
84
|
+
if (!fs.existsSync(absolutePathToEmailsDirectory)) return;
|
|
85
|
+
|
|
86
|
+
const dirents = await fs.promises.readdir(absolutePathToEmailsDirectory, {
|
|
87
|
+
withFileTypes: true,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const isEmailPredicates = await Promise.all(
|
|
91
|
+
dirents.map((dirent) =>
|
|
92
|
+
isFileAnEmail(path.join(absolutePathToEmailsDirectory, dirent.name)),
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
const emailFilenames = dirents
|
|
96
|
+
.filter((_, i) => isEmailPredicates[i])
|
|
97
|
+
.map((dirent) =>
|
|
98
|
+
keepFileExtensions
|
|
99
|
+
? dirent.name
|
|
100
|
+
: dirent.name.replace(path.extname(dirent.name), ''),
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const subDirectories = await Promise.all(
|
|
104
|
+
dirents
|
|
105
|
+
.filter(
|
|
106
|
+
(dirent) =>
|
|
107
|
+
dirent.isDirectory() &&
|
|
108
|
+
!dirent.name.startsWith('_') &&
|
|
109
|
+
dirent.name !== 'static',
|
|
110
|
+
)
|
|
111
|
+
.map((dirent) => {
|
|
112
|
+
const direntAbsolutePath = path.join(
|
|
113
|
+
absolutePathToEmailsDirectory,
|
|
114
|
+
dirent.name,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return getEmailsDirectoryMetadata(
|
|
118
|
+
direntAbsolutePath,
|
|
119
|
+
keepFileExtensions,
|
|
120
|
+
true,
|
|
121
|
+
baseDirectoryPath,
|
|
122
|
+
) as Promise<EmailsDirectory>;
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const emailsMetadata = {
|
|
127
|
+
absolutePath: absolutePathToEmailsDirectory,
|
|
128
|
+
relativePath: path.relative(
|
|
129
|
+
baseDirectoryPath,
|
|
130
|
+
absolutePathToEmailsDirectory,
|
|
131
|
+
),
|
|
132
|
+
directoryName: absolutePathToEmailsDirectory.split(path.sep).pop()!,
|
|
133
|
+
emailFilenames,
|
|
134
|
+
subDirectories,
|
|
135
|
+
} satisfies EmailsDirectory;
|
|
136
|
+
|
|
137
|
+
return isSubDirectory
|
|
138
|
+
? mergeDirectoriesWithSubDirectories(emailsMetadata)
|
|
139
|
+
: emailsMetadata;
|
|
140
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import url from 'node:url';
|
|
3
|
+
import { createJiti } from 'jiti';
|
|
4
|
+
import { addDevDependency } from 'nypm';
|
|
5
|
+
import prompts from 'prompts';
|
|
6
|
+
import { packageJson } from './packageJson.js';
|
|
7
|
+
|
|
8
|
+
const ensurePreviewServerInstalled = async (
|
|
9
|
+
message: string,
|
|
10
|
+
): Promise<never> => {
|
|
11
|
+
const response = await prompts({
|
|
12
|
+
type: 'confirm',
|
|
13
|
+
name: 'installPreviewServer',
|
|
14
|
+
message,
|
|
15
|
+
initial: true,
|
|
16
|
+
});
|
|
17
|
+
if (response.installPreviewServer) {
|
|
18
|
+
console.log('Installing "@react-email/preview-server"');
|
|
19
|
+
await addDevDependency(
|
|
20
|
+
`@react-email/preview-server@${packageJson.version}`,
|
|
21
|
+
);
|
|
22
|
+
process.exit(0);
|
|
23
|
+
} else {
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const getPreviewServerLocation = async () => {
|
|
29
|
+
const usersProject = createJiti(process.cwd());
|
|
30
|
+
let previewServerLocation!: string;
|
|
31
|
+
try {
|
|
32
|
+
previewServerLocation = path.dirname(
|
|
33
|
+
url.fileURLToPath(usersProject.esmResolve('@react-email/preview-server')),
|
|
34
|
+
);
|
|
35
|
+
} catch (_exception) {
|
|
36
|
+
await ensurePreviewServerInstalled(
|
|
37
|
+
'To run the preview server, the package "@react-email/preview-server" must be installed. Would you like to install it?',
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const { version } = await usersProject.import<{
|
|
41
|
+
version: string;
|
|
42
|
+
}>('@react-email/preview-server');
|
|
43
|
+
if (version !== packageJson.version) {
|
|
44
|
+
await ensurePreviewServerInstalled(
|
|
45
|
+
`To run the preview server, the version of "@react-email/preview-server" must match the version of "react-email" (${packageJson.version}). Would you like to install it?`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return previewServerLocation;
|
|
50
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
export const getEnvVariablesForPreviewApp = (
|
|
4
|
+
relativePathToEmailsDirectory: string,
|
|
5
|
+
previewServerLocation: string,
|
|
6
|
+
cwd: string,
|
|
7
|
+
resendApiKey?: string,
|
|
8
|
+
) => {
|
|
9
|
+
return {
|
|
10
|
+
REACT_EMAIL_INTERNAL_EMAILS_DIR_RELATIVE_PATH:
|
|
11
|
+
relativePathToEmailsDirectory,
|
|
12
|
+
REACT_EMAIL_INTERNAL_EMAILS_DIR_ABSOLUTE_PATH: path.resolve(
|
|
13
|
+
cwd,
|
|
14
|
+
relativePathToEmailsDirectory,
|
|
15
|
+
),
|
|
16
|
+
REACT_EMAIL_INTERNAL_PREVIEW_SERVER_LOCATION: previewServerLocation,
|
|
17
|
+
REACT_EMAIL_INTERNAL_USER_PROJECT_LOCATION: cwd,
|
|
18
|
+
REACT_EMAIL_INTERNAL_RESEND_API_KEY: resendApiKey,
|
|
19
|
+
} as const;
|
|
20
|
+
};
|