Skip to content

Commit 9bfee86

Browse files
authoredFeb 5, 2024
perf: replace glob with fast-glob (#2280)
* perf: replace glob with fast-glob * chore: fix tests * Use regular glob for build/link scripts
1 parent 7c05c1d commit 9bfee86

17 files changed

+141
-104
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
},
2424
"devDependencies": {
2525
"@babel/core": "^7.0.0",
26+
"@babel/eslint-parser": "^7.12.0",
2627
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
2728
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
2829
"@babel/plugin-transform-runtime": "^7.6.2",
2930
"@babel/preset-env": "^7.0.0",
3031
"@babel/preset-typescript": "^7.3.3",
31-
"@babel/eslint-parser": "^7.12.0",
3232
"@react-native-community/eslint-config": "^3.2.0",
3333
"@types/glob": "^7.1.1",
3434
"@types/jest": "^26.0.15",
@@ -44,6 +44,7 @@
4444
"eslint-plugin-ft-flow": "^2.0.1",
4545
"eslint-plugin-import": "^2.25.3",
4646
"execa": "^5.0.0",
47+
"fast-glob": "^3.3.2",
4748
"glob": "^7.1.3",
4849
"husky": "^8.0.2",
4950
"jest": "^26.6.2",

‎packages/cli-clean/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@react-native-community/cli-tools": "13.5.1",
1212
"chalk": "^4.1.2",
1313
"execa": "^5.0.0",
14-
"glob": "^7.1.3"
14+
"fast-glob": "^3.3.2"
1515
},
1616
"files": [
1717
"build",
@@ -20,7 +20,6 @@
2020
],
2121
"devDependencies": {
2222
"@react-native-community/cli-types": "13.5.1",
23-
"@types/glob": "^7.1.1",
2423
"@types/prompts": "^2.4.4"
2524
},
2625
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-clean",

‎packages/cli-clean/src/clean.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {existsSync as fileExists, rm} from 'fs';
66
import os from 'os';
77
import path from 'path';
88
import {promisify} from 'util';
9-
import glob from 'glob';
9+
import glob from 'fast-glob';
1010

