Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 26, 2021
1 parent 64d2422 commit e93cfd2
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 342 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12

This comment has been minimized.

Copy link
@AviVahl

AviVahl Aug 26, 2021

@sindresorhus you may want to turn 12 and 14 back on. I don't think node:path works on 12...

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Aug 26, 2021

Author Owner

It does work. That's not why I removed these. I removed them because of a bug in the linter: xojs/xo#555

This comment has been minimized.

Copy link
@kachkaev
- 10
- 16
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
277 changes: 140 additions & 137 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,138 +1,141 @@
/* eslint-disable @typescript-eslint/unified-signatures */
import {Options as LocatePathOptions} from 'locate-path';

declare const stop: unique symbol;

declare namespace findUp {
interface Options extends LocatePathOptions {}

type StopSymbol = typeof stop;

type Match = string | StopSymbol | undefined;
}

declare const findUp: {
sync: {
/**
Synchronously check if a path exists.
@param path - Path to the file or directory.
@returns Whether the path exists.
@example
```
import findUp = require('find-up');
console.log(findUp.sync.exists('/Users/sindresorhus/unicorn.png'));
//=> true
```
*/
exists: (path: string) => boolean;

/**
Synchronously find a file or directory by walking up parent directories.
@param name - Name of the file or directory to find. Can be multiple.
@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
*/
(name: string | readonly string[], options?: findUp.Options): string | undefined;

/**
Synchronously find a file or directory by walking up parent directories.
@param matcher - Called for each directory in the search. Return a path or `findUp.stop` to stop the search.
@returns The first path found or `undefined` if none could be found.
@example
```
import path = require('path');
import findUp = require('find-up');
console.log(findUp.sync(directory => {
const hasUnicorns = findUp.sync.exists(path.join(directory, 'unicorn.png'));
return hasUnicorns && directory;
}, {type: 'directory'}));
//=> '/Users/sindresorhus'
```
*/
(matcher: (directory: string) => findUp.Match, options?: findUp.Options): string | undefined;
};

/**
Check if a path exists.
@param path - Path to a file or directory.
@returns Whether the path exists.
@example
```
import findUp = require('find-up');
(async () => {
console.log(await findUp.exists('/Users/sindresorhus/unicorn.png'));
//=> true
})();
```
*/
exists: (path: string) => Promise<boolean>;

/**
Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`.
*/
readonly stop: findUp.StopSymbol;

/**
Find a file or directory by walking up parent directories.
@param name - Name of the file or directory to find. Can be multiple.
@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
@example
```
// /
// └── Users
// └── sindresorhus
// ├── unicorn.png
// └── foo
// └── bar
// ├── baz
// └── example.js
// example.js
import findUp = require('find-up');
(async () => {
console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
})();
```
*/
(name: string | readonly string[], options?: findUp.Options): Promise<string | undefined>;

/**
Find a file or directory by walking up parent directories.
@param matcher - Called for each directory in the search. Return a path or `findUp.stop` to stop the search.
@returns The first path found or `undefined` if none could be found.
@example
```
import path = require('path');
import findUp = require('find-up');
(async () => {
console.log(await findUp(async directory => {
const hasUnicorns = await findUp.exists(path.join(directory, 'unicorn.png'));
return hasUnicorns && directory;
}, {type: 'directory'}));
//=> '/Users/sindresorhus'
})();
```
*/
(matcher: (directory: string) => (findUp.Match | Promise<findUp.Match>), options?: findUp.Options): Promise<string | undefined>;
};

export = findUp;
import {Options} from 'locate-path';

/**
Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`.
*/
export const findUpStop: unique symbol;

export type Match = string | typeof findUpStop | undefined;

/**
Find a file or directory by walking up parent directories.
@param name - The name of the file or directory to find. Can be multiple.
@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
@example
```
// /
// └── Users
// └── sindresorhus
// ├── unicorn.png
// └── foo
// └── bar
// ├── baz
// └── example.js
// example.js
import {findUp} from 'find-up';
console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
```
*/
export function findUp(name: string | readonly string[], options?: Options): Promise<string | undefined>;

