Skip to content

Commit

Permalink
perf: replace glob with fast-glob (#2280)
Browse files Browse the repository at this point in the history
* perf: replace glob with fast-glob

* chore: fix tests

* Use regular glob for build/link scripts
  • Loading branch information
abejfehr authored Feb 5, 2024
1 parent 7c05c1d commit 9bfee86
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 104 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/eslint-parser": "^7.12.0",
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@babel/eslint-parser": "^7.12.0",
"@react-native-community/eslint-config": "^3.2.0",
"@types/glob": "^7.1.1",
"@types/jest": "^26.0.15",
Expand All @@ -44,6 +44,7 @@
"eslint-plugin-ft-flow": "^2.0.1",
"eslint-plugin-import": "^2.25.3",
"execa": "^5.0.0",
"fast-glob": "^3.3.2",
"glob": "^7.1.3",
"husky": "^8.0.2",
"jest": "^26.6.2",
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-clean/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@react-native-community/cli-tools": "13.5.1",
"chalk": "^4.1.2",
"execa": "^5.0.0",
"glob": "^7.1.3"
"fast-glob": "^3.3.2"
},
"files": [
"build",
Expand All @@ -20,7 +20,6 @@
],
"devDependencies": {
"@react-native-community/cli-types": "13.5.1",
"@types/glob": "^7.1.1",
"@types/prompts": "^2.4.4"
},
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-clean",
Expand Down
12 changes: 2 additions & 10 deletions packages/cli-clean/src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {existsSync as fileExists, rm} from 'fs';
import os from 'os';
import path from 'path';
import {promisify} from 'util';
import glob from 'glob';
import glob from 'fast-glob';

