Skip to content

Commit

Permalink
feat(react-email): Improved error feedback on all CLI commands (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmfern committed Dec 2, 2024
1 parent 7ae1acf commit 6781316
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-zebras-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-email": patch
---

Improve error messages for all CLI commands
4 changes: 2 additions & 2 deletions packages/react-email/src/actions/render-email-by-path.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import chalk from 'chalk';
import { getEmailComponent } from '../utils/get-email-component';
import type { ErrorObject } from '../utils/types/error-object';
import { improveErrorWithSourceMap } from '../utils/improve-error-with-sourcemap';
import { closeOraOnSIGNIT } from '../utils/close-ora-on-sigint';
import { registerSpinnerAutostopping } from '../utils/register-spinner-autostopping';

export interface RenderedEmailMetadata {
markup: string;
Expand All @@ -33,7 +33,7 @@ export const renderEmailByPath = async (
text: `Rendering email template ${emailFilename}\n`,
prefixText: ' ',
}).start();
closeOraOnSIGNIT(spinner);
registerSpinnerAutostopping(spinner);
}

const result = await getEmailComponent(emailPath);
Expand Down
8 changes: 4 additions & 4 deletions packages/react-email/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getEmailsDirectoryMetadata,
} from '../../actions/get-emails-directory-metadata';
import { cliPacakgeLocation } from '../utils';
import { closeOraOnSIGNIT } from '../../utils/close-ora-on-sigint';
import { registerSpinnerAutostopping } from '../../utils/register-spinner-autostopping';
import logSymbols from 'log-symbols';

interface Args {
Expand Down Expand Up @@ -224,11 +224,11 @@ export const build = async ({
text: 'Starting build process...',
prefixText: ' ',
}).start();
closeOraOnSIGNIT(spinner);
registerSpinnerAutostopping(spinner);

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

const emailsDirPath = path.join(process.cwd(), emailsDirRelativePath);
Expand Down
3 changes: 2 additions & 1 deletion packages/react-email/src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ interface Args {
export const dev = async ({ dir: emailsDirRelativePath, port }: Args) => {
try {
if (!fs.existsSync(emailsDirRelativePath)) {
throw new Error(`Missing ${emailsDirRelativePath} folder`);
console.error(`Missing ${emailsDirRelativePath} folder`);
process.exit(1);
}

const devServer = await startDevServer(
Expand Down
18 changes: 6 additions & 12 deletions packages/react-email/src/cli/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ora from 'ora';
import logSymbols from 'log-symbols';
import type { Options } from '@react-email/render';
import normalize from 'normalize-path';
import { closeOraOnSIGNIT } from '../../utils/close-ora-on-sigint';
import { registerSpinnerAutostopping } from '../../utils/register-spinner-autostopping';
import { tree } from '../utils';
import {
EmailsDirectory,
Expand Down Expand Up @@ -49,7 +49,7 @@ export const exportTemplates = async (
let spinner: ora.Ora | undefined;
if (!options.silent) {
spinner = ora('Preparing files...\n').start();
closeOraOnSIGNIT(spinner);
registerSpinnerAutostopping(spinner);
}

const emailsDirectoryMetadata = await getEmailsDirectoryMetadata(
Expand Down Expand Up @@ -90,14 +90,7 @@ export const exportTemplates = async (
text: 'Failed to build emails',
});
}

console.warn(buildFailure.warnings);
console.error(buildFailure.errors);
throw new Error(
`esbuild bundling process for email templates failed:\n${allTemplates
.map((p) => `- ${p}`)
.join('\n')}`,
);
process.exit(1);
}

if (spinner) {
Expand Down Expand Up @@ -144,7 +137,7 @@ export const exportTemplates = async (
});
}
console.error(exception);
throw exception;
process.exit(1);
}
}
if (spinner) {
Expand Down Expand Up @@ -178,9 +171,10 @@ export const exportTemplates = async (
text: 'Failed to copy static files',
});
}
throw new Error(
console.error(
`Something went wrong while copying the file to ${pathToWhereEmailMarkupShouldBeDumped}/static, ${exception}`,
);
process.exit(1);
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/react-email/src/cli/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export const start = async () => {
'./.react-email',
);
if (!fs.existsSync(builtPreviewPath)) {
throw new Error(
"Could not find `.react-email`, maybe you haven't ran `email build`?",
console.error(
"Could not find .react-email, maybe you haven't ran email build?",
);
process.exit(1);
}

const nextStart = spawn('npm', ['start'], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ora from 'ora';
import logSymbols from 'log-symbols';
import chalk from 'chalk';
import packageJson from '../../../../package.json';
import { closeOraOnSIGNIT } from '../../../utils/close-ora-on-sigint';
import { registerSpinnerAutostopping } from '../../../utils/register-spinner-autostopping';
import { serveStaticFile } from './serve-static-file';
import { getEnvVariablesForPreviewApp } from './get-env-variables-for-preview-app';

Expand Down Expand Up @@ -113,7 +113,7 @@ export const startDevServer = async (
prefixText: ' ',
}).start();

closeOraOnSIGNIT(spinner);
registerSpinnerAutostopping(spinner);
const timeBeforeNextReady = performance.now();

// these environment variables are used on the next app
Expand Down
15 changes: 0 additions & 15 deletions packages/react-email/src/utils/close-ora-on-sigint.ts

This file was deleted.

28 changes: 28 additions & 0 deletions packages/react-email/src/utils/register-spinner-autostopping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import logSymbols from 'log-symbols';
import type { Ora } from 'ora';

const spinners = new Set<Ora>();

process.on('SIGINT', () => {
spinners.forEach((spinner) => {
if (spinner.isSpinning) {
spinner.stop();
}
});
});

process.on('exit', (code) => {
if (code !== 0) {
spinners.forEach((spinner) => {
if (spinner.isSpinning) {
spinner.stopAndPersist({
symbol: logSymbols.error,
});
}
});
}
});

export const registerSpinnerAutostopping = (spinner: Ora) => {
spinners.add(spinner);
};

0 comments on commit 6781316

Please sign in to comment.