Skip to content

fix: replace traversal packages with empathic #6576

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 14 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/build-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@bugsnag/js": "^8.0.0",
"@iarna/toml": "^2.2.5",
"dot-prop": "^9.0.0",
"find-up": "^7.0.0",
"empathic": "^2.0.0",
"minimatch": "^9.0.0",
"read-pkg": "^9.0.0",
"semver": "^7.3.8",
Expand Down
48 changes: 43 additions & 5 deletions packages/build-info/src/node/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { promises as fs } from 'fs'
import { basename, dirname, isAbsolute, join, relative, resolve } from 'path'

import { findUp, findUpMultiple } from 'find-up'
import { up as walkUp } from 'empathic/walk'

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

Expand Down Expand Up @@ -68,12 +68,50 @@ export class NodeFS extends FileSystem {
}

/** Node implementation of finding a file or directory by walking up parent directories. */
findUp(name: string | readonly string[], options: findUpOptions = {}): Promise<string | undefined> {
return findUp(name, options)
async findUp(name: string | string[], options: findUpOptions = {}): Promise<string | undefined> {
const walkOptions = {
cwd: options.cwd,
last: options.stopAt,
}
const names = typeof name === 'string' ? [name] : name
for (const dir of walkUp('.', walkOptions)) {
for (const potentialName of names) {
const filePath = join(dir, potentialName)
try {
const stats = await fs.stat(filePath)
const type = stats.isFile() ? 'file' : 'directory'
if (options.type === type || !options.type) {
return filePath
}
} catch {
// ignore
}
}
}
}

/** Node implementation of finding files or directories by walking up parent directories. */
findUpMultiple(name: string | readonly string[], options: findUpOptions = {}): Promise<string[]> {
return findUpMultiple(name, options)
async findUpMultiple(name: string | readonly string[], options: findUpOptions = {}): Promise<string[]> {
const results: string[] = []
const normalisedNames = typeof name === 'string' ? [name] : name
const walkOptions = {
cwd: options.cwd,
last: options.stopAt,
}
for (const dir of walkUp(options.cwd ?? '.', walkOptions)) {
for (const potentialName of normalisedNames) {
const filePath = join(dir, potentialName)
try {
const stats = await fs.stat(filePath)
const type = stats.isFile() ? 'file' : 'directory'
if (options.type === type || !options.type) {
results.push(filePath)
}
} catch {
// ignore
}
}
}
return results
}
}
2 changes: 1 addition & 1 deletion packages/build-info/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
setupFiles: ['tests/test-setup.ts'],
deps: {
// this is to work inside memfs as well
inline: ['find-up', 'locate-path'],
inline: ['empathic', 'locate-path'],
},
},
})
2 changes: 1 addition & 1 deletion packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"ansi-escapes": "^7.0.0",
"ansis": "^4.1.0",
"clean-stack": "^5.0.0",
"empathic": "^2.0.0",
"execa": "^8.0.0",
"fdir": "^6.0.1",
"figures": "^6.0.0",
Expand All @@ -98,7 +99,6 @@
"p-locate": "^6.0.0",
"p-map": "^7.0.0",
"p-reduce": "^3.0.0",
"package-directory": "^8.0.0",
"path-exists": "^5.0.0",
"pretty-ms": "^9.0.0",
"ps-list": "^8.0.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/build/src/install/local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { packageDirectory } from 'package-directory'
import { dirname } from 'path'

import { up as packagePath } from 'empathic/package'

import { logInstallLocalPluginsDeps } from '../log/messages/install.js'

Expand Down Expand Up @@ -57,6 +59,7 @@ const hasPackageDir = function ({ packageDir }) {

// We only install dependencies of local plugins that have their own `package.json`
const removeMainRoot = async function (localPluginsOptions, buildDir) {
const mainPackageDir = await packageDirectory({ cwd: buildDir })
const mainPackagePath = packagePath({ cwd: buildDir })
const mainPackageDir = mainPackagePath ? dirname(mainPackagePath) : undefined
return localPluginsOptions.filter(({ packageDir }) => packageDir !== mainPackageDir)
}
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@
"cron-parser": "^4.1.0",
"deepmerge": "^4.2.2",
"dot-prop": "^9.0.0",
"empathic": "^2.0.0",
"execa": "^8.0.0",
"fast-safe-stringify": "^2.0.7",
"figures": "^6.0.0",
"filter-obj": "^6.0.0",
"find-up": "^7.0.0",
"indent-string": "^5.0.0",
"is-plain-obj": "^4.0.0",
"map-obj": "^5.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/options/repository_root.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname } from 'path'

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

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

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

if (repositoryRootA === undefined) {
return cwd
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync } from 'fs'
import { join, resolve } from 'path'

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

const FILENAME = 'netlify.toml'
Expand Down
6 changes: 3 additions & 3 deletions packages/edge-bundler/node/npm_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath, pathToFileURL } from 'url'

