Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Make PackageJson optional for starting a dev server #28594

Merged
merged 12 commits into from
Aug 21, 2024
36 changes: 26 additions & 10 deletions code/core/src/core-server/build-dev.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { BuilderOptions, CLIOptions, LoadOptions, Options } from '@storybook/core/types';
import {
getConfigInfo,
getProjectRoot,
loadAllPresets,
loadMainConfig,
resolveAddonName,
resolvePathInStorybookCache,
serverResolve,
validateFrameworkName,
versions,
} from '@storybook/core/common';
import prompts from 'prompts';
import invariant from 'tiny-invariant';
Expand All @@ -29,18 +31,33 @@ import { warnWhenUsingArgTypesRegex } from './utils/warnWhenUsingArgTypesRegex';
import { buildOrThrow } from './utils/build-or-throw';

export async function buildDevStandalone(
options: CLIOptions & LoadOptions & BuilderOptions
options: CLIOptions &
LoadOptions &
BuilderOptions & {
storybookVersion?: string;
previewConfigPath?: string;
}
): Promise<{ port: number; address: string; networkAddress: string }> {
const { packageJson, versionUpdates } = options;
invariant(
packageJson.version !== undefined,
`Expected package.json#version to be defined in the "${packageJson.name}" package}`
);
let { storybookVersion, previewConfigPath } = options;
const configDir = resolve(options.configDir);
if (packageJson) {
invariant(
packageJson.version !== undefined,
`Expected package.json#version to be defined in the "${packageJson.name}" package}`
);
storybookVersion = packageJson.version;
previewConfigPath = getConfigInfo(packageJson, configDir).previewConfig;
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (!storybookVersion) {
storybookVersion = versions.storybook;
}
}
// updateInfo are cached, so this is typically pretty fast
const [port, versionCheck] = await Promise.all([
getServerPort(options.port, { exactPort: options.exactPort }),
versionUpdates
? updateCheck(packageJson.version)
? updateCheck(storybookVersion)
: Promise.resolve({ success: false, cached: false, data: {}, time: Date.now() }),
]);

Expand All @@ -57,7 +74,6 @@ export async function buildDevStandalone(
}

const rootDir = getProjectRoot();
const configDir = resolve(options.configDir);
const cacheKey = oneWayHash(relative(rootDir, configDir));

const cacheOutputDir = resolvePathInStorybookCache('public', cacheKey);
Expand Down Expand Up @@ -89,13 +105,13 @@ export async function buildDevStandalone(
frameworkName = frameworkName || 'custom';

try {
await warnOnIncompatibleAddons(packageJson.version);
await warnOnIncompatibleAddons(storybookVersion);
} catch (e) {
console.warn('Storybook failed to check addon compatibility', e);
}

try {
await warnWhenUsingArgTypesRegex(packageJson, configDir, config);
await warnWhenUsingArgTypesRegex(previewConfigPath, config);
} catch (e) {}

// Load first pass: We need to determine the builder
Expand Down Expand Up @@ -221,7 +237,7 @@ export async function buildDevStandalone(
if (!options.quiet) {
outputStartupInformation({
updateInfo: versionCheck,
version: packageJson.version,
version: storybookVersion,
name,
address,
networkAddress,
Expand Down
15 changes: 6 additions & 9 deletions code/core/src/core-server/utils/warnWhenUsingArgTypesRegex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { PackageJson, StorybookConfig } from '@storybook/core/types';
import { getConfigInfo } from '@storybook/core/common';
import type { StorybookConfig } from '@storybook/core/types';
import { readFile } from 'fs-extra';
import * as babel from '@babel/core';
import type { BabelFile } from '@babel/core';
Expand All @@ -8,12 +7,10 @@ import { dedent } from 'ts-dedent';
import chalk from 'chalk';

export async function warnWhenUsingArgTypesRegex(
packageJson: PackageJson,
configDir: string,
previewConfigPath: string | undefined,
config: StorybookConfig
) {
const { previewConfig } = getConfigInfo(packageJson, configDir);
const previewContent = previewConfig ? await readFile(previewConfig, 'utf8') : '';
const previewContent = previewConfigPath ? await readFile(previewConfigPath, 'utf8') : '';

const hasVisualTestAddon =
config?.addons?.some((it) =>
Expand All @@ -22,10 +19,10 @@ export async function warnWhenUsingArgTypesRegex(
: it.name === '@chromatic-com/storybook'
) ?? false;

if (hasVisualTestAddon && previewConfig && previewContent.includes('argTypesRegex')) {
if (hasVisualTestAddon && previewConfigPath && previewContent.includes('argTypesRegex')) {
// @ts-expect-error File is not yet exposed, see https://github.com/babel/babel/issues/11350#issuecomment-644118606
const file: BabelFile = new babel.File(
{ filename: previewConfig },
{ filename: previewConfigPath },
{ code: previewContent, ast: babelParse(previewContent) }
);

Expand All @@ -37,7 +34,7 @@ export async function warnWhenUsingArgTypesRegex(
'actions.argTypesRegex'
)} together with the visual test addon:

${path.buildCodeFrameError(previewConfig).message}
${path.buildCodeFrameError(previewConfigPath).message}

We recommend removing the ${chalk.cyan(
'argTypesRegex'
Expand Down
2 changes: 1 addition & 1 deletion code/core/src/types/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export type PackageJson = PackageJsonFromTypeFest & Record<string, any>;
// TODO: This could be exported to the outside world and used in `options.ts` file of each `@storybook/APP`
// like it's described in docs/api/new-frameworks.md
export interface LoadOptions {
packageJson: PackageJson;
packageJson?: PackageJson;
outputDir?: string;
configDir?: string;
cacheKey?: string;
Expand Down