-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Remove dependency upon
find-up
. (#5019)
- Loading branch information
Showing
16 changed files
with
143 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,6 @@ | |
}, | ||
"dependencies": { | ||
"cspell-glob": "workspace:*", | ||
"find-up": "^6.3.0" | ||
"find-up-simple": "^1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { describe, expect, test, vi } from 'vitest'; | ||
import path from 'node:path'; | ||
import { findUp } from './findUp.js'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const packageRoot = path.resolve(__dirname, '../../..'); | ||
const reposRoot = path.resolve(packageRoot, '../..'); | ||
const relPackage = path.relative(packageRoot, __dirname); | ||
|
||
const cwd = process.cwd(); | ||
|
||
describe('findUp', () => { | ||
test('should find the file in the current directory', async () => { | ||
const result = await findUp('README.md'); | ||
expect(result).toBe(path.resolve(packageRoot, 'README.md')); | ||
}); | ||
|
||
test('should find the `fixtures` in the current directory', async () => { | ||
const result = await findUp('fixtures', { type: 'directory' }); | ||
expect(result).toBe(path.resolve(packageRoot, './fixtures')); | ||
}); | ||
|
||
test('should find the directory in the current directory', async () => { | ||
const result = await findUp('fixtures', { type: 'directory' }); | ||
expect(result).toBe(path.resolve(packageRoot, './fixtures')); | ||
}); | ||
|
||
test('should stop searching at the specified directory', async () => { | ||
const result = await findUp('missing.txt', { stopAt: reposRoot }); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
test('should return undefined if the file or directory is not found', async () => { | ||
const result = await findUp('nonexistent.txt'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
test('using a predicate', async () => { | ||
const predicate = vi.fn((dir: string) => (dir === cwd ? dir : 'found')); | ||
const result = await findUp(predicate, { cwd: __dirname }); | ||
expect(result).toBe('found'); | ||
}); | ||
|
||
test.each` | ||
name | cwd | expected | ||
${'README.md'} | ${undefined} | ${path.resolve(packageRoot, 'README.md')} | ||
${'README.md'} | ${'..'} | ${path.resolve(reposRoot, 'README.md')} | ||
${path.basename(__filename)} | ${path.join(relPackage, 'deeper/and/deeper')} | ${__filename} | ||
${['fixtures', 'package.json']} | ${__dirname} | ${path.resolve(packageRoot, 'package.json')} | ||
`('findUp $name $cwd', async ({ name, cwd, expected }) => { | ||
const result = await findUp(name, { cwd }); | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import path from 'path'; | ||
import { stat } from 'node:fs/promises'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
interface FindUpOptions { | ||
cwd?: string; | ||
type?: 'file' | 'directory'; | ||
stopAt?: string; | ||
} | ||
|
||
type FindUpPredicate = (dir: string) => string | undefined | Promise<string | undefined>; | ||
|
||
export async function findUp( | ||
name: string | string[] | FindUpPredicate, | ||
options: FindUpOptions = {}, | ||
): Promise<string | undefined> { | ||
const { cwd = process.cwd(), type: entryType = 'file', stopAt } = options; | ||
let dir = path.resolve(toDirPath(cwd)); | ||
const root = path.parse(dir).root; | ||
const predicate = makePredicate(name, entryType); | ||
const stopAtDir = path.resolve(toDirPath(stopAt || root)); | ||
|
||
while (dir !== root && dir !== stopAtDir) { | ||
const found = await predicate(dir); | ||
if (found !== undefined) return found; | ||
dir = path.dirname(dir); | ||
} | ||
return undefined; | ||
} | ||
|
||
function makePredicate(name: string | string[] | FindUpPredicate, entryType: 'file' | 'directory'): FindUpPredicate { | ||
if (typeof name === 'function') return name; | ||
|
||
const checkStat = entryType === 'file' ? 'isFile' : 'isDirectory'; | ||
|
||
function checkName(dir: string, name: string) { | ||
const f = path.join(dir, name); | ||
return stat(f) | ||
.then((stats) => (stats[checkStat]() && f) || undefined) | ||
.catch(() => undefined); | ||
} | ||
|
||
if (!Array.isArray(name)) return (dir) => checkName(dir, name); | ||
|
||
return async (dir) => { | ||
const pending = name.map((n) => checkName(dir, n)); | ||
for (const p of pending) { | ||
const found = await p; | ||
if (found) return found; | ||
} | ||
return undefined; | ||
}; | ||
} | ||
|
||
function toDirPath(urlOrPath: string | URL) { | ||
return urlOrPath instanceof URL ? fileURLToPath(new URL('.', urlOrPath)) : urlOrPath; | ||
} |
Oops, something went wrong.