Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Catch when prettier failed to prettify main and preview config files #24604

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 25 additions & 17 deletions code/lib/cli/src/generators/configure.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fse from 'fs-extra';
import path from 'path';
import { dedent } from 'ts-dedent';
import { logger } from '@storybook/node-logger';
import { externalFrameworks, SupportedLanguage } from '../project_types';

interface ConfigureMainOptions {
Expand Down Expand Up @@ -33,8 +34,6 @@ interface ConfigurePreviewOptions {
rendererId: string;
}

const logger = console;

/**
* We need to clean up the paths in case of pnp
* input: "path.dirname(require.resolve(path.join('@storybook/react-webpack5', 'package.json')))"
Expand Down Expand Up @@ -96,20 +95,25 @@ export async function configureMain({
finalPrefixes.push(`/** @type { import('${frameworkPackage}').StorybookConfig } */`);
}

const mainJsContents = mainConfigTemplate
let mainJsContents = mainConfigTemplate
.replace('<<import>>', `${imports.join('\n\n')}\n\n`)
.replace('<<prefix>>', finalPrefixes.length > 0 ? `${finalPrefixes.join('\n\n')}\n` : '')
.replace('<<type>>', isTypescript ? ': StorybookConfig' : '')
.replace('<<mainContents>>', mainContents);

const prettier = (await import('prettier')).default;

const mainPath = `./${storybookConfigFolder}/main.${isTypescript ? 'ts' : 'js'}`;
const prettyMain = prettier.format(dedent(mainJsContents), {
...prettier.resolveConfig.sync(process.cwd()),
filepath: mainPath,
});
await fse.writeFile(mainPath, prettyMain, { encoding: 'utf8' });

try {
const prettier = (await import('prettier')).default;
mainJsContents = prettier.format(dedent(mainJsContents), {
...prettier.resolveConfig.sync(process.cwd()),
filepath: mainPath,
});
} catch {
logger.verbose(`Failed to prettify ${mainPath}`);
}

await fse.writeFile(mainPath, mainJsContents, { encoding: 'utf8' });
}

export async function configurePreview(options: ConfigurePreviewOptions) {
Expand Down Expand Up @@ -140,7 +144,7 @@ export async function configurePreview(options: ConfigurePreviewOptions) {
.filter(Boolean)
.join('\n');

const preview = dedent`
let preview = dedent`
${prefix}${prefix.length > 0 ? '\n' : ''}
${
!isTypescript && rendererPackage
Expand All @@ -163,11 +167,15 @@ export async function configurePreview(options: ConfigurePreviewOptions) {
.replace(' \n', '')
.trim();

const prettier = (await import('prettier')).default;
try {
const prettier = (await import('prettier')).default;
preview = prettier.format(preview, {
...prettier.resolveConfig.sync(process.cwd()),
filepath: previewPath,
});
} catch {
logger.verbose(`Failed to prettify ${previewPath}`);
}

const prettyPreview = prettier.format(preview, {
...prettier.resolveConfig.sync(process.cwd()),
filepath: previewPath,
});
await fse.writeFile(previewPath, prettyPreview, { encoding: 'utf8' });
await fse.writeFile(previewPath, preview, { encoding: 'utf8' });
}