Skip to content

Commit 9aa8a85

Browse files
authored
Merge branch 'next' into shilman/cli-new-users
2 parents fa9320f + 4e90d5e commit 9aa8a85

File tree

4 files changed

+81
-37
lines changed

4 files changed

+81
-37
lines changed

code/lib/cli-storybook/src/automigrate/fixes/__test__/main-config-without-wrappers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const config = {
22
stories: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
3-
addons: ['@storybook/addon-essentials', '@storybook/addon-vitest'],
3+
addons: [
4+
{
5+
name: '@chromatic-com/storybook',
6+
options: {},
7+
},
8+
'@storybook/addon-vitest',
9+
],
410
framework: {
511
name: '@storybook/angular',
612
options: {},

code/lib/cli-storybook/src/automigrate/fixes/wrap-require-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ export function wrapValueWithRequireWrapper(config: ConfigFile, node: t.Node) {
193193
}
194194

195195
if (t.isArrayExpression(n)) {
196-
n.elements.forEach((element, index, elements) => {
197-
if (t.isStringLiteral(element)) {
198-
elements[index] = getReferenceToRequireWrapper(config, element.value);
196+
n.elements.forEach((element) => {
197+
if (element && isRequireWrapperNecessary(element)) {
198+
wrapValueWithRequireWrapper(config, element);
199199
}
200200
});
201201
}

code/lib/cli-storybook/src/automigrate/fixes/wrap-require.test.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import { describe, expect, it, vi } from 'vitest';
33

44
import * as detect from 'storybook/internal/cli';
55

6-
import { wrapRequire } from './wrap-require';
6+
import type { RunOptions } from '../types';
7+
import { type WrapRequireRunOptions, wrapRequire } from './wrap-require';
78

89
vi.mock('storybook/internal/cli', async (importOriginal) => ({
910
...(await importOriginal<typeof import('storybook/internal/cli')>()),
1011
detectPnp: vi.fn(),
1112
}));
1213

14+
vi.mock('node:fs/promises', async (importOriginal) => ({
15+
...(await importOriginal<typeof import('node:fs/promises')>()),
16+
writeFile: vi.fn(),
17+
}));
18+
1319
describe('wrapRequire', () => {
1420
describe('check', () => {
1521
it('should return null if not in a monorepo and pnp is not enabled', async () => {
@@ -21,7 +27,7 @@ describe('wrapRequire', () => {
2127
},
2228
storybookVersion: '7.0.0',
2329
mainConfigPath: require.resolve('./__test__/main-config-without-wrappers.js'),
24-
} as any);
30+
} as RunOptions<WrapRequireRunOptions>);
2531

2632
await expect(check).resolves.toBeNull();
2733
});
@@ -35,7 +41,7 @@ describe('wrapRequire', () => {
3541
},
3642
storybookVersion: '7.0.0',
3743
mainConfigPath: require.resolve('./__test__/main-config-without-wrappers.js'),
38-
} as any);
44+
} as RunOptions<WrapRequireRunOptions>);
3945

4046
await expect(check).resolves.toEqual({
4147
isConfigTypescript: false,
@@ -54,7 +60,7 @@ describe('wrapRequire', () => {
5460
},
5561
storybookVersion: '7.0.0',
5662
mainConfigPath: require.resolve('./__test__/main-config-without-wrappers.js'),
57-
} as any);
63+
} as RunOptions<WrapRequireRunOptions>);
5864

5965
await expect(check).resolves.toEqual({
6066
isConfigTypescript: false,
@@ -73,9 +79,51 @@ describe('wrapRequire', () => {
7379
},
7480
storybookVersion: '7.0.0',
7581
mainConfigPath: require.resolve('./__test__/main-config-with-wrappers.js'),
76-
} as any);
82+
} as RunOptions<WrapRequireRunOptions>);
7783

7884
await expect(check).resolves.toBeNull();
7985
});
8086
});
87+
88+
describe('run', () => {
89+
it('should wrap the require wrapper', async () => {
90+
await wrapRequire.run?.({
91+
mainConfigPath: require.resolve('./__test__/main-config-without-wrappers.js'),
92+
result: {
93+
isConfigTypescript: false,
94+
},
95+
} as RunOptions<WrapRequireRunOptions>);
96+
97+
const writeFile = vi.mocked((await import('node:fs/promises')).writeFile);
98+
99+
const call = writeFile.mock.calls[0];
100+
101+
expect(call[1]).toMatchInlineSnapshot(`
102+
"import { dirname, join } from "path";
103+
const config = {
104+
stories: ['../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
105+
addons: [
106+
{
107+
name: getAbsolutePath("@chromatic-com/storybook"),
108+
options: {},
109+
},
110+
getAbsolutePath("@storybook/addon-vitest"),
111+
],
112+
framework: {
113+
name: getAbsolutePath("@storybook/angular"),
114+
options: {},
115+
},
116+
docs: {
117+
autodocs: 'tag',
118+
},
119+
};
120+
export default config;
121+
122+
function getAbsolutePath(value) {
123+
return dirname(require.resolve(join(value, "package.json")));
124+
}
125+
"
126+
`);
127+
});
128+
});
81129
});