import { resolve, ParsedImportMap } from '@import-maps/resolve'
import { build } from 'esbuild'
import { findUp } from 'find-up'
import { up as findUp } from 'empathic/find'
import { parseImports } from 'parse-imports'
import tmp from 'tmp-promise'

Expand Down Expand Up @@ -39,7 +39,7 @@ const getTypePathFromTypesPackage = async (
packageName: string,
packageJsonPath: string,
): Promise<string | undefined> => {
const typesPackagePath = await findUp(`node_modules/${getTypesPackageName(packageName)}/package.json`, {
const typesPackagePath = findUp(`node_modules/${getTypesPackageName(packageName)}/package.json`, {
cwd: packageJsonPath,
})
if (!typesPackagePath) {
Expand Down Expand Up @@ -80,7 +80,7 @@ function packageName(specifier: string) {

const safelyDetectTypes = async (pkg: string, basePath: string): Promise<string | undefined> => {
try {
const json = await findUp(`node_modules/${packageName(pkg)}/package.json`, {
const json = findUp(`node_modules/${packageName(pkg)}/package.json`, {
cwd: basePath,
})
if (json) {
Expand Down
14 changes: 4 additions & 10 deletions packages/edge-bundler/node/package_json.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { readFileSync } from 'fs'
import { join } from 'path'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'

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

const getPackagePath = () => {
const packagePath = findUpSync(
(directory: string) => {
if (pathExistsSync(join(directory, 'package.json'))) {
return directory
}
},
{ cwd: fileURLToPath(import.meta.url), type: 'directory' },
)
const packageJsonPath = findUp('package.json', { cwd: fileURLToPath(import.meta.url) })
const packagePath = packageJsonPath ? dirname(packageJsonPath) : undefined

// We should never get here, but let's show a somewhat useful error message.
if (packagePath === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion packages/edge-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"ajv-errors": "^3.0.0",
"better-ajv-errors": "^1.2.0",
"common-path-prefix": "^3.0.0",
"empathic": "^2.0.0",
"env-paths": "^3.0.0",
"esbuild": "0.25.6",
"execa": "^8.0.0",
"find-up": "^7.0.0",
"get-port": "^7.0.0",
"node-stream-zip": "^1.15.0",
"p-retry": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/zip-it-and-ship-it/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"es-module-lexer": "^1.0.0",
"esbuild": "0.25.6",
"execa": "^8.0.0",
"empathic": "^2.0.0",
"fast-glob": "^3.3.3",
"filter-obj": "^6.0.0",
"find-up": "^7.0.0",
"is-path-inside": "^4.0.0",
"junk": "^4.0.0",
"locate-path": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRequire } from 'module'
import { version as nodeVersion } from 'process'

import { findUp } from 'find-up'
import { up as walkUp } from 'empathic/walk'
import { pathExists } from 'path-exists'
// @ts-expect-error doesnt export async
import { async as asyncResolve } from 'resolve'
Expand Down Expand Up @@ -94,7 +94,12 @@ const resolvePathFollowSymlinks = function (path: string, baseDirs: string[]) {
// unlikely, and we don't have any better alternative.
const resolvePackageFallback = async function (moduleName: string, baseDirs: string[], error: Error) {
const mainFilePath = resolvePathFollowSymlinks(moduleName, baseDirs)
const packagePath = await findUp(isPackageDir.bind(null, moduleName), { cwd: mainFilePath, type: 'directory' })
let packagePath: string | undefined
for (const dir of walkUp(mainFilePath)) {
if (await isPackageDir(moduleName, dir)) {
packagePath = dir
}
}

if (packagePath === undefined) {
throw error
Expand Down
Loading
Loading