/**
Find a file or directory by walking up parent directories.
@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
@returns The first path found or `undefined` if none could be found.
@example
```
import path from 'node:path';
import {findUp, pathExists} from 'find-up';
console.log(await findUp(async directory => {
const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
return hasUnicorns && directory;
}, {type: 'directory'}));
//=> '/Users/sindresorhus'
```
*/
export function findUp(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string | undefined>;

/**
Synchronously find a file or directory by walking up parent directories.
@param name - The name of the file or directory to find. Can be multiple.
@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
@example
```
// /
// └── Users
// └── sindresorhus
// ├── unicorn.png
// └── foo
// └── bar
// ├── baz
// └── example.js
// example.js
import {findUpSync} from 'find-up';
console.log(findUpSync('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(findUpSync(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
```
*/
export function findUpSync(name: string | readonly string[], options?: Options): string | undefined;

/**
Synchronously find a file or directory by walking up parent directories.
@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
@returns The first path found or `undefined` if none could be found.
@example
```
import path from 'node:path';
import {findUpSync, pathExistsSync} from 'find-up';
console.log(findUpSync(directory => {
const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png'));
return hasUnicorns && directory;
}, {type: 'directory'}));
//=> '/Users/sindresorhus'
```
*/
export function findUpSync(matcher: (directory: string) => Match, options?: Options): string | undefined;

/**
Check if a path exists.
@param path - The path to a file or directory.
@returns Whether the path exists.
@example
```
import {pathExists} from 'find-up';
console.log(await pathExists('/Users/sindresorhus/unicorn.png'));
//=> true
```
*/
export function pathExists(path: string): Promise<boolean>;

/**
Synchronously check if a path exists.
@param path - Path to the file or directory.
@returns Whether the path exists.
@example
```
import {pathExistsSync} from 'find-up';
console.log(pathExistsSync('/Users/sindresorhus/unicorn.png'));
//=> true
```
*/
export function pathExistsSync(path: string): boolean;

export {Options} from 'locate-path';
37 changes: 17 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';
const path = require('path');
const locatePath = require('locate-path');
const pathExists = require('path-exists');
import path from 'node:path';
import {locatePath, locatePathSync} from 'locate-path';

const stop = Symbol('findUp.stop');
export const findUpStop = Symbol('findUpStop');

module.exports = async (name, options = {}) => {
export async function findUp(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const paths = [].concat(name);
const paths = [name].flat();

const runMatcher = async locateOptions => {
if (typeof name !== 'function') {
Expand All @@ -28,7 +26,7 @@ module.exports = async (name, options = {}) => {
// eslint-disable-next-line no-await-in-loop
const foundPath = await runMatcher({...options, cwd: directory});

if (foundPath === stop) {
if (foundPath === findUpStop) {
return;
}

Expand All @@ -42,21 +40,21 @@ module.exports = async (name, options = {}) => {

directory = path.dirname(directory);
}
};
}

module.exports.sync = (name, options = {}) => {
export function findUpSync(name, options = {}) {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);
const paths = [].concat(name);
const paths = [name].flat();

const runMatcher = locateOptions => {
if (typeof name !== 'function') {
return locatePath.sync(paths, locateOptions);
return locatePathSync(paths, locateOptions);
}

const foundPath = name(locateOptions.cwd);
if (typeof foundPath === 'string') {
return locatePath.sync([foundPath], locateOptions);
return locatePathSync([foundPath], locateOptions);
}

return foundPath;
Expand All @@ -66,7 +64,7 @@ module.exports.sync = (name, options = {}) => {
while (true) {
const foundPath = runMatcher({...options, cwd: directory});

if (foundPath === stop) {
if (foundPath === findUpStop) {
return;
}

Expand All @@ -80,10 +78,9 @@ module.exports.sync = (name, options = {}) => {

directory = path.dirname(directory);
}
};
}

module.exports.exists = pathExists;

module.exports.sync.exists = pathExists.sync;

module.exports.stop = stop;
export {
pathExists,
pathExistsSync,
} from 'path-exists';
Loading

0 comments on commit e93cfd2

Please sign in to comment.