Skip to content
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

feat: add @stdlib/fs/resolve-parent-paths-by #2897

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
220 changes: 220 additions & 0 deletions lib/node_modules/@stdlib/fs/resolve-parent-paths-by/README.md
gururaj1512 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<!--

@license Apache-2.0

Copyright (c) 2021 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# resolveParentPathsBy

> Resolve paths from a set of paths according to a predicate function by walking parent directories.

<section class="usage">

## Usage

```javascript
var resolveParentPathsBy = require( '@stdlib/fs/resolve-parent-paths-by' );
```

<a name="resolve-parent-paths-by"></a>

#### resolveParentPathsBy( paths\[, options], predicate, clbk )

Asynchronously resolves paths from a set of paths according to a `predicate` function by walking parent directories.

```javascript
resolveParentPathsBy( [ 'package.json', 'package-lock.json' ], predicate, onPath );

function predicate( path, next ) {
var bool = ( /\/test\//.test( path ) === false );
next( null, bool );
}

function onPath( error, path ) {
if ( error ) {
throw error;
}
console.log( path );
// e.g., => '...'
}
```

The function accepts the following `options`:

- **dir**: base directory from which to begin walking. May be either an absolute path or a path relative to the current working directory.

- **mode**: mode of operation. The following modes are supported:

- `first`: return the first resolved path.
- `some`: return one or more paths resolved within the first directory level containing a match.
- `all`: return all resolved paths within the first directory level containing matches for all provided paths.
- `each`: independently return the first resolved path for each path at any directory level.

Default: `'all'`.

By default, the function begins walking from the current working directory. To specify an alternative directory, set the `dir` option.

```javascript
var opts = {
'dir': __dirname
};
resolveParentPathsBy( [ 'package.json' ], opts, predicate, onPath );

function predicate( path, next ) {
var bool = ( /\/test\//.test( path ) === false );
next( null, bool );
}

function onPath( error, path ) {
if ( error ) {
throw error;
}
console.log( path );
// e.g., => '...'
}
```

When invoked, the `predicate` function is provided two arguments:

- `path`: a resolved path.
- `next`: a callback which should be called once the `predicate` function has finished processing a resolved path.

If a `predicate` function calls the `next` callback with a `truthy` second argument, the function proceeds to next function and returns the resolved path.

If unable to resolve a path, the function returns `null` as the path result.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't return null, either an empty array or Array<null> in case of each mode. I would suggest being specific and moving this to the ## Notes section. I'll update resolve-parent-paths accordingly too.

cc: @kgryte


By default, the function requires that a directory contains matches for all provided paths before returning results. To specify an alternative operation mode, set the `mode` option.

```javascript
var opts = {
gururaj1512 marked this conversation as resolved.
Show resolved Hide resolved
'dir': __dirname,
'mode': 'first'
};
resolveParentPathsBy( [ 'package.json', 'package-lock.json' ], opts, predicate, onPaths );

function onPaths( error, paths ) {
if ( error ) {
throw error;
}
console.log( paths );
// => '...'
}
```

#### resolveParentPathsBy.sync( path\[, options], predicate )

Synchronously resolves paths from a set of paths according to a `predicate` function by walking parent directories.

```javascript
function predicate( path ) {
return ( /\/test\//.test( path ) === false );
}

var path = resolveParentPathsBy.sync( [ 'package.json', 'README.md' ], predicate );
// e.g., returns [ '...', '...' ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- In `some` mode, the return order of asynchronously resolved paths is not guaranteed.
- This implementation is **not** similar in functionality to core [`path.resolve`][node-core-path-resolve]. The latter performs string manipulation to generate a full path. This implementation walks parent directories to perform a **search**, thereby touching the file system. Accordingly, this implementation resolves a _real_ absolute file path and is intended for use when a target's location in a parent directory is unknown relative to a child directory; e.g., when wanting to find a package root from deep within a package directory.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

<!-- eslint-disable stdlib/no-dynamic-require -->

```javascript
var resolveParentPathsBy = require( '@stdlib/fs/resolve-parent-paths-by' );

var opts = {
'dir': __dirname
};

/* Sync */

function predicateSync( path ) {
return true;
}

var out = resolveParentPathsBy.sync( [ 'package.json', 'README.md' ], opts, predicateSync );
// returns [ '...', '...' ]

out = resolveParentPathsBy.sync( [ 'non_existent_basename/package.json' ], opts, predicateSync );
// => []

opts.mode = 'first';
out = resolveParentPathsBy.sync( [ 'non_existent_basename/package.json', 'package.json' ], opts, predicateSync );
// => returns [ '...' ]

/* Async */

function predicateAsync( path, next ) {
setTimeout( onTimeout, 0 );

function onTimeout() {
next( null, true );
}
}

function onPath( error, path ) {
if ( error ) {
throw error;
}
console.log( path );
}

resolveParentPathsBy( [ 'package.json', 'README.md' ], opts, predicateAsync, onPath );
resolveParentPathsBy( [ './../non_existent_path/package.json' ], predicateAsync, onPath );
gururaj1512 marked this conversation as resolved.
Show resolved Hide resolved
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[node-core-path-resolve]: https://nodejs.org/api/path.html#path_path_resolve_paths

<!-- </related-links> -->

</section>

<!-- /.links -->
Loading
Loading