@ohos-ports/stdlib-fs 0.0.0-stage → 0.4.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +230 -2
- package/SECURITY.md +5 -0
- package/append-file/README.md +239 -0
- package/append-file/bin/cli +110 -0
- package/append-file/docs/repl.txt +72 -0
- package/append-file/docs/types/index.d.ts +133 -0
- package/append-file/docs/types/test.ts +193 -0
- package/append-file/docs/usage.txt +10 -0
- package/append-file/etc/cli_opts.json +22 -0
- package/append-file/lib/async.js +58 -0
- package/append-file/lib/index.js +61 -0
- package/append-file/lib/sync.js +58 -0
- package/append-file/package.json +69 -0
- package/close/README.md +172 -0
- package/close/docs/repl.txt +45 -0
- package/close/docs/types/index.d.ts +135 -0
- package/close/docs/types/test.ts +89 -0
- package/close/lib/index.js +73 -0
- package/close/lib/main.js +68 -0
- package/close/lib/sync.js +65 -0
- package/close/package.json +64 -0
- package/docs/types/index.d.ts +502 -0
- package/exists/README.md +252 -0
- package/exists/bin/cli +83 -0
- package/exists/docs/repl.txt +42 -0
- package/exists/docs/types/index.d.ts +112 -0
- package/exists/docs/types/test.ts +87 -0
- package/exists/docs/usage.txt +8 -0
- package/exists/etc/cli_opts.json +14 -0
- package/exists/lib/async.js +85 -0
- package/exists/lib/index.js +66 -0
- package/exists/lib/sync.js +62 -0
- package/exists/package.json +69 -0
- package/lib/index.js +186 -0
- package/open/README.md +204 -0
- package/open/docs/repl.txt +65 -0
- package/open/docs/types/index.d.ts +150 -0
- package/open/docs/types/test.ts +129 -0
- package/open/lib/defaults.json +4 -0
- package/open/lib/index.js +63 -0
- package/open/lib/main.js +64 -0
- package/open/lib/sync.js +68 -0
- package/open/package.json +64 -0
- package/package.json +67 -4
- package/read-dir/README.md +232 -0
- package/read-dir/bin/cli +83 -0
- package/read-dir/docs/repl.txt +44 -0
- package/read-dir/docs/types/index.d.ts +99 -0
- package/read-dir/docs/types/test.ts +85 -0
- package/read-dir/docs/usage.txt +8 -0
- package/read-dir/etc/cli_opts.json +14 -0
- package/read-dir/lib/async.js +57 -0
- package/read-dir/lib/index.js +62 -0
- package/read-dir/lib/sync.js +52 -0
- package/read-dir/package.json +69 -0
- package/read-file/README.md +246 -0
- package/read-file/bin/cli +91 -0
- package/read-file/docs/repl.txt +68 -0
- package/read-file/docs/types/index.d.ts +138 -0
- package/read-file/docs/types/test.ts +147 -0
- package/read-file/docs/usage.txt +10 -0
- package/read-file/etc/cli_opts.json +21 -0
- package/read-file/lib/index.js +61 -0
- package/read-file/lib/main.js +59 -0
- package/read-file/lib/sync.js +61 -0
- package/read-file/package.json +68 -0
- package/read-file-list/README.md +197 -0
- package/read-file-list/bin/cli +100 -0
- package/read-file-list/docs/repl.txt +81 -0
- package/read-file-list/docs/types/index.d.ts +166 -0
- package/read-file-list/docs/types/test.ts +166 -0
- package/read-file-list/docs/usage.txt +10 -0
- package/read-file-list/etc/cli_opts.json +21 -0
- package/read-file-list/lib/async.js +202 -0
- package/read-file-list/lib/clear_pending.js +47 -0
- package/read-file-list/lib/clear_timeout.js +23 -0
- package/read-file-list/lib/config.json +4 -0
- package/read-file-list/lib/delay.js +47 -0
- package/read-file-list/lib/index.js +66 -0
- package/read-file-list/lib/sync.js +94 -0
- package/read-file-list/package.json +72 -0
- package/read-json/README.md +176 -0
- package/read-json/docs/repl.txt +72 -0
- package/read-json/docs/types/index.d.ts +170 -0
- package/read-json/docs/types/test.ts +175 -0
- package/read-json/lib/async.js +114 -0
- package/read-json/lib/index.js +65 -0
- package/read-json/lib/sync.js +89 -0
- package/read-json/package.json +66 -0
- package/read-ndjson/README.md +166 -0
- package/read-ndjson/docs/repl.txt +72 -0
- package/read-ndjson/docs/types/index.d.ts +170 -0
- package/read-ndjson/docs/types/test.ts +179 -0
- package/read-ndjson/lib/async.js +114 -0
- package/read-ndjson/lib/index.js +65 -0
- package/read-ndjson/lib/sync.js +89 -0
- package/read-ndjson/package.json +68 -0
- package/read-wasm/README.md +158 -0
- package/read-wasm/docs/repl.txt +58 -0
- package/read-wasm/docs/types/index.d.ts +153 -0
- package/read-wasm/docs/types/test.ts +127 -0
- package/read-wasm/lib/async.js +106 -0
- package/read-wasm/lib/index.js +67 -0
- package/read-wasm/lib/sync.js +86 -0
- package/read-wasm/package.json +67 -0
- package/rename/README.md +259 -0
- package/rename/bin/cli +89 -0
- package/rename/docs/repl.txt +80 -0
- package/rename/docs/types/index.d.ts +109 -0
- package/rename/docs/types/test.ts +111 -0
- package/rename/docs/usage.txt +7 -0
- package/rename/etc/cli_opts.json +15 -0
- package/rename/lib/index.js +60 -0
- package/rename/lib/main.js +51 -0
- package/rename/lib/sync.js +53 -0
- package/rename/package.json +73 -0
- package/resolve-parent-path/README.md +204 -0
- package/resolve-parent-path/bin/cli +85 -0
- package/resolve-parent-path/docs/repl.txt +58 -0
- package/resolve-parent-path/docs/types/index.d.ts +128 -0
- package/resolve-parent-path/docs/types/test.ts +117 -0
- package/resolve-parent-path/docs/usage.txt +9 -0
- package/resolve-parent-path/etc/cli_opts.json +17 -0
- package/resolve-parent-path/lib/async.js +116 -0
- package/resolve-parent-path/lib/index.js +58 -0
- package/resolve-parent-path/lib/sync.js +83 -0
- package/resolve-parent-path/lib/validate.js +67 -0
- package/resolve-parent-path/package.json +78 -0
- package/resolve-parent-path-by/README.md +220 -0
- package/resolve-parent-path-by/docs/repl.txt +105 -0
- package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
- package/resolve-parent-path-by/docs/types/test.ts +225 -0
- package/resolve-parent-path-by/lib/async.js +156 -0
- package/resolve-parent-path-by/lib/index.js +67 -0
- package/resolve-parent-path-by/lib/sync.js +98 -0
- package/resolve-parent-path-by/lib/validate.js +67 -0
- package/resolve-parent-path-by/package.json +76 -0
- package/resolve-parent-paths/README.md +228 -0
- package/resolve-parent-paths/bin/cli +93 -0
- package/resolve-parent-paths/docs/repl.txt +84 -0
- package/resolve-parent-paths/docs/types/index.d.ts +145 -0
- package/resolve-parent-paths/docs/types/test.ts +196 -0
- package/resolve-parent-paths/docs/usage.txt +10 -0
- package/resolve-parent-paths/etc/cli_opts.json +18 -0
- package/resolve-parent-paths/lib/index.js +60 -0
- package/resolve-parent-paths/lib/main.js +415 -0
- package/resolve-parent-paths/lib/sync.js +231 -0
- package/resolve-parent-paths/lib/validate.js +81 -0
- package/resolve-parent-paths/package.json +78 -0
- package/unlink/README.md +233 -0
- package/unlink/bin/cli +81 -0
- package/unlink/docs/repl.txt +62 -0
- package/unlink/docs/types/index.d.ts +105 -0
- package/unlink/docs/types/test.ts +85 -0
- package/unlink/docs/usage.txt +7 -0
- package/unlink/etc/cli_opts.json +15 -0
- package/unlink/lib/index.js +60 -0
- package/unlink/lib/main.js +50 -0
- package/unlink/lib/sync.js +52 -0
- package/unlink/package.json +70 -0
- package/write-file/README.md +250 -0
- package/write-file/bin/cli +110 -0
- package/write-file/docs/repl.txt +74 -0
- package/write-file/docs/types/index.d.ts +133 -0
- package/write-file/docs/types/test.ts +193 -0
- package/write-file/docs/usage.txt +10 -0
- package/write-file/etc/cli_opts.json +22 -0
- package/write-file/lib/index.js +60 -0
- package/write-file/lib/main.js +58 -0
- package/write-file/lib/sync.js +58 -0
- package/write-file/package.json +68 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2021 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
|
+
# resolveParentPathBy
|
|
22
|
+
|
|
23
|
+
> Resolve a path according to a predicate function by walking parent directories.
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var resolveParentPathBy = require( '@stdlib/fs/resolve-parent-path-by' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<a name="resolve-parent-path-by"></a>
|
|
34
|
+
|
|
35
|
+
#### resolveParentPathBy( path\[, options], predicate, clbk )
|
|
36
|
+
|
|
37
|
+
Asynchronously resolves a path according to a `predicate` function by walking parent directories.
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
resolveParentPathBy( 'package.json', predicate, onPath );
|
|
41
|
+
|
|
42
|
+
function predicate( path, next ) {
|
|
43
|
+
var bool = ( /\/test\//.test( path ) === false );
|
|
44
|
+
next( null, bool );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function onPath( error, path ) {
|
|
48
|
+
if ( error ) {
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
console.log( path );
|
|
52
|
+
// e.g., => '...'
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The function accepts the following `options`:
|
|
57
|
+
|
|
58
|
+
- **dir**: base directory from which to begin walking. May be either an absolute path or a path relative to the current working directory.
|
|
59
|
+
|
|
60
|
+
By default, the function begins walking from the current working directory. To specify an alternative directory, set the `dir` option.
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
var opts = {
|
|
64
|
+
'dir': __dirname
|
|
65
|
+
};
|
|
66
|
+
resolveParentPathBy( 'package.json', opts, predicate, onPath );
|
|
67
|
+
|
|
68
|
+
function predicate( path, next ) {
|
|
69
|
+
var bool = ( /\/test\//.test( path ) === false );
|
|
70
|
+
next( null, bool );
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function onPath( error, path ) {
|
|
74
|
+
if ( error ) {
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
console.log( path );
|
|
78
|
+
// e.g., => '...'
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
When invoked, the `predicate` function is provided two arguments:
|
|
83
|
+
|
|
84
|
+
- **path**: a resolved path.
|
|
85
|
+
- **next**: a callback which should be called once the `predicate` function has finished processing a resolved path.
|
|
86
|
+
|
|
87
|
+
If a `predicate` function calls the `next` callback with a truthy second argument, the function stops processing any additional paths and returns the resolved path as the test result.
|
|
88
|
+
|
|
89
|
+
If unable to resolve a path, the function returns `null` as the path result.
|
|
90
|
+
|
|
91
|
+
#### resolveParentPathBy.sync( path\[, options], predicate )
|
|
92
|
+
|
|
93
|
+
Synchronously resolves a path according to a `predicate` function by walking parent directories.
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
function predicate( path ) {
|
|
97
|
+
return ( /\/test\//.test( path ) === false );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
var path = resolveParentPathBy.sync( 'package.json', predicate );
|
|
101
|
+
// e.g., returns '...'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The function accepts the same `options` as [`resolveParentPathBy()`](#resolve-parent-path-by).
|
|
105
|
+
|
|
106
|
+
When invoked, the `predicate` function is provided one argument:
|
|
107
|
+
|
|
108
|
+
- **path**: a resolved path.
|
|
109
|
+
|
|
110
|
+
The function immediately returns upon encountering a truthy `predicate` function return value.
|
|
111
|
+
|
|
112
|
+
If unable to resolve a path, the function returns `null` as the path result.
|
|
113
|
+
|
|
114
|
+
</section>
|
|
115
|
+
|
|
116
|
+
<!-- /.usage -->
|
|
117
|
+
|
|
118
|
+
<section class="notes">
|
|
119
|
+
|
|
120
|
+
## Notes
|
|
121
|
+
|
|
122
|
+
- If a provided `predicate` function calls the `next` callback with a truthy `error` argument, the function suspends execution and immediately calls the `done` callback for subsequent `error` handling.
|
|
123
|
+
- For `resolveParentPathBy`, execution is **not** guaranteed to be asynchronous. To guarantee asynchrony, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
|
|
124
|
+
- 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.
|
|
125
|
+
|
|
126
|
+
</section>
|
|
127
|
+
|
|
128
|
+
<!-- /.notes -->
|
|
129
|
+
|
|
130
|
+
<section class="examples">
|
|
131
|
+
|
|
132
|
+
## Examples
|
|
133
|
+
|
|
134
|
+
<!-- eslint no-undef: "error" -->
|
|
135
|
+
|
|
136
|
+
<!-- eslint-disable stdlib/no-dynamic-require -->
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
var resolveParentPathBy = require( '@stdlib/fs/resolve-parent-path-by' );
|
|
140
|
+
|
|
141
|
+
var opts = {
|
|
142
|
+
'dir': __dirname
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/* Sync */
|
|
146
|
+
|
|
147
|
+
function predicateSync( path ) {
|
|
148
|
+
var pkg = require( path );
|
|
149
|
+
if ( pkg.name !== '@stdlib/stdlib' ) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
var out = resolveParentPathBy.sync( 'package.json', opts, predicateSync );
|
|
156
|
+
console.log( out );
|
|
157
|
+
// e.g., => '...'
|
|
158
|
+
|
|
159
|
+
out = resolveParentPathBy.sync( 'non_existent_basename/package.json', predicateSync );
|
|
160
|
+
console.log( out );
|
|
161
|
+
// => null
|
|
162
|
+
|
|
163
|
+
/* Async */
|
|
164
|
+
|
|
165
|
+
function predicateAsync( path, next ) {
|
|
166
|
+
setTimeout( onTimeout, 0 );
|
|
167
|
+
|
|
168
|
+
function onTimeout() {
|
|
169
|
+
var pkg = require( path );
|
|
170
|
+
if ( pkg.name !== '@stdlib/stdlib' ) {
|
|
171
|
+
return next( null, false );
|
|
172
|
+
}
|
|
173
|
+
next( null, true );
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function onPath( error, path ) {
|
|
178
|
+
if ( error ) {
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
console.log( path );
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
resolveParentPathBy( 'package.json', opts, predicateAsync, onPath );
|
|
185
|
+
resolveParentPathBy( './../non_existent_path/package.json', predicateAsync, onPath );
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
</section>
|
|
189
|
+
|
|
190
|
+
<!-- /.examples -->
|
|
191
|
+
|
|
192
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
193
|
+
|
|
194
|
+
<section class="related">
|
|
195
|
+
|
|
196
|
+
* * *
|
|
197
|
+
|
|
198
|
+
## See Also
|
|
199
|
+
|
|
200
|
+
- <span class="package-name">[`@stdlib/fs/resolve-parent-path`][@stdlib/fs/resolve-parent-path]</span><span class="delimiter">: </span><span class="description">resolve a path by walking parent directories.</span>
|
|
201
|
+
|
|
202
|
+
</section>
|
|
203
|
+
|
|
204
|
+
<!-- /.related -->
|
|
205
|
+
|
|
206
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
207
|
+
|
|
208
|
+
<section class="links">
|
|
209
|
+
|
|
210
|
+
[node-core-path-resolve]: https://nodejs.org/api/path.html#path_path_resolve_paths
|
|
211
|
+
|
|
212
|
+
<!-- <related-links> -->
|
|
213
|
+
|
|
214
|
+
[@stdlib/fs/resolve-parent-path]: https://github.com/stdlib-js/fs/tree/main/resolve-parent-path
|
|
215
|
+
|
|
216
|
+
<!-- </related-links> -->
|
|
217
|
+
|
|
218
|
+
</section>
|
|
219
|
+
|
|
220
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( path[, options], predicate, clbk )
|
|
3
|
+
Asynchronously resolves a path according to a predicate function by walking
|
|
4
|
+
parent directories.
|
|
5
|
+
|
|
6
|
+
When invoked, the predicate function is provided two arguments:
|
|
7
|
+
|
|
8
|
+
- path: a resolved path.
|
|
9
|
+
- next: a callback to be invoked after processing a resolved path.
|
|
10
|
+
|
|
11
|
+
The `next` callback takes two arguments:
|
|
12
|
+
|
|
13
|
+
- error: error argument.
|
|
14
|
+
- result: test result.
|
|
15
|
+
|
|
16
|
+
If a provided predicate function calls the `next` callback with a truthy
|
|
17
|
+
`error` argument, the function suspends execution and immediately calls the
|
|
18
|
+
`done` callback for subsequent `error` handling.
|
|
19
|
+
|
|
20
|
+
The function immediately returns upon encountering a non-falsy `result`
|
|
21
|
+
value and calls the `done` callback with `null` as the first argument and
|
|
22
|
+
the resolved path as the second argument.
|
|
23
|
+
|
|
24
|
+
If unable to resolve a path, the function returns `null` as the path result.
|
|
25
|
+
|
|
26
|
+
Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony,
|
|
27
|
+
wrap the `done` callback in a function which either executes at the end of
|
|
28
|
+
the current stack (e.g., `nextTick`) or during a subsequent turn of the
|
|
29
|
+
event loop (e.g., `setImmediate`, `setTimeout`).
|
|
30
|
+
|
|
31
|
+
Parameters
|
|
32
|
+
----------
|
|
33
|
+
path: string
|
|
34
|
+
Path to resolve.
|
|
35
|
+
|
|
36
|
+
options: Object (optional)
|
|
37
|
+
Options.
|
|
38
|
+
|
|
39
|
+
options.dir: string (optional)
|
|
40
|
+
Base directory from which to search. Default: current working directory.
|
|
41
|
+
|
|
42
|
+
predicate: Function
|
|
43
|
+
The test function to invoke for each resolved path.
|
|
44
|
+
|
|
45
|
+
clbk: Function
|
|
46
|
+
Callback to invoke after resolving a path.
|
|
47
|
+
|
|
48
|
+
Examples
|
|
49
|
+
--------
|
|
50
|
+
> function predicate( path, next ) {
|
|
51
|
+
... setTimeout( onTimeout, path );
|
|
52
|
+
... function onTimeout() {
|
|
53
|
+
... console.log( path );
|
|
54
|
+
... next( null, false );
|
|
55
|
+
... }
|
|
56
|
+
... };
|
|
57
|
+
> function onPath( error, path ) {
|
|
58
|
+
... if ( error ) {
|
|
59
|
+
... console.error( error.message );
|
|
60
|
+
... } else {
|
|
61
|
+
... console.log( path );
|
|
62
|
+
... }
|
|
63
|
+
... };
|
|
64
|
+
> {{alias}}( 'package.json', predicate, onPath );
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
{{alias}}.sync( path[, options], predicate )
|
|
68
|
+
Synchronously resolves a path according to a predicate function by walking
|
|
69
|
+
parent directories.
|
|
70
|
+
|
|
71
|
+
The predicate function is provided one argument:
|
|
72
|
+
|
|
73
|
+
- path: a resolved path.
|
|
74
|
+
|
|
75
|
+
The function immediately returns upon encountering a truthy return value.
|
|
76
|
+
|
|
77
|
+
If unable to resolve a path, the function returns `null` as the path result.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
path: string
|
|
82
|
+
Path to resolve.
|
|
83
|
+
|
|
84
|
+
options: Object (optional)
|
|
85
|
+
Options.
|
|
86
|
+
|
|
87
|
+
options.dir: string (optional)
|
|
88
|
+
Base directory from which to search. Default: current working directory.
|
|
89
|
+
|
|
90
|
+
predicate: Function
|
|
91
|
+
The test function to invoke for each resolved path.
|
|
92
|
+
|
|
93
|
+
Returns
|
|
94
|
+
-------
|
|
95
|
+
out: string|null
|
|
96
|
+
Resolved path.
|
|
97
|
+
|
|
98
|
+
Examples
|
|
99
|
+
--------
|
|
100
|
+
> function predicate() { return false; };
|
|
101
|
+
> var out = {{alias}}.sync( 'package.json', predicate );
|
|
102
|
+
|
|
103
|
+
See Also
|
|
104
|
+
--------
|
|
105
|
+
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Interface defining function options.
|
|
23
|
+
*/
|
|
24
|
+
interface Options {
|
|
25
|
+
/**
|
|
26
|
+
* Base directory from which to search (default: current working directory).
|
|
27
|
+
*/
|
|
28
|
+
dir?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Callback function.
|
|
33
|
+
*/
|
|
34
|
+
type Nullary = () => void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Callback function.
|
|
38
|
+
*
|
|
39
|
+
* @param error - encountered error or null
|
|
40
|
+
*/
|
|
41
|
+
type Unary = ( error: Error | null ) => void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Callback function.
|
|
45
|
+
*
|
|
46
|
+
* @param error - encountered error or null
|
|
47
|
+
* @param result - test result
|
|
48
|
+
*/
|
|
49
|
+
type Binary = ( error: Error | null, result: boolean ) => void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Callback function.
|
|
53
|
+
*
|
|
54
|
+
* @param error - encountered error or null
|
|
55
|
+
* @param result - test result
|
|
56
|
+
*/
|
|
57
|
+
type NextCallback = Nullary | Unary | Binary;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Checks whether a resolved path passes a test.
|
|
61
|
+
*
|
|
62
|
+
* @param path - resolved path
|
|
63
|
+
* @returns test result
|
|
64
|
+
*/
|
|
65
|
+
type SyncPredicate = ( path: string ) => boolean;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks whether a resolved path passes a test.
|
|
69
|
+
*
|
|
70
|
+
* @param path - resolved path
|
|
71
|
+
* @param next - callback which should be called once the `predicate` function has finished processing a resolved path
|
|
72
|
+
*/
|
|
73
|
+
type AsyncPredicate = ( path: string, next: NextCallback ) => void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Callback invoked after resolving a path according to a predicate function.
|
|
77
|
+
*
|
|
78
|
+
* @param error - error object
|
|
79
|
+
* @param path - resolved path
|
|
80
|
+
*/
|
|
81
|
+
type DoneCallback = ( error: Error | null, path: string | null ) => void;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Interface for resolving a path according to a predicate function by walking parent directories.
|
|
85
|
+
*/
|
|
86
|
+
interface ResolveParentPathBy {
|
|
87
|
+
/**
|
|
88
|
+
* Asynchronously resolves a path according to a predicate function by walking parent directories.
|
|
89
|
+
*
|
|
90
|
+
* @param path - path to resolve
|
|
91
|
+
* @param options - function options
|
|
92
|
+
* @param options.dir - base directory
|
|
93
|
+
* @param predicate - predicate function
|
|
94
|
+
* @param clbk - callback to invoke after resolving a path
|
|
95
|
+
* @throws must provide valid options
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* resolveParentPathBy( 'package.json', { 'dir': __dirname }, predicate, onPath );
|
|
99
|
+
*
|
|
100
|
+
* function predicate( path, next ) {
|
|
101
|
+
* next( null, true );
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* function onPath( error, path ) {
|
|
105
|
+
* if ( error ) {
|
|
106
|
+
* throw error;
|
|
107
|
+
* }
|
|
108
|
+
* console.log( path );
|
|
109
|
+
* }
|
|
110
|
+
*/
|
|
111
|
+
( path: string, options: Options, predicate: AsyncPredicate, clbk: DoneCallback ): void;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Asynchronously resolves a path according to a predicate function by walking parent directories.
|
|
115
|
+
*
|
|
116
|
+
* @param path - path to resolve
|
|
117
|
+
* @param predicate - predicate function
|
|
118
|
+
* @param clbk - callback to invoke after resolving a path
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* resolveParentPathBy( 'package.json', predicate, onPath );
|
|
122
|
+
*
|
|
123
|
+
* function predicate( path, next ) {
|
|
124
|
+
* next( null, true );
|
|
125
|
+
* }
|
|
126
|
+
*
|
|
127
|
+
* function onPath( error, path ) {
|
|
128
|
+
* if ( error ) {
|
|
129
|
+
* throw error;
|
|
130
|
+
* }
|
|
131
|
+
* console.log( path );
|
|
132
|
+
* }
|
|
133
|
+
*/
|
|
134
|
+
( path: string, predicate: AsyncPredicate, clbk: DoneCallback ): void;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Synchronously resolves a path according to a predicate function by walking parent directories.
|
|
138
|
+
*
|
|
139
|
+
* ## Notes
|
|
140
|
+
*
|
|
141
|
+
* - If unable to resolve a path, the function returns `null` as the path result.
|
|
142
|
+
*
|
|
143
|
+
* @param path - path to resolve
|
|
144
|
+
* @param options - function options
|
|
145
|
+
* @param options.dir - base directory
|
|
146
|
+
* @param predicate - predicate function
|
|
147
|
+
* @throws must provide valid options
|
|
148
|
+
* @returns resolved path or null
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* function predicate() {
|
|
152
|
+
* return true;
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* var path = resolveParentPathBy.sync( 'package.json', { 'dir': __dirname }, predicate );
|
|
156
|
+
* // e.g., returns '...'
|
|
157
|
+
*/
|
|
158
|
+
sync( path: string, options: Options, predicate: SyncPredicate ): string | null;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Synchronously resolves a path according to a predicate function by walking parent directories.
|
|
162
|
+
*
|
|
163
|
+
* ## Notes
|
|
164
|
+
*
|
|
165
|
+
* - If unable to resolve a path, the function returns `null` as the path result.
|
|
166
|
+
*
|
|
167
|
+
* @param path - path to resolve
|
|
168
|
+
* @param predicate - predicate function
|
|
169
|
+
* @throws must provide valid options
|
|
170
|
+
* @returns resolved path or null
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* function predicate() {
|
|
174
|
+
* return true;
|
|
175
|
+
* }
|
|
176
|
+
*
|
|
177
|
+
* var path = resolveParentPathBy.sync( 'package.json', predicate );
|
|
178
|
+
* // e.g., returns '...'
|
|
179
|
+
*/
|
|
180
|
+
sync( path: string, predicate: SyncPredicate ): string | null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Asynchronously resolves a path according to a predicate function by walking parent directories.
|
|
185
|
+
*
|
|
186
|
+
* @param path - path to resolve
|
|
187
|
+
* @param options - function options
|
|
188
|
+
* @param options.dir - base directory
|
|
189
|
+
* @param predicate - predicate function
|
|
190
|
+
* @param clbk - callback to invoke after resolving a path
|
|
191
|
+
* @throws must provide valid options
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* resolveParentPathBy( 'package.json', predicate, onPath );
|
|
195
|
+
*
|
|
196
|
+
* function predicate( path, next ) {
|
|
197
|
+
* next( null, true );
|
|
198
|
+
* }
|
|
199
|
+
*
|
|
200
|
+
* function onPath( error, path ) {
|
|
201
|
+
* if ( error ) {
|
|
202
|
+
* throw error;
|
|
203
|
+
* }
|
|
204
|
+
* console.log( path );
|
|
205
|
+
* }
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* function predicate() {
|
|
209
|
+
* return true;
|
|
210
|
+
* }
|
|
211
|
+
*
|
|
212
|
+
* var path = resolveParentPathBy.sync( 'package.json', predicate );
|
|
213
|
+
* // e.g., returns '...'
|
|
214
|
+
*/
|
|
215
|
+
declare var resolveParentPathBy: ResolveParentPathBy;
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
// EXPORTS //
|
|
219
|
+
|
|
220
|
+
export = resolveParentPathBy;
|