Skip to content

Commit

Permalink
fix(react-email): Public directory (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukinoshita authored Apr 25, 2023
1 parent a74694b commit 561c6ad
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions packages/react-email/source/utils/generate-email-preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logSymbols from 'log-symbols';
import { PACKAGE_EMAILS_PATH, PACKAGE_PUBLIC_PATH } from './constants';
import {
CURRENT_PATH,
PACKAGE_EMAILS_PATH,
PACKAGE_PUBLIC_PATH,
} from './constants';
import fs from 'fs';
import ora from 'ora';
import shell from 'shelljs';
Expand All @@ -14,13 +18,13 @@ export const generateEmailsPreview = async (
) => {
try {
const spinner = ora('Generating emails preview').start();
closeOraOnSIGNIT(spinner)
closeOraOnSIGNIT(spinner);

if (type === 'all' || type === 'templates') {
await createEmailPreviews(emailDir);
}
if (type === 'all' || type === 'static') {
await createStaticFiles(emailDir);
await createStaticFiles();
}

spinner.stopAndPersist({
Expand Down Expand Up @@ -81,26 +85,32 @@ const createEmailPreviews = async (emailDir: string) => {
}
};

const createStaticFiles = async (emailDir: string) => {
const createStaticFiles = async () => {
const hasPublicDirectory = fs.existsSync(PACKAGE_PUBLIC_PATH);

if (hasPublicDirectory) {
await fs.promises.rm(PACKAGE_PUBLIC_PATH, { recursive: true });
}

await fse.ensureDir(path.join(PACKAGE_PUBLIC_PATH, 'static'));

const result = shell.cp(
'-r',
path.join(emailDir, 'static'),
path.join(PACKAGE_PUBLIC_PATH),
const userHasStaticDirectory = fs.existsSync(
path.join(CURRENT_PATH, 'static'),
);
if (result.code > 0) {
throw new Error(
`Something went wrong while copying the file to ${path.join(
emailDir,
'static',
)}, ${result.cat()}`,

if (userHasStaticDirectory) {
const result = shell.cp(
'-r',
path.join(CURRENT_PATH, 'static'),
path.join(PACKAGE_PUBLIC_PATH),
);

if (result.code > 0) {
throw new Error(
`Something went wrong while copying the file to ${path.join(
CURRENT_PATH,
'static',
)}, ${result.cat()}`,
);
}
}
};

0 comments on commit 561c6ad

Please sign in to comment.