@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,196 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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 resolveParentPaths = require( './index' );
20
+
21
+ /**
22
+ * Callback function.
23
+ *
24
+ * @param error - error object
25
+ * @param path - resolved path
26
+ */
27
+ function done( error: Error | null, paths: Array<string|null> ): void {
28
+ if ( error || paths === null ) {
29
+ throw error;
30
+ }
31
+ }
32
+
33
+
34
+ // TESTS //
35
+
36
+ // The function does not have a return value...
37
+ {
38
+ resolveParentPaths( [ 'package.json' ], done ); // $ExpectType void
39
+ resolveParentPaths( [ 'package.json' ], {}, done ); // $ExpectType void
40
+ }
41
+
42
+ // The compiler throws an error if the function is provided a first argument which is not an array of strings...
43
+ {
44
+ resolveParentPaths( 123, done ); // $ExpectError
45
+ resolveParentPaths( false, done ); // $ExpectError
46
+ resolveParentPaths( true, done ); // $ExpectError
47
+ resolveParentPaths( null, done ); // $ExpectError
48
+ resolveParentPaths( undefined, done ); // $ExpectError
49
+ resolveParentPaths( {}, done ); // $ExpectError
50
+ resolveParentPaths( ( x: number ): number => x, done ); // $ExpectError
51
+ resolveParentPaths( 'beep', done ); // $ExpectError
52
+ resolveParentPaths( [ 1, 2 ], done ); // $ExpectError
53
+
54
+ resolveParentPaths( 123, {}, done ); // $ExpectError
55
+ resolveParentPaths( false, {}, done ); // $ExpectError
56
+ resolveParentPaths( true, {}, done ); // $ExpectError
57
+ resolveParentPaths( null, {}, done ); // $ExpectError
58
+ resolveParentPaths( undefined, {}, done ); // $ExpectError
59
+ resolveParentPaths( {}, {}, done ); // $ExpectError
60
+ resolveParentPaths( ( x: number ): number => x, {}, done ); // $ExpectError
61
+ resolveParentPaths( 'beep', {}, done ); // $ExpectError
62
+ resolveParentPaths( [ 1, 2 ], {}, done ); // $ExpectError
63
+ }
64
+
65
+ // The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature...
66
+ {
67
+ resolveParentPaths( [ '/var/log/' ], 1 ); // $ExpectError
68
+ resolveParentPaths( [ '/var/log/' ], false ); // $ExpectError
69
+ resolveParentPaths( [ '/var/log/' ], true ); // $ExpectError
70
+ resolveParentPaths( [ '/var/log/' ], null ); // $ExpectError
71
+ resolveParentPaths( [ '/var/log/' ], undefined ); // $ExpectError
72
+ resolveParentPaths( [ '/var/log/' ], [] ); // $ExpectError
73
+ resolveParentPaths( [ '/var/log/' ], {} ); // $ExpectError
74
+ resolveParentPaths( [ '/var/log/' ], ( x: number ): number => x ); // $ExpectError
75
+
76
+ resolveParentPaths( [ '/var/log/' ], {}, 1 ); // $ExpectError
77
+ resolveParentPaths( [ '/var/log/' ], {}, false ); // $ExpectError
78
+ resolveParentPaths( [ '/var/log/' ], {}, true ); // $ExpectError
79
+ resolveParentPaths( [ '/var/log/' ], {}, null ); // $ExpectError
80
+ resolveParentPaths( [ '/var/log/' ], {}, undefined ); // $ExpectError
81
+ resolveParentPaths( [ '/var/log/' ], {}, [] ); // $ExpectError
82
+ resolveParentPaths( [ '/var/log/' ], {}, {} ); // $ExpectError
83
+ resolveParentPaths( [ '/var/log/' ], {}, ( x: number ): number => x ); // $ExpectError
84
+ }
85
+
86
+ // The compiler throws an error if the function is provided an options argument which is not an object...
87
+ {
88
+ resolveParentPaths( [ 'package.json' ], '5', done ); // $ExpectError
89
+ resolveParentPaths( [ 'package.json' ], 5, done ); // $ExpectError
90
+ resolveParentPaths( [ 'package.json' ], true, done ); // $ExpectError
91
+ resolveParentPaths( [ 'package.json' ], false, done ); // $ExpectError
92
+ resolveParentPaths( [ 'package.json' ], null, done ); // $ExpectError
93
+ resolveParentPaths( [ 'package.json' ], undefined, done ); // $ExpectError
94
+ resolveParentPaths( [ 'package.json' ], [], done ); // $ExpectError
95
+ resolveParentPaths( [ 'package.json' ], ( x: number ): number => x, done ); // $ExpectError
96
+ }
97
+
98
+ // The compiler throws an error if the function is provided an `dir` option which is not a string...
99
+ {
100
+ resolveParentPaths( [ 'package.json' ], { 'dir': 123 }, done ); // $ExpectError
101
+ resolveParentPaths( [ 'package.json' ], { 'dir': true }, done ); // $ExpectError
102
+ resolveParentPaths( [ 'package.json' ], { 'dir': false }, done ); // $ExpectError
103
+ resolveParentPaths( [ 'package.json' ], { 'dir': null }, done ); // $ExpectError
104
+ resolveParentPaths( [ 'package.json' ], { 'dir': [] }, done ); // $ExpectError
105
+ resolveParentPaths( [ 'package.json' ], { 'dir': {} }, done ); // $ExpectError
106
+ resolveParentPaths( [ 'package.json' ], { 'dir': ( x: number ): number => x }, done ); // $ExpectError
107
+ }
108
+
109
+ // The compiler throws an error if the function is provided an `mode` option which is not a recognized string...
110
+ {
111
+ resolveParentPaths( [ 'package.json' ], { 'mode': 'beep' }, done ); // $ExpectError
112
+ resolveParentPaths( [ 'package.json' ], { 'mode': 123 }, done ); // $ExpectError
113
+ resolveParentPaths( [ 'package.json' ], { 'mode': true }, done ); // $ExpectError
114
+ resolveParentPaths( [ 'package.json' ], { 'mode': false }, done ); // $ExpectError
115
+ resolveParentPaths( [ 'package.json' ], { 'mode': null }, done ); // $ExpectError
116
+ resolveParentPaths( [ 'package.json' ], { 'mode': [] }, done ); // $ExpectError
117
+ resolveParentPaths( [ 'package.json' ], { 'mode': {} }, done ); // $ExpectError
118
+ resolveParentPaths( [ 'package.json' ], { 'mode': ( x: number ): number => x }, done ); // $ExpectError
119
+ }
120
+
121
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
122
+ {
123
+ resolveParentPaths(); // $ExpectError
124
+ resolveParentPaths( [ 'C:\\foo\\bar\\baz' ] ); // $ExpectError
125
+ resolveParentPaths( [ 'C:\\foo\\bar\\baz' ], {} ); // $ExpectError
126
+ resolveParentPaths( [ 'C:\\foo\\bar\\baz' ], {}, done, {} ); // $ExpectError
127
+ }
128
+
129
+ // Attached to main export is a `sync` method which returns an array...
130
+ {
131
+ resolveParentPaths.sync( [ 'package.json' ] ); // $ExpectType (string | null)[]
132
+ resolveParentPaths.sync( [ 'package.json' ], {} ); // $ExpectType (string | null)[]
133
+ }
134
+
135
+ // The compiler throws an error if the `sync` method is provided a first argument which is not an array of strings...
136
+ {
137
+ resolveParentPaths.sync( 123 ); // $ExpectError
138
+ resolveParentPaths.sync( false ); // $ExpectError
139
+ resolveParentPaths.sync( true ); // $ExpectError
140
+ resolveParentPaths.sync( null ); // $ExpectError
141
+ resolveParentPaths.sync( undefined ); // $ExpectError
142
+ resolveParentPaths.sync( {} ); // $ExpectError
143
+ resolveParentPaths.sync( ( x: number ): number => x ); // $ExpectError
144
+ resolveParentPaths.sync( 'beep' ); // $ExpectError
145
+ resolveParentPaths.sync( [ 1, 2 ] ); // $ExpectError
146
+
147
+ resolveParentPaths.sync( 123, {} ); // $ExpectError
148
+ resolveParentPaths.sync( false, {} ); // $ExpectError
149
+ resolveParentPaths.sync( true, {} ); // $ExpectError
150
+ resolveParentPaths.sync( null, {} ); // $ExpectError
151
+ resolveParentPaths.sync( undefined, {} ); // $ExpectError
152
+ resolveParentPaths.sync( {}, {} ); // $ExpectError
153
+ resolveParentPaths.sync( ( x: number ): number => x, {} ); // $ExpectError
154
+ resolveParentPaths.sync( 'beep', {} ); // $ExpectError
155
+ resolveParentPaths.sync( [ 1, 2 ], {} ); // $ExpectError
156
+ }
157
+
158
+ // The compiler throws an error if the `sync` method is provided an options argument which is not an object...
159
+ {
160
+ resolveParentPaths.sync( [ 'package.json' ], '5' ); // $ExpectError
161
+ resolveParentPaths.sync( [ 'package.json' ], 5 ); // $ExpectError
162
+ resolveParentPaths.sync( [ 'package.json' ], true ); // $ExpectError
163
+ resolveParentPaths.sync( [ 'package.json' ], false ); // $ExpectError
164
+ resolveParentPaths.sync( [ 'package.json' ], null ); // $ExpectError
165
+ resolveParentPaths.sync( [ 'package.json' ], [] ); // $ExpectError
166
+ resolveParentPaths.sync( [ 'package.json' ], ( x: number ): number => x ); // $ExpectError
167
+ }
168
+
169
+ // The compiler throws an error if the `sync` method is provided an `dir` option which is not a string...
170
+ {
171
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': 123 } ); // $ExpectError
172
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': true } ); // $ExpectError
173
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': false } ); // $ExpectError
174
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': null } ); // $ExpectError
175
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': [] } ); // $ExpectError
176
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': {} } ); // $ExpectError
177
+ resolveParentPaths.sync( [ 'package.json' ], { 'dir': ( x: number ): number => x } ); // $ExpectError
178
+ }
179
+
180
+ // The compiler throws an error if the `sync` method is provided an `mode` option which is not a recognized string...
181
+ {
182
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': 'beep' } ); // $ExpectError
183
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': 123 } ); // $ExpectError
184
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': true } ); // $ExpectError
185
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': false } ); // $ExpectError
186
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': null } ); // $ExpectError
187
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': [] } ); // $ExpectError
188
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': {} } ); // $ExpectError
189
+ resolveParentPaths.sync( [ 'package.json' ], { 'mode': ( x: number ): number => x } ); // $ExpectError
190
+ }
191
+
192
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
193
+ {
194
+ resolveParentPaths.sync(); // $ExpectError
195
+ resolveParentPaths.sync( [ 'package.json' ], {}, {} ); // $ExpectError
196
+ }
@@ -0,0 +1,10 @@
1
+
2
+ Usage: resolve-parent-paths [options] <path> [<path>...]
3
+
4
+ Options:
5
+
6
+ -h, --help Print this message.
7
+ -V, --version Print the package version.
8
+ --dir dir Base search directory.
9
+ --mode mode Mode of operation.
10
+
@@ -0,0 +1,18 @@
1
+ {
2
+ "string": [
3
+ "dir",
4
+ "mode"
5
+ ],
6
+ "boolean": [
7
+ "help",
8
+ "version"
9
+ ],
10
+ "alias": {
11
+ "help": [
12
+ "h"
13
+ ],
14
+ "version": [
15
+ "V"
16
+ ]
17
+ }
18
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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 paths from a set of paths by walking parent directories.
23
+ *
24
+ * @module @stdlib/fs/resolve-parent-paths
25
+ *
26
+ * @example
27
+ * var resolveParentPaths = require( '@stdlib/fs/resolve-parent-paths' );
28
+ *
29
+ * resolveParentPaths( [ 'package.json', 'package-lock.json' ], onPaths );
30
+ *
31
+ * function onPaths( error, paths ) {
32
+ * if ( error ) {
33
+ * throw error;
34
+ * }
35
+ * console.log( paths );
36
+ * }
37
+ *
38
+ * @example
39
+ * var resolveParentPaths = require( '@stdlib/fs/resolve-parent-paths' );
40
+ *
41
+ * var paths = resolveParentPaths.sync( [ 'package.json', 'package-lock.json' ] );
42
+ */
43
+
44
+ // MODULES //
45
+
46
+ var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
47
+ var main = require( './main.js' );
48
+ var sync = require( './sync.js' );
49
+
50
+
51
+ // MAIN //
52
+
53
+ setReadOnly( main, 'sync', sync );
54
+
55
+
56
+ // EXPORTS //
57
+
58
+ module.exports = main;
59
+
60
+ // exports: { "sync": "main.sync" }
@@ -0,0 +1,415 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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 isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25
+ var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' );
26
+ var isFunction = require( '@stdlib/assert/is-function' );
27
+ var cwd = require( '@stdlib/process/cwd' );
28
+ var exists = require( './../../exists' );
29
+ var nulls = require( '@stdlib/array/base/nulls' );
30
+ var format = require( '@stdlib/string/format' );
31
+ var validate = require( './validate.js' );
32
+
33
+
34
+ // VARIABLES //
35
+
36
+ var MODES = {
37
+ 'first': first,
38
+ 'some': some,
39
+ 'all': all,
40
+ 'each': each
41
+ };
42
+
43
+
44
+ // FUNCTIONS //
45
+
46
+ /**
47
+ * Asynchronously resolves the first path match from a set of paths by walking parent directories.
48
+ *
49
+ * @private
50
+ * @param {Array<string>} paths - paths to resolve
51
+ * @param {string} dir - base directory
52
+ * @param {Callback} done - callback to invoke after resolving paths
53
+ * @returns {void}
54
+ */
55
+ function first( paths, dir, done ) {
56
+ var child;
57
+ var spath;
58
+ var idx;
59
+ var out;
60
+
61
+ out = [];
62
+
63
+ // Start at a base directory and continue moving up through each parent directory...
64
+ spath = resolve( dir, paths[ 0 ] );
65
+
66
+ idx = 1; // index of next path
67
+ exists( spath, onExists );
68
+
69
+ /**
70
+ * Callback invoked after checking for path existence.
71
+ *
72
+ * @private
73
+ * @param {(Error|null)} error - error object
74
+ * @param {boolean} bool - boolean indicating if a path exists
75
+ * @returns {void}
76
+ */
77
+ function onExists( error, bool ) { // eslint-disable-line n/handle-callback-err
78
+ if ( bool ) {
79
+ out.push( spath );
80
+ return done( null, out );
81
+ }
82
+ // If we have traversed all paths at the current directory level, resolve parent directory...
83
+ if ( idx === paths.length ) {
84
+ // Resolve a parent directory:
85
+ child = dir;
86
+ dir = resolve( dir, '..' );
87
+
88
+ // If we have already reached root, we cannot resolve any higher directories...
89
+ if ( child === dir ) {
90
+ return done( null, out );
91
+ }
92
+ idx = 0;
93
+ }
94
+ spath = resolve( dir, paths[ idx ] );
95
+ idx += 1;
96
+ exists( spath, onExists );
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Asynchronously resolves one or more paths from a set of paths at a directory level by walking parent directories.
102
+ *
103
+ * @private
104
+ * @param {Array<string>} paths - paths to resolve
105
+ * @param {string} dir - base directory
106
+ * @param {Callback} done - callback to invoke after resolving paths
107
+ * @returns {void}
108
+ */
109
+ function some( paths, dir, done ) {
110
+ var child;
111
+ var spath;
112
+ var FLG;
113
+ var out;
114
+
115
+ FLG = 0; // initialize flag to track if we are done traversing a directory level
116
+ out = [];
117
+
118
+ // Start at a base directory and continue moving up through each parent directory...
119
+ return next( dir );
120
+
121
+ /**
122
+ * Resolves paths within a directory.
123
+ *
124
+ * @private
125
+ * @param {string} dir - directory to search
126
+ */
127
+ function next( dir ) {
128
+ var i;
129
+ for ( i = 0; i < paths.length; i++ ) {
130
+ spath = resolve( dir, paths[ i ] );
131
+ exists( spath, getCallback( spath ) );
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Returns a callback to be invoked upon checking for path existence.
137
+ *
138
+ * @private
139
+ * @param {string} spath - resolved path
140
+ * @returns {Callback} callback
141
+ */
142
+ function getCallback( spath ) {
143
+ return onExists;
144
+
145
+ /**
146
+ * Callback invoked after checking for path existence.
147
+ *
148
+ * @private
149
+ * @param {(Error|null)} error - error object
150
+ * @param {boolean} bool - boolean indicating if a path exists
151
+ * @returns {void}
152
+ */
153
+ function onExists( error, bool ) { // eslint-disable-line n/handle-callback-err
154
+ if ( bool ) {
155
+ out.push( spath );
156
+ }
157
+ FLG += 1;
158
+ if ( FLG === paths.length ) {
159
+ // Check if we have resolved any paths...
160
+ if ( out.length > 0 ) {
161
+ return done( null, out );
162
+ }
163
+ // Resolve a parent directory:
164
+ child = dir;
165
+ dir = resolve( dir, '..' );
166
+
167
+ // Reset flag:
168
+ FLG = 0;
169
+
170
+ // If we have already reached root, we cannot resolve any higher directories...
171
+ if ( child === dir ) {
172
+ return done( null, out );
173
+ }
174
+ // Resolve paths at next directory level:
175
+ return next( dir );
176
+ }
177
+ }
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Asynchronously resolves all paths from a set of paths at a directory level by walking parent directories.
183
+ *
184
+ * @private
185
+ * @param {Array<string>} paths - paths to resolve
186
+ * @param {string} dir - base directory
187
+ * @param {Callback} done - callback to invoke after resolving paths
188
+ * @returns {void}
189
+ */
190
+ function all( paths, dir, done ) {
191
+ var count;
192
+ var child;
193
+ var spath;
194
+ var FLG;
195
+ var out;
196
+
197
+ count = 0; // initialize counter to track if we are done resolving all paths
198
+ FLG = 0; // initialize flag to track if we are done traversing a directory level
199
+ out = nulls( paths.length );
200
+
201
+ // Start at a base directory and continue moving up through each parent directory...
202
+ return next( dir );
203
+
204
+ /**
205
+ * Resolves paths within a directory.
206
+ *
207
+ * @private
208
+ * @param {string} dir - directory to search
209
+ */
210
+ function next( dir ) {
211
+ var i;
212
+ for ( i = 0; i < paths.length; i++ ) {
213
+ spath = resolve( dir, paths[ i ] );
214
+ exists( spath, getCallback( i, spath ) );
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Returns a callback to be invoked upon checking for path existence.
220
+ *
221
+ * @private
222
+ * @param {NonNegativeInteger} idx - index
223
+ * @param {string} spath - resolved path
224
+ * @returns {Callback} callback
225
+ */
226
+ function getCallback( idx, spath ) {
227
+ return onExists;
228
+
229
+ /**
230
+ * Callback invoked after checking for path existence.
231
+ *
232
+ * @private
233
+ * @param {(Error|null)} error - error object
234
+ * @param {boolean} bool - boolean indicating if a path exists
235
+ * @returns {void}
236
+ */
237
+ function onExists( error, bool ) { // eslint-disable-line n/handle-callback-err
238
+ if ( bool ) {
239
+ out[ idx ] = spath;
240
+ count += 1;
241
+ }
242
+ FLG += 1;
243
+ if ( FLG === paths.length ) {
244
+ // Check if we have resolved any path...
245
+ if ( count === paths.length ) {
246
+ return done( null, out );
247
+ }
248
+ // Resolve a parent directory:
249
+ child = dir;
250
+ dir = resolve( dir, '..' );
251
+
252
+ // Reset flag and buffers:
253
+ FLG = 0;
254
+ out = [];
255
+ count = 0;
256
+
257
+ // If we have already reached root, we cannot resolve any higher directories...
258
+ if ( child === dir ) {
259
+ return done( null, out );
260
+ }
261
+ // Resolve paths at next directory level:
262
+ return next( dir );
263
+ }
264
+ }
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Asynchronously resolves each path from a set of paths by walking parent directories.
270
+ *
271
+ * @private
272
+ * @param {Array<string>} paths - paths to resolve
273
+ * @param {string} dir - base directory
274
+ * @param {Callback} done - callback to invoke after resolving paths
275
+ * @returns {void}
276
+ */
277
+ function each( paths, dir, done ) {
278
+ var count;
279
+ var child;
280
+ var spath;
281
+ var out;
282
+ var i;
283
+
284
+ count = 0; // initialize counter to track if we are done resolving all paths
285
+ out = nulls( paths.length );
286
+
287
+ // Start at a base directory and continue moving up through each parent directory...
288
+ for ( i = 0; i < paths.length; i++ ) {
289
+ spath = resolve( dir, paths[ i ] );
290
+ exists( spath, getCallback( i, spath, dir ) );
291
+ }
292
+
293
+ /**
294
+ * Determines whether all paths have been resolved.
295
+ *
296
+ * @private
297
+ * @returns {void}
298
+ */
299
+ function next() {
300
+ count += 1;
301
+ if ( count === paths.length ) {
302
+ return done( null, out );
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Returns a callback to be invoked upon checking for path existence.
308
+ *
309
+ * @private
310
+ * @param {NonNegativeInteger} idx - index
311
+ * @param {string} spath - resolved path
312
+ * @param {string} dir - base directory
313
+ * @returns {Callback} callback
314
+ */
315
+ function getCallback( idx, spath, dir ) {
316
+ return onExists;
317
+
318
+ /**
319
+ * Callback invoked after checking for path existence.
320
+ *
321
+ * @private
322
+ * @param {(Error|null)} error - error object
323
+ * @param {boolean} bool - boolean indicating if a path exists
324
+ * @returns {void}
325
+ */
326
+ function onExists( error, bool ) { // eslint-disable-line n/handle-callback-err
327
+ if ( bool ) {
328
+ out[ idx ] = spath;
329
+ return next();
330
+ }
331
+ // Resolve a parent directory:
332
+ child = dir;
333
+ dir = resolve( dir, '..' );
334
+
335
+ // If we have already reached root, we cannot resolve any higher directories...
336
+ if ( child === dir ) {
337
+ out[ idx ] = null;
338
+ return next();
339
+ }
340
+ // Resolve path at next directory level:
341
+ spath = resolve( dir, paths[ idx ] );
342
+ exists( spath, getCallback( idx, spath, dir ) );
343
+ }
344
+ }
345
+ }
346
+
347
+
348
+ // MAIN //
349
+
350
+ /**
351
+ * Asynchronously resolves paths from a set of paths by walking parent directories.
352
+ *
353
+ * @param {Array<string>} paths - paths to resolve
354
+ * @param {Options} [options] - function options
355
+ * @param {string} [options.dir] - base directory
356
+ * @param {string} [options.mode] - mode of operation
357
+ * @param {Callback} clbk - callback to invoke after resolving paths
358
+ * @throws {TypeError} first argument must be an array of strings
359
+ * @throws {TypeError} callback argument must be a function
360
+ * @throws {TypeError} options argument must be an object
361
+ * @throws {TypeError} must provide valid options
362
+ * @returns {void}
363
+ *
364
+ * @example
365
+ * resolveParentPaths( [ 'package.json', 'package-lock.json' ], onPaths );
366
+ *
367
+ * function onPaths( error, paths ) {
368
+ * if ( error ) {
369
+ * throw error;
370
+ * }
371
+ * console.log( paths );
372
+ * }
373
+ */
374
+ function resolveParentPaths( paths, options, clbk ) {
375
+ var opts;
376
+ var done;
377
+ var mode;
378
+ var dir;
379
+ var fcn;
380
+ var err;
381
+
382
+ if ( !isStringArray( paths ) ) {
383
+ if ( isArrayLikeObject( paths ) && paths.length === 0 ) {
384
+ return [];
385
+ }
386
+ throw new TypeError( format( 'invalid argument. First argument must be an array of strings. Value: `%s`.', paths ) );
387
+ }
388
+ opts = {};
389
+ if ( arguments.length > 2 ) {
390
+ done = clbk;
391
+ err = validate( opts, options );
392
+ if ( err ) {
393
+ throw err;
394
+ }
395
+ } else {
396
+ done = options;
397
+ }
398
+ if ( !isFunction( done ) ) {
399
+ throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
400
+ }
401
+ if ( opts.dir ) {
402
+ dir = resolve( cwd(), opts.dir );
403
+ } else {
404
+ dir = cwd();
405
+ }
406
+ mode = opts.mode || 'all';
407
+
408
+ fcn = MODES[ mode ];
409
+ fcn( paths, dir, done );
410
+ }
411
+
412
+
413
+ // EXPORTS //
414
+
415
+ module.exports = resolveParentPaths;