Skip to content

Commit 961d9b2

Browse files
committed
chore: replace find-up with empathic
Replaces `find-up` with `empathic`, a faster library. We can follow this up with replacements of other similar dependencies duplicating this work, so everything is consolidated around `empathic`.
1 parent 068452d commit 961d9b2

File tree

14 files changed

+81
-49
lines changed

14 files changed

+81
-49
lines changed

package-lock.json

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/build-info/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@bugsnag/js": "^8.0.0",
4848
"@iarna/toml": "^2.2.5",
4949
"dot-prop": "^9.0.0",
50-
"find-up": "^7.0.0",
50+
"empathic": "^2.0.0",
5151
"minimatch": "^9.0.0",
5252
"read-pkg": "^9.0.0",
5353
"semver": "^7.3.8",

packages/build-info/src/node/file-system.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { promises as fs } from 'fs'
1+
import { promises as fs, existsSync } from 'fs'
22
import { basename, dirname, isAbsolute, join, relative, resolve } from 'path'
33

4-
import { findUp, findUpMultiple } from 'find-up'
4+
import { any as findUpAny, up as findUp } from 'empathic/find'
5+
import { up as walkUp } from 'empathic/walk'
56

67
import { DirType, Environment, FileSystem, findUpOptions } from '../file-system.js'
78

@@ -68,12 +69,25 @@ export class NodeFS extends FileSystem {
6869
}
6970

7071
/** Node implementation of finding a file or directory by walking up parent directories. */
71-
findUp(name: string | readonly string[], options: findUpOptions = {}): Promise<string | undefined> {
72-
return findUp(name, options)
72+
findUp(name: string | string[], options: findUpOptions = {}): Promise<string | undefined> {
73+
if (typeof name === 'string') {
74+
return Promise.resolve(findUp(name, options))
75+
}
76+
return Promise.resolve(findUpAny(name, options))
7377
}
7478

7579
/** Node implementation of finding files or directories by walking up parent directories. */
7680
findUpMultiple(name: string | readonly string[], options: findUpOptions = {}): Promise<string[]> {
77-
return findUpMultiple(name, options)
81+
const results: string[] = []
82+
const normalisedNames = typeof name === 'string' ? [name] : name;
83+
for (const dir of walkUp(options.cwd ?? '.', options)) {
84+
for (const potentialName of normalisedNames) {
85+
const filePath = join(dir, potentialName);
86+
if (existsSync(filePath)) {
87+
results.push(filePath);
88+
}
89+
}
90+
}
91+
return Promise.resolve(results)
7892
}
7993
}

packages/build-info/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineConfig({
1515
setupFiles: ['tests/test-setup.ts'],
1616
deps: {
1717
// this is to work inside memfs as well
18-
inline: ['find-up', 'locate-path'],
18+
inline: ['empathic', 'locate-path'],
1919
},
2020
},
2121
})

packages/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
"cron-parser": "^4.1.0",
6767
"deepmerge": "^4.2.2",
6868
"dot-prop": "^9.0.0",
69+
"empathic": "^2.0.0",
6970
"execa": "^8.0.0",
7071
"fast-safe-stringify": "^2.0.7",
7172
"figures": "^6.0.0",
7273
"filter-obj": "^6.0.0",
73-
"find-up": "^7.0.0",
7474
"indent-string": "^5.0.0",
7575
"is-plain-obj": "^4.0.0",
7676
"map-obj": "^5.0.0",

packages/config/src/options/repository_root.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { dirname } from 'path'
22

3-
import { findUp } from 'find-up'
3+
import { dir as findUp } from 'empathic/find'
44

55
// Find out repository root among (in priority order):
66
// - `repositoryRoot` option
@@ -11,7 +11,7 @@ export const getRepositoryRoot = async function ({ repositoryRoot, cwd }) {
1111
return repositoryRoot
1212
}
1313

14-
const repositoryRootA = await findUp('.git', { cwd, type: 'directory' })
14+
const repositoryRootA = findUp('.git', { cwd })
1515

1616
if (repositoryRootA === undefined) {
1717
return cwd

packages/config/src/path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync } from 'fs'
22
import { join, resolve } from 'path'
33

4-
import { findUp } from 'find-up'
4+
import { file as findUp } from 'empathic/find'
55
import pLocate from 'p-locate'
66

77
const FILENAME = 'netlify.toml'

packages/edge-bundler/node/npm_dependencies.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileURLToPath, pathToFileURL } from 'url'
55

66
import { resolve, ParsedImportMap } from '@import-maps/resolve'
77
import { build } from 'esbuild'
8-
import { findUp } from 'find-up'
8+
import { up as findUp } from 'empathic/find'
99
import { parseImports } from 'parse-imports'
1010
import tmp from 'tmp-promise'
1111

@@ -40,7 +40,7 @@ const getTypePathFromTypesPackage = async (
4040
packageName: string,
4141
packageJsonPath: string,
4242
): Promise<string | undefined> => {
43-
const typesPackagePath = await findUp(`node_modules/${getTypesPackageName(packageName)}/package.json`, {
43+
const typesPackagePath = findUp(`node_modules/${getTypesPackageName(packageName)}/package.json`, {
4444
cwd: packageJsonPath,
4545
})
4646
if (!typesPackagePath) {
@@ -81,7 +81,7 @@ function packageName(specifier: string) {
8181

8282
const safelyDetectTypes = async (pkg: string, basePath: string): Promise<string | undefined> => {
8383
try {
84-
const json = await findUp(`node_modules/${packageName(pkg)}/package.json`, {
84+
const json = findUp(`node_modules/${packageName(pkg)}/package.json`, {
8585
cwd: basePath,
8686
})
8787
if (json) {

packages/edge-bundler/node/package_json.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { readFileSync } from 'fs'
2-
import { join } from 'path'
2+
import { dirname, join } from 'path'
33
import { fileURLToPath } from 'url'
44

5-
import { findUpSync, pathExistsSync } from 'find-up'
5+
import { file as findUp } from 'empathic/find'
66

77
const getPackagePath = () => {
8-
const packagePath = findUpSync(
9-
(directory: string) => {
10-
if (pathExistsSync(join(directory, 'package.json'))) {
11-
return directory
12-
}
13-
},
14-
{ cwd: fileURLToPath(import.meta.url), type: 'directory' },
8+
const packageJsonPath = findUp(
9+
'package.json',
10+
{ cwd: dirname(fileURLToPath(import.meta.url)) },
1511
)
12+
const packagePath = packageJsonPath ? dirname(packageJsonPath) : undefined
1613

1714
// We should never get here, but let's show a somewhat useful error message.
1815
if (packagePath === undefined) {

packages/edge-bundler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
"ajv-errors": "^3.0.0",
6565
"better-ajv-errors": "^1.2.0",
6666
"common-path-prefix": "^3.0.0",
67+
"empathic": "^2.0.0",
6768
"env-paths": "^3.0.0",
6869
"esbuild": "0.25.6",
6970
"execa": "^8.0.0",
70-
"find-up": "^7.0.0",
7171
"get-package-name": "^2.2.0",
7272
"get-port": "^7.0.0",
7373
"is-path-inside": "^4.0.0",

0 commit comments

Comments
 (0)