|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# resolveParentPaths |
| 22 | + |
| 23 | +> Resolve paths from a set of paths by walking parent directories. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var resolveParentPaths = require( '@stdlib/fs/resolve-parent-paths' ); |
| 31 | +``` |
| 32 | + |
| 33 | +<a name="resolve-parent-paths"></a> |
| 34 | + |
| 35 | +#### resolveParentPaths( paths\[, options], clbk ) |
| 36 | + |
| 37 | +Asynchronously resolves paths from a set of paths by walking parent directories. |
| 38 | + |
| 39 | +```javascript |
| 40 | +resolveParentPaths( [ 'package.json', 'package-lock.json' ], onPaths ); |
| 41 | + |
| 42 | +function onPaths( error, paths ) { |
| 43 | + if ( error ) { |
| 44 | + throw error; |
| 45 | + } |
| 46 | + console.log( paths ); |
| 47 | + // => '...' |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +The function accepts the following `options`: |
| 52 | + |
| 53 | +- **dir**: base directory from which to begin walking. May be either an absolute path or a path relative to the current working directory. |
| 54 | + |
| 55 | +- **mode**: mode of operation. The following modes are supported: |
| 56 | + |
| 57 | + - `first`: return the first resolved path. |
| 58 | + - `some`: return one or more paths resolved within the first directory level containing a match. |
| 59 | + - `all`: return all resolved paths within the first directory level containing matches for all provided paths. |
| 60 | + - `each`: independently return the first resolved path for each path at any directory level. |
| 61 | + |
| 62 | + Default: `'all'`. |
| 63 | + |
| 64 | +By default, the function begins walking from the current working directory. To specify an alternative directory, set the `dir` option. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var opts = { |
| 68 | + 'dir': __dirname |
| 69 | +}; |
| 70 | +resolveParentPaths( [ 'package.json' ], opts, onPaths ); |
| 71 | + |
| 72 | +function onPaths( error, paths ) { |
| 73 | + if ( error ) { |
| 74 | + throw error; |
| 75 | + } |
| 76 | + console.log( paths ); |
| 77 | + // => '...' |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +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. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var opts = { |
| 85 | + 'dir': __dirname, |
| 86 | + 'mode': 'first' |
| 87 | +}; |
| 88 | +resolveParentPaths( [ 'package.json', 'package-lock.json' ], opts, onPaths ); |
| 89 | + |
| 90 | +function onPaths( error, paths ) { |
| 91 | + if ( error ) { |
| 92 | + throw error; |
| 93 | + } |
| 94 | + console.log( paths ); |
| 95 | + // => '...' |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +#### resolveParentPaths.sync( paths\[, options] ) |
| 100 | + |
| 101 | +Synchronously resolves paths from a set of paths by walking parent directories. |
| 102 | + |
| 103 | +```javascript |
| 104 | +var paths = resolveParentPaths.sync( [ 'package.json', 'README.md' ] ); |
| 105 | +// returns [ '...', '...' ] |
| 106 | +``` |
| 107 | + |
| 108 | +The function accepts the same `options` as [`resolveParentPaths()`](#resolve-parent-paths). |
| 109 | + |
| 110 | +</section> |
| 111 | + |
| 112 | +<!-- /.usage --> |
| 113 | + |
| 114 | +<section class="notes"> |
| 115 | + |
| 116 | +## Notes |
| 117 | + |
| 118 | +- In `some` mode, the return order of asynchronously resolved paths is not guaranteed. |
| 119 | +- 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 _real_ absolute file paths 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. |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.notes --> |
| 124 | + |
| 125 | +<section class="examples"> |
| 126 | + |
| 127 | +## Examples |
| 128 | + |
| 129 | +<!-- eslint no-undef: "error" --> |
| 130 | + |
| 131 | +```javascript |
| 132 | +var resolveParentPaths = require( '@stdlib/fs/resolve-parent-paths' ); |
| 133 | + |
| 134 | +var opts = { |
| 135 | + 'dir': __dirname |
| 136 | +}; |
| 137 | + |
| 138 | +/* Sync */ |
| 139 | + |
| 140 | +var out = resolveParentPaths.sync( [ 'package.json', 'README.md' ], opts ); |
| 141 | +// returns [ '...', '...' ] |
| 142 | + |
| 143 | +out = resolveParentPaths.sync( [ 'non_existent_basename' ], opts ); |
| 144 | +// returns [] |
| 145 | + |
| 146 | +opts.mode = 'first'; |
| 147 | +out = resolveParentPaths.sync( [ 'non_existent_basename', 'package.json' ], opts ); |
| 148 | +// returns [ '...' ] |
| 149 | + |
| 150 | +/* Async */ |
| 151 | + |
| 152 | +resolveParentPaths( [ 'package.json', 'README.md' ], opts, onPaths ); |
| 153 | +resolveParentPaths( [ './../non_existent_path' ], onPaths ); |
| 154 | + |
| 155 | +function onPaths( error, paths ) { |
| 156 | + if ( error ) { |
| 157 | + throw error; |
| 158 | + } |
| 159 | + console.log( paths ); |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +</section> |
| 164 | + |
| 165 | +<!-- /.examples --> |
| 166 | + |
| 167 | +* * * |
| 168 | + |
| 169 | +<section class="cli"> |
| 170 | + |
| 171 | +## CLI |
| 172 | + |
| 173 | +<section class="usage"> |
| 174 | + |
| 175 | +### Usage |
| 176 | + |
| 177 | +```text |
| 178 | +Usage: resolve-parent-paths [options] <path> [<path>...] |
| 179 | +
|
| 180 | +Options: |
| 181 | +
|
| 182 | + -h, --help Print this message. |
| 183 | + -V, --version Print the package version. |
| 184 | + --dir dir Base search directory. |
| 185 | + --mode mode Mode of operation. |
| 186 | +``` |
| 187 | + |
| 188 | +</section> |
| 189 | + |
| 190 | +<!-- /.usage --> |
| 191 | + |
| 192 | +<section class="examples"> |
| 193 | + |
| 194 | +### Examples |
| 195 | + |
| 196 | +```bash |
| 197 | +$ resolve-parent-paths package.json package-lock.json |
| 198 | +``` |
| 199 | + |
| 200 | +</section> |
| 201 | + |
| 202 | +<!-- /.examples --> |
| 203 | + |
| 204 | +</section> |
| 205 | + |
| 206 | +<!-- /.cli --> |
| 207 | + |
| 208 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 209 | + |
| 210 | +<section class="related"> |
| 211 | + |
| 212 | +</section> |
| 213 | + |
| 214 | +<!-- /.related --> |
| 215 | + |
| 216 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 217 | + |
| 218 | +<section class="links"> |
| 219 | + |
| 220 | +[node-core-path-resolve]: https://nodejs.org/api/path.html#path_path_resolve_paths |
| 221 | + |
| 222 | +<!-- <related-links> --> |
| 223 | + |
| 224 | +<!-- </related-links> --> |
| 225 | + |
| 226 | +</section> |
| 227 | + |
| 228 | +<!-- /.links --> |
0 commit comments