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

chore: replace glob with fast-glob #2016

Closed
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.3",
"execa": "^5.0.0",
"glob": "^7.1.3",
"fast-glob": "^3.3.0",
"husky": "^8.0.2",
"jest": "^26.6.2",
"jest-circus": "^26.6.2",
Expand Down
2 changes: 1 addition & 1 deletion 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.0",
"joi": "^17.2.1"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-platform-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@react-native-community/cli-tools": "12.0.0-alpha.7",
"chalk": "^4.1.2",
"execa": "^5.0.0",
"glob": "^7.1.3",
"fast-glob": "^3.3.0",
"logkitty": "^0.7.1"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import glob from 'fast-glob';
import {extractComponentDescriptors} from './extractComponentDescriptors';

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

import glob from 'glob';
import glob from 'fast-glob';
import path from 'path';

export default function findManifest(folder: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
*/

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
2 changes: 1 addition & 1 deletion packages/cli-platform-ios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"@react-native-community/cli-tools": "12.0.0-alpha.7",
"chalk": "^4.1.2",
"execa": "^5.0.0",
"fast-glob": "^3.3.0",
"fast-xml-parser": "^4.0.12",
"glob": "^7.1.3",
"ora": "^5.4.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ 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('/')).toBeNull();
expect(findPodfilePath('/empty')).toBeNull();
});

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

it('prints a warning when multile Podfiles are found', () => {
Expand All @@ -28,7 +39,7 @@ describe('ios::findPodfilePath', () => {
foo: projects.project,
bar: projects.project,
});
expect(findPodfilePath('/')).toContain('bar/ios/Podfile');
expect(findPodfilePath('/multiple')).toContain('/multiple/bar/ios/Podfile');
expect(warn.mock.calls).toMatchSnapshot();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,44 @@ 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',
},
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('/multiple/user/packet')).toBe(
'/multiple/user/packet/Another.podspec',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,44 @@ 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 {
"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 {
"sourceDir": "/ios",
"sourceDir": "/multiple/ios",
"watchModeCommandParams": undefined,
"xcodeProject": null,
}
Expand Down
6 changes: 4 additions & 2 deletions packages/cli-platform-ios/src/config/findAllPodfilePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* LICENSE file in the root directory of this source tree.
*
*/
import glob from 'glob';

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

// 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-ios/src/config/findPodspec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import glob from 'glob';
import {unixifyPaths} from '@react-native-community/cli-tools';
import glob from 'fast-glob';
import path from 'path';

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
1 change: 1 addition & 0 deletions packages/cli-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {default as hookStdout} from './hookStdout';
export {getLoader, NoopLoader, Loader} from './loader';
export {default as findProjectRoot} from './findProjectRoot';
export {default as printRunDoctorTip} from './printRunDoctorTip';
export {default as unixifyPaths} from './unixifyPaths';
export * as link from './doclink';

export * from './errors';
11 changes: 11 additions & 0 deletions packages/cli-tools/src/unixifyPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
*
* @param path string
* @returns string
*
* This function converts Windows paths to Unix paths.
*/

export default function unixifyPaths(path: string): string {
return path.replace(/^([a-zA-Z]+:|\.\/)/, '');
}
4 changes: 2 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

const fs = require('fs');
const path = require('path');
const glob = require('glob');
const glob = require('fast-glob');
const babel = require('@babel/core');
const chalk = require('chalk');
const micromatch = require('micromatch');
Expand Down Expand Up @@ -56,7 +56,7 @@ function buildNodePackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {
nodir: true,
onlyFiles: true,
});

process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`));
Expand Down
2 changes: 1 addition & 1 deletion scripts/linkPackages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const execa = require('execa');
const chalk = require('chalk');
const path = require('path');
const glob = require('glob');
const glob = require('fast-glob');

const projects = glob.sync('packages/*/package.json');

Expand Down
2 changes: 1 addition & 1 deletion scripts/update-metro.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const glob = require('glob').sync;
const glob = require('fast-glob').sync;
const chalk = require('chalk');

/**
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6286,6 +6286,17 @@ fast-glob@^3.1.1:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.0.tgz#7c40cb491e1e2ed5664749e87bfb516dbe8727c0"
integrity sha512-ChDuvbOypPuNjO8yIDf36x7BlZX1smcUMTTcyoIjycexOxd6DFsKsg21qVBzEmr3G7fUKIRy2/psii+CIUt7FA==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
Expand Down
Loading