Skip to content

Commit 6781316

Browse files
committedDec 2, 2024·
feat(react-email): Improved error feedback on all CLI commands (#1784)
1 parent 7ae1acf commit 6781316

File tree

9 files changed

+52
-38
lines changed

9 files changed

+52
-38
lines changed
 

‎.changeset/smooth-zebras-learn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-email": patch
3+
---
4+
5+
Improve error messages for all CLI commands

‎packages/react-email/src/actions/render-email-by-path.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import chalk from 'chalk';
77
import { getEmailComponent } from '../utils/get-email-component';
88
import type { ErrorObject } from '../utils/types/error-object';
99
import { improveErrorWithSourceMap } from '../utils/improve-error-with-sourcemap';
10-
import { closeOraOnSIGNIT } from '../utils/close-ora-on-sigint';
10+
import { registerSpinnerAutostopping } from '../utils/register-spinner-autostopping';
1111

1212
export interface RenderedEmailMetadata {
1313
markup: string;
@@ -33,7 +33,7 @@ export const renderEmailByPath = async (
3333
text: `Rendering email template ${emailFilename}\n`,
3434
prefixText: ' ',
3535
}).start();
36-
closeOraOnSIGNIT(spinner);
36+
registerSpinnerAutostopping(spinner);
3737
}
3838

3939
const result = await getEmailComponent(emailPath);

‎packages/react-email/src/cli/commands/build.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getEmailsDirectoryMetadata,
88
} from '../../actions/get-emails-directory-metadata';
99
import { cliPacakgeLocation } from '../utils';
10-
import { closeOraOnSIGNIT } from '../../utils/close-ora-on-sigint';
10+
import { registerSpinnerAutostopping } from '../../utils/register-spinner-autostopping';
1111
import logSymbols from 'log-symbols';
1212

1313
interface Args {
@@ -224,11 +224,11 @@ export const build = async ({
224224
text: 'Starting build process...',
225225
prefixText: ' ',
226226
}).start();
227-
closeOraOnSIGNIT(spinner);
227+
registerSpinnerAutostopping(spinner);
228228

229-
spinner.text = 'Checking if emails folder exists';
229+
spinner.text = `Checking if ${emailsDirRelativePath} folder exists`;
230230
if (!fs.existsSync(emailsDirRelativePath)) {
231-
throw new Error(`Missing ${emailsDirRelativePath} folder`);
231+
process.exit(1);
232232
}
233233

234234
const emailsDirPath = path.join(process.cwd(), emailsDirRelativePath);

‎packages/react-email/src/cli/commands/dev.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ interface Args {
99
export const dev = async ({ dir: emailsDirRelativePath, port }: Args) => {
1010
try {
1111
if (!fs.existsSync(emailsDirRelativePath)) {
12-
throw new Error(`Missing ${emailsDirRelativePath} folder`);
12+
console.error(`Missing ${emailsDirRelativePath} folder`);
13+
process.exit(1);
1314
}
1415

1516
const devServer = await startDevServer(

‎packages/react-email/src/cli/commands/export.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ora from 'ora';
77
import logSymbols from 'log-symbols';
88
import type { Options } from '@react-email/render';
99
import normalize from 'normalize-path';
10-
import { closeOraOnSIGNIT } from '../../utils/close-ora-on-sigint';
10+
import { registerSpinnerAutostopping } from '../../utils/register-spinner-autostopping';
1111
import { tree } from '../utils';
1212
import {
1313
EmailsDirectory,
@@ -49,7 +49,7 @@ export const exportTemplates = async (
4949
let spinner: ora.Ora | undefined;
5050
if (!options.silent) {
5151
spinner = ora('Preparing files...\n').start();
52-
closeOraOnSIGNIT(spinner);
52+
registerSpinnerAutostopping(spinner);
5353
}
5454

5555
const emailsDirectoryMetadata = await getEmailsDirectoryMetadata(
@@ -90,14 +90,7 @@ export const exportTemplates = async (
9090
text: 'Failed to build emails',
9191
});
9292
}
93-
94-
console.warn(buildFailure.warnings);
95-
console.error(buildFailure.errors);
96-
throw new Error(
97-
`esbuild bundling process for email templates failed:\n${allTemplates
98-
.map((p) => `- ${p}`)
99-
.join('\n')}`,
100-
);
93+
process.exit(1);
10194
}
10295

10396
if (spinner) {
@@ -144,7 +137,7 @@ export const exportTemplates = async (
144137
});
145138
}
146139
console.error(exception);
147-
throw exception;
140+
process.exit(1);
148141
}
149142
}
150143
if (spinner) {
@@ -178,9 +171,10 @@ export const exportTemplates = async (
178171
text: 'Failed to copy static files',
179172
});
180173
}
181-
throw new Error(
174+
console.error(
182175
`Something went wrong while copying the file to ${pathToWhereEmailMarkupShouldBeDumped}/static, ${exception}`,
183176
);
177+
process.exit(1);
184178
}
185179
}
186180

‎packages/react-email/src/cli/commands/start.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export const start = async () => {
1010
'./.react-email',
1111
);
1212
if (!fs.existsSync(builtPreviewPath)) {
13-
throw new Error(
14-
"Could not find `.react-email`, maybe you haven't ran `email build`?",
13+
console.error(
14+
"Could not find .react-email, maybe you haven't ran email build?",
1515
);
16+
process.exit(1);
1617
}
1718

1819
const nextStart = spawn('npm', ['start'], {

‎packages/react-email/src/cli/utils/preview/start-dev-server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ora from 'ora';
66
import logSymbols from 'log-symbols';
77
import chalk from 'chalk';
88
import packageJson from '../../../../package.json';
9-
import { closeOraOnSIGNIT } from '../../../utils/close-ora-on-sigint';
9+
import { registerSpinnerAutostopping } from '../../../utils/register-spinner-autostopping';
1010
import { serveStaticFile } from './serve-static-file';
1111
import { getEnvVariablesForPreviewApp } from './get-env-variables-for-preview-app';
1212

@@ -113,7 +113,7 @@ export const startDevServer = async (
113113
prefixText: ' ',
114114
}).start();
115115

116-
closeOraOnSIGNIT(spinner);
116+
registerSpinnerAutostopping(spinner);
117117
const timeBeforeNextReady = performance.now();
118118

119119
// these environment variables are used on the next app

‎packages/react-email/src/utils/close-ora-on-sigint.ts

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import logSymbols from 'log-symbols';
2+
import type { Ora } from 'ora';
3+
4+
const spinners = new Set<Ora>();
5+
6+
process.on('SIGINT', () => {
7+
spinners.forEach((spinner) => {
8+
if (spinner.isSpinning) {
9+
spinner.stop();
10+
}
11+
});
12+
});
13+
14+
process.on('exit', (code) => {
15+
if (code !== 0) {
16+
spinners.forEach((spinner) => {
17+
if (spinner.isSpinning) {
18+
spinner.stopAndPersist({
19+
symbol: logSymbols.error,
20+
});
21+
}
22+
});
23+
}
24+
});
25+
26+
export const registerSpinnerAutostopping = (spinner: Ora) => {
27+
spinners.add(spinner);
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.