@ohos-ports/fs-resolve-parent-path 0.0.0-stage → 0.2.3-beta.2

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 ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
package/NOTICE ADDED
@@ -0,0 +1 @@
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md CHANGED
@@ -1,3 +1,262 @@
1
- # Temporary Holding Version
1
+ <!--
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2018 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
+
22
+ <details>
23
+ <summary>
24
+ About stdlib...
25
+ </summary>
26
+ <p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
27
+ <p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
28
+ <p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
29
+ <p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
30
+ </details>
31
+
32
+ # Resolve Parent Path
33
+
34
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
35
+
36
+ > Resolve a path by walking parent directories.
37
+
38
+ <section class="installation">
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @stdlib/fs-resolve-parent-path
44
+ ```
45
+
46
+ </section>
47
+
48
+ <section class="usage">
49
+
50
+ ## Usage
51
+
52
+ ```javascript
53
+ var resolveParentPath = require( '@stdlib/fs-resolve-parent-path' );
54
+ ```
55
+
56
+ <a name="resolve-parent-path"></a>
57
+
58
+ #### resolveParentPath( path\[, options], clbk )
59
+
60
+ Asynchronously resolves a path by walking parent directories.
61
+
62
+ ```javascript
63
+ resolveParentPath( 'package.json', onPath );
64
+
65
+ function onPath( error, path ) {
66
+ if ( error ) {
67
+ throw error;
68
+ }
69
+ console.log( path );
70
+ // => '...'
71
+ }
72
+ ```
73
+
74
+ The function accepts the following `options`:
75
+
76
+ - **dir**: base directory from which to begin walking. May be either an absolute path or a path relative to the current working directory.
77
+
78
+ By default, the function begins walking from the current working directory. To specify an alternative directory, set the `dir` option.
79
+
80
+ ```javascript
81
+ var opts = {
82
+ 'dir': __dirname
83
+ };
84
+ resolveParentPath( 'package.json', opts, onPath );
85
+
86
+ function onPath( error, path ) {
87
+ if ( error ) {
88
+ throw error;
89
+ }
90
+ console.log( path );
91
+ // => '...'
92
+ }
93
+ ```
94
+
95
+ #### resolveParentPath.sync( path\[, options] )
96
+
97
+ Synchronously resolves a path by walking parent directories.
98
+
99
+ ```javascript
100
+ var path = resolveParentPath.sync( 'package.json' );
101
+ // returns '...'
102
+ ```
103
+
104
+ The function accepts the same `options` as [`resolveParentPath()`](#resolve-parent-path).
105
+
106
+ </section>
107
+
108
+ <!-- /.usage -->
109
+
110
+ <section class="notes">
111
+
112
+ ## Notes
113
+
114
+ - If unable to resolve a path, both functions return `null`.
115
+ - 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.
116
+
117
+ </section>
118
+
119
+ <!-- /.notes -->
120
+
121
+ <section class="examples">
122
+
123
+ ## Examples
124
+
125
+ <!-- eslint no-undef: "error" -->
126
+
127
+ ```javascript
128
+ var resolveParentPath = require( '@stdlib/fs-resolve-parent-path' );
129
+
130
+ var opts = {
131
+ 'dir': __dirname
132
+ };
133
+
134
+ /* Sync */
135
+
136
+ var out = resolveParentPath.sync( 'package.json', opts );
137
+ // returns '...'
138
+
139
+ out = resolveParentPath.sync( 'non_existent_basename' );
140
+ // returns null
141
+
142
+ /* Async */
143
+
144
+ resolveParentPath( 'package.json', opts, onPath );
145
+ resolveParentPath( './../non_existent_path', onPath );
146
+
147
+ function onPath( error, path ) {
148
+ if ( error ) {
149
+ throw error;
150
+ }
151
+ console.log( path );
152
+ }
153
+ ```
154
+
155
+ </section>
156
+
157
+ <!-- /.examples -->
158
+
159
+
160
+
161
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
162
+
163
+ <section class="related">
164
+
165
+ ## See Also
166
+
167
+ - <span class="package-name">[`@stdlib/fs-resolve-parent-path-cli`][@stdlib/fs-resolve-parent-path-cli]</span><span class="delimiter">: </span><span class="description">CLI package for use as a command-line utility.</span>
168
+ - <span class="package-name">[`@stdlib/fs-resolve-parent-path-by`][@stdlib/fs/resolve-parent-path-by]</span><span class="delimiter">: </span><span class="description">resolve a path according to a predicate function by walking parent directories.</span>
169
+
170
+ </section>
171
+
172
+ <!-- /.related -->
173
+
174
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
175
+
176
+
177
+ <section class="main-repo" >
178
+
179
+ * * *
180
+
181
+ ## Notice
182
+
183
+ This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
184
+
185
+ For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
186
+
187
+ #### Community
188
+
189
+ [![Chat][chat-image]][chat-url]
190
+
191
+ ---
192
+
193
+ ## License
194
+
195
+ See [LICENSE][stdlib-license].
196
+
197
+
198
+ ## Copyright
199
+
200
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
201
+
202
+ </section>
203
+
204
+ <!-- /.stdlib -->
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
+ [@stdlib/fs-resolve-parent-path-cli]: https://www.npmjs.com/package/@stdlib/fs-resolve-parent-path-cli
211
+
212
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/fs-resolve-parent-path.svg
213
+ [npm-url]: https://npmjs.org/package/@stdlib/fs-resolve-parent-path
214
+
215
+ [test-image]: https://github.com/stdlib-js/fs-resolve-parent-path/actions/workflows/test.yml/badge.svg?branch=v0.2.3
216
+ [test-url]: https://github.com/stdlib-js/fs-resolve-parent-path/actions/workflows/test.yml?query=branch:v0.2.3
217
+
218
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/fs-resolve-parent-path/main.svg
219
+ [coverage-url]: https://codecov.io/github/stdlib-js/fs-resolve-parent-path?branch=main
220
+
221
+ <!--
222
+
223
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/fs-resolve-parent-path.svg
224
+ [dependencies-url]: https://david-dm.org/stdlib-js/fs-resolve-parent-path/main
225
+
226
+ -->
227
+
228
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
229
+ [chat-url]: https://stdlib.zulipchat.com
230
+
231
+ [stdlib]: https://github.com/stdlib-js/stdlib
232
+
233
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
234
+
235
+ [cli-section]: https://github.com/stdlib-js/fs-resolve-parent-path#cli
236
+ [cli-url]: https://github.com/stdlib-js/fs-resolve-parent-path/tree/cli
237
+ [@stdlib/fs-resolve-parent-path]: https://github.com/stdlib-js/fs-resolve-parent-path/tree/main
238
+
239
+ [umd]: https://github.com/umdjs/umd
240
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
241
+
242
+ [deno-url]: https://github.com/stdlib-js/fs-resolve-parent-path/tree/deno
243
+ [deno-readme]: https://github.com/stdlib-js/fs-resolve-parent-path/blob/deno/README.md
244
+ [umd-url]: https://github.com/stdlib-js/fs-resolve-parent-path/tree/umd
245
+ [umd-readme]: https://github.com/stdlib-js/fs-resolve-parent-path/blob/umd/README.md
246
+ [esm-url]: https://github.com/stdlib-js/fs-resolve-parent-path/tree/esm
247
+ [esm-readme]: https://github.com/stdlib-js/fs-resolve-parent-path/blob/esm/README.md
248
+ [branches-url]: https://github.com/stdlib-js/fs-resolve-parent-path/blob/main/branches.md
249
+
250
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/fs-resolve-parent-path/main/LICENSE
251
+
252
+ [node-core-path-resolve]: https://nodejs.org/api/path.html#path_path_resolve_paths
253
+
254
+ <!-- <related-links> -->
255
+
256
+ [@stdlib/fs/resolve-parent-path-by]: https://www.npmjs.com/package/@stdlib/fs-resolve-parent-path-by
257
+
258
+ <!-- </related-links> -->
259
+
260
+ </section>
261
+
262
+ <!-- /.links -->
package/SECURITY.md ADDED
@@ -0,0 +1,5 @@
1
+ # Security
2
+
3
+ > Policy for reporting security vulnerabilities.
4
+
5
+ See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security).
@@ -0,0 +1,3 @@
1
+ /// <reference path="../docs/types/index.d.ts" />
2
+ import resolveParentPath from '../docs/types/index';
3
+ export = resolveParentPath;
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";var o=function(r,e){return function(){return e||r((e={exports:{}}).exports,e),e.exports}};var d=o(function(L,q){
2
+ var T=require('@ohos-ports/assert-is-plain-object/dist'),V=require('@ohos-ports/assert-has-own-property/dist'),F=require('@ohos-ports/assert-is-string/dist').isPrimitive,c=require('@ohos-ports/error-tools-fmtprodmsg/dist');function S(r,e){return T(e)?V(e,"dir")&&(r.dir=e.dir,!F(r.dir))?new TypeError(c('0OP2W',"dir",r.dir)):null:new TypeError(c('0OP2V',e));}q.exports=S
3
+ });var y=o(function(M,P){
4
+ var v=require("path").resolve,j=require('@ohos-ports/assert-is-string/dist').isPrimitive,k=require('@ohos-ports/assert-is-function/dist'),g=require('@ohos-ports/process-cwd/dist'),w=require('@ohos-ports/fs-exists/dist'),h=require('@ohos-ports/error-tools-fmtprodmsg/dist'),C=d();function R(r,e,s){var a,u,i,t,n,l;if(!j(r))throw new TypeError(h('0OP3F',r));if(i={},arguments.length>2){if(t=s,l=C(i,e),l)throw l}else t=e;if(!k(t))throw new TypeError(h('0OP2b',t));i.dir?n=v(g(),i.dir):n=g(),a=v(n,r),w(a,m);function m(J,O){if(O)return t(null,a);if(u=n,n=v(n,".."),u===n)return t(null,null);a=v(n,r),w(a,m)}}P.exports=R
5
+ });var x=o(function(N,p){
6
+ var f=require("path").resolve,z=require('@ohos-ports/assert-is-string/dist').isPrimitive,b=require('@ohos-ports/process-cwd/dist'),A=require('@ohos-ports/fs-exists/dist').sync,B=require('@ohos-ports/error-tools-fmtprodmsg/dist'),D=d();function G(r,e){var s,a,u,i,t;if(!z(r))throw new TypeError(B('0OP3F',r));if(u={},arguments.length>1&&(t=D(u,e),t))throw t;for(u.dir?i=f(b(),u.dir):i=b();a!==i;){if(s=f(i,r),A(s))return s;a=i,i=f(i,"..")}return null}p.exports=G
7
+ });var H=require('@ohos-ports/utils-define-nonenumerable-read-only-property/dist'),E=y(),I=x();H(E,"sync",I);module.exports=E;
8
+ /** @license Apache-2.0 */
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../lib/validate.js", "../lib/async.js", "../lib/sync.js", "../lib/index.js"],
4
+ "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {string} [options.dir] - base directory\n* @returns {(Error|null)} error object or null\n*\n* @example\n* var opts = {};\n* var options = {\n* 'dir': '/foo/bar/baz'\n* };\n*\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'dir' ) ) {\n\t\topts.dir = options.dir;\n\t\tif ( !isString( opts.dir ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'dir', opts.dir ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar resolve = require( 'path' ).resolve;\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar cwd = require( '@stdlib/process-cwd' );\nvar exists = require( '@stdlib/fs-exists' );\nvar format = require( '@stdlib/string-format' );\nvar validate = require( './validate.js' );\n\n\n// MAIN //\n\n/**\n* Asynchronously resolves a path by walking parent directories.\n*\n* @param {string} path - path to resolve\n* @param {Options} [options] - function options\n* @param {string} [options.dir] - base directory\n* @param {Callback} clbk - callback to invoke after resolving a path\n* @throws {TypeError} first argument must be a string\n* @throws {TypeError} callback argument must be a function\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n*\n* @example\n* resolveParentPath( 'package.json', onPath );\n*\n* function onPath( error, path ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( path );\n* }\n*/\nfunction resolveParentPath( path, options, clbk ) {\n\tvar spath;\n\tvar child;\n\tvar opts;\n\tvar done;\n\tvar dir;\n\tvar err;\n\tif ( !isString( path ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', path ) );\n\t}\n\topts = {};\n\tif ( arguments.length > 2 ) {\n\t\tdone = clbk;\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t} else {\n\t\tdone = options;\n\t}\n\tif ( !isFunction( done ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );\n\t}\n\tif ( opts.dir ) {\n\t\tdir = resolve( cwd(), opts.dir );\n\t} else {\n\t\tdir = cwd();\n\t}\n\tspath = resolve( dir, path );\n\texists( spath, onExists );\n\n\t/**\n\t* Callback invoked after checking for path existence.\n\t*\n\t* @private\n\t* @param {(Error|null)} error - error object\n\t* @param {boolean} bool - boolean indicating if a path exists\n\t* @returns {void}\n\t*/\n\tfunction onExists( error, bool ) { // eslint-disable-line handle-callback-err\n\t\tif ( bool ) {\n\t\t\treturn done( null, spath );\n\t\t}\n\t\t// Resolve a parent directory:\n\t\tchild = dir;\n\t\tdir = resolve( dir, '..' );\n\n\t\t// If we have already reached root, we cannot resolve any higher directories...\n\t\tif ( child === dir ) {\n\t\t\treturn done( null, null );\n\t\t}\n\t\t// Resolve the next search path:\n\t\tspath = resolve( dir, path );\n\t\texists( spath, onExists );\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = resolveParentPath;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar resolve = require( 'path' ).resolve;\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar cwd = require( '@stdlib/process-cwd' );\nvar exists = require( '@stdlib/fs-exists' ).sync;\nvar format = require( '@stdlib/string-format' );\nvar validate = require( './validate.js' );\n\n\n// MAIN //\n\n/**\n* Synchronously resolves a path by walking parent directories.\n*\n* @param {string} path - path to resolve\n* @param {Options} [options] - function options\n* @param {string} [options.dir] - base directory\n* @throws {TypeError} first argument must be a string\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @returns {(string|null)} resolved path or null\n*\n* @example\n* var path = resolveParentPath( 'package.json' );\n*/\nfunction resolveParentPath( path, options ) {\n\tvar spath;\n\tvar child;\n\tvar opts;\n\tvar dir;\n\tvar err;\n\tif ( !isString( path ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', path ) );\n\t}\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t}\n\tif ( opts.dir ) {\n\t\tdir = resolve( cwd(), opts.dir );\n\t} else {\n\t\tdir = cwd();\n\t}\n\t// Start at a base directory and continue moving up through each parent directory until able to resolve a search path or until reaching the root directory...\n\twhile ( child !== dir ) {\n\t\tspath = resolve( dir, path );\n\t\tif ( exists( spath ) ) {\n\t\t\treturn spath;\n\t\t}\n\t\tchild = dir;\n\t\tdir = resolve( dir, '..' );\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = resolveParentPath;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Resolve a path by walking parent directories.\n*\n* @module @stdlib/fs-resolve-parent-path\n*\n* @example\n* var resolveParentPath = require( '@stdlib/fs-resolve-parent-path' );\n*\n* resolveParentPath( 'package.json', onPath );\n*\n* function onPath( error, path ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( path );\n* }\n*\n* @example\n* var resolveParentPath = require( '@stdlib/fs-resolve-parent-path' );\n*\n* var path = resolveParentPath.sync( 'package.json' );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar resolveParentPath = require( './async.js' );\nvar sync = require( './sync.js' );\n\n\n// MAIN //\n\nsetReadOnly( resolveParentPath, 'sync', sync );\n\n\n// EXPORTS //\n\nmodule.exports = resolveParentPath;\n"],
5
+ "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAW,QAAS,0BAA2B,EAAE,YACjDC,EAAS,QAAS,uBAAwB,EAyB9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMN,EAAUM,CAAQ,EAGnBL,EAAYK,EAAS,KAAM,IAC/BD,EAAK,IAAMC,EAAQ,IACd,CAACJ,EAAUG,EAAK,GAAI,GACjB,IAAI,UAAWF,EAAQ,8DAA+D,MAAOE,EAAK,GAAI,CAAE,EAG1G,KARC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAShH,CAKAP,EAAO,QAAUK,IClEjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,QAAS,MAAO,EAAE,QAC5BC,EAAW,QAAS,0BAA2B,EAAE,YACjDC,EAAa,QAAS,4BAA6B,EACnDC,EAAM,QAAS,qBAAsB,EACrCC,EAAS,QAAS,mBAAoB,EACtCC,EAAS,QAAS,uBAAwB,EAC1CC,EAAW,IA2Bf,SAASC,EAAmBC,EAAMC,EAASC,EAAO,CACjD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACJ,GAAK,CAACf,EAAUO,CAAK,EACpB,MAAM,IAAI,UAAWH,EAAQ,kEAAmEG,CAAK,CAAE,EAGxG,GADAK,EAAO,CAAC,EACH,UAAU,OAAS,GAGvB,GAFAC,EAAOJ,EACPM,EAAMV,EAAUO,EAAMJ,CAAQ,EACzBO,EACJ,MAAMA,OAGPF,EAAOL,EAER,GAAK,CAACP,EAAYY,CAAK,EACtB,MAAM,IAAI,UAAWT,EAAQ,uEAAwES,CAAK,CAAE,EAExGD,EAAK,IACTE,EAAMf,EAASG,EAAI,EAAGU,EAAK,GAAI,EAE/BE,EAAMZ,EAAI,EAEXQ,EAAQX,EAASe,EAAKP,CAAK,EAC3BJ,EAAQO,EAAOM,CAAS,EAUxB,SAASA,EAAUC,EAAOC,EAAO,CAChC,GAAKA,EACJ,OAAOL,EAAM,KAAMH,CAAM,EAO1B,GAJAC,EAAQG,EACRA,EAAMf,EAASe,EAAK,IAAK,EAGpBH,IAAUG,EACd,OAAOD,EAAM,KAAM,IAAK,EAGzBH,EAAQX,EAASe,EAAKP,CAAK,EAC3BJ,EAAQO,EAAOM,CAAS,CACzB,CACD,CAKAlB,EAAO,QAAUQ,ICnHjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,QAAS,MAAO,EAAE,QAC5BC,EAAW,QAAS,0BAA2B,EAAE,YACjDC,EAAM,QAAS,qBAAsB,EACrCC,EAAS,QAAS,mBAAoB,EAAE,KACxCC,EAAS,QAAS,uBAAwB,EAC1CC,EAAW,IAmBf,SAASC,EAAmBC,EAAMC,EAAU,CAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACJ,GAAK,CAACZ,EAAUM,CAAK,EACpB,MAAM,IAAI,UAAWH,EAAQ,kEAAmEG,CAAK,CAAE,EAGxG,GADAI,EAAO,CAAC,EACH,UAAU,OAAS,IACvBE,EAAMR,EAAUM,EAAMH,CAAQ,EACzBK,GACJ,MAAMA,EASR,IANKF,EAAK,IACTC,EAAMZ,EAASE,EAAI,EAAGS,EAAK,GAAI,EAE/BC,EAAMV,EAAI,EAGHQ,IAAUE,GAAM,CAEvB,GADAH,EAAQT,EAASY,EAAKL,CAAK,EACtBJ,EAAQM,CAAM,EAClB,OAAOA,EAERC,EAAQE,EACRA,EAAMZ,EAASY,EAAK,IAAK,CAC1B,CACA,OAAO,IACR,CAKAb,EAAO,QAAUO,ICrCjB,IAAIQ,EAAc,QAAS,uDAAwD,EAC/EC,EAAoB,IACpBC,EAAO,IAKXF,EAAaC,EAAmB,OAAQC,CAAK,EAK7C,OAAO,QAAUD",
6
+ "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isString", "format", "validate", "opts", "options", "require_async", "__commonJSMin", "exports", "module", "resolve", "isString", "isFunction", "cwd", "exists", "format", "validate", "resolveParentPath", "path", "options", "clbk", "spath", "child", "opts", "done", "dir", "err", "onExists", "error", "bool", "require_sync", "__commonJSMin", "exports", "module", "resolve", "isString", "cwd", "exists", "format", "validate", "resolveParentPath", "path", "options", "spath", "child", "opts", "dir", "err", "setReadOnly", "resolveParentPath", "sync"]
7
+ }
@@ -0,0 +1,128 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 invoked after resolving a path.
33
+ *
34
+ * @param err - error argument
35
+ * @param path - resolved path
36
+ */
37
+ type Callback = ( err: Error | null, path: string | null ) => void;
38
+
39
+ /**
40
+ * Interface for resolving a path by walking parent directories.
41
+ */
42
+ interface ResolveParentPath {
43
+ /**
44
+ * Asynchronously resolves a path by walking parent directories.
45
+ *
46
+ * @param path - path to resolve
47
+ * @param options - function options
48
+ * @param options.dir - base directory
49
+ * @param clbk - callback to invoke after resolving a path
50
+ * @throws must provide valid options
51
+ *
52
+ * @example
53
+ * resolveParentPath( 'package.json', { 'dir': __dirname }, onPath );
54
+ *
55
+ * function onPath( error, path ) {
56
+ * if ( error ) {
57
+ * throw error;
58
+ * }
59
+ * console.log( path );
60
+ * }
61
+ */
62
+ ( path: string, options: Options, clbk: Callback ): void;
63
+
64
+ /**
65
+ * Asynchronously resolves a path by walking parent directories.
66
+ *
67
+ * @param path - path to resolve
68
+ * @param clbk - callback to invoke after resolving a path
69
+ *
70
+ * @example
71
+ * resolveParentPath( 'package.json', onPath );
72
+ *
73
+ * function onPath( error, path ) {
74
+ * if ( error ) {
75
+ * throw error;
76
+ * }
77
+ * console.log( path );
78
+ * }
79
+ */
80
+ ( path: string, clbk: Callback ): void;
81
+
82
+ /**
83
+ * Synchronously resolves a path by walking parent directories.
84
+ *
85
+ * ## Notes
86
+ *
87
+ * - If unable to resolve a path, the function returns `null` as the path result.
88
+ *
89
+ * @param path - path to resolve
90
+ * @param options - function options
91
+ * @param options.dir - base directory
92
+ * @throws must provide valid options
93
+ * @returns resolved path or null
94
+ *
95
+ * @example
96
+ * var path = resolveParentPath.sync( 'package.json', { 'dir': __dirname } );
97
+ */
98
+ sync( path: string, options?: Options ): string | null;
99
+ }
100
+
101
+ /**
102
+ * Asynchronously resolves a path by walking parent directories.
103
+ *
104
+ * @param path - path to resolve
105
+ * @param options - function options
106
+ * @param options.dir - base directory
107
+ * @param clbk - callback to invoke after resolving a path
108
+ * @throws must provide valid options
109
+ *
110
+ * @example
111
+ * resolveParentPath( 'package.json', onPath );
112
+ *
113
+ * function onPath( error, path ) {
114
+ * if ( error ) {
115
+ * throw error;
116
+ * }
117
+ * console.log( path );
118
+ * }
119
+ *
120
+ * @example
121
+ * var path = resolveParentPath.sync( 'package.json' );
122
+ */
123
+ declare var resolveParentPath: ResolveParentPath;
124
+
125
+
126
+ // EXPORTS //
127
+
128
+ export = resolveParentPath;
package/lib/async.js ADDED
@@ -0,0 +1,116 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var resolve = require( 'path' ).resolve;
24
+ var isString = require( '@ohos-ports/assert-is-string' ).isPrimitive;
25
+ var isFunction = require( '@ohos-ports/assert-is-function' );
26
+ var cwd = require( '@ohos-ports/process-cwd' );
27
+ var exists = require( '@ohos-ports/fs-exists' );
28
+ var format = require( '@ohos-ports/string-format' );
29
+ var validate = require( './validate.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Asynchronously resolves a path by walking parent directories.
36
+ *
37
+ * @param {string} path - path to resolve
38
+ * @param {Options} [options] - function options
39
+ * @param {string} [options.dir] - base directory
40
+ * @param {Callback} clbk - callback to invoke after resolving a path
41
+ * @throws {TypeError} first argument must be a string
42
+ * @throws {TypeError} callback argument must be a function
43
+ * @throws {TypeError} options argument must be an object
44
+ * @throws {TypeError} must provide valid options
45
+ *
46
+ * @example
47
+ * resolveParentPath( 'package.json', onPath );
48
+ *
49
+ * function onPath( error, path ) {
50
+ * if ( error ) {
51
+ * throw error;
52
+ * }
53
+ * console.log( path );
54
+ * }
55
+ */
56
+ function resolveParentPath( path, options, clbk ) {
57
+ var spath;
58
+ var child;
59
+ var opts;
60
+ var done;
61
+ var dir;
62
+ var err;
63
+ if ( !isString( path ) ) {
64
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', path ) );
65
+ }
66
+ opts = {};
67
+ if ( arguments.length > 2 ) {
68
+ done = clbk;
69
+ err = validate( opts, options );
70
+ if ( err ) {
71
+ throw err;
72
+ }
73
+ } else {
74
+ done = options;
75
+ }
76
+ if ( !isFunction( done ) ) {
77
+ throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
78
+ }
79
+ if ( opts.dir ) {
80
+ dir = resolve( cwd(), opts.dir );
81
+ } else {
82
+ dir = cwd();
83
+ }
84
+ spath = resolve( dir, path );
85
+ exists( spath, onExists );
86
+
87
+ /**
88
+ * Callback invoked after checking for path existence.
89
+ *
90
+ * @private
91
+ * @param {(Error|null)} error - error object
92
+ * @param {boolean} bool - boolean indicating if a path exists
93
+ * @returns {void}
94
+ */
95
+ function onExists( error, bool ) { // eslint-disable-line handle-callback-err
96
+ if ( bool ) {
97
+ return done( null, spath );
98
+ }
99
+ // Resolve a parent directory:
100
+ child = dir;
101
+ dir = resolve( dir, '..' );
102
+
103
+ // If we have already reached root, we cannot resolve any higher directories...
104
+ if ( child === dir ) {
105
+ return done( null, null );
106
+ }
107
+ // Resolve the next search path:
108
+ spath = resolve( dir, path );
109
+ exists( spath, onExists );
110
+ }
111
+ }
112
+
113
+
114
+ // EXPORTS //
115
+
116
+ module.exports = resolveParentPath;
package/lib/index.js ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ 'use strict';
20
+
21
+ /**
22
+ * Resolve a path by walking parent directories.
23
+ *
24
+ * @module @stdlib/fs-resolve-parent-path
25
+ *
26
+ * @example
27
+ * var resolveParentPath = require( '@ohos-ports/fs-resolve-parent-path' );
28
+ *
29
+ * resolveParentPath( 'package.json', onPath );
30
+ *
31
+ * function onPath( error, path ) {
32
+ * if ( error ) {
33
+ * throw error;
34
+ * }
35
+ * console.log( path );
36
+ * }
37
+ *
38
+ * @example
39
+ * var resolveParentPath = require( '@ohos-ports/fs-resolve-parent-path' );
40
+ *
41
+ * var path = resolveParentPath.sync( 'package.json' );
42
+ */
43
+
44
+ // MODULES //
45
+
46
+ var setReadOnly = require( '@ohos-ports/utils-define-nonenumerable-read-only-property' );
47
+ var resolveParentPath = require( './async.js' );
48
+ var sync = require( './sync.js' );
49
+
50
+
51
+ // MAIN //
52
+
53
+ setReadOnly( resolveParentPath, 'sync', sync );
54
+
55
+
56
+ // EXPORTS //
57
+
58
+ module.exports = resolveParentPath;
package/lib/sync.js ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var resolve = require( 'path' ).resolve;
24
+ var isString = require( '@ohos-ports/assert-is-string' ).isPrimitive;
25
+ var cwd = require( '@ohos-ports/process-cwd' );
26
+ var exists = require( '@ohos-ports/fs-exists' ).sync;
27
+ var format = require( '@ohos-ports/string-format' );
28
+ var validate = require( './validate.js' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Synchronously resolves a path by walking parent directories.
35
+ *
36
+ * @param {string} path - path to resolve
37
+ * @param {Options} [options] - function options
38
+ * @param {string} [options.dir] - base directory
39
+ * @throws {TypeError} first argument must be a string
40
+ * @throws {TypeError} options argument must be an object
41
+ * @throws {TypeError} must provide valid options
42
+ * @returns {(string|null)} resolved path or null
43
+ *
44
+ * @example
45
+ * var path = resolveParentPath( 'package.json' );
46
+ */
47
+ function resolveParentPath( path, options ) {
48
+ var spath;
49
+ var child;
50
+ var opts;
51
+ var dir;
52
+ var err;
53
+ if ( !isString( path ) ) {
54
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', path ) );
55
+ }
56
+ opts = {};
57
+ if ( arguments.length > 1 ) {
58
+ err = validate( opts, options );
59
+ if ( err ) {
60
+ throw err;
61
+ }
62
+ }
63
+ if ( opts.dir ) {
64
+ dir = resolve( cwd(), opts.dir );
65
+ } else {
66
+ dir = cwd();
67
+ }
68
+ // Start at a base directory and continue moving up through each parent directory until able to resolve a search path or until reaching the root directory...
69
+ while ( child !== dir ) {
70
+ spath = resolve( dir, path );
71
+ if ( exists( spath ) ) {
72
+ return spath;
73
+ }
74
+ child = dir;
75
+ dir = resolve( dir, '..' );
76
+ }
77
+ return null;
78
+ }
79
+
80
+
81
+ // EXPORTS //
82
+
83
+ module.exports = resolveParentPath;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var isObject = require( '@ohos-ports/assert-is-plain-object' );
24
+ var hasOwnProp = require( '@ohos-ports/assert-has-own-property' );
25
+ var isString = require( '@ohos-ports/assert-is-string' ).isPrimitive;
26
+ var format = require( '@ohos-ports/string-format' );
27
+
28
+
29
+ // MAIN //
30
+
31
+ /**
32
+ * Validates function options.
33
+ *
34
+ * @private
35
+ * @param {Object} opts - destination object
36
+ * @param {Options} options - function options
37
+ * @param {string} [options.dir] - base directory
38
+ * @returns {(Error|null)} error object or null
39
+ *
40
+ * @example
41
+ * var opts = {};
42
+ * var options = {
43
+ * 'dir': '/foo/bar/baz'
44
+ * };
45
+ *
46
+ * var err = validate( opts, options );
47
+ * if ( err ) {
48
+ * throw err;
49
+ * }
50
+ */
51
+ function validate( opts, options ) {
52
+ if ( !isObject( options ) ) {
53
+ return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
54
+ }
55
+ if ( hasOwnProp( options, 'dir' ) ) {
56
+ opts.dir = options.dir;
57
+ if ( !isString( opts.dir ) ) {
58
+ return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'dir', opts.dir ) );
59
+ }
60
+ }
61
+ return null;
62
+ }
63
+
64
+
65
+ // EXPORTS //
66
+
67
+ module.exports = validate;
package/package.json CHANGED
@@ -1,6 +1,88 @@
1
1
  {
2
2
  "name": "@ohos-ports/fs-resolve-parent-path",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.3-beta.2",
4
+ "description": "Resolve a path by walking parent directories.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "doc": "./docs",
19
+ "lib": "./lib",
20
+ "dist": "./dist"
21
+ },
22
+ "types": "./docs/types",
23
+ "scripts": {},
24
+ "homepage": "https://stdlib.io",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git://github.com/stdlib-js/fs-resolve-parent-path.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/stdlib-js/stdlib/issues"
31
+ },
32
+ "dependencies": {
33
+ "@ohos-ports/assert-has-own-property": "0.2.3-beta.1",
34
+ "@ohos-ports/assert-is-function": "0.2.3-beta.2",
35
+ "@ohos-ports/assert-is-plain-object": "0.2.3-beta.2",
36
+ "@ohos-ports/assert-is-string": "0.2.3-beta.2",
37
+ "@ohos-ports/fs-exists": "0.2.3-beta.2",
38
+ "@ohos-ports/process-cwd": "0.2.3-beta.1",
39
+ "@ohos-ports/string-format": "0.2.3-beta.1",
40
+ "@ohos-ports/utils-define-nonenumerable-read-only-property": "0.2.3-beta.2",
41
+ "@ohos-ports/error-tools-fmtprodmsg": "0.2.3-beta.1"
42
+ },
43
+ "devDependencies": {},
44
+ "engines": {
45
+ "node": ">=0.10.0",
46
+ "npm": ">2.7.0"
47
+ },
48
+ "os": [
49
+ "aix",
50
+ "darwin",
51
+ "freebsd",
52
+ "linux",
53
+ "macos",
54
+ "openbsd",
55
+ "sunos",
56
+ "win32",
57
+ "windows",
58
+ "openharmony"
59
+ ],
60
+ "keywords": [
61
+ "stdlib",
62
+ "stdfs",
63
+ "fs",
64
+ "resolve",
65
+ "parent",
66
+ "path",
67
+ "async",
68
+ "sync",
69
+ "file",
70
+ "directory",
71
+ "dir",
72
+ "find",
73
+ "up",
74
+ "findup",
75
+ "find-up",
76
+ "upsearch",
77
+ "search",
78
+ "lookup",
79
+ "look-up",
80
+ "locate",
81
+ "walk",
82
+ "filesystem"
83
+ ],
84
+ "funding": {
85
+ "type": "opencollective",
86
+ "url": "https://opencollective.com/stdlib"
87
+ }
88
+ }