@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.
Files changed (173) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +230 -2
  4. package/SECURITY.md +5 -0
  5. package/append-file/README.md +239 -0
  6. package/append-file/bin/cli +110 -0
  7. package/append-file/docs/repl.txt +72 -0
  8. package/append-file/docs/types/index.d.ts +133 -0
  9. package/append-file/docs/types/test.ts +193 -0
  10. package/append-file/docs/usage.txt +10 -0
  11. package/append-file/etc/cli_opts.json +22 -0
  12. package/append-file/lib/async.js +58 -0
  13. package/append-file/lib/index.js +61 -0
  14. package/append-file/lib/sync.js +58 -0
  15. package/append-file/package.json +69 -0
  16. package/close/README.md +172 -0
  17. package/close/docs/repl.txt +45 -0
  18. package/close/docs/types/index.d.ts +135 -0
  19. package/close/docs/types/test.ts +89 -0
  20. package/close/lib/index.js +73 -0
  21. package/close/lib/main.js +68 -0
  22. package/close/lib/sync.js +65 -0
  23. package/close/package.json +64 -0
  24. package/docs/types/index.d.ts +502 -0
  25. package/exists/README.md +252 -0
  26. package/exists/bin/cli +83 -0
  27. package/exists/docs/repl.txt +42 -0
  28. package/exists/docs/types/index.d.ts +112 -0
  29. package/exists/docs/types/test.ts +87 -0
  30. package/exists/docs/usage.txt +8 -0
  31. package/exists/etc/cli_opts.json +14 -0
  32. package/exists/lib/async.js +85 -0
  33. package/exists/lib/index.js +66 -0
  34. package/exists/lib/sync.js +62 -0
  35. package/exists/package.json +69 -0
  36. package/lib/index.js +186 -0
  37. package/open/README.md +204 -0
  38. package/open/docs/repl.txt +65 -0
  39. package/open/docs/types/index.d.ts +150 -0
  40. package/open/docs/types/test.ts +129 -0
  41. package/open/lib/defaults.json +4 -0
  42. package/open/lib/index.js +63 -0
  43. package/open/lib/main.js +64 -0
  44. package/open/lib/sync.js +68 -0
  45. package/open/package.json +64 -0
  46. package/package.json +67 -4
  47. package/read-dir/README.md +232 -0
  48. package/read-dir/bin/cli +83 -0
  49. package/read-dir/docs/repl.txt +44 -0
  50. package/read-dir/docs/types/index.d.ts +99 -0
  51. package/read-dir/docs/types/test.ts +85 -0
  52. package/read-dir/docs/usage.txt +8 -0
  53. package/read-dir/etc/cli_opts.json +14 -0
  54. package/read-dir/lib/async.js +57 -0
  55. package/read-dir/lib/index.js +62 -0
  56. package/read-dir/lib/sync.js +52 -0
  57. package/read-dir/package.json +69 -0
  58. package/read-file/README.md +246 -0
  59. package/read-file/bin/cli +91 -0
  60. package/read-file/docs/repl.txt +68 -0
  61. package/read-file/docs/types/index.d.ts +138 -0
  62. package/read-file/docs/types/test.ts +147 -0
  63. package/read-file/docs/usage.txt +10 -0
  64. package/read-file/etc/cli_opts.json +21 -0
  65. package/read-file/lib/index.js +61 -0
  66. package/read-file/lib/main.js +59 -0
  67. package/read-file/lib/sync.js +61 -0
  68. package/read-file/package.json +68 -0
  69. package/read-file-list/README.md +197 -0
  70. package/read-file-list/bin/cli +100 -0
  71. package/read-file-list/docs/repl.txt +81 -0
  72. package/read-file-list/docs/types/index.d.ts +166 -0
  73. package/read-file-list/docs/types/test.ts +166 -0
  74. package/read-file-list/docs/usage.txt +10 -0
  75. package/read-file-list/etc/cli_opts.json +21 -0
  76. package/read-file-list/lib/async.js +202 -0
  77. package/read-file-list/lib/clear_pending.js +47 -0
  78. package/read-file-list/lib/clear_timeout.js +23 -0
  79. package/read-file-list/lib/config.json +4 -0
  80. package/read-file-list/lib/delay.js +47 -0
  81. package/read-file-list/lib/index.js +66 -0
  82. package/read-file-list/lib/sync.js +94 -0
  83. package/read-file-list/package.json +72 -0
  84. package/read-json/README.md +176 -0
  85. package/read-json/docs/repl.txt +72 -0
  86. package/read-json/docs/types/index.d.ts +170 -0
  87. package/read-json/docs/types/test.ts +175 -0
  88. package/read-json/lib/async.js +114 -0
  89. package/read-json/lib/index.js +65 -0
  90. package/read-json/lib/sync.js +89 -0
  91. package/read-json/package.json +66 -0
  92. package/read-ndjson/README.md +166 -0
  93. package/read-ndjson/docs/repl.txt +72 -0
  94. package/read-ndjson/docs/types/index.d.ts +170 -0
  95. package/read-ndjson/docs/types/test.ts +179 -0
  96. package/read-ndjson/lib/async.js +114 -0
  97. package/read-ndjson/lib/index.js +65 -0
  98. package/read-ndjson/lib/sync.js +89 -0
  99. package/read-ndjson/package.json +68 -0
  100. package/read-wasm/README.md +158 -0
  101. package/read-wasm/docs/repl.txt +58 -0
  102. package/read-wasm/docs/types/index.d.ts +153 -0
  103. package/read-wasm/docs/types/test.ts +127 -0
  104. package/read-wasm/lib/async.js +106 -0
  105. package/read-wasm/lib/index.js +67 -0
  106. package/read-wasm/lib/sync.js +86 -0
  107. package/read-wasm/package.json +67 -0
  108. package/rename/README.md +259 -0
  109. package/rename/bin/cli +89 -0
  110. package/rename/docs/repl.txt +80 -0
  111. package/rename/docs/types/index.d.ts +109 -0
  112. package/rename/docs/types/test.ts +111 -0
  113. package/rename/docs/usage.txt +7 -0
  114. package/rename/etc/cli_opts.json +15 -0
  115. package/rename/lib/index.js +60 -0
  116. package/rename/lib/main.js +51 -0
  117. package/rename/lib/sync.js +53 -0
  118. package/rename/package.json +73 -0
  119. package/resolve-parent-path/README.md +204 -0
  120. package/resolve-parent-path/bin/cli +85 -0
  121. package/resolve-parent-path/docs/repl.txt +58 -0
  122. package/resolve-parent-path/docs/types/index.d.ts +128 -0
  123. package/resolve-parent-path/docs/types/test.ts +117 -0
  124. package/resolve-parent-path/docs/usage.txt +9 -0
  125. package/resolve-parent-path/etc/cli_opts.json +17 -0
  126. package/resolve-parent-path/lib/async.js +116 -0
  127. package/resolve-parent-path/lib/index.js +58 -0
  128. package/resolve-parent-path/lib/sync.js +83 -0
  129. package/resolve-parent-path/lib/validate.js +67 -0
  130. package/resolve-parent-path/package.json +78 -0
  131. package/resolve-parent-path-by/README.md +220 -0
  132. package/resolve-parent-path-by/docs/repl.txt +105 -0
  133. package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
  134. package/resolve-parent-path-by/docs/types/test.ts +225 -0
  135. package/resolve-parent-path-by/lib/async.js +156 -0
  136. package/resolve-parent-path-by/lib/index.js +67 -0
  137. package/resolve-parent-path-by/lib/sync.js +98 -0
  138. package/resolve-parent-path-by/lib/validate.js +67 -0
  139. package/resolve-parent-path-by/package.json +76 -0
  140. package/resolve-parent-paths/README.md +228 -0
  141. package/resolve-parent-paths/bin/cli +93 -0
  142. package/resolve-parent-paths/docs/repl.txt +84 -0
  143. package/resolve-parent-paths/docs/types/index.d.ts +145 -0
  144. package/resolve-parent-paths/docs/types/test.ts +196 -0
  145. package/resolve-parent-paths/docs/usage.txt +10 -0
  146. package/resolve-parent-paths/etc/cli_opts.json +18 -0
  147. package/resolve-parent-paths/lib/index.js +60 -0
  148. package/resolve-parent-paths/lib/main.js +415 -0
  149. package/resolve-parent-paths/lib/sync.js +231 -0
  150. package/resolve-parent-paths/lib/validate.js +81 -0
  151. package/resolve-parent-paths/package.json +78 -0
  152. package/unlink/README.md +233 -0
  153. package/unlink/bin/cli +81 -0
  154. package/unlink/docs/repl.txt +62 -0
  155. package/unlink/docs/types/index.d.ts +105 -0
  156. package/unlink/docs/types/test.ts +85 -0
  157. package/unlink/docs/usage.txt +7 -0
  158. package/unlink/etc/cli_opts.json +15 -0
  159. package/unlink/lib/index.js +60 -0
  160. package/unlink/lib/main.js +50 -0
  161. package/unlink/lib/sync.js +52 -0
  162. package/unlink/package.json +70 -0
  163. package/write-file/README.md +250 -0
  164. package/write-file/bin/cli +110 -0
  165. package/write-file/docs/repl.txt +74 -0
  166. package/write-file/docs/types/index.d.ts +133 -0
  167. package/write-file/docs/types/test.ts +193 -0
  168. package/write-file/docs/usage.txt +10 -0
  169. package/write-file/etc/cli_opts.json +22 -0
  170. package/write-file/lib/index.js +60 -0
  171. package/write-file/lib/main.js +58 -0
  172. package/write-file/lib/sync.js +58 -0
  173. package/write-file/package.json +68 -0
