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

Build: Replace globby with fdir #19297

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion code/lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@
"envinfo": "^7.7.3",
"execa": "^5.0.0",
"express": "^4.17.1",
"fdir": "^5.2.0",
"find-up": "^5.0.0",
"fs-extra": "^9.0.1",
"get-port": "^5.1.1",
"globby": "^11.0.2",
"jscodeshift": "^0.13.1",
"leven": "^3.1.0",
"picomatch": "^2.3.1",
"prompts": "^2.4.0",
"puppeteer-core": "^2.1.1",
"read-pkg-up": "^7.0.1",
Expand Down
14 changes: 7 additions & 7 deletions code/lib/cli/scripts/generate-sb-packages-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
const { readJson, writeFile } = require('fs-extra');
const { exec } = require('child_process');
const path = require('path');
const globby = require('globby');
const FDir = require('fdir').fdir;
const semver = require('@storybook/semver');
const { default: dedent } = require('ts-dedent');

const rootDirectory = path.join(__dirname, '..', '..', '..');
const glob = new FDir()
.withFullPaths()
.exclude((p) => p.indexOf('node_modules') > -1)
.glob(`${rootDirectory}/@(frameworks|addons|lib|renderers|presets)/**/package.json`)
.crawl(rootDirectory);

const logger = console;