1111
type Args = {
1212
include?: string;
@@ -38,15 +38,7 @@ function isDirectoryPattern(directory: string): boolean {
3838
export async function cleanDir(directory: string): Promise<void> {
3939
try {
4040
if (isDirectoryPattern(directory)) {
41-
const directories = await new Promise<string[]>((resolve, reject) => {
42-
glob(directory, {}, (err, foundDirectories) => {
43-
if (err) {
44-
reject(err);
45-
} else {
46-
resolve(foundDirectories);
47-
}
48-
});
49-
});
41+
const directories = await glob.async(directory, {onlyFiles: false});
5042

5143
for (const dir of directories) {
5244
await rmAsync(dir, rmAsyncOptions);

‎packages/cli-config/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"chalk": "^4.1.2",
1313
"cosmiconfig": "^5.1.0",
1414
"deepmerge": "^4.3.0",
15-
"glob": "^7.1.3",
15+
"fast-glob": "^3.3.2",
1616
"joi": "^17.2.1"
1717
},
1818
"files": [
@@ -22,8 +22,7 @@
2222
],
2323
"devDependencies": {
2424
"@react-native-community/cli-types": "13.5.1",
25-
"@types/cosmiconfig": "^5.0.3",
26-
"@types/glob": "^7.1.1"
25+
"@types/cosmiconfig": "^5.0.3"
2726
},
2827
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-config",
2928
"repository": {

‎packages/cli-platform-android/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"@react-native-community/cli-tools": "13.5.1",
1111
"chalk": "^4.1.2",
1212
"execa": "^5.0.0",
13+
"fast-glob": "^3.3.2",
1314
"fast-xml-parser": "^4.2.4",
14-
"glob": "^7.1.3",
1515
"logkitty": "^0.7.1"
1616
},
1717
"files": [
@@ -22,8 +22,7 @@
2222
],
2323
"devDependencies": {
2424
"@react-native-community/cli-types": "13.5.1",
25-
"@types/fs-extra": "^8.1.0",
26-
"@types/glob": "^7.1.1"
25+
"@types/fs-extra": "^8.1.0"
2726
},
2827
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-platform-android",
2928
"repository": {

‎packages/cli-platform-android/src/config/findComponentDescriptors.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import glob from 'glob';
3+
import glob from 'fast-glob';
44
import {extractComponentDescriptors} from './extractComponentDescriptors';
5+
import {unixifyPaths} from '@react-native-community/cli-tools';
56

67
export function findComponentDescriptors(packageRoot: string) {
78
const files = glob.sync('**/+(*.js|*.jsx|*.ts|*.tsx)', {
8-
cwd: packageRoot,
9-
nodir: true,
10-
ignore: '**/node_modules/**',
9+
cwd: unixifyPaths(packageRoot),
10+
onlyFiles: true,
11+
ignore: ['**/node_modules/**'],
1112
});
1213
const codegenComponent = files
1314
.map((filePath) =>

‎packages/cli-platform-android/src/config/findManifest.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
*
77
*/
88

9-
import glob from 'glob';
9+
import glob from 'fast-glob';
1010
import path from 'path';
11+
import {unixifyPaths} from '@react-native-community/cli-tools';
1112

1213
export default function findManifest(folder: string) {
1314
let manifestPaths = glob.sync(path.join('**', 'AndroidManifest.xml'), {
14-
cwd: folder,
15+
cwd: unixifyPaths(folder),
1516
ignore: [
1617
'node_modules/**',
1718
'**/build/**',

‎packages/cli-platform-android/src/config/findPackageClassName.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
*/
88

99
import fs from 'fs';
10-
import glob from 'glob';
10+
import glob from 'fast-glob';
1111
import path from 'path';
12+
import {unixifyPaths} from '@react-native-community/cli-tools';
1213

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

1617
const packages = files
1718
.map((filePath) => fs.readFileSync(path.join(folder, filePath), 'utf8'))

‎packages/cli-platform-apple/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
"@react-native-community/cli-tools": "13.5.1",
1111
"chalk": "^4.1.2",
1212
"execa": "^5.0.0",
13+
"fast-glob": "^3.3.2",
1314
"fast-xml-parser": "^4.0.12",
14-
"glob": "^7.1.3",
1515
"ora": "^5.4.1"
1616
},
1717
"devDependencies": {
1818
"@react-native-community/cli-types": "13.5.1",
19-
"@types/glob": "^7.1.1",
2019
"@types/lodash": "^4.14.149",
2120
"hasbin": "^1.2.3"
2221
},

‎packages/cli-platform-apple/src/config/__tests__/findPodfilePath.test.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,39 @@ jest.mock('fs');
77

88
const fs = require('fs');
99

10-
afterEach(() => {
11-
jest.resetAllMocks();
12-
});
13-
1410
describe('ios::findPodfilePath', () => {
11+
beforeAll(() => {
12+
fs.__setMockFilesystem({
13+
empty: {},
14+
flat: {
15+
...projects.project,
16+
},
17+
multiple: {
18+
bar: {
19+
...projects.project,
20+
},
21+
foo: {
22+
...projects.project,
23+
},
24+
},
25+
});
26+
});
27+
1528
it('returns null if there is no Podfile', () => {
16-
fs.__setMockFilesystem({});
17-
expect(findPodfilePath('/', 'ios')).toBeNull();
29+
expect(findPodfilePath('/empty', 'ios')).toBeNull();
1830
});
1931

2032
it('returns Podfile path if it exists', () => {
21-
fs.__setMockFilesystem(projects.project);
22-
expect(findPodfilePath('/', 'ios')).toContain('ios/Podfile');
33+
expect(findPodfilePath('/flat', 'ios')).toContain('ios/Podfile');
2334
});
2435

2536
it('prints a warning when multile Podfiles are found', () => {
2637
const warn = jest.spyOn(logger, 'warn').mockImplementation();
27-
fs.__setMockFilesystem({
28-
foo: projects.project,
29-
bar: projects.project,
30-
});
31-
expect(findPodfilePath('/', 'ios')).toContain('bar/ios/Podfile');
38+
expect(findPodfilePath('/multiple', 'ios')).toContain(
39+
'/multiple/bar/ios/Podfile',
40+
);
3241
expect(warn.mock.calls).toMatchSnapshot();
3342
});
3443

35-
it('igores Podfiles in Example folder', () => {});
44+
it('ignores Podfiles in Example folder', () => {});
3645
});

‎packages/cli-platform-apple/src/config/__tests__/findPodspec.test.ts

+32-25
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,48 @@ jest.mock('fs');
1414
const fs = require('fs');
1515

1616
describe('ios::findPodspec', () => {
17+
beforeAll(() => {
18+
fs.__setMockFilesystem({
19+
empty: {},
20+
flat: {
21+
'TestPod.podspec': 'empty',
22+
},
23+
multiple: {
24+
user: {
25+
PacketName: {
26+
'Another.podspec': 'empty',
27+
'PacketName.podspec': 'empty',
28+
},
29+
},
30+
},
31+
multiple2: {
32+
user: {
33+
packet: {
34+
'Another.podspec': 'empty',
35+
'PacketName.podspec': 'empty',
36+
},
37+
},
38+
},
39+
});
40+
});
41+
1742
it('returns null if there is not podspec file', () => {
18-
fs.__setMockFilesystem({});
19-
expect(findPodspec('')).toBeNull();
43+
expect(findPodspec('/empty')).toBeNull();
2044
});
2145

2246
it('returns podspec name if only one exists', () => {
23-
fs.__setMockFilesystem({
24-
'TestPod.podspec': 'empty',
25-
});
26-
expect(findPodspec('/')).toBe('/TestPod.podspec');
47+
expect(findPodspec('/flat')).toBe('/flat/TestPod.podspec');
2748
});
2849

2950
it('returns podspec name that match packet directory', () => {
30-
fs.__setMockFilesystem({
31-
user: {
32-
PacketName: {
33-
'Another.podspec': 'empty',
34-
'PacketName.podspec': 'empty',
35-
},
36-
},
37-
});
38-
expect(findPodspec('/user/PacketName')).toBe(
39-
'/user/PacketName/PacketName.podspec',
51+
expect(findPodspec('/multiple/user/PacketName')).toBe(
52+
'/multiple/user/PacketName/PacketName.podspec',
4053
);
4154
});
4255

4356
it('returns first podspec name if not match in directory', () => {
44-
fs.__setMockFilesystem({
45-
user: {
46-
packet: {
47-
'Another.podspec': 'empty',
48-
'PacketName.podspec': 'empty',
49-
},
50-
},
51-
});
52-
expect(findPodspec('/user/packet')).toBe('/user/packet/Another.podspec');
57+
expect(findPodspec('/multiple2/user/packet')).toBe(
58+
'/multiple2/user/packet/Another.podspec',
59+
);
5360
});
5461
});

‎packages/cli-platform-apple/src/config/__tests__/getProjectConfig.test.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,46 @@ jest.mock('fs');
1515
const fs = require('fs');
1616

1717
describe('ios::getProjectConfig', () => {
18-
it('returns `null` if Podfile was not found', () => {
19-
fs.__setMockFilesystem({});
20-
expect(projectConfig('/', {})).toBe(null);
21-
});
22-
it('returns an object with ios project configuration', () => {
18+
beforeAll(() => {
2319
fs.__setMockFilesystem({
24-
ios: {
25-
Podfile: '',
20+
empty: {},
21+
flat: {
22+
ios: {
23+
Podfile: '',
24+
},
25+
},
26+
multiple: {
27+
sample: {
28+
Podfile: '',
29+
},
30+
ios: {
31+
Podfile: '',
32+
},
33+
example: {
34+
Podfile: '',
35+
},
2636
},
2737
});
28-
expect(projectConfig('/', {})).toMatchInlineSnapshot(`
38+
});
39+
40+
it('returns `null` if Podfile was not found', () => {
41+
expect(projectConfig('/empty', {})).toBe(null);
42+
});
43+
it('returns an object with ios project configuration', () => {
44+
expect(projectConfig('/flat', {})).toMatchInlineSnapshot(`
2945
Object {
3046
"automaticPodsInstallation": undefined,
31-
"sourceDir": "/ios",
47+
"sourceDir": "/flat/ios",
3248
"watchModeCommandParams": undefined,
3349
"xcodeProject": null,
3450
}
3551
`);
3652
});
3753
it('returns correct configuration when multiple Podfile are present', () => {
38-
fs.__setMockFilesystem({
39-
sample: {
40-
Podfile: '',
41-
},
42-
ios: {
43-
Podfile: '',
44-
},
45-
example: {
46-
Podfile: '',
47-
},
48-
});
49-
expect(projectConfig('/', {})).toMatchInlineSnapshot(`
54+
expect(projectConfig('/multiple', {})).toMatchInlineSnapshot(`
5055
Object {
5156
"automaticPodsInstallation": undefined,
52-
"sourceDir": "/ios",
57+
"sourceDir": "/multiple/ios",
5358
"watchModeCommandParams": undefined,
5459
"xcodeProject": null,
5560
}

‎packages/cli-platform-apple/src/config/findAllPodfilePaths.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
import glob from 'glob';
8+
import glob from 'fast-glob';
9+
import {unixifyPaths} from '@react-native-community/cli-tools';
910

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

1314
export default function findAllPodfilePaths(cwd: string) {
1415
return glob.sync('**/Podfile', {
15-
cwd,
16+
cwd: unixifyPaths(cwd),
1617
ignore: GLOB_EXCLUDE_PATTERN,
1718
});
1819
}

‎packages/cli-platform-apple/src/config/findPodspec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import glob from 'glob';
1+
import glob from 'fast-glob';
22
import path from 'path';
3+
import {unixifyPaths} from '@react-native-community/cli-tools';
34

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

78
if (podspecs.length === 0) {
89
return null;

0 commit comments

Comments
 (0)
Please sign in to comment.