Skip to content

Commit 398da27

Browse files
committed
CLI: Capture the version specifier used in create-storybook
1 parent ed921e5 commit 398da27

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

code/lib/create-storybook/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"find-up": "^7.0.0",
5555
"ora": "^5.4.1",
5656
"picocolors": "^1.1.0",
57+
"process-ancestry": "^0.0.2",
5758
"prompts": "^2.4.0",
5859
"react": "^18.2.0",
5960
"tiny-invariant": "^1.3.1",

code/lib/create-storybook/src/initiate.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { telemetry } from 'storybook/internal/telemetry';
55

66
import prompts from 'prompts';
77

8-
import { promptInstallType, promptNewUser } from './initiate';
8+
import { getStorybookVersionFromAncestry, promptInstallType, promptNewUser } from './initiate';
99

1010
vi.mock('prompts', { spy: true });
1111
vi.mock('storybook/internal/telemetry');
@@ -155,3 +155,47 @@ describe('promptInstallType', () => {
155155
`);
156156
});
157157
});
158+
159+
describe('getStorybookVersionFromAncestry', () => {
160+
it('possible storybook path', () => {
161+
const ancestry = [{ command: 'node' }, { command: '[email protected]' }, { command: 'npm' }];
162+
expect(getStorybookVersionFromAncestry(ancestry as any)).toBeUndefined();
163+
});
164+
165+
it('create storybook', () => {
166+
const ancestry = [
167+
{ command: 'node' },
168+
{ command: 'npm create [email protected]' },
169+
{ command: 'npm' },
170+
];
171+
expect(getStorybookVersionFromAncestry(ancestry as any)).toBe('7.0.0-alpha.3');
172+
});
173+
174+
it('storybook init', () => {
175+
const ancestry = [
176+
{ command: 'node' },
177+
{ command: 'npx [email protected] init' },
178+
{ command: 'npm' },
179+
];
180+
expect(getStorybookVersionFromAncestry(ancestry as any)).toBe('7.0.0');
181+
});
182+
183+
it('storybook init no version', () => {
184+
const ancestry = [{ command: 'node' }, { command: 'npx storybook init' }, { command: 'npm' }];
185+
expect(getStorybookVersionFromAncestry(ancestry as any)).toBeUndefined();
186+
});
187+
188+
it('returns version from ancestry command', () => {
189+
const ancestry = [
190+
{ command: 'node' },
191+
{ command: 'npx create-storybook@latest' },
192+
{ command: 'npm' },
193+
];
194+
expect(getStorybookVersionFromAncestry(ancestry as any)).toBe('latest');
195+
});
196+
197+
it('returns undefined if no storybook version found', () => {
198+
const ancestry = [{ command: 'node' }, { command: 'npm' }];
199+
expect(getStorybookVersionFromAncestry(ancestry as any)).toBeUndefined();
200+
});
201+
});

code/lib/create-storybook/src/initiate.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { telemetry } from 'storybook/internal/telemetry';
3333
import boxen from 'boxen';
3434
import { findUp } from 'find-up';
3535
import picocolors from 'picocolors';
36+
import { getProcessAncestry } from 'process-ancestry';
3637
import prompts from 'prompts';
3738
import { lt, prerelease } from 'semver';
3839
import { dedent } from 'ts-dedent';
@@ -382,6 +383,18 @@ export const promptInstallType = async ({
382383
return installType;
383384
};
384385

386+
export function getStorybookVersionFromAncestry(
387+
ancestry: ReturnType<typeof getProcessAncestry>
388+
): string | undefined {
389+
for (const ancestor of ancestry) {
390+
const match = ancestor.command?.match(/[ \-]storybook@([^\s]+)/);
391+
if (match) {
392+
return match[1];
393+
}
394+
}
395+
return undefined;
396+
}
397+
385398
export async function doInitiate(options: CommandOptions): Promise<
386399
| {
387400
shouldRunDev: true;
@@ -425,6 +438,13 @@ export async function doInitiate(options: CommandOptions): Promise<
425438
const isPrerelease = prerelease(currentVersion);
426439
const isOutdated = lt(currentVersion, latestVersion);
427440
const borderColor = isOutdated ? '#FC521F' : '#F1618C';
441+
let versionSpecifier = undefined;
442+
try {
443+
const ancestry = getProcessAncestry();
444+
versionSpecifier = getStorybookVersionFromAncestry(ancestry);
445+
} catch (err) {
446+
//
447+
}
428448

429449
const messages = {
430450
welcome: `Adding Storybook version ${picocolors.bold(currentVersion)} to your project..`,
@@ -635,7 +655,12 @@ export async function doInitiate(options: CommandOptions): Promise<
635655
}
636656

637657
if (!options.disableTelemetry) {
638-
await telemetry('init', { projectType, features: telemetryFeatures, newUser });
658+
await telemetry('init', {
659+
projectType,
660+
features: telemetryFeatures,
661+
newUser,
662+
versionSpecifier,
663+
});
639664
}
640665

641666
if ([ProjectType.REACT_NATIVE, ProjectType.REACT_NATIVE_AND_RNW].includes(projectType)) {

code/yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11822,6 +11822,7 @@ __metadata:
1182211822
find-up: "npm:^7.0.0"
1182311823
ora: "npm:^5.4.1"
1182411824
picocolors: "npm:^1.1.0"
11825+
process-ancestry: "npm:^0.0.2"
1182511826
prompts: "npm:^2.4.0"
1182611827
react: "npm:^18.2.0"
1182711828
semver: "npm:^7.6.2"
@@ -21725,6 +21726,13 @@ __metadata:
2172521726
languageName: node
2172621727
linkType: hard
2172721728

21729+
"process-ancestry@npm:^0.0.2":
21730+
version: 0.0.2
21731+
resolution: "process-ancestry@npm:0.0.2"
21732+
checksum: 10c0/bca0290b7d3bd35da3a30bb958b2b0f2724f468b80d1b41e58d9a6a49fc42ff466faf49c2d085e94b7b660fee654be26e047b43f6b6065613ca123432bd409a9
21733+
languageName: node
21734+
linkType: hard
21735+
2172821736
"process-nextick-args@npm:~2.0.0":
2172921737
version: 2.0.1
2173021738
resolution: "process-nextick-args@npm:2.0.1"

0 commit comments

Comments
 (0)