From 17610cda76af975b7d141ad2778753833a008bd8 Mon Sep 17 00:00:00 2001 From: Abe Fehr Date: Sat, 27 Jan 2024 13:07:54 -0500 Subject: [PATCH] perf: replace glob with fast-glob --- package.json | 5 +-- packages/cli-clean/package.json | 3 +- packages/cli-clean/src/clean.ts | 12 +----- packages/cli-config/package.json | 5 +-- packages/cli-platform-android/package.json | 5 +-- .../src/config/findComponentDescriptors.ts | 8 ++-- .../src/config/findManifest.ts | 4 +- .../src/config/findPackageClassName.ts | 4 +- packages/cli-platform-apple/package.json | 3 +- .../src/config/findAllPodfilePaths.ts | 4 +- .../src/config/findPodspec.ts | 4 +- scripts/build.js | 6 +-- scripts/linkPackages.js | 4 +- scripts/update-metro.js | 38 ++++++++++--------- yarn.lock | 27 ++++++------- 15 files changed, 59 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index 2f9361d00..8abe9b427 100644 --- a/package.json +++ b/package.json @@ -23,14 +23,13 @@ }, "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", "@types/node": "^18.0.0", "@types/node-fetch": "^2.3.7", @@ -44,7 +43,7 @@ "eslint-plugin-ft-flow": "^2.0.1", "eslint-plugin-import": "^2.25.3", "execa": "^5.0.0", - "glob": "^7.1.3", + "fast-glob": "^3.3.2", "husky": "^8.0.2", "jest": "^26.6.2", "jest-circus": "^26.6.2", diff --git a/packages/cli-clean/package.json b/packages/cli-clean/package.json index 61ee3ced4..2d942ed50 100644 --- a/packages/cli-clean/package.json +++ b/packages/cli-clean/package.json @@ -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", @@ -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", diff --git a/packages/cli-clean/src/clean.ts b/packages/cli-clean/src/clean.ts index 5924ee347..fb0c74bff 100644 --- a/packages/cli-clean/src/clean.ts +++ b/packages/cli-clean/src/clean.ts @@ -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 fg from 'fast-glob'; type Args = { include?: string; @@ -38,15 +38,7 @@ function isDirectoryPattern(directory: string): boolean { export async function cleanDir(directory: string): Promise { try { if (isDirectoryPattern(directory)) { - const directories = await new Promise((resolve, reject) => { - glob(directory, {}, (err, foundDirectories) => { - if (err) { - reject(err); - } else { - resolve(foundDirectories); - } - }); - }); + const directories = await fg.async(directory, {onlyFiles: false}); for (const dir of directories) { await rmAsync(dir, rmAsyncOptions); diff --git a/packages/cli-config/package.json b/packages/cli-config/package.json index 95cb543c6..5c00aa7c6 100644 --- a/packages/cli-config/package.json +++ b/packages/cli-config/package.json @@ -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": [ @@ -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": { diff --git a/packages/cli-platform-android/package.json b/packages/cli-platform-android/package.json index 0e183c006..4af45eab2 100644 --- a/packages/cli-platform-android/package.json +++ b/packages/cli-platform-android/package.json @@ -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": [ @@ -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": { diff --git a/packages/cli-platform-android/src/config/findComponentDescriptors.ts b/packages/cli-platform-android/src/config/findComponentDescriptors.ts index 5335eb78f..1e0e70630 100644 --- a/packages/cli-platform-android/src/config/findComponentDescriptors.ts +++ b/packages/cli-platform-android/src/config/findComponentDescriptors.ts @@ -1,13 +1,13 @@ import fs from 'fs'; import path from 'path'; -import glob from 'glob'; +import fg from 'fast-glob'; import {extractComponentDescriptors} from './extractComponentDescriptors'; export function findComponentDescriptors(packageRoot: string) { - const files = glob.sync('**/+(*.js|*.jsx|*.ts|*.tsx)', { + const files = fg.sync('**/+(*.js|*.jsx|*.ts|*.tsx)', { cwd: packageRoot, - nodir: true, - ignore: '**/node_modules/**', + onlyFiles: true, + ignore: ['**/node_modules/**'], }); const codegenComponent = files .map((filePath) => diff --git a/packages/cli-platform-android/src/config/findManifest.ts b/packages/cli-platform-android/src/config/findManifest.ts index 9955d47d0..516d1d0c1 100644 --- a/packages/cli-platform-android/src/config/findManifest.ts +++ b/packages/cli-platform-android/src/config/findManifest.ts @@ -6,11 +6,11 @@ * */ -import glob from 'glob'; +import fg from 'fast-glob'; import path from 'path'; export default function findManifest(folder: string) { - let manifestPaths = glob.sync(path.join('**', 'AndroidManifest.xml'), { + let manifestPaths = fg.sync(path.join('**', 'AndroidManifest.xml'), { cwd: folder, ignore: [ 'node_modules/**', diff --git a/packages/cli-platform-android/src/config/findPackageClassName.ts b/packages/cli-platform-android/src/config/findPackageClassName.ts index 307c294a9..62f39a44d 100644 --- a/packages/cli-platform-android/src/config/findPackageClassName.ts +++ b/packages/cli-platform-android/src/config/findPackageClassName.ts @@ -7,11 +7,11 @@ */ import fs from 'fs'; -import glob from 'glob'; +import fg from 'fast-glob'; import path from 'path'; export default function getPackageClassName(folder: string) { - const files = glob.sync('**/+(*.java|*.kt)', {cwd: folder}); + const files = fg.sync('**/+(*.java|*.kt)', {cwd: folder}); const packages = files .map((filePath) => fs.readFileSync(path.join(folder, filePath), 'utf8')) diff --git a/packages/cli-platform-apple/package.json b/packages/cli-platform-apple/package.json index 2bb2cd478..e5cfd97d6 100644 --- a/packages/cli-platform-apple/package.json +++ b/packages/cli-platform-apple/package.json @@ -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" }, diff --git a/packages/cli-platform-apple/src/config/findAllPodfilePaths.ts b/packages/cli-platform-apple/src/config/findAllPodfilePaths.ts index 8ba86d1d2..08343d30a 100644 --- a/packages/cli-platform-apple/src/config/findAllPodfilePaths.ts +++ b/packages/cli-platform-apple/src/config/findAllPodfilePaths.ts @@ -5,13 +5,13 @@ * LICENSE file in the root directory of this source tree. * */ -import glob from 'glob'; +import fg 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', { + return fg.sync('**/Podfile', { cwd, ignore: GLOB_EXCLUDE_PATTERN, }); diff --git a/packages/cli-platform-apple/src/config/findPodspec.ts b/packages/cli-platform-apple/src/config/findPodspec.ts index c0d5e979a..184de8923 100644 --- a/packages/cli-platform-apple/src/config/findPodspec.ts +++ b/packages/cli-platform-apple/src/config/findPodspec.ts @@ -1,8 +1,8 @@ -import glob from 'glob'; +import fg from 'fast-glob'; import path from 'path'; export default function findPodspec(folder: string): string | null { - const podspecs = glob.sync('*.podspec', {cwd: folder}); + const podspecs = fg.sync('*.podspec', {cwd: folder}); if (podspecs.length === 0) { return null; diff --git a/scripts/build.js b/scripts/build.js index 112420ca0..10866be06 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -20,7 +20,7 @@ const fs = require('fs'); const path = require('path'); -const glob = require('glob'); +const fg = require('fast-glob'); const babel = require('@babel/core'); const chalk = require('chalk'); const micromatch = require('micromatch'); @@ -55,8 +55,8 @@ function getBuildPath(file, buildFolder) { function buildNodePackage(p) { const srcDir = path.resolve(p, SRC_DIR); const pattern = path.resolve(srcDir, '**/*'); - const files = glob.sync(pattern, { - nodir: true, + const files = fg.sync(pattern, { + onlyFiles: true, }); process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`)); diff --git a/scripts/linkPackages.js b/scripts/linkPackages.js index 407ffb0d0..dba37c9ca 100644 --- a/scripts/linkPackages.js +++ b/scripts/linkPackages.js @@ -1,9 +1,9 @@ const execa = require('execa'); const chalk = require('chalk'); const path = require('path'); -const glob = require('glob'); +const fg = require('fast-glob'); -const projects = glob.sync('packages/*/package.json'); +const projects = fg.sync('packages/*/package.json'); projects.forEach((project) => { const cwd = path.dirname(project); diff --git a/scripts/update-metro.js b/scripts/update-metro.js index f9ad62a7d..c7f5359a6 100644 --- a/scripts/update-metro.js +++ b/scripts/update-metro.js @@ -7,7 +7,7 @@ const fs = require('fs'); const path = require('path'); const cp = require('child_process'); -const glob = require('glob').sync; +const fg = require('fast-glob').sync; const chalk = require('chalk'); /** @@ -42,25 +42,27 @@ const updateDependencies = (depsObject) => { }; const start = new Date().getTime(); -['./package.json', ...glob('./packages/*/package.json')].forEach((pkgPath) => { - const pkg = require(path.join(process.cwd(), pkgPath)); +['./package.json', ...fg.sync('./packages/*/package.json')].forEach( + (pkgPath) => { + const pkg = require(path.join(process.cwd(), pkgPath)); - const updatedDependency = pkg.dependencies - ? Object.assign(pkg, { - dependencies: updateDependencies(pkg.dependencies), - }) - : pkg; - const updatedDevDependency = pkg.devDependencies - ? Object.assign(updatedDependency, { - devDependencies: updateDependencies(pkg.devDependencies), - }) - : updatedDependency; + const updatedDependency = pkg.dependencies + ? Object.assign(pkg, { + dependencies: updateDependencies(pkg.dependencies), + }) + : pkg; + const updatedDevDependency = pkg.devDependencies + ? Object.assign(updatedDependency, { + devDependencies: updateDependencies(pkg.devDependencies), + }) + : updatedDependency; - fs.writeFileSync( - pkgPath, - `${JSON.stringify(updatedDevDependency, null, 2)}\n`, - ); -}); + fs.writeFileSync( + pkgPath, + `${JSON.stringify(updatedDevDependency, null, 2)}\n`, + ); + }, +); const end = new Date().getTime(); const ellapsedTime = (end - start) / 1000; console.log(`✨ Done in ${ellapsedTime}s.`); diff --git a/yarn.lock b/yarn.lock index 485b8809b..e6cc3eb33 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2212,11 +2212,6 @@ dependencies: "@types/express" "*" -"@types/events@*": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" - integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== - "@types/express-serve-static-core@^4.17.33": version "4.17.33" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.33.tgz#de35d30a9d637dc1450ad18dd583d75d5733d543" @@ -2243,15 +2238,6 @@ dependencies: "@types/node" "*" -"@types/glob@^7.1.1": - version "7.1.1" - resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575" - integrity sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== - dependencies: - "@types/events" "*" - "@types/minimatch" "*" - "@types/node" "*" - "@types/graceful-fs@^4.1.2", "@types/graceful-fs@^4.1.3": version "4.1.3" resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f" @@ -2331,7 +2317,7 @@ resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d" integrity sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw== -"@types/minimatch@*", "@types/minimatch@^3.0.3": +"@types/minimatch@^3.0.3": version "3.0.5" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40" integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ== @@ -5620,6 +5606,17 @@ fast-glob@^3.2.9, fast-glob@^3.3.1: merge2 "^1.3.0" micromatch "^4.0.4" +fast-glob@^3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129" + integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow== + 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"