Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion code/lib/create-storybook/src/initiate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { telemetry } from 'storybook/internal/telemetry';

import prompts from 'prompts';

import { getStorybookVersionFromAncestry, promptInstallType, promptNewUser } from './initiate';
import {
getCliIntegrationFromAncestry,
getStorybookVersionFromAncestry,
promptInstallType,
promptNewUser,
} from './initiate';

vi.mock('prompts', { spy: true });
vi.mock('storybook/internal/telemetry');
Expand Down Expand Up @@ -218,3 +223,25 @@ describe('getStorybookVersionFromAncestry', () => {
expect(getStorybookVersionFromAncestry(ancestry as any)).toBeUndefined();
});
});

describe('getCliIntegrationFromAncestry', () => {
it('returns the CLI integration if nested calls', () => {
const ancestry = [{ command: 'node' }, { command: 'npx sv add' }, { command: 'npx sv create' }];
expect(getCliIntegrationFromAncestry(ancestry as any)).toBe('sv create');
});

it('returns the CLI integration if found', () => {
const ancestry = [{ command: 'node' }, { command: 'npx sv add' }];
expect(getCliIntegrationFromAncestry(ancestry as any)).toBe('sv add');
});

it('returns the CLI integration if found', () => {
const ancestry = [{ command: 'node' }, { command: 'npx sv@latest add' }];
expect(getCliIntegrationFromAncestry(ancestry as any)).toBe('sv add');
});
Comment on lines +238 to +241
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Duplicate test description - both test cases have identical descriptions but test different scenarios

Suggested change
it('returns the CLI integration if found', () => {
const ancestry = [{ command: 'node' }, { command: 'npx sv@latest add' }];
expect(getCliIntegrationFromAncestry(ancestry as any)).toBe('sv add');
});
it('returns the CLI integration if found with version stripped', () => {
const ancestry = [{ command: 'node' }, { command: 'npx sv@latest add' }];
expect(getCliIntegrationFromAncestry(ancestry as any)).toBe('sv add');
});


it('returns undefined if no CLI integration found', () => {
const ancestry = [{ command: 'node' }, { command: 'npm' }];
expect(getCliIntegrationFromAncestry(ancestry as any)).toBeUndefined();
});
});
15 changes: 15 additions & 0 deletions code/lib/create-storybook/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ export function getStorybookVersionFromAncestry(
return undefined;
}

export function getCliIntegrationFromAncestry(
ancestry: ReturnType<typeof getProcessAncestry>
): string | undefined {
for (const ancestor of ancestry.toReversed()) {
const match = ancestor.command?.match(/\s(sv(@[^ ]+)? create|sv(@[^ ]+)? add)/i);
if (match) {
return match[1].includes('add') ? 'sv add' : 'sv create';
}
}
return undefined;
}

export async function doInitiate(options: CommandOptions): Promise<
| {
shouldRunDev: true;
Expand Down Expand Up @@ -439,9 +451,11 @@ export async function doInitiate(options: CommandOptions): Promise<
const isOutdated = lt(currentVersion, latestVersion);
const borderColor = isOutdated ? '#FC521F' : '#F1618C';
let versionSpecifier = undefined;
let cliIntegration = undefined;
try {
const ancestry = getProcessAncestry();
versionSpecifier = getStorybookVersionFromAncestry(ancestry);
cliIntegration = getCliIntegrationFromAncestry(ancestry);
} catch (err) {
//
}
Expand Down Expand Up @@ -660,6 +674,7 @@ export async function doInitiate(options: CommandOptions): Promise<
features: telemetryFeatures,
newUser,
versionSpecifier,
cliIntegration,
});
}

Expand Down
Loading