type Args = {
include?: string;
Expand Down Expand Up @@ -38,15 +38,7 @@ function isDirectoryPattern(directory: string): boolean {
export async function cleanDir(directory: string): Promise<void> {
try {
if (isDirectoryPattern(directory)) {
const directories = await new Promise<string[]>((resolve, reject) => {
glob(directory, {}, (err, foundDirectories) => {
if (err) {
reject(err);
} else {
resolve(foundDirectories);
}
});
});
const directories = await glob.async(directory, {onlyFiles: false});

for (const dir of directories) {
await rmAsync(dir, rmAsyncOptions);
Expand Down
5 changes: 2 additions & 3 deletions packages/cli-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"chalk": "^4.1.2",
"cosmiconfig": "^5.1.0",
"deepmerge": "^4.3.0",
"glob": "^7.1.3",
"fast-glob": "^3.3.2",
"joi": "^17.2.1"
},
"files": [
Expand All @@ -22,8 +22,7 @@
],
"devDependencies": {
"@react-native-community/cli-types": "13.5.1",
"@types/cosmiconfig": "^5.0.3",
"@types/glob": "^7.1.1"
"@types/cosmiconfig": "^5.0.3"
},
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-config",
"repository": {
Expand Down
5 changes: 2 additions & 3 deletions packages/cli-platform-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"@react-native-community/cli-tools": "13.5.1",
"chalk": "^4.1.2",
"execa": "^5.0.0",
"fast-glob": "^3.3.2",
"fast-xml-parser": "^4.2.4",
"glob": "^7.1.3",
"logkitty": "^0.7.1"
},
"files": [
Expand All @@ -22,8 +22,7 @@
],
"devDependencies": {
"@react-native-community/cli-types": "13.5.1",
"@types/fs-extra": "^8.1.0",
"@types/glob": "^7.1.1"
"@types/fs-extra": "^8.1.0"
},
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-platform-android",
"repository": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import glob from 'fast-glob';
import {extractComponentDescriptors} from './extractComponentDescriptors';
import {unixifyPaths} from '@react-native-community/cli-tools';

export function findComponentDescriptors(packageRoot: string) {
const files = glob.sync('**/+(*.js|*.jsx|*.ts|*.tsx)', {
cwd: packageRoot,
nodir: true,
ignore: '**/node_modules/**',
cwd: unixifyPaths(packageRoot),
onlyFiles: true,
ignore: ['**/node_modules/**'],
});
const codegenComponent = files
.map((filePath) =>
Expand Down
5 changes: 3 additions & 2 deletions packages/cli-platform-android/src/config/findManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
*
*/

import glob from 'glob';
import glob from 'fast-glob';
import path from 'path';
import {unixifyPaths} from '@react-native-community/cli-tools';

export default function findManifest(folder: string) {
let manifestPaths = glob.sync(path.join('**', 'AndroidManifest.xml'), {
cwd: folder,
cwd: unixifyPaths(folder),
ignore: [
'node_modules/**',
'**/build/**',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/

import fs from 'fs';
import glob from 'glob';
import glob from 'fast-glob';
import path from 'path';
import {unixifyPaths} from '@react-native-community/cli-tools';

export default function getPackageClassName(folder: string) {
const files = glob.sync('**/+(*.java|*.kt)', {cwd: folder});
const files = glob.sync('**/+(*.java|*.kt)', {cwd: unixifyPaths(folder)});

const packages = files
.map((filePath) => fs.readFileSync(path.join(folder, filePath), 'utf8'))
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-platform-apple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
"@react-native-community/cli-tools": "13.5.1",
"chalk": "^4.1.2",
"execa": "^5.0.0",
"fast-glob": "^3.3.2",
"fast-xml-parser": "^4.0.12",
"glob": "^7.1.3",
"ora": "^5.4.1"
},
"devDependencies": {
"@react-native-community/cli-types": "13.5.1",
"@types/glob": "^7.1.1",
"@types/lodash": "^4.14.149",
"hasbin": "^1.2.3"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,39 @@ jest.mock('fs');

const fs = require('fs');

afterEach(() => {
jest.resetAllMocks();
});

describe('ios::findPodfilePath', () => {
beforeAll(() => {
fs.__setMockFilesystem({
empty: {},
flat: {
...projects.project,
},
multiple: {
bar: {
...projects.project,
},
foo: {
...projects.project,
},
},
});
});

it('returns null if there is no Podfile', () => {
fs.__setMockFilesystem({});
expect(findPodfilePath('/', 'ios')).toBeNull();
expect(findPodfilePath('/empty', 'ios')).toBeNull();
});

it('returns Podfile path if it exists', () => {
fs.__setMockFilesystem(projects.project);
expect(findPodfilePath('/', 'ios')).toContain('ios/Podfile');
expect(findPodfilePath('/flat', 'ios')).toContain('ios/Podfile');
});

it('prints a warning when multile Podfiles are found', () => {
const warn = jest.spyOn(logger, 'warn').mockImplementation();
fs.__setMockFilesystem({
foo: projects.project,
bar: projects.project,
});
expect(findPodfilePath('/', 'ios')).toContain('bar/ios/Podfile');
expect(findPodfilePath('/multiple', 'ios')).toContain(
'/multiple/bar/ios/Podfile',
);
expect(warn.mock.calls).toMatchSnapshot();
});

it('igores Podfiles in Example folder', () => {});
it('ignores Podfiles in Example folder', () => {});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,48 @@ jest.mock('fs');
const fs = require('fs');

describe('ios::findPodspec', () => {
beforeAll(() => {
fs.__setMockFilesystem({
empty: {},
flat: {
'TestPod.podspec': 'empty',
},
multiple: {
user: {
PacketName: {
'Another.podspec': 'empty',
'PacketName.podspec': 'empty',
},
},
},
multiple2: {
user: {
packet: {
'Another.podspec': 'empty',
'PacketName.podspec': 'empty',
},
},
},
});
});

it('returns null if there is not podspec file', () => {
fs.__setMockFilesystem({});
expect(findPodspec('')).toBeNull();
expect(findPodspec('/empty')).toBeNull();
});

it('returns podspec name if only one exists', () => {
fs.__setMockFilesystem({
'TestPod.podspec': 'empty',
});
expect(findPodspec('/')).toBe('/TestPod.podspec');
expect(findPodspec('/flat')).toBe('/flat/TestPod.podspec');
});

it('returns podspec name that match packet directory', () => {
fs.__setMockFilesystem({
user: {
PacketName: {
'Another.podspec': 'empty',
'PacketName.podspec': 'empty',
},
},
});
expect(findPodspec('/user/PacketName')).toBe(
'/user/PacketName/PacketName.podspec',
expect(findPodspec('/multiple/user/PacketName')).toBe(
'/multiple/user/PacketName/PacketName.podspec',
);
});

it('returns first podspec name if not match in directory', () => {
fs.__setMockFilesystem({
user: {
packet: {
'Another.podspec': 'empty',
'PacketName.podspec': 'empty',
},
},
});
expect(findPodspec('/user/packet')).toBe('/user/packet/Another.podspec');
expect(findPodspec('/multiple2/user/packet')).toBe(
'/multiple2/user/packet/Another.podspec',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,46 @@ jest.mock('fs');
const fs = require('fs');

describe('ios::getProjectConfig', () => {
it('returns `null` if Podfile was not found', () => {
fs.__setMockFilesystem({});
expect(projectConfig('/', {})).toBe(null);
});
it('returns an object with ios project configuration', () => {
beforeAll(() => {
fs.__setMockFilesystem({
ios: {
Podfile: '',
empty: {},
flat: {
ios: {
Podfile: '',
},
},
multiple: {
sample: {
Podfile: '',
},
ios: {
Podfile: '',
},
example: {
Podfile: '',
},
},
});
expect(projectConfig('/', {})).toMatchInlineSnapshot(`
});

it('returns `null` if Podfile was not found', () => {
expect(projectConfig('/empty', {})).toBe(null);
});
it('returns an object with ios project configuration', () => {
expect(projectConfig('/flat', {})).toMatchInlineSnapshot(`
Object {
"automaticPodsInstallation": undefined,
"sourceDir": "/ios",
"sourceDir": "/flat/ios",
"watchModeCommandParams": undefined,
"xcodeProject": null,
}
`);
});
it('returns correct configuration when multiple Podfile are present', () => {
fs.__setMockFilesystem({
sample: {
Podfile: '',
},
ios: {
Podfile: '',
},
example: {
Podfile: '',
},
});
expect(projectConfig('/', {})).toMatchInlineSnapshot(`
expect(projectConfig('/multiple', {})).toMatchInlineSnapshot(`
Object {
"automaticPodsInstallation": undefined,
"sourceDir": "/ios",
"sourceDir": "/multiple/ios",
"watchModeCommandParams": undefined,
"xcodeProject": null,
}
Expand Down
5 changes: 3 additions & 2 deletions packages/cli-platform-apple/src/config/findAllPodfilePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* LICENSE file in the root directory of this source tree.
*
*/
import glob from 'glob';
import glob from 'fast-glob';
import {unixifyPaths} from '@react-native-community/cli-tools';

// These folders will be excluded from search to speed it up
const GLOB_EXCLUDE_PATTERN = ['**/@(Pods|node_modules|Carthage|vendor)/**'];

export default function findAllPodfilePaths(cwd: string) {
return glob.sync('**/Podfile', {
cwd,
cwd: unixifyPaths(cwd),
ignore: GLOB_EXCLUDE_PATTERN,
});
}
5 changes: 3 additions & 2 deletions packages/cli-platform-apple/src/config/findPodspec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import glob from 'glob';
import glob from 'fast-glob';
import path from 'path';
import {unixifyPaths} from '@react-native-community/cli-tools';

export default function findPodspec(folder: string): string | null {
const podspecs = glob.sync('*.podspec', {cwd: folder});
const podspecs = glob.sync('*.podspec', {cwd: unixifyPaths(folder)});

if (podspecs.length === 0) {
return null;
Expand Down
Loading

0 comments on commit 9bfee86

Please sign in to comment.