Skip to content

Commit

Permalink
Merge pull request #306 from chromaui/255-automatically-configure-tur…
Browse files Browse the repository at this point in the history
…bosnap

Suggest enabling TurboSnap and use all suggestions in generated config file
  • Loading branch information
ghengeveld authored May 15, 2024
2 parents 77a14e4 + 7d1ae70 commit 41487ab
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/components/Screen.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const Default: Story = {};
const configuration = {
configFile: "chromatic.config.json",
projectId: "Project:6480e1b0042842f149cfd74c",
onlyChanged: true,
externals: ["public/**"],
autoAcceptChanges: "main",
exitOnceUploaded: true,
Expand All @@ -40,7 +39,7 @@ export const Configuration: Story = {
withSharedState(CONFIG_INFO, {
configuration,
problems: { storybookBaseDir: "src/frontend" },
suggestions: { zip: true },
suggestions: { onlyChanged: true, zip: true },
} satisfies ConfigInfoPayload),
],
parameters: {
Expand All @@ -55,7 +54,6 @@ export const ConfigurationProblems = {
withSharedState(CONFIG_INFO, {
configuration,
problems: { storybookBaseDir: "src/frontend" },
suggestions: { zip: true },
}),
],
} satisfies StoryObj<typeof meta>;
Expand All @@ -65,7 +63,7 @@ export const ConfigurationSuggestions = {
withSetup(() => localStorage.removeItem(CONFIG_INFO_DISMISSED)),
withSharedState(CONFIG_INFO, {
configuration,
suggestions: { zip: true },
suggestions: { onlyChanged: true, zip: true },
}),
],
} satisfies StoryObj<typeof meta>;
Expand Down
14 changes: 12 additions & 2 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ const getConfigInfo = async (
problems.storybookConfigDir = configDir;
}

if (!configuration.zip) {
if (configuration.onlyChanged === undefined) {
suggestions.onlyChanged = true;
}

if (configuration.zip === undefined) {
suggestions.zip = true;
}

Expand Down Expand Up @@ -209,7 +213,13 @@ async function serverChannel(channel: Channel, options: Options & { configFile?:
// No config file may be found (file is about to be created)
const { configFile: foundConfigFile, ...config } = await getConfiguration(writtenConfigFile);
const targetConfigFile = foundConfigFile || writtenConfigFile || "chromatic.config.json";
await updateChromaticConfig(targetConfigFile, { ...config, projectId, zip: true });
const { problems, suggestions } = await getConfigInfo(config, options);
await updateChromaticConfig(targetConfigFile, {
...config,
...problems,
...suggestions,
projectId,
});

projectInfoState.value = {
...projectInfoState.value,
Expand Down
2 changes: 1 addition & 1 deletion src/screens/VisualTests/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export const Configuration = ({ onClose }: ConfigurationProps) => {
<CloseIcon aria-label="Close" />
</StyledCloseButton>
<PageWrapper>
<Heading>Configuration </Heading>
<Heading>Configuration</Heading>
{configFile ? (
<PageDescription>
This is a read-only representation of the Chromatic configuration options found in{" "}
Expand Down
5 changes: 4 additions & 1 deletion src/utils/updateChromaticConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ import type { Configuration } from "chromatic/node";
import { writeFile } from "jsonfile";

export async function updateChromaticConfig(configFile: string, configuration: Configuration) {
await writeFile(configFile, configuration, { spaces: 2 });
const formatted = Object.entries(configuration)
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce((acc, [key, val]) => (val === null ? acc : Object.assign(acc, { [key]: val })), {});
await writeFile(configFile, formatted, { spaces: 2 });
}

0 comments on commit 41487ab

Please sign in to comment.