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
2 changes: 1 addition & 1 deletion code/addons/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
"@vitest/browser": "^3.2.4",
"@vitest/runner": "^3.2.4",
"boxen": "^8.0.1",
"empathic": "^2.0.0",
"es-toolkit": "^1.36.0",
"execa": "^8.0.1",
"find-up": "^7.0.0",
"istanbul-lib-report": "^3.0.1",
"micromatch": "^4.0.8",
"pathe": "^1.1.2",
Expand Down
6 changes: 3 additions & 3 deletions code/addons/vitest/src/node/vitest-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
import { getProjectRoot, resolvePathInStorybookCache } from 'storybook/internal/common';
import type { StoryId, StoryIndex, StoryIndexEntry } from 'storybook/internal/types';

import * as find from 'empathic/find';
import { findUp } from 'find-up';
import path, { dirname, join, normalize } from 'pathe';
import slash from 'slash';

Expand Down Expand Up @@ -64,12 +64,12 @@ export class VitestManager {
: { enabled: false }
) as CoverageOptions;

const vitestWorkspaceConfig = find.any(
const vitestWorkspaceConfig = await findUp(
[
...VITEST_WORKSPACE_FILE_EXTENSION.map((ext) => `vitest.workspace.${ext}`),
...VITEST_CONFIG_FILE_EXTENSIONS.map((ext) => `vitest.config.${ext}`),
],
{ last: getProjectRoot() }
{ stopAt: getProjectRoot() }
);

const projectName = 'storybook:' + process.env.STORYBOOK_CONFIG_DIR;
Expand Down
26 changes: 14 additions & 12 deletions code/addons/vitest/src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import { experimental_loadStorybook } from 'storybook/internal/core-server';
import { readConfig, writeConfig } from 'storybook/internal/csf-tools';
import { logger } from 'storybook/internal/node-logger';

import * as find from 'empathic/find';
import * as pkg from 'empathic/package';
// eslint-disable-next-line depend/ban-dependencies
import { execa } from 'execa';
import { findUp } from 'find-up';
import { dirname, relative, resolve } from 'pathe';
import prompts from 'prompts';
import { coerce, satisfies } from 'semver';
Expand Down Expand Up @@ -61,10 +60,10 @@ const logErrors = (...args: Parameters<typeof printError>) => {
printError(...args);
};

const findFile = (basename: string, extensions = EXTENSIONS) =>
find.any(
const findFile = async (basename: string, extensions = EXTENSIONS) =>
findUp(
extensions.map((ext) => basename + ext),
{ last: getProjectRoot() }
{ stopAt: getProjectRoot() }
);

export default async function postInstall(options: PostinstallOptions) {
Expand Down Expand Up @@ -321,7 +320,7 @@ export default async function postInstall(options: PostinstallOptions) {
}

const fileExtension =
allDeps.typescript || findFile('tsconfig', [...EXTENSIONS, '.json']) ? 'ts' : 'js';
allDeps.typescript || (await findFile('tsconfig', [...EXTENSIONS, '.json'])) ? 'ts' : 'js';

const vitestSetupFile = resolve(options.configDir, `vitest.setup.${fileExtension}`);
if (existsSync(vitestSetupFile)) {
Expand Down Expand Up @@ -368,11 +367,11 @@ export default async function postInstall(options: PostinstallOptions) {
);

const vitestWorkspaceFile =
findFile('vitest.workspace', ['.ts', '.js', '.json']) ||
findFile('vitest.projects', ['.ts', '.js', '.json']);
const viteConfigFile = findFile('vite.config');
const vitestConfigFile = findFile('vitest.config');
const vitestShimFile = findFile('vitest.shims.d');
(await findFile('vitest.workspace', ['.ts', '.js', '.json'])) ||
(await findFile('vitest.projects', ['.ts', '.js', '.json']));
const viteConfigFile = await findFile('vite.config');
const vitestConfigFile = await findFile('vitest.config');
const vitestShimFile = await findFile('vitest.shims.d');
const rootConfig = vitestConfigFile || viteConfigFile;

const browserConfig = `{
Expand Down Expand Up @@ -577,7 +576,10 @@ async function getPackageNameFromPath(input: string): Promise<string> {
return path;
}

const packageJsonPath = pkg.up({ cwd: path });
const packageJsonPath = await findUp('package.json', {
cwd: path,
});

if (!packageJsonPath) {
throw new Error(`Could not find package.json in path: ${path}`);
}
Expand Down
3 changes: 2 additions & 1 deletion code/builders/builder-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"@types/find-cache-dir": "^3.2.1",
"@types/node": "^22.0.0",
"empathic": "^2.0.0",
"es-module-lexer": "^1.5.0",
"find-cache-dir": "^3.0.0",
"glob": "^10.0.0",
"knitwork": "^1.1.0",
"magic-string": "^0.30.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

import * as pkg from 'empathic/package';
import { init, parse } from 'es-module-lexer';
import findCacheDirectory from 'find-cache-dir';
import MagicString from 'magic-string';
import type { Alias, Plugin } from 'vite';

Expand Down Expand Up @@ -52,7 +52,10 @@ export async function externalGlobalsPlugin(externals: Record<string, string>):
}
const newAlias = mergeAlias([], config.resolve?.alias) as Alias[];

const cachePath = pkg.cache('sb-vite-plugin-externals', { create: true })!;
const cachePath = findCacheDirectory({
name: 'sb-vite-plugin-externals',
create: true,
}) as string;
await Promise.all(
(Object.keys(externals) as Array<keyof typeof externals>).map(async (externalKey) => {
const externalCachePath = join(cachePath, `${externalKey}.js`);
Expand Down
6 changes: 5 additions & 1 deletion code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
"@types/detect-port": "^1.3.0",
"@types/diff": "^5.0.9",
"@types/ejs": "^3.1.1",
"@types/find-cache-dir": "^5.0.0",
"@types/js-yaml": "^4.0.5",
"@types/node": "^22.0.0",
"@types/npmlog": "^7.0.0",
Expand Down Expand Up @@ -304,12 +305,14 @@
"diff": "^5.2.0",
"downshift": "^9.0.4",
"ejs": "^3.1.10",
"empathic": "^2.0.0",
"es-toolkit": "^1.36.0",
"esbuild": "^0.18.0 || ^0.19.0 || ^0.20.0 || ^0.21.0 || ^0.22.0 || ^0.23.0 || ^0.24.0 || ^0.25.0",
"execa": "^8.0.1",
"exsolve": "^1.0.7",
"fd-package-json": "^1.2.0",
"fetch-retry": "^6.0.0",
"find-cache-dir": "^5.0.0",
"find-up": "^7.0.0",
"flush-promises": "^1.0.2",
"fuse.js": "^3.6.1",
"get-npm-tarball-url": "^2.0.3",
Expand Down Expand Up @@ -345,6 +348,7 @@
"react-textarea-autosize": "^8.3.0",
"react-transition-group": "^4.4.5",
"require-from-string": "^2.0.2",
"resolve-from": "^5.0.0",
"resolve.exports": "^2.0.3",
"sirv": "^2.0.4",
"slash": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion code/core/src/cli/detect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ vi.mock(import('fs'), async (importOriginal) => {
});

vi.mock('storybook/internal/node-logger');
vi.mock('empathic/find');
vi.mock('find-up');

const MOCK_FRAMEWORK_FILES: {
name: string;
Expand Down
8 changes: 4 additions & 4 deletions code/core/src/cli/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { JsPackageManager, PackageJsonWithMaybeDeps } from 'storybook/inter
import { HandledError, commandLog, getProjectRoot } from 'storybook/internal/common';
import { logger } from 'storybook/internal/node-logger';

import * as find from 'empathic/find';
import { findUpSync } from 'find-up';
import prompts from 'prompts';
import semver from 'semver';

Expand Down Expand Up @@ -112,8 +112,8 @@ export function detectFrameworkPreset(
* @returns CoreBuilder
*/
export async function detectBuilder(packageManager: JsPackageManager, projectType: ProjectType) {
const viteConfig = find.any(viteConfigFiles, { last: getProjectRoot() });
const webpackConfig = find.any(webpackConfigFiles, { last: getProjectRoot() });
const viteConfig = findUpSync(viteConfigFiles, { stopAt: getProjectRoot() });
const webpackConfig = findUpSync(webpackConfigFiles, { stopAt: getProjectRoot() });
const dependencies = packageManager.getAllDependencies();

if (viteConfig || (dependencies.vite && dependencies.webpack === undefined)) {
Expand Down Expand Up @@ -171,7 +171,7 @@ export function isStorybookInstantiated(configDir = resolve(process.cwd(), '.sto
}

export async function detectPnp() {
return !!find.any(['.pnp.js', '.pnp.cjs']);
return !!findUpSync(['.pnp.js', '.pnp.cjs']);
}

export async function detectLanguage(packageManager: JsPackageManager) {
Expand Down
24 changes: 12 additions & 12 deletions code/core/src/cli/eslintPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFile, writeFile } from 'node:fs/promises';

import { beforeEach, describe, expect, it, vi } from 'vitest';

import * as find from 'empathic/find';
import { findUp } from 'find-up';
import { dedent } from 'ts-dedent';

import type { PackageJsonWithDepsAndDevDeps } from '../common';
Expand All @@ -14,8 +14,8 @@ import {
normalizeExtends,
} from './eslintPlugin';

vi.mock('empathic/find', () => ({
up: vi.fn(),
vi.mock('find-up', () => ({
findUp: vi.fn(),
}));

vi.mock(import('node:fs/promises'), async (importOriginal) => {
Expand All @@ -38,32 +38,32 @@ describe('extractEslintInfo', () => {
} satisfies Partial<JsPackageManager>;

beforeEach(() => {
vi.mocked(find).up.mockClear();
vi.mocked(findUp).mockClear();
mockPackageManager.getAllDependencies.mockClear();
});

it('should find ESLint config file with supported extension', async () => {
vi.mocked(find).up.mockImplementation((fileName) => {
vi.mocked(findUp).mockImplementation(async (fileName) => {
return String(fileName) === '.eslintrc.js' ? String(fileName) : undefined;
});

const result = findEslintFile(process.cwd());
const result = await findEslintFile(process.cwd());
expect(result).toBe('.eslintrc.js');
});

it('should return undefined if no ESLint config file is found', async () => {
vi.mocked(find).up.mockImplementation(() => undefined);
vi.mocked(findUp).mockImplementation(async () => undefined);

const result = findEslintFile(process.cwd());
const result = await findEslintFile(process.cwd());
expect(result).toBeUndefined();
});

it('should throw error for unsupported ESLint config file extensions', async () => {
vi.mocked(find).up.mockImplementation(() => {
vi.mocked(findUp).mockImplementation(async () => {
return '.eslintrc.yaml';
});

expect(() => findEslintFile(process.cwd())).toThrowError(
await expect(findEslintFile(process.cwd())).rejects.toThrowError(
'Unsupported ESLint config extension: .yaml'
);
});
Expand All @@ -72,7 +72,7 @@ describe('extractEslintInfo', () => {
mockPackageManager.getAllDependencies.mockReturnValue({});
mockPackageManager.primaryPackageJson.packageJson = { dependencies: {}, devDependencies: {} };

vi.mocked(find).up.mockImplementation(() => undefined);
vi.mocked(findUp).mockImplementation(async () => undefined);

const result = await extractEslintInfo(mockPackageManager as any);

Expand All @@ -96,7 +96,7 @@ describe('extractEslintInfo', () => {
operationDir: '/some/path',
};

vi.mocked(find).up.mockImplementation((fileName) =>
vi.mocked(findUp).mockImplementation(async (fileName) =>
String(fileName) === '.eslintrc.js' ? String(fileName) : undefined
);

Expand Down
10 changes: 5 additions & 5 deletions code/core/src/cli/eslintPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logger, prompt } from 'storybook/internal/node-logger';

import commentJson from 'comment-json';
import detectIndent from 'detect-indent';
import * as find from 'empathic/find';
import { findUp } from 'find-up';
import picocolors from 'picocolors';
import { dedent } from 'ts-dedent';

Expand All @@ -15,13 +15,13 @@ import { babelParse, recast, types as t, traverse } from '../babel';
export const SUPPORTED_ESLINT_EXTENSIONS = ['ts', 'mts', 'cts', 'mjs', 'js', 'cjs', 'json'];
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml'];

export const findEslintFile = (instanceDir: string) => {
export const findEslintFile = async (instanceDir: string) => {
const filePrefixes = ['eslint.config', '.eslintrc'];

// Check for unsupported files
for (const prefix of filePrefixes) {
for (const ext of UNSUPPORTED_ESLINT_EXTENSIONS) {
const file = find.up(`${prefix}.${ext}`, { cwd: instanceDir, last: getProjectRoot() });
const file = await findUp(`${prefix}.${ext}`, { cwd: instanceDir, stopAt: getProjectRoot() });
if (file) {
throw new Error(`Unsupported ESLint config extension: .${ext}`);
}
Expand All @@ -31,7 +31,7 @@ export const findEslintFile = (instanceDir: string) => {
// Find supported ESLint config files
for (const prefix of filePrefixes) {
for (const ext of SUPPORTED_ESLINT_EXTENSIONS) {
const file = find.up(`${prefix}.${ext}`, { cwd: instanceDir, last: getProjectRoot() });
const file = await findUp(`${prefix}.${ext}`, { cwd: instanceDir, stopAt: getProjectRoot() });
if (file) {
return file;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ export async function extractEslintInfo(packageManager: JsPackageManager): Promi
let eslintConfigFile: string | undefined = undefined;

try {
eslintConfigFile = findEslintFile(packageManager.instanceDir);
eslintConfigFile = await findEslintFile(packageManager.instanceDir);
} catch (err) {
if (err instanceof Error && err.message.includes('Unsupported ESLint')) {
unsupportedExtension = String(err);
Expand Down
4 changes: 2 additions & 2 deletions code/core/src/cli/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ vi.mock('node:fs/promises', async (importOriginal) => {
};
});

vi.mock('empathic/find', () => ({
up: vi.fn(),
vi.mock('find-up', () => ({
sync: vi.fn(),
}));

vi.mock('path', async (importOriginal) => {
Expand Down
6 changes: 4 additions & 2 deletions code/core/src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { join, resolve } from 'node:path';
import {
type JsPackageManager,
type PackageJson,
type PackageJsonWithDepsAndDevDeps,
frameworkToRenderer,
getProjectRoot,
} from 'storybook/internal/common';
import { versions as storybookMonorepoPackages } from 'storybook/internal/common';
import { logger } from 'storybook/internal/node-logger';
import type { SupportedFrameworks, SupportedRenderers } from 'storybook/internal/types';

import * as find from 'empathic/find';
import { findUpSync } from 'find-up';
import picocolors from 'picocolors';
import { coerce, satisfies } from 'semver';
import stripJsonComments from 'strip-json-comments';
Expand Down Expand Up @@ -259,7 +261,7 @@ export async function adjustTemplate(templatePath: string, templateData: Record<
}

export async function isNxProject() {
return find.up('nx.json', { last: getProjectRoot() });
return findUpSync('nx.json', { stopAt: getProjectRoot() });
}

export function coerceSemver(version: string) {
Expand Down
11 changes: 8 additions & 3 deletions code/core/src/common/js-package-manager/BUNProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'node:path';
import { logger } from 'storybook/internal/node-logger';
import { FindPackageVersionsError } from 'storybook/internal/server-errors';

import * as find from 'empathic/find';
import { findUpSync } from 'find-up';
import sort from 'semver/functions/sort.js';

import { getProjectRoot } from '../utils/paths';
Expand Down Expand Up @@ -85,8 +85,13 @@ export class BUNProxy extends JsPackageManager {
}

public async getModulePackageJSON(packageName: string): Promise<PackageJson | null> {
const wantedPath = join('node_modules', packageName, 'package.json');
const packageJsonPath = find.up(wantedPath, { cwd: this.cwd, last: getProjectRoot() });
const packageJsonPath = findUpSync(
(dir) => {
const possiblePath = join(dir, 'node_modules', packageName, 'package.json');
return existsSync(possiblePath) ? possiblePath : undefined;
},
{ cwd: this.cwd, stopAt: getProjectRoot() }
);

if (!packageJsonPath) {
return null;
Expand Down
Loading
Loading