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
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default async (
inject: false,
template,
templateParameters: {
version: packageJson.version,
version: packageJson?.version,
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
Expand Down
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,13 +1,15 @@
import { join, relative, resolve } from 'node:path';

import {
getConfigInfo,
getProjectRoot,
loadAllPresets,
loadMainConfig,
resolveAddonName,
resolvePathInStorybookCache,
serverResolve,
validateFrameworkName,
versions,
} from '@storybook/core/common';
import { oneWayHash, telemetry } from '@storybook/core/telemetry';
import type { BuilderOptions, CLIOptions, LoadOptions, Options } from '@storybook/core/types';
Expand All @@ -32,18 +34,33 @@ import { warnOnIncompatibleAddons } from './utils/warnOnIncompatibleAddons';
import { warnWhenUsingArgTypesRegex } from './utils/warnWhenUsingArgTypesRegex';

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 ?? undefined;
} 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 @@ -60,7 +77,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 @@ -92,13 +108,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 @@ -224,7 +240,7 @@ export async function buildDevStandalone(
if (!options.quiet) {
outputStartupInformation({
updateInfo: versionCheck,
version: packageJson.version,
version: storybookVersion,
name,
address,
networkAddress,
Expand Down
2 changes: 1 addition & 1 deletion code/core/src/core-server/presets/common-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const babel = async (_: unknown, options: Options) => {
};

export const title = (previous: string, options: Options) =>
previous || options.packageJson.name || false;
previous || options.packageJson?.name || false;

export const logLevel = (previous: any, options: Options) => previous || options.loglevel || 'info';

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 { getConfigInfo } from '@storybook/core/common';
import type { PackageJson, StorybookConfig } from '@storybook/core/types';
import type { StorybookConfig } from '@storybook/core/types';

import { babelParse } from '@storybook/core/csf-tools';

Expand All @@ -10,12 +9,10 @@ import { readFile } from 'fs-extra';
import { dedent } from 'ts-dedent';

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 @@ -24,10 +21,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 @@ -39,7 +36,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 @@ -151,7 +151,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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const processCraConfig = async (
*
* See: https://github.com/storybookjs/storybook/pull/9157
*/
const storybookVersion = semver.coerce(options.packageJson.version) || '';
const storybookVersion = semver.coerce(options.packageJson?.version) || '';
const isStorybook530 = semver.gte(storybookVersion, '5.3.0');
const babelOptions = await options.presets.apply('babel');

Expand Down