Skip to content

Commit

Permalink
Merge pull request #1095 from chromaui/jmhobbs/cap-2283-turbosnap-beh…
Browse files Browse the repository at this point in the history
…avior-changed-after-implementing-rspack

Add nicer formatting to TS bail reasons.
  • Loading branch information
jmhobbs authored Oct 17, 2024
2 parents 6890d08 + be770f7 commit 5899099
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-node-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: latest
node-version: 'lts/*'
- run: corepack enable
- run: yarn
- run: yarn build
Expand Down
50 changes: 26 additions & 24 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,30 +332,7 @@ export interface Context {
sentinelUrls?: string[];
uploadedBytes?: number;
uploadedFiles?: number;
turboSnap?: Partial<{
unavailable?: boolean;
rootPath: string;
baseDir: string;
storybookDir: string;
staticDirs: string[];
globs: string[];
modules: string[];
tracedFiles: string[];
tracedPaths: Set<string>;
changedDependencyNames: Set<string>;
changedManifestFiles: Set<string>;
affectedModuleIds: Set<string | number>;
bailReason: {
changedPackageFiles?: string[];
changedStorybookFiles?: string[];
changedStaticFiles?: string[];
changedExternalFiles?: string[];
invalidChangedFiles?: true;
missingStatsFile?: true;
noAncestorBuild?: true;
rebuild?: true;
};
}>;
turboSnap?: TurboSnap;
mergeBase?: string;
onlyStoryFiles?: string[];
untracedFiles?: string[];
Expand Down Expand Up @@ -396,4 +373,29 @@ export interface TargetInfo {
formFields: Record<string, string>;
}

export interface TurboSnap {
unavailable?: boolean;
rootPath?: string;
baseDir?: string;
storybookDir?: string;
staticDirs?: string[];
globs?: string[];
modules?: string[];
tracedFiles?: string[];
tracedPaths?: Set<string>;
changedDependencyNames?: Set<string>;
changedManifestFiles?: Set<string>;
affectedModuleIds?: Set<string | number>;
bailReason?: {
changedPackageFiles?: string[];
changedStorybookFiles?: string[];
changedStaticFiles?: string[];
changedExternalFiles?: string[];
invalidChangedFiles?: true;
missingStatsFile?: true;
noAncestorBuild?: true;
rebuild?: true;
};
}

export { type Configuration } from './lib/getConfiguration';
20 changes: 20 additions & 0 deletions node-src/ui/messages/info/tracedAffectedFiles.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ export const TracedAffectedFilesExpanded = () =>
}
);

export const TracedAffectedFilesExpandedBailed = () =>
tracedAffectedFiles(
{
options: { traceChanged: 'expanded' },
turboSnap: {
rootPath,
tracedPaths: new Set(tracedPaths),
bailReason: {
changedStorybookFiles: ['.storybook/preview.tsx', '.storybook/preview.less'],
},
},
} as any,
{
changedFiles: ['src/app/dashboard/index.ts'],
affectedModules,
modulesByName,
normalize: (f) => f,
}
);

export const TracedAffectedFilesCompact = () =>
tracedAffectedFiles(
{
Expand Down
40 changes: 38 additions & 2 deletions node-src/ui/messages/info/tracedAffectedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import pluralize from 'pluralize';

import { Context, Module } from '../../../types';
import { Context, Module, TurboSnap } from '../../../types';
import { info } from '../../components/icons';

const printFilePath = (filepath: string, basedir: string, expanded: boolean) => {
Expand Down Expand Up @@ -54,7 +54,9 @@ export default (
let directoryDebug;

if (expanded) {
const bailReason = ctx.turboSnap?.bailReason ? `${ctx.turboSnap.bailReason}\n\n` : '';
const bailReason = ctx.turboSnap?.bailReason
? `${formatBailReason(ctx.turboSnap.bailReason)}\n\n`
: '';
const rootPath = `${chalk.magenta(rootDirectoryNote)} ${ctx.turboSnap?.rootPath}\n\n`;
const basePath = `${chalk.magenta(baseDirectoryNote)} ${basedir}\n\n`;
const storybookPath = `${chalk.magenta(storybookDirectoryNote)} ${storybookConfigDirectory}\n\n`;
Expand Down Expand Up @@ -128,3 +130,37 @@ export default (
const note = chalk`\n\nSet {bold ${flag}} to {bold 'expanded'} to reveal underlying modules.`;
return `${summary}:\n\n${traces.join('\n\n')}${expanded ? '' : note}`;
};

const formatBailReasonSection = (name: string, files: string[]) => {
return chalk`{bold ${name}}\n` + files.map((f) => ` - ${f}`).join('\n');
};

function formatBailReason(bailReason: TurboSnap['bailReason']) {
if (!bailReason) {
return '';
}

const sections: string[] = [];

if (bailReason.changedPackageFiles) {
sections.push(formatBailReasonSection('Changed Package Files', bailReason.changedPackageFiles));
}

if (bailReason.changedStorybookFiles) {
sections.push(
formatBailReasonSection('Changed Storybook Files', bailReason.changedStorybookFiles)
);
}

if (bailReason.changedExternalFiles) {
sections.push(
formatBailReasonSection('Changed External Files', bailReason.changedExternalFiles)
);
}

if (bailReason.changedStaticFiles) {
sections.push(formatBailReasonSection('Changed Static Files', bailReason.changedStaticFiles));
}

return sections.join('\n');
}

0 comments on commit 5899099

Please sign in to comment.