code/lib/cli-storybook/src/automigrate/fixes/wrap-require.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
wrapValueWithRequireWrapper,
1515
} from './wrap-require-utils';
1616

17-
interface WrapRequireRunOptions {
17+
export interface WrapRequireRunOptions {
1818
storybookVersion: string;
1919
isStorybookInMonorepo: boolean;
2020
isPnp: boolean;
@@ -61,34 +61,24 @@ export const wrapRequire: Fix<WrapRequireRunOptions> = {
6161
},
6262

6363
async run({ dryRun, mainConfigPath, result }) {
64-
return new Promise((resolve, reject) => {
65-
updateMainConfig({ dryRun: !!dryRun, mainConfigPath }, (mainConfig) => {
66-
try {
67-
getFieldsForRequireWrapper(mainConfig).forEach((node) => {
68-
wrapValueWithRequireWrapper(mainConfig, node);
69-
});
70-
71-
if (getRequireWrapperName(mainConfig) === null) {
72-
if (
73-
mainConfig?.fileName?.endsWith('.cjs') ||
74-
mainConfig?.fileName?.endsWith('.cts') ||
75-
mainConfig?.fileName?.endsWith('.cjsx') ||
76-
mainConfig?.fileName?.endsWith('.ctsx')
77-
) {
78-
mainConfig.setRequireImport(['dirname', 'join'], 'path');
79-
} else {
80-
mainConfig.setImport(['dirname', 'join'], 'path');
81-
}
82-
mainConfig.setBodyDeclaration(
83-
getRequireWrapperAsCallExpression(result.isConfigTypescript)
84-
);
85-
}
86-
87-
resolve();
88-
} catch (e) {
89-
reject(e);
90-
}
64+
await updateMainConfig({ dryRun: !!dryRun, mainConfigPath }, (mainConfig) => {
65+
getFieldsForRequireWrapper(mainConfig).forEach((node) => {
66+
wrapValueWithRequireWrapper(mainConfig, node);
9167
});
68+
69+
if (getRequireWrapperName(mainConfig) === null) {
70+
if (
71+
mainConfig?.fileName?.endsWith('.cjs') ||
72+
mainConfig?.fileName?.endsWith('.cts') ||
73+
mainConfig?.fileName?.endsWith('.cjsx') ||
74+
mainConfig?.fileName?.endsWith('.ctsx')
75+
) {
76+
mainConfig.setRequireImport(['dirname', 'join'], 'path');
77+
} else {
78+
mainConfig.setImport(['dirname', 'join'], 'path');
79+
}
80+
mainConfig.setBodyDeclaration(getRequireWrapperAsCallExpression(result.isConfigTypescript));
81+
}
9282
});
9383
},
9484
};

0 commit comments

Comments
 (0)