@@ -0,0 +1,225 @@
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
+ import resolveParentPathBy = require( './index' );
20
+
21
+ /**
22
+ * Callback to invoke after processing a path.
23
+ *
24
+ * @param error - error object or null
25
+ * @param result - test result
26
+ */
27
+ type Callback = ( error: Error | null, result: boolean ) => void;
28
+
29
+ /**
30
+ * Checks whether a path passes a test.
31
+ *
32
+ * @param path - resolved path
33
+ * @param next - callback
34
+ */
35
+ function predicate( path: string, next: Callback ): void {
36
+ next( null, ( path === path ) );
37
+ }
38
+
39
+ /**
40
+ * Checks whether a path passes a test.
41
+ *
42
+ * @param path - resolved path
43
+ */
44
+ function predicateSync( path: string ): boolean {
45
+ return ( path === path );
46
+ }
47
+
48
+ /**
49
+ * Callback invoked upon resolving a path.
50
+ *
51
+ * @param error - error object
52
+ * @param path - resolved path
53
+ */
54
+ function done( error: Error | null, path: string | null ): void {
55
+ if ( error || path === null ) {
56
+ throw new Error( 'beep' );
57
+ }
58
+ }
59
+
60
+
61
+ // TESTS //
62
+
63
+ // The function returns void...
64
+ {
65
+ resolveParentPathBy( 'package.json', predicate, done ); // $ExpectType void
66
+ resolveParentPathBy( 'package.json', {}, predicate, done ); // $ExpectType void
67
+ }
68
+
69
+ // The compiler throws an error if the function is provided a first argument which is not a string...
70
+ {
71
+ resolveParentPathBy( 123, predicate, done ); // $ExpectError
72
+ resolveParentPathBy( false, predicate, done ); // $ExpectError
73
+ resolveParentPathBy( true, predicate, done ); // $ExpectError
74
+ resolveParentPathBy( null, predicate, done ); // $ExpectError
75
+ resolveParentPathBy( undefined, predicate, done ); // $ExpectError
76
+ resolveParentPathBy( [], predicate, done ); // $ExpectError
77
+ resolveParentPathBy( {}, predicate, done ); // $ExpectError
78
+ resolveParentPathBy( ( x: number ): number => x, predicate, done ); // $ExpectError
79
+
80
+ resolveParentPathBy( 123, {}, predicate, done ); // $ExpectError
81
+ resolveParentPathBy( false, {}, predicate, done ); // $ExpectError
82
+ resolveParentPathBy( true, {}, predicate, done ); // $ExpectError
83
+ resolveParentPathBy( null, {}, predicate, done ); // $ExpectError
84
+ resolveParentPathBy( undefined, {}, predicate, done ); // $ExpectError
85
+ resolveParentPathBy( [], {}, predicate, done ); // $ExpectError
86
+ resolveParentPathBy( {}, {}, predicate, done ); // $ExpectError
87
+ resolveParentPathBy( ( x: number ): number => x, {}, predicate, done ); // $ExpectError
88
+ }
89
+
90
+ // The compiler throws an error if the function is provided a predicate function argument which is not a function with the expected signature...
91
+ {
92
+ resolveParentPathBy( '/var/log/', 1, done ); // $ExpectError
93
+ resolveParentPathBy( '/var/log/', false, done ); // $ExpectError
94
+ resolveParentPathBy( '/var/log/', true, done ); // $ExpectError
95
+ resolveParentPathBy( '/var/log/', null, done ); // $ExpectError
96
+ resolveParentPathBy( '/var/log/', undefined, done ); // $ExpectError
97
+ resolveParentPathBy( '/var/log/', [], done ); // $ExpectError
98
+ resolveParentPathBy( '/var/log/', {}, done ); // $ExpectError
99
+ resolveParentPathBy( '/var/log/', ( x: number ): number => x, done ); // $ExpectError
100
+
101
+ resolveParentPathBy( '/var/log/', {}, 1, done ); // $ExpectError
102
+ resolveParentPathBy( '/var/log/', {}, false, done ); // $ExpectError
103
+ resolveParentPathBy( '/var/log/', {}, true, done ); // $ExpectError
104
+ resolveParentPathBy( '/var/log/', {}, null, done ); // $ExpectError
105
+ resolveParentPathBy( '/var/log/', {}, undefined, done ); // $ExpectError
106
+ resolveParentPathBy( '/var/log/', {}, [], done ); // $ExpectError
107
+ resolveParentPathBy( '/var/log/', {}, {}, done ); // $ExpectError
108
+ resolveParentPathBy( '/var/log/', {}, ( x: number ): number => x, done ); // $ExpectError
109
+ }
110
+
111
+ // The compiler throws an error if the function is provided a "done" callback argument which is not a function with the expected signature...
112
+ {
113
+ resolveParentPathBy( '/var/log/', predicate, 1 ); // $ExpectError
114
+ resolveParentPathBy( '/var/log/', predicate, false ); // $ExpectError
115
+ resolveParentPathBy( '/var/log/', predicate, true ); // $ExpectError
116
+ resolveParentPathBy( '/var/log/', predicate, null ); // $ExpectError
117
+ resolveParentPathBy( '/var/log/', predicate, undefined ); // $ExpectError
118
+ resolveParentPathBy( '/var/log/', predicate, [] ); // $ExpectError
119
+ resolveParentPathBy( '/var/log/', predicate, {} ); // $ExpectError
120
+ resolveParentPathBy( '/var/log/', predicate, ( x: number ): number => x ); // $ExpectError
121
+
122
+ resolveParentPathBy( '/var/log/', {}, predicate, 1 ); // $ExpectError
123
+ resolveParentPathBy( '/var/log/', {}, predicate, false ); // $ExpectError
124
+ resolveParentPathBy( '/var/log/', {}, predicate, true ); // $ExpectError
125
+ resolveParentPathBy( '/var/log/', {}, predicate, null ); // $ExpectError
126
+ resolveParentPathBy( '/var/log/', {}, predicate, undefined ); // $ExpectError
127
+ resolveParentPathBy( '/var/log/', {}, predicate, [] ); // $ExpectError
128
+ resolveParentPathBy( '/var/log/', {}, predicate, {} ); // $ExpectError
129
+ resolveParentPathBy( '/var/log/', {}, predicate, ( x: number ): number => x ); // $ExpectError
130
+ }
131
+
132
+ // The compiler throws an error if the function is provided an options argument which is not an object...
133
+ {
134
+ resolveParentPathBy( 'package.json', null, predicate, done ); // $ExpectError
135
+ }
136
+
137
+ // The compiler throws an error if the function is provided an `dir` option which is not a string...
138
+ {
139
+ resolveParentPathBy( 'package.json', { 'dir': 123 }, predicate, done ); // $ExpectError
140
+ resolveParentPathBy( 'package.json', { 'dir': true }, predicate, done ); // $ExpectError
141
+ resolveParentPathBy( 'package.json', { 'dir': false }, predicate, done ); // $ExpectError
142
+ resolveParentPathBy( 'package.json', { 'dir': null }, predicate, done ); // $ExpectError
143
+ resolveParentPathBy( 'package.json', { 'dir': [] }, predicate, done ); // $ExpectError
144
+ resolveParentPathBy( 'package.json', { 'dir': {} }, predicate, done ); // $ExpectError
145
+ resolveParentPathBy( 'package.json', { 'dir': ( x: number ): number => x }, predicate, done ); // $ExpectError
146
+ }
147
+
148
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
149
+ {
150
+ resolveParentPathBy(); // $ExpectError
151
+ resolveParentPathBy( 'C:\\foo\\bar\\baz' ); // $ExpectError
152
+ resolveParentPathBy( 'C:\\foo\\bar\\baz', {} ); // $ExpectError
153
+ resolveParentPathBy( 'C:\\foo\\bar\\baz', predicate ); // $ExpectError
154
+ resolveParentPathBy( 'C:\\foo\\bar\\baz', {}, predicate ); // $ExpectError
155
+ resolveParentPathBy( 'C:\\foo\\bar\\baz', {}, predicate, done, {} ); // $ExpectError
156
+ }
157
+
158
+ // Attached to the main export is a `sync` method which returns a string or null...
159
+ {
160
+ resolveParentPathBy.sync( 'package.json', predicateSync ); // $ExpectType string | null
161
+ resolveParentPathBy.sync( 'package.json', {}, predicateSync ); // $ExpectType string | null
162
+ }
163
+
164
+ // The compiler throws an error if the `sync` method is provided a first argument which is not a string...
165
+ {
166
+ resolveParentPathBy.sync( 123, predicateSync ); // $ExpectError
167
+ resolveParentPathBy.sync( false, predicateSync ); // $ExpectError
168
+ resolveParentPathBy.sync( true, predicateSync ); // $ExpectError
169
+ resolveParentPathBy.sync( null, predicateSync ); // $ExpectError
170
+ resolveParentPathBy.sync( undefined, predicateSync ); // $ExpectError
171
+ resolveParentPathBy.sync( [], predicateSync ); // $ExpectError
172
+ resolveParentPathBy.sync( {}, predicateSync ); // $ExpectError
173
+ resolveParentPathBy.sync( ( x: number ): number => x, predicateSync ); // $ExpectError
174
+
175
+ resolveParentPathBy.sync( 123, {}, predicateSync ); // $ExpectError
176
+ resolveParentPathBy.sync( false, {}, predicateSync ); // $ExpectError
177
+ resolveParentPathBy.sync( true, {}, predicateSync ); // $ExpectError
178
+ resolveParentPathBy.sync( null, {}, predicateSync ); // $ExpectError
179
+ resolveParentPathBy.sync( undefined, {}, predicateSync ); // $ExpectError
180
+ resolveParentPathBy.sync( [], {}, predicateSync ); // $ExpectError
181
+ resolveParentPathBy.sync( {}, {}, predicateSync ); // $ExpectError
182
+ resolveParentPathBy.sync( ( x: number ): number => x, {}, predicateSync ); // $ExpectError
183
+ }
184
+
185
+ // The compiler throws an error if the `sync` method is provided a predicate function argument which is not a function with the expected signature...
186
+ {
187
+ resolveParentPathBy.sync( 'package.json', 123 ); // $ExpectError
188
+ resolveParentPathBy.sync( 'package.json', false ); // $ExpectError
189
+ resolveParentPathBy.sync( 'package.json', true ); // $ExpectError
190
+ resolveParentPathBy.sync( 'package.json', null ); // $ExpectError
191
+ resolveParentPathBy.sync( 'package.json', undefined ); // $ExpectError
192
+ resolveParentPathBy.sync( 'package.json', [] ); // $ExpectError
193
+ resolveParentPathBy.sync( 'package.json', {} ); // $ExpectError
194
+
195
+ resolveParentPathBy.sync( 'package.json', {}, 123 ); // $ExpectError
196
+ resolveParentPathBy.sync( 'package.json', {}, false ); // $ExpectError
197
+ resolveParentPathBy.sync( 'package.json', {}, true ); // $ExpectError
198
+ resolveParentPathBy.sync( 'package.json', {}, null ); // $ExpectError
199
+ resolveParentPathBy.sync( 'package.json', {}, undefined ); // $ExpectError
200
+ resolveParentPathBy.sync( 'package.json', {}, [] ); // $ExpectError
201
+ resolveParentPathBy.sync( 'package.json', {}, {} ); // $ExpectError
202
+ }
203
+
204
+ // The compiler throws an error if the `sync` method is provided an options argument which is not an object...
205
+ {
206
+ resolveParentPathBy.sync( 'package.json', null, predicateSync ); // $ExpectError
207
+ }
208
+
209
+ // The compiler throws an error if the `sync` method is provided an `dir` option which is not a string...
210
+ {
211
+ resolveParentPathBy.sync( 'package.json', { 'dir': 123 }, predicateSync ); // $ExpectError
212
+ resolveParentPathBy.sync( 'package.json', { 'dir': true }, predicateSync ); // $ExpectError
213
+ resolveParentPathBy.sync( 'package.json', { 'dir': false }, predicateSync ); // $ExpectError
214
+ resolveParentPathBy.sync( 'package.json', { 'dir': null }, predicateSync ); // $ExpectError
215
+ resolveParentPathBy.sync( 'package.json', { 'dir': [] }, predicateSync ); // $ExpectError
216
+ resolveParentPathBy.sync( 'package.json', { 'dir': {} }, predicateSync ); // $ExpectError
217
+ resolveParentPathBy.sync( 'package.json', { 'dir': ( x: number ): number => x }, predicateSync ); // $ExpectError
218
+ }
219
+
220
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
221
+ {
222
+ resolveParentPathBy.sync(); // $ExpectError
223
+ resolveParentPathBy.sync( 'package.json' ); // $ExpectError
224
+ resolveParentPathBy.sync( 'package.json', {}, predicateSync, {} ); // $ExpectError
225
+ }
@@ -0,0 +1,156 @@
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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var resolve = require( 'path' ).resolve;
24
+ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25
+ var isFunction = require( '@stdlib/assert/is-function' );
26
+ var format = require( '@stdlib/string/format' );
27
+ var cwd = require( '@stdlib/process/cwd' );
28
+ var exists = require( './../../exists' );
29
+ var validate = require( './validate.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Asynchronously resolves a path according to a predicate function 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 {Function} predicate - callback which tests whether a resolved path passes a test
41
+ * @param {Function} clbk - callback to invoke after resolving a path
42
+ * @throws {TypeError} first argument must be a string
43
+ * @throws {TypeError} predicate function argument must be a function
44
+ * @throws {TypeError} callback argument must be a function
45
+ * @throws {TypeError} options argument must be an object
46
+ * @throws {TypeError} must provide valid options
47
+ *
48
+ * @example
49
+ * resolveParentPathBy( 'package.json', predicate, onPath );
50
+ *
51
+ * function predicate( path, next ) {
52
+ * next( null, true );
53
+ * }
54
+ *
55
+ * function onPath( error, path ) {
56
+ * if ( error ) {
57
+ * throw error;
58
+ * }
59
+ * console.log( path );
60
+ * }
61
+ */
62
+ function resolveParentPathBy( path, options, predicate, clbk ) {
63
+ var spath;
64
+ var child;
65
+ var test;
66
+ var opts;
67
+ var done;
68
+ var dir;
69
+ var err;
70
+ if ( !isString( path ) ) {
71
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', path ) );
72
+ }
73
+ opts = {};
74
+ if ( arguments.length > 3 ) {
75
+ err = validate( opts, options );
76
+ if ( err ) {
77
+ throw err;
78
+ }
79
+ test = predicate;
80
+ done = clbk;
81
+ } else {
82
+ test = options;
83
+ done = predicate;
84
+ }
85
+ if ( !isFunction( test ) ) {
86
+ throw new TypeError( format( 'invalid argument. Predicate function must be a function. Value: `%s`.', test ) );
87
+ }
88
+ if ( !isFunction( done ) ) {
89
+ throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
90
+ }
91
+ if ( opts.dir ) {
92
+ dir = resolve( cwd(), opts.dir );
93
+ } else {
94
+ dir = cwd();
95
+ }
96
+ spath = resolve( dir, path );
97
+ exists( spath, onExists );
98
+
99
+ /**
100
+ * Resolves the next candidate path.
101
+ *
102
+ * @private
103
+ * @returns {void}
104
+ */
105
+ function next() {
106
+ // Resolve a parent directory:
107
+ child = dir;
108
+ dir = resolve( dir, '..' );
109
+
110
+ // If we have already reached root, we cannot resolve any higher directories...
111
+ if ( child === dir ) {
112
+ return done( null, null );
113
+ }
114
+ // Resolve the next search path:
115
+ spath = resolve( dir, path );
116
+ exists( spath, onExists );
117
+ }
118
+
119
+ /**
120
+ * Callback invoked after checking for path existence.
121
+ *
122
+ * @private
123
+ * @param {(Error|null)} error - error object
124
+ * @param {boolean} bool - boolean indicating if a path exists
125
+ * @returns {void}
126
+ */
127
+ function onExists( error, bool ) { // eslint-disable-line n/handle-callback-err
128
+ if ( bool ) {
129
+ return test( spath, onTest );
130
+ }
131
+ next();
132
+ }
133
+
134
+ /**
135
+ * Callback invoked after testing a resolved path.
136
+ *
137
+ * @private
138
+ * @param {(Error|null)} error - error object
139
+ * @param {boolean} bool - boolean indicating if a path exists
140
+ * @returns {void}
141
+ */
142
+ function onTest( error, bool ) {
143
+ if ( error ) {
144
+ return done( error );
145
+ }
146
+ if ( bool ) {
147
+ return done( null, spath );
148
+ }
149
+ next();
150
+ }
151
+ }
152
+
153
+
154
+ // EXPORTS //
155
+
156
+ module.exports = resolveParentPathBy;
@@ -0,0 +1,67 @@
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
+ 'use strict';
20
+
21
+ /**
22
+ * Resolve a path according to a predicate function by walking parent directories.
23
+ *
24
+ * @module @stdlib/fs/resolve-parent-path-by
25
+ *
26
+ * @example
27
+ * var resolveParentPathBy = require( '@stdlib/fs/resolve-parent-path-by' );
28
+ *
29
+ * resolveParentPathBy( 'package.json', predicate, onPath );
30
+ *
31
+ * function predicate( path, next ) {
32
+ * next( null, true );
33
+ * }
34
+ *
35
+ * function onPath( error, path ) {
36
+ * if ( error ) {
37
+ * throw error;
38
+ * }
39
+ * console.log( path );
40
+ * }
41
+ *
42
+ * @example
43
+ * var resolveParentPathBy = require( '@stdlib/fs/resolve-parent-path-by' );
44
+ *
45
+ * function predicate() {
46
+ * return true;
47
+ * }
48
+ *
49
+ * var path = resolveParentPathBy.sync( 'package.json', predicate );
50
+ * // e.g., returns '...'
51
+ */
52
+
53
+ // MODULES //
54
+
55
+ var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
56
+ var resolveParentPathBy = require( './async.js' );
57
+ var sync = require( './sync.js' );
58
+
59
+
60
+ // MAIN //
61
+
62
+ setReadOnly( resolveParentPathBy, 'sync', sync );
63
+
64
+
65
+ // EXPORTS //
66
+
67
+ module.exports = resolveParentPathBy;
@@ -0,0 +1,98 @@
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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var resolve = require( 'path' ).resolve;
24
+ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25
+ var isFunction = require( '@stdlib/assert/is-function' );
26
+ var cwd = require( '@stdlib/process/cwd' );
27
+ var exists = require( './../../exists' ).sync;
28
+ var format = require( '@stdlib/string/format' );
29
+ var validate = require( './validate.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Synchronously resolves a path according to a predicate function 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 {Function} predicate - callback which tests whether a resolved path passes a test
41
+ * @throws {TypeError} first argument must be a string
42
+ * @throws {TypeError} options argument must be an object
43
+ * @throws {TypeError} must provide valid options
44
+ * @throws {TypeError} last argument must be a function
45
+ * @returns {(string|null)} resolved path or null
46
+ *
47
+ * @example
48
+ * function predicate() {
49
+ * return true;
50
+ * }
51
+ *
52
+ * var path = resolveParentPathBy( 'package.json', predicate );
53
+ * // e.g., returns '...'
54
+ */
55
+ function resolveParentPathBy( path, options, predicate ) {
56
+ var spath;
57
+ var child;
58
+ var test;
59
+ var opts;
60
+ var dir;
61
+ var err;
62
+ if ( !isString( path ) ) {
63
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', path ) );
64
+ }
65
+ opts = {};
66
+ if ( arguments.length > 2 ) {
67
+ err = validate( opts, options );
68
+ if ( err ) {
69
+ throw err;
70
+ }
71
+ test = predicate;
72
+ } else {
73
+ test = options;
74
+ }
75
+ if ( !isFunction( test ) ) {
76
+ throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', test ) );
77
+ }
78
+ if ( opts.dir ) {
79
+ dir = resolve( cwd(), opts.dir );
80
+ } else {
81
+ dir = cwd();
82
+ }
83
+ // 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...
84
+ while ( child !== dir ) {
85
+ spath = resolve( dir, path );
86
+ if ( exists( spath ) && test( spath ) ) {
87
+ return spath;
88
+ }
89
+ child = dir;
90
+ dir = resolve( dir, '..' );
91
+ }
92
+ return null;
93
+ }
94
+
95
+
96
+ // EXPORTS //
97
+
98
+ module.exports = resolveParentPathBy;
@@ -0,0 +1,67 @@
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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var isObject = require( '@stdlib/assert/is-plain-object' );
24
+ var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25
+ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26
+ var format = require( '@stdlib/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;
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@stdlib/fs/resolve-parent-path-by",
3
+ "version": "0.0.0",
4
+ "description": "Resolve a path according to a predicate function 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
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {},
26
+ "homepage": "https://github.com/stdlib-js/stdlib",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git://github.com/stdlib-js/stdlib.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/stdlib-js/stdlib/issues"
33
+ },
34
+ "dependencies": {},
35
+ "devDependencies": {},
36
+ "engines": {
37
+ "node": ">=0.10.0",
38
+ "npm": ">2.7.0"
39
+ },
40
+ "os": [
41
+ "aix",
42
+ "darwin",
43
+ "freebsd",
44
+ "linux",
45
+ "macos",
46
+ "openbsd",
47
+ "sunos",
48
+ "win32",
49
+ "windows"
50
+ ],
51
+ "keywords": [
52
+ "stdlib",
53
+ "stdfs",
54
+ "fs",
55
+ "resolve",
56
+ "parent",
57
+ "path",
58
+ "async",
59
+ "sync",
60
+ "file",
61
+ "directory",
62
+ "dir",
63
+ "find",
64
+ "up",
65
+ "findup",
66
+ "find-up",
67
+ "upsearch",
68
+ "search",
69
+ "lookup",
70
+ "look-up",
71
+ "locate",
72
+ "walk",
73
+ "filesystem",
74
+ "predicate"
75
+ ]
76
+ }