Expand All @@ -16,12 +21,7 @@ const run = async () => {

if (!semver.valid(updatedVersion)) throw new Error(`Invalid version: ${updatedVersion}`);

const storybookPackagesPaths = await globby(
`${rootDirectory}/@(frameworks|addons|lib|renderers|presets)/**/package.json`,
{
ignore: '**/node_modules/**/*',
}
);
const storybookPackagesPaths = glob.sync();
IanVS marked this conversation as resolved.
Show resolved Hide resolved

const packageToVersionMap = (
await Promise.all(
Expand Down
14 changes: 10 additions & 4 deletions code/lib/cli/src/warn.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import globby from 'globby';
import { fdir as FDir } from 'fdir';
import { logger } from '@storybook/node-logger';
import { warn } from './warn';

jest.mock('@storybook/node-logger');
jest.mock('globby');
const syncMock = jest.fn();
const crawlerMock = jest.fn().mockImplementation(() => {
return {
sync: syncMock,
};
});
jest.spyOn(FDir.prototype, 'crawl').mockImplementation(crawlerMock);

describe('warn', () => {
beforeEach(() => {
Expand All @@ -21,15 +27,15 @@ describe('warn', () => {

describe('when TypeScript is not installed as a dependency', () => {
it('should not warn if `.tsx?` files are not found', () => {
(globby.sync as jest.Mock).mockReturnValueOnce([]);
syncMock.mockReturnValueOnce({ files: 0 });
warn({
hasTSDependency: false,
});
expect(logger.warn).toHaveBeenCalledTimes(0);
});

it('should warn if `.tsx?` files are found', () => {
(globby.sync as jest.Mock).mockReturnValueOnce(['a.ts']);
syncMock.mockReturnValueOnce({ files: 1 });
warn({
hasTSDependency: false,
});
Expand Down
8 changes: 5 additions & 3 deletions code/lib/cli/src/warn.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import globby from 'globby';
import { fdir as FDir } from 'fdir';
import { logger } from '@storybook/node-logger';

interface Options {
hasTSDependency: boolean;
}

const crawler = new FDir().onlyCounts().glob('**/*.@(ts|tsx)', '!**/node_modules', '!**/*.d.ts');

export const warn = ({ hasTSDependency }: Options) => {
if (!hasTSDependency) {
const hasTSFiles = !!globby.sync(['**/*.@(ts|tsx)', '!**/node_modules', '!**/*.d.ts']).length;
if (hasTSFiles) {
const counts = crawler.crawl(process.cwd()).sync();
if ('files' in counts && counts.files > 0) {
logger.warn(
'We have detected TypeScript files in your project directory, however TypeScript is not listed as a project dependency.'
);
Expand Down
3 changes: 2 additions & 1 deletion code/lib/codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
"@storybook/csf-tools": "7.0.0-alpha.35",
"@storybook/node-logger": "7.0.0-alpha.35",
"cross-spawn": "^7.0.3",
"globby": "^11.0.2",
"fdir": "^5.2.0",
"jscodeshift": "^0.13.1",
"lodash": "^4.17.21",
"picomatch": "^2.3.1",
"prettier": ">=2.2.1 <=2.3.0",
"recast": "^0.19.0"
},
Expand Down
9 changes: 7 additions & 2 deletions code/lib/codemod/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import globby from 'globby';
import { fdir as FDir } from 'fdir';
import { sync as spawnSync } from 'cross-spawn';
import { jscodeshiftToPrettierParser } from './lib/utils';

Expand Down Expand Up @@ -53,7 +53,12 @@ export async function runCodemod(codemod, { glob, logger, dryRun, rename, parser
if (knownParser !== 'babel') inferredParser = extension;
}

const files = await globby([glob, '!**/node_modules', '!**/dist']);
const files = new FDir()
.withFullPaths()
.glob(glob)
.exclude((p) => p.includes('/node_modules') || p.includes('/dist'))
.crawl(process.cwd())
.sync();
IanVS marked this conversation as resolved.
Show resolved Hide resolved
logger.log(`=> Applying ${codemod}: ${files.length} files`);
if (!dryRun) {
const parserArgs = inferredParser ? ['--parser', inferredParser] : [];
Expand Down
3 changes: 2 additions & 1 deletion code/lib/core-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@
"compression": "^1.7.4",
"detect-port": "^1.3.0",
"express": "^4.17.1",
"fdir": "^5.2.0",
"fs-extra": "^9.0.1",
"global": "^4.4.0",
"globby": "^11.0.2",
"ip": "^2.0.0",
"lodash": "^4.17.21",
"node-fetch": "^2.6.7",
"open": "^8.4.0",
"picomatch": "^2.3.1",
"pretty-hrtime": "^1.0.3",
"prompts": "^2.4.0",
"read-pkg-up": "^7.0.1",
Expand Down
12 changes: 7 additions & 5 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs-extra';
import glob from 'globby';
import { fdir as FDir } from 'fdir';
import slash from 'slash';

import type {
Expand Down Expand Up @@ -90,10 +90,12 @@ export class StoryIndexGenerator {
this.specifiers.map(async (specifier) => {
const pathToSubIndex = {} as SpecifierStoriesCache;

const fullGlob = slash(
path.join(this.options.workingDir, specifier.directory, specifier.files)
);
const files = await glob(fullGlob);
const files = new FDir()
IanVS marked this conversation as resolved.
Show resolved Hide resolved
.withFullPaths()
.glob(slash(path.join(this.options.workingDir, specifier.directory, specifier.files)))
.crawl(slash(path.join(this.options.workingDir, specifier.directory)))
.sync() as string[];

files.sort().forEach((absolutePath: Path) => {
const ext = path.extname(absolutePath);
if (ext === '.storyshot') {
Expand Down
16 changes: 9 additions & 7 deletions code/lib/core-server/src/utils/watch-story-specifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Watchpack from 'watchpack';
import slash from 'slash';
import fs from 'fs';
import path from 'path';
import glob from 'globby';
import { fdir as FDir } from 'fdir';
import uniq from 'lodash/uniq';

import type { NormalizedStoriesSpecifier } from '@storybook/core-common';
Expand Down Expand Up @@ -66,16 +66,18 @@ export function watchStorySpecifiers(
.map(async (specifier) => {
// If `./path/to/dir` was added, check all files matching `./path/to/dir/**/*.stories.*`
// (where the last bit depends on `files`).
const dirGlob = path.join(
options.workingDir,
importPath,
const globDir = path.posix.join(options.workingDir, importPath);
const globPattern = path.posix.join(
'**',
// files can be e.g. '**/foo/*/*.js' so we just want the last bit,
// because the directoru could already be within the files part (e.g. './x/foo/bar')
// because the directory could already be within the files part (e.g. './x/foo/bar')
path.basename(specifier.files)
);
// glob only supports forward slashes
const files = await glob(dirGlob.replace(/\\/g, '/'));
const files = new FDir()
.withFullPaths()
.glob(globPattern)
.crawl(globDir)
.sync() as string[];
IanVS marked this conversation as resolved.
Show resolved Hide resolved

files.forEach((filePath) => {
const fileImportPath = toImportPath(
Expand Down
16 changes: 13 additions & 3 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7429,12 +7429,13 @@ __metadata:
envinfo: ^7.7.3
execa: ^5.0.0
express: ^4.17.1
fdir: ^5.2.0
find-up: ^5.0.0
fs-extra: ^9.0.1
get-port: ^5.1.1
globby: ^11.0.2
jscodeshift: ^0.13.1
leven: ^3.1.0
picomatch: ^2.3.1
prompts: ^2.4.0
puppeteer-core: ^2.1.1
read-pkg-up: ^7.0.1
Expand Down Expand Up @@ -7501,11 +7502,12 @@ __metadata:
"@storybook/csf-tools": 7.0.0-alpha.35
"@storybook/node-logger": 7.0.0-alpha.35
cross-spawn: ^7.0.3
globby: ^11.0.2
fdir: ^5.2.0
jest: ^26.6.3
jest-specific-snapshot: ^4.0.0
jscodeshift: ^0.13.1
lodash: ^4.17.21
picomatch: ^2.3.1
prettier: ">=2.2.1 <=2.3.0"
recast: ^0.19.0
typescript: ~4.6.3
Expand Down Expand Up @@ -7657,15 +7659,16 @@ __metadata:
compression: ^1.7.4
detect-port: ^1.3.0
express: ^4.17.1
fdir: ^5.2.0
fs-extra: ^9.0.1
global: ^4.4.0
globby: ^11.0.2
ip: ^2.0.0
jest-os-detection: ^1.3.1
jest-specific-snapshot: ^4.0.0
lodash: ^4.17.21
node-fetch: ^2.6.7
open: ^8.4.0
picomatch: ^2.3.1
pretty-hrtime: ^1.0.3
prompts: ^2.4.0
read-pkg-up: ^7.0.1
Expand Down Expand Up @@ -21083,6 +21086,13 @@ __metadata:
languageName: node
linkType: hard

"fdir@npm:^5.2.0":
version: 5.2.0
resolution: "fdir@npm:5.2.0"
checksum: 1b8997beb551bf7f919ecc62442ef48936f5cdd45ff1a4160fa64f37c6aae8be2bb061862edd669cff3033d09ceb435081505a3f2604edb3fdae983615e2ae11
languageName: node
linkType: hard

"fetch-blob@npm:^3.1.2, fetch-blob@npm:^3.1.4":
version: 3.2.0
resolution: "fetch-blob@npm:3.2.0"
